@delali/narsil 0.3.1-next.157 → 0.3.1-next.161

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 (565) hide show
  1. package/dist/adapters/filesystem.mjs +3 -3
  2. package/dist/adapters/gcs.mjs +3 -3
  3. package/dist/adapters/indexeddb.mjs +1 -1
  4. package/dist/adapters/s3.mjs +3 -3
  5. package/dist/browser/{chunk-N23GL72Q.mjs → chunk-4RNSFVWI.mjs} +20777 -19056
  6. package/dist/browser/chunk-MMHIIL5B.mjs +564 -0
  7. package/dist/browser/chunk-MZHQ65VI.mjs +1228 -0
  8. package/dist/browser/chunk-WC2DEXWU.mjs +29 -0
  9. package/dist/browser/{chunk-VLJQW4J6.mjs → core-leaf-TJAGWPZZ.mjs} +4674 -3102
  10. package/dist/{react-native/chunks/durability-config-QXVZEO7T.mjs → browser/durability-config-SGB5ZP5K.mjs} +8 -3
  11. package/dist/browser/embed-RF2MPGAO.mjs +222 -0
  12. package/dist/{react-native/chunks/geo-index-PX3BIHAH.mjs → browser/geo-index-GNHY4PUB.mjs} +190 -93
  13. package/dist/{react-native/chunks/module-TTEVPRCW.mjs → browser/module-362VSIGR.mjs} +1570 -275
  14. package/dist/browser/msgpack-leaf-44XNWSSY.mjs +302 -0
  15. package/dist/browser/optional-reads-XMVPG6HG.mjs +3230 -0
  16. package/dist/browser/optional-writes-WMZX4OEC.mjs +3196 -0
  17. package/dist/browser/phrase-module-22TKMXWI.mjs +616 -0
  18. package/dist/browser/resolve-vector-text-KI65IHMD.mjs +60 -0
  19. package/dist/browser/{restore-hooks-IE3GGYHG.mjs → restore-queue-S4BWOGMU.mjs} +98 -24
  20. package/dist/{react-native/chunks/runtime-R5E3TB3J.mjs → browser/runtime-NU6VOVH2.mjs} +61 -25
  21. package/dist/browser/segment-codec-leaf-A427QJUX.mjs +554 -0
  22. package/dist/browser/segment-merge-leaf-7BZ54JPI.mjs +1636 -0
  23. package/dist/browser/storage-leaf-X6CNRHJS.mjs +18221 -0
  24. package/dist/browser/store-leaf-AEMTAVHX.mjs +2069 -0
  25. package/dist/browser/{chunk-GFAZPSWK.mjs → suggestions-leaf-3Y6MR6OF.mjs} +441 -119
  26. package/dist/browser/worker-copies-WQCPP3TS.mjs +2750 -0
  27. package/dist/{capabilities-D3AoBJoF.d.ts → capabilities-BKDrry0z.d.ts} +2 -2
  28. package/dist/chunk-2JRERUNZ.mjs +217 -0
  29. package/dist/chunk-2RIS76RJ.mjs +212 -0
  30. package/dist/chunk-3B775F3C.mjs +252 -0
  31. package/dist/{chunk-24FNVRKB.mjs → chunk-3D6RZGJH.mjs} +3 -3
  32. package/dist/{chunk-INJYO3BU.mjs → chunk-4CAMH7FO.mjs} +1 -1
  33. package/dist/{chunk-U476IXKQ.mjs → chunk-4PY4JYMY.mjs} +2 -1
  34. package/dist/chunk-6EZW2GQL.mjs +978 -0
  35. package/dist/chunk-75UNINFA.mjs +271 -0
  36. package/dist/chunk-77AUVDQ5.mjs +7 -0
  37. package/dist/{chunk-JTYPJF3T.mjs → chunk-7MWYORQC.mjs} +2 -2
  38. package/dist/{chunk-TGP5M5R7.mjs → chunk-7T4LAYT4.mjs} +1 -1
  39. package/dist/{chunk-XIHUI4KB.mjs → chunk-AB27FWZM.mjs} +8 -3
  40. package/dist/{chunk-FUD5XSZ5.mjs → chunk-BXM7XM4G.mjs} +7 -3
  41. package/dist/{chunk-HJJI72X7.mjs → chunk-C7YQHCSO.mjs} +1 -1
  42. package/dist/{chunk-RVGRFOGN.mjs → chunk-D456ZFPQ.mjs} +2 -1
  43. package/dist/{chunk-KKQC526K.mjs → chunk-E2F6LZR3.mjs} +19 -21
  44. package/dist/{chunk-SMOOSIAW.mjs → chunk-FE3MRIBN.mjs} +104 -13
  45. package/dist/{chunk-KYYABCSW.mjs → chunk-FETZYCH7.mjs} +15 -1
  46. package/dist/{chunk-XXJZMTZT.mjs → chunk-FVZ5DB6Y.mjs} +3 -2
  47. package/dist/{chunk-EYD7K3H6.mjs → chunk-GF4K7IUK.mjs} +317 -120
  48. package/dist/chunk-GXCDWLQI.mjs +193 -0
  49. package/dist/{chunk-IIRIG7H5.mjs → chunk-H64YDF5M.mjs} +3 -3
  50. package/dist/{browser/chunk-VY5KW3HN.mjs → chunk-HOOR4CP5.mjs} +63 -7
  51. package/dist/chunk-IB4BYLE3.mjs +127 -0
  52. package/dist/{chunk-3C7MTCCI.mjs → chunk-IE3Q62QD.mjs} +2 -2
  53. package/dist/chunk-IH3G6SJO.mjs +40 -0
  54. package/dist/chunk-JV74RKCY.mjs +3041 -0
  55. package/dist/{chunk-ZPZ3YGXV.mjs → chunk-K67I3RF4.mjs} +261 -101
  56. package/dist/chunk-KLVMK3R5.mjs +47 -0
  57. package/dist/{chunk-5VV4R6P7.mjs → chunk-LAWIJRMJ.mjs} +4154 -2577
  58. package/dist/chunk-LF4GB4PO.mjs +281 -0
  59. package/dist/chunk-NC4QLCZC.mjs +59 -0
  60. package/dist/chunk-OOO54OVC.mjs +627 -0
  61. package/dist/chunk-OUWZKNO3.mjs +207 -0
  62. package/dist/{chunk-FJKKCV4D.mjs → chunk-P5YLI5ED.mjs} +343 -182
  63. package/dist/{chunk-4ZTHGTVX.mjs → chunk-PTTMGRG5.mjs} +9 -10
  64. package/dist/{react-native/chunks/chunk-DZ7H7D2C.mjs → chunk-Q3ZZH5Z7.mjs} +6 -0
  65. package/dist/{browser/chunk-EKMGCRSR.mjs → chunk-QBVTYY4J.mjs} +2 -9
  66. package/dist/{chunk-W5EB5XKI.mjs → chunk-R4CE4IVU.mjs} +341 -183
  67. package/dist/chunk-S2POFTLN.mjs +10 -0
  68. package/dist/{chunk-KJ3RTZDJ.mjs → chunk-S3BSDBF7.mjs} +10 -3
  69. package/dist/{chunk-4LMSZAK6.mjs → chunk-SHGN3QIF.mjs} +69 -13
  70. package/dist/chunk-SXIRM4MX.mjs +3549 -0
  71. package/dist/{chunk-TBC3XUL2.mjs → chunk-TGPKS2UM.mjs} +47 -1
  72. package/dist/chunk-TINW4BSH.mjs +31301 -0
  73. package/dist/{cold-module-VOJXMNZB.mjs → chunk-TSLV5P2J.mjs} +397 -80
  74. package/dist/{chunk-HHBUCIUY.mjs → chunk-TW543F33.mjs} +3 -3
  75. package/dist/chunk-U6B7DUQW.mjs +788 -0
  76. package/dist/{chunk-OQLD2BSW.mjs → chunk-V3SGPXWQ.mjs} +8 -9
  77. package/dist/chunk-W34VJXY7.mjs +4723 -0
  78. package/dist/chunk-YETKPX5M.mjs +5 -0
  79. package/dist/chunk-ZQ6QY42L.mjs +59 -0
  80. package/dist/{client-kqSMevVi.d.ts → client-CTp8RRiK.d.ts} +4 -4
  81. package/dist/client.d.ts +8 -8
  82. package/dist/client.mjs +66 -18
  83. package/dist/{config-B4v_Htpu.d.ts → config-PCNzWjHL.d.ts} +6 -3
  84. package/dist/{config-CTP01fns.d.ts → config-_WT5grSL.d.ts} +3 -3
  85. package/dist/core-leaf-KXFGP74A.mjs +7 -0
  86. package/dist/digest-leaf-KXBGKYHZ.mjs +1 -0
  87. package/dist/direct-executor-MKTZBR26.mjs +16 -0
  88. package/dist/distribution/coordinator/etcd.d.ts +2 -2
  89. package/dist/distribution/coordinator/etcd.mjs +7 -4
  90. package/dist/distribution/coordinator/in-memory.d.ts +2 -2
  91. package/dist/distribution/coordinator/in-memory.mjs +1 -2
  92. package/dist/distribution/transport/grpc.mjs +7 -2
  93. package/dist/distribution/transport/tcp.mjs +7 -1
  94. package/dist/distribution.d.ts +7 -7
  95. package/dist/distribution.mjs +1199 -1092
  96. package/dist/{browser/durability-config-TIYKW6XX.mjs → durability-config-XV4XR7Y4.mjs} +7 -3
  97. package/dist/embed-4H7RTBVL.mjs +221 -0
  98. package/dist/embeddings/openai.mjs +2 -2
  99. package/dist/{engine-CkPn2PWE.d.ts → engine-DBRVR_d-.d.ts} +1252 -1239
  100. package/dist/{browser/geo-index-OTV7VPIR.mjs → geo-index-2ZMMWWSK.mjs} +190 -93
  101. package/dist/{index-C4AmUnl1.d.ts → index-Ck9w5WHz.d.ts} +1 -1
  102. package/dist/index.browser.mjs +4 -16
  103. package/dist/index.d.ts +1495 -78
  104. package/dist/index.mjs +14 -50
  105. package/dist/index.react-native.mjs +11 -28
  106. package/dist/invalidation/filesystem.mjs +1 -2
  107. package/dist/languages/albanian.mjs +1 -1
  108. package/dist/languages/amharic.mjs +1 -1
  109. package/dist/languages/arabic.mjs +1 -1
  110. package/dist/languages/armenian.mjs +1 -1
  111. package/dist/languages/azerbaijani.mjs +1 -1
  112. package/dist/languages/bambara.mjs +1 -1
  113. package/dist/languages/basque.mjs +1 -1
  114. package/dist/languages/belarusian.mjs +1 -1
  115. package/dist/languages/bengali.mjs +1 -1
  116. package/dist/languages/bosnian.mjs +1 -1
  117. package/dist/languages/breton.mjs +1 -1
  118. package/dist/languages/bulgarian.mjs +1 -1
  119. package/dist/languages/burmese.mjs +1 -1
  120. package/dist/languages/catalan.mjs +1 -1
  121. package/dist/languages/chinese.mjs +1 -1
  122. package/dist/languages/croatian.mjs +1 -1
  123. package/dist/languages/czech.mjs +1 -1
  124. package/dist/languages/dagbani.mjs +1 -1
  125. package/dist/languages/danish.mjs +1 -1
  126. package/dist/languages/dutch.mjs +1 -1
  127. package/dist/languages/english.mjs +1 -1
  128. package/dist/languages/esperanto.mjs +1 -1
  129. package/dist/languages/estonian.mjs +1 -1
  130. package/dist/languages/ewe.mjs +1 -1
  131. package/dist/languages/faroese.mjs +1 -1
  132. package/dist/languages/fijian.mjs +1 -1
  133. package/dist/languages/finnish.mjs +1 -1
  134. package/dist/languages/french.mjs +1 -1
  135. package/dist/languages/ga.mjs +1 -1
  136. package/dist/languages/galician.mjs +1 -1
  137. package/dist/languages/georgian.mjs +1 -1
  138. package/dist/languages/german.mjs +1 -1
  139. package/dist/languages/greek.mjs +1 -1
  140. package/dist/languages/guarani.mjs +1 -1
  141. package/dist/languages/gujarati.mjs +1 -1
  142. package/dist/languages/haitian-creole.mjs +1 -1
  143. package/dist/languages/hausa.mjs +1 -1
  144. package/dist/languages/hawaiian.mjs +1 -1
  145. package/dist/languages/hebrew.mjs +1 -1
  146. package/dist/languages/hindi.mjs +1 -1
  147. package/dist/languages/hungarian.mjs +1 -1
  148. package/dist/languages/icelandic.mjs +1 -1
  149. package/dist/languages/igbo.mjs +1 -1
  150. package/dist/languages/indonesian.mjs +1 -1
  151. package/dist/languages/irish.mjs +1 -1
  152. package/dist/languages/italian.mjs +1 -1
  153. package/dist/languages/japanese.mjs +1 -1
  154. package/dist/languages/kannada.mjs +1 -1
  155. package/dist/languages/kazakh.mjs +1 -1
  156. package/dist/languages/khmer.mjs +1 -1
  157. package/dist/languages/kinyarwanda.mjs +1 -1
  158. package/dist/languages/kirundi.mjs +1 -1
  159. package/dist/languages/korean.mjs +1 -1
  160. package/dist/languages/kurmanji.mjs +1 -1
  161. package/dist/languages/kyrgyz.mjs +1 -1
  162. package/dist/languages/lao.mjs +1 -1
  163. package/dist/languages/latin.mjs +1 -1
  164. package/dist/languages/latvian.mjs +1 -1
  165. package/dist/languages/lingala.mjs +1 -1
  166. package/dist/languages/lithuanian.mjs +1 -1
  167. package/dist/languages/luxembourgish.mjs +1 -1
  168. package/dist/languages/macedonian.mjs +1 -1
  169. package/dist/languages/malagasy.mjs +1 -1
  170. package/dist/languages/malay.mjs +1 -1
  171. package/dist/languages/malayalam.mjs +1 -1
  172. package/dist/languages/maltese.mjs +1 -1
  173. package/dist/languages/maori.mjs +1 -1
  174. package/dist/languages/marathi.mjs +1 -1
  175. package/dist/languages/nepali.mjs +1 -1
  176. package/dist/languages/norwegian.mjs +1 -1
  177. package/dist/languages/oromo.mjs +1 -1
  178. package/dist/languages/persian.mjs +1 -1
  179. package/dist/languages/polish.mjs +1 -1
  180. package/dist/languages/portuguese.mjs +1 -1
  181. package/dist/languages/punjabi.mjs +1 -1
  182. package/dist/languages/romanian.mjs +1 -1
  183. package/dist/languages/russian.mjs +1 -1
  184. package/dist/languages/samoan.mjs +1 -1
  185. package/dist/languages/sanskrit.mjs +1 -1
  186. package/dist/languages/scottish-gaelic.mjs +1 -1
  187. package/dist/languages/serbian.mjs +1 -1
  188. package/dist/languages/shona.mjs +1 -1
  189. package/dist/languages/sinhala.mjs +1 -1
  190. package/dist/languages/slovak.mjs +1 -1
  191. package/dist/languages/slovenian.mjs +1 -1
  192. package/dist/languages/sorani.mjs +1 -1
  193. package/dist/languages/spanish.mjs +1 -1
  194. package/dist/languages/swahili.mjs +1 -1
  195. package/dist/languages/swedish.mjs +1 -1
  196. package/dist/languages/tagalog.mjs +1 -1
  197. package/dist/languages/tamil.mjs +1 -1
  198. package/dist/languages/tatar.mjs +1 -1
  199. package/dist/languages/telugu.mjs +1 -1
  200. package/dist/languages/thai.mjs +1 -1
  201. package/dist/languages/tibetan.mjs +1 -1
  202. package/dist/languages/tigrinya.mjs +1 -1
  203. package/dist/languages/tongan.mjs +1 -1
  204. package/dist/languages/turkish.mjs +1 -1
  205. package/dist/languages/twi.mjs +1 -1
  206. package/dist/languages/ukrainian.mjs +1 -1
  207. package/dist/languages/urdu.mjs +1 -1
  208. package/dist/languages/vietnamese.mjs +1 -1
  209. package/dist/languages/welsh.mjs +1 -1
  210. package/dist/languages/wolof.mjs +1 -1
  211. package/dist/languages/xhosa.mjs +1 -1
  212. package/dist/languages/yoruba.mjs +1 -1
  213. package/dist/languages/zulu.mjs +1 -1
  214. package/dist/{long-query-thread-QDFQSJD6.mjs → long-query-thread-JMGNGF6V.mjs} +74 -58
  215. package/dist/{module-N55W4Q5C.mjs → module-ZJ6ARG6V.mjs} +444 -111
  216. package/dist/msgpack-leaf-5WFRP6JM.mjs +9 -0
  217. package/dist/optional-reads-BR3BOVZW.mjs +3373 -0
  218. package/dist/optional-writes-DDFHYUOH.mjs +218 -0
  219. package/dist/{paged-partition-archive-RLRGOXDX.mjs → paged-partition-archive-G576XGVE.mjs} +29 -69
  220. package/dist/partial-words.browser.mjs +1649 -0
  221. package/dist/partial-words.mjs +15 -14
  222. package/dist/{chunk-CITFCZKF.mjs → pattern.browser.mjs} +153 -186
  223. package/dist/pattern.mjs +6 -8
  224. package/dist/persistence/durability/checkpoint-worker.d.ts +32 -24
  225. package/dist/persistence/durability/checkpoint-worker.mjs +27 -59
  226. package/dist/phrase-module-XK3T44VU.mjs +634 -0
  227. package/dist/query-actions-OMXJEC2J.mjs +118 -0
  228. package/dist/query-suggestions/merge-worker.mjs +18 -54
  229. package/dist/query-suggestions.browser.mjs +26 -227
  230. package/dist/query-suggestions.d.ts +2 -2
  231. package/dist/query-suggestions.mjs +16 -67
  232. package/dist/react-native/chunks/chunk-4EXKQ2IB.mjs +565 -0
  233. package/dist/react-native/chunks/chunk-52QD7CBJ.mjs +52 -0
  234. package/dist/react-native/chunks/chunk-72OKYHZL.mjs +969 -0
  235. package/dist/react-native/chunks/chunk-AQOWRH2C.mjs +802 -0
  236. package/dist/react-native/chunks/{chunk-4COWCEY7.mjs → chunk-FTQ7JNMU.mjs} +341 -180
  237. package/dist/react-native/chunks/chunk-G3CUQ333.mjs +42 -0
  238. package/dist/react-native/chunks/{chunk-EJ33LCHV.mjs → chunk-HRDMIQ63.mjs} +53 -1
  239. package/dist/react-native/chunks/{chunk-4AV5PPH6.mjs → chunk-IY6U6HRA.mjs} +63 -7
  240. package/dist/react-native/chunks/chunk-J2NQW353.mjs +212 -0
  241. package/dist/react-native/chunks/chunk-L4R5ITVG.mjs +29047 -0
  242. package/dist/react-native/chunks/{chunk-OXM66LO6.mjs → chunk-OK3WUA6N.mjs} +2 -10
  243. package/dist/react-native/chunks/{chunk-MVXELMJX.mjs → chunk-PVMEHBQO.mjs} +1 -1
  244. package/dist/{browser/chunk-SRMZXJSL.mjs → react-native/chunks/chunk-TQ26BKBH.mjs} +6 -0
  245. package/dist/{browser/chunk-GPPOG6PT.mjs → react-native/chunks/chunk-VUJCSQG5.mjs} +3 -1
  246. package/dist/react-native/chunks/{chunk-ENLMYKSX.mjs → chunk-XEGKQKLY.mjs} +5385 -3114
  247. package/dist/react-native/chunks/chunk-YOTP2FPE.mjs +232 -0
  248. package/dist/react-native/chunks/chunk-ZIU6TWXG.mjs +127 -0
  249. package/dist/react-native/chunks/core-leaf-5B4JXCVA.mjs +6 -0
  250. package/dist/react-native/chunks/digest-leaf-X6OIFDIU.mjs +1 -0
  251. package/dist/{durability-config-GE4U7MCZ.mjs → react-native/chunks/durability-config-3IOXIPYZ.mjs} +7 -3
  252. package/dist/react-native/chunks/embed-3WQ2CCR5.mjs +221 -0
  253. package/dist/{geo-index-MRH5JMIR.mjs → react-native/chunks/geo-index-QEYKEVPH.mjs} +190 -93
  254. package/dist/react-native/chunks/{chunk-CKNRFDTZ.mjs → gzip.react-native-DLO4WW27.mjs} +2 -1
  255. package/dist/{browser/module-TCT66FWA.mjs → react-native/chunks/module-VUA27HZZ.mjs} +1018 -540
  256. package/dist/react-native/chunks/msgpack-leaf-TR66ZVAX.mjs +7 -0
  257. package/dist/react-native/chunks/optional-reads-WDTLLHUT.mjs +3364 -0
  258. package/dist/react-native/chunks/optional-writes-FZV3QW34.mjs +267 -0
  259. package/dist/react-native/chunks/phrase-module-KCQWVXYZ.mjs +635 -0
  260. package/dist/react-native/chunks/resolve-vector-text-LXRYXF7M.mjs +59 -0
  261. package/dist/react-native/chunks/restore-queue-EI2V2I4S.mjs +179 -0
  262. package/dist/{browser/runtime-OTAGEFQW.mjs → react-native/chunks/runtime-BUCSAK4U.mjs} +61 -25
  263. package/dist/react-native/chunks/segment-codec-leaf-6ULP7H3I.mjs +12 -0
  264. package/dist/react-native/chunks/segment-merge-leaf-F4GQZNK6.mjs +13 -0
  265. package/dist/react-native/chunks/storage-leaf-OAXBU2L6.mjs +18042 -0
  266. package/dist/react-native/chunks/store-leaf-GY2ZZFXB.mjs +14 -0
  267. package/dist/react-native/chunks/{chunk-WW2X2AEI.mjs → suggestions-leaf-SQEZ4IBS.mjs} +332 -118
  268. package/dist/react-native/chunks/worker-copies-EEHSCFJW.mjs +2627 -0
  269. package/dist/react-native/partial-words.mjs +954 -35
  270. package/dist/react-native/pattern.mjs +238 -111
  271. package/dist/react-native/query-suggestions.mjs +33 -239
  272. package/dist/react-native/sync.mjs +95 -52
  273. package/dist/react-native/synonyms.mjs +256 -112
  274. package/dist/react.d.ts +9 -9
  275. package/dist/react.mjs +6 -0
  276. package/dist/{rerank-MQ37IYRV.mjs → rerank-Y6MWBMTT.mjs} +17 -8
  277. package/dist/rerankers/http.mjs +2 -2
  278. package/dist/resolve-vector-text-4PDRTPNQ.mjs +2 -0
  279. package/dist/restore-queue-KPYSRJ4A.mjs +184 -0
  280. package/dist/runtime-4GKUIAKV.mjs +595 -0
  281. package/dist/{schema-Jwt928nJ.d.ts → schema-C_7yRJLt.d.ts} +1 -1
  282. package/dist/segment-codec-leaf-6IFBSBPN.mjs +17 -0
  283. package/dist/segment-merge-leaf-VE3FUSVE.mjs +17 -0
  284. package/dist/server.d.ts +33 -13
  285. package/dist/server.mjs +81 -100
  286. package/dist/{browser/durability-wiring-GVZEE46O.mjs → storage-leaf-HMIIOUYN.mjs} +2334 -931
  287. package/dist/store-leaf-M7T7SUZJ.mjs +19 -0
  288. package/dist/stored-values-BWIMpiT_.d.ts +25 -0
  289. package/dist/suggestions-leaf-Q3Q5D6XV.mjs +53 -0
  290. package/dist/sync/filesystem.d.ts +3 -3
  291. package/dist/sync/filesystem.mjs +27 -11
  292. package/dist/sync/indexeddb.browser.mjs +287 -0
  293. package/dist/sync/indexeddb.d.ts +3 -3
  294. package/dist/sync/indexeddb.mjs +7 -2
  295. package/dist/sync.browser.mjs +88 -72
  296. package/dist/sync.d.ts +4 -4
  297. package/dist/sync.mjs +89 -59
  298. package/dist/{chunk-I5B7RXU7.mjs → synonyms.browser.mjs} +248 -135
  299. package/dist/synonyms.mjs +15 -11
  300. package/dist/{tasks-CoNjxbop.d.ts → tasks-B4tT4vmP.d.ts} +4 -4
  301. package/dist/thread-SNILJXX3.mjs +30 -0
  302. package/dist/{types-DPMjfOr_.d.ts → types-CTg0KaMy.d.ts} +2 -2
  303. package/dist/vector/search-worker.d.ts +7 -1
  304. package/dist/vector/search-worker.mjs +9 -13
  305. package/dist/worker-copies-ZLZS2CUM.mjs +2152 -0
  306. package/dist/workers/entry.mjs +60 -30
  307. package/openapi.json +66 -50
  308. package/package.json +233 -8
  309. package/dist/analysis-change-CKEMM2MC.mjs +0 -55
  310. package/dist/analysis-change-entry-SEDMJ6MW.mjs +0 -1
  311. package/dist/archive-history-E4R73RWD.mjs +0 -29
  312. package/dist/browser/analysis-change-ZKGJFENM.mjs +0 -942
  313. package/dist/browser/analysis-change-entry-5BFUVMHJ.mjs +0 -1
  314. package/dist/browser/archive-history-4IP5XQZY.mjs +0 -29
  315. package/dist/browser/chunk-25MTVDYD.mjs +0 -432
  316. package/dist/browser/chunk-3I27KXQD.mjs +0 -34
  317. package/dist/browser/chunk-3NLMB5CC.mjs +0 -217
  318. package/dist/browser/chunk-4RD3AFSB.mjs +0 -32
  319. package/dist/browser/chunk-5W3MMTLG.mjs +0 -19
  320. package/dist/browser/chunk-5YWB3E2W.mjs +0 -92
  321. package/dist/browser/chunk-7IGKGWSQ.mjs +0 -79
  322. package/dist/browser/chunk-AI5UI46D.mjs +0 -12
  323. package/dist/browser/chunk-ATEBYXP6.mjs +0 -10
  324. package/dist/browser/chunk-BLT4RHCJ.mjs +0 -241
  325. package/dist/browser/chunk-C26SPCGQ.mjs +0 -2636
  326. package/dist/browser/chunk-C4FJ3WDY.mjs +0 -177
  327. package/dist/browser/chunk-CXNTHHA7.mjs +0 -63
  328. package/dist/browser/chunk-DSXIBE6N.mjs +0 -78
  329. package/dist/browser/chunk-DUH6QUVU.mjs +0 -2374
  330. package/dist/browser/chunk-ELV4OZOI.mjs +0 -29
  331. package/dist/browser/chunk-EXQZGUQH.mjs +0 -193
  332. package/dist/browser/chunk-G64Q6OJF.mjs +0 -96
  333. package/dist/browser/chunk-HOZW3PKP.mjs +0 -9
  334. package/dist/browser/chunk-HUCSHDTB.mjs +0 -100
  335. package/dist/browser/chunk-JPTQZWJQ.mjs +0 -10
  336. package/dist/browser/chunk-K3IPRX6D.mjs +0 -2559
  337. package/dist/browser/chunk-ML7THVLJ.mjs +0 -38
  338. package/dist/browser/chunk-MY2XKH22.mjs +0 -40
  339. package/dist/browser/chunk-NLWTBAGB.mjs +0 -111
  340. package/dist/browser/chunk-NXQG7DRA.mjs +0 -318
  341. package/dist/browser/chunk-OEDCEZXJ.mjs +0 -92
  342. package/dist/browser/chunk-OIZS5KIX.mjs +0 -6
  343. package/dist/browser/chunk-RA6LSBCM.mjs +0 -43
  344. package/dist/browser/chunk-TK63WFXK.mjs +0 -90
  345. package/dist/browser/chunk-TY7L6OXO.mjs +0 -69
  346. package/dist/browser/chunk-U3JOQGWX.mjs +0 -19
  347. package/dist/browser/chunk-U4YDIBUT.mjs +0 -653
  348. package/dist/browser/chunk-UATIJ4KU.mjs +0 -21
  349. package/dist/browser/chunk-UKOWD2K5.mjs +0 -315
  350. package/dist/browser/chunk-UTMHUHUV.mjs +0 -150
  351. package/dist/browser/chunk-V43N6TZO.mjs +0 -857
  352. package/dist/browser/chunk-VI7BQR4U.mjs +0 -21
  353. package/dist/browser/chunk-WN464PTL.mjs +0 -375
  354. package/dist/browser/chunk-WXLBZJMT.mjs +0 -25
  355. package/dist/browser/chunk-X7TXYUKO.mjs +0 -86
  356. package/dist/browser/chunk-XAWNZ4G2.mjs +0 -151
  357. package/dist/browser/chunk-Y6IBAUNO.mjs +0 -561
  358. package/dist/browser/chunk-ZI2V4VD7.mjs +0 -21
  359. package/dist/browser/cold-module-YNUUDNOM.mjs +0 -761
  360. package/dist/browser/engine-hooks-HBVS6YM4.mjs +0 -18
  361. package/dist/browser/gzip-5S7ROJFJ.mjs +0 -1
  362. package/dist/browser/import-5ESVMFDJ.mjs +0 -20
  363. package/dist/browser/methods-KTQCSIN4.mjs +0 -30
  364. package/dist/browser/module-NDSQLPMQ.mjs +0 -25
  365. package/dist/browser/operations-OJ3BXS55.mjs +0 -5
  366. package/dist/browser/phrase-module-HAT3TLZN.mjs +0 -328
  367. package/dist/browser/pool-3JLET5RO.mjs +0 -297
  368. package/dist/browser/release-HYXPVJVS.mjs +0 -32
  369. package/dist/browser/restore-queue-LQBBTODR.mjs +0 -55
  370. package/dist/browser/retention-TY3L3BFN.mjs +0 -211
  371. package/dist/browser/snapshot-5FHPPCNZ.mjs +0 -965
  372. package/dist/browser/start-2UZTOBG5.mjs +0 -26
  373. package/dist/browser/sync-state-P5XG6LTQ.mjs +0 -15
  374. package/dist/browser/write-ULXAN5B4.mjs +0 -196
  375. package/dist/chunk-2NE7VCYE.mjs +0 -212
  376. package/dist/chunk-2ROUFMQX.mjs +0 -543
  377. package/dist/chunk-37CD3OKS.mjs +0 -87
  378. package/dist/chunk-3MXV3VHR.mjs +0 -46
  379. package/dist/chunk-3OH67G7R.mjs +0 -214
  380. package/dist/chunk-4B7JIGQD.mjs +0 -106
  381. package/dist/chunk-5YNRKI2C.mjs +0 -81
  382. package/dist/chunk-6HTGOXIK.mjs +0 -875
  383. package/dist/chunk-6OEGXLBX.mjs +0 -57
  384. package/dist/chunk-6RC4K46J.mjs +0 -86
  385. package/dist/chunk-7GFE3PEY.mjs +0 -40
  386. package/dist/chunk-7GXJUJ24.mjs +0 -49
  387. package/dist/chunk-7V5IUP2T.mjs +0 -63
  388. package/dist/chunk-A4L7YTKX.mjs +0 -458
  389. package/dist/chunk-BEZH7VEH.mjs +0 -23
  390. package/dist/chunk-BNL5OPRV.mjs +0 -634
  391. package/dist/chunk-BTPGQC6Z.mjs +0 -27
  392. package/dist/chunk-C7XQYG6U.mjs +0 -50
  393. package/dist/chunk-CQZXZ75V.mjs +0 -562
  394. package/dist/chunk-CWS55TEX.mjs +0 -242
  395. package/dist/chunk-CZQ33DLW.mjs +0 -78
  396. package/dist/chunk-EEBCDP36.mjs +0 -32
  397. package/dist/chunk-FB6463IN.mjs +0 -1499
  398. package/dist/chunk-FCJ553HS.mjs +0 -655
  399. package/dist/chunk-FEZVDOWX.mjs +0 -6996
  400. package/dist/chunk-FOCV4CBZ.mjs +0 -87
  401. package/dist/chunk-FQBFZAWG.mjs +0 -27
  402. package/dist/chunk-G6HRSGOU.mjs +0 -74
  403. package/dist/chunk-GES7U2PM.mjs +0 -68
  404. package/dist/chunk-GH4WQ22V.mjs +0 -34
  405. package/dist/chunk-GPWMUPCA.mjs +0 -1126
  406. package/dist/chunk-GQPTDQCL.mjs +0 -54
  407. package/dist/chunk-HJSN7UYG.mjs +0 -10
  408. package/dist/chunk-I54T6CQP.mjs +0 -117
  409. package/dist/chunk-IPN7S4KX.mjs +0 -130
  410. package/dist/chunk-J4IJTTEX.mjs +0 -226
  411. package/dist/chunk-JSV6OQ4C.mjs +0 -74
  412. package/dist/chunk-K4ZIGL2O.mjs +0 -290
  413. package/dist/chunk-KAKQ74LS.mjs +0 -53
  414. package/dist/chunk-KCQYFO4K.mjs +0 -62
  415. package/dist/chunk-KHKIRS5D.mjs +0 -113
  416. package/dist/chunk-KOKM4PQN.mjs +0 -52
  417. package/dist/chunk-KP7TYHWT.mjs +0 -92
  418. package/dist/chunk-KZ3NX2P7.mjs +0 -21
  419. package/dist/chunk-L5ORED7V.mjs +0 -25
  420. package/dist/chunk-LDNCLRZU.mjs +0 -218
  421. package/dist/chunk-LHKOSI2J.mjs +0 -58
  422. package/dist/chunk-LQG5G2LY.mjs +0 -39
  423. package/dist/chunk-MATMVH65.mjs +0 -9
  424. package/dist/chunk-MP7ABPTD.mjs +0 -189
  425. package/dist/chunk-MW6BR4QP.mjs +0 -316
  426. package/dist/chunk-MWD5VPN7.mjs +0 -21
  427. package/dist/chunk-ODYKO5TO.mjs +0 -101
  428. package/dist/chunk-OG7UGJ6F.mjs +0 -63
  429. package/dist/chunk-ORN5BLL7.mjs +0 -11
  430. package/dist/chunk-OYABFKTW.mjs +0 -1974
  431. package/dist/chunk-P5LANSH6.mjs +0 -121
  432. package/dist/chunk-PELHVTO7.mjs +0 -24
  433. package/dist/chunk-PIH5SJ4N.mjs +0 -7481
  434. package/dist/chunk-PSZI4O24.mjs +0 -253
  435. package/dist/chunk-PV2CG7EJ.mjs +0 -886
  436. package/dist/chunk-PWGYQXML.mjs +0 -470
  437. package/dist/chunk-Q2UL2TPC.mjs +0 -21
  438. package/dist/chunk-QHPYS427.mjs +0 -66
  439. package/dist/chunk-QJVOLKU6.mjs +0 -111
  440. package/dist/chunk-QLDABJZM.mjs +0 -20
  441. package/dist/chunk-QYRNLLLI.mjs +0 -99
  442. package/dist/chunk-RCX4WNLC.mjs +0 -151
  443. package/dist/chunk-RFBLQLLZ.mjs +0 -16
  444. package/dist/chunk-RIHIVFIQ.mjs +0 -240
  445. package/dist/chunk-RLOES5SX.mjs +0 -31
  446. package/dist/chunk-RWURUDSP.mjs +0 -2639
  447. package/dist/chunk-RZWWB2XR.mjs +0 -96
  448. package/dist/chunk-S5LN746F.mjs +0 -10
  449. package/dist/chunk-SOI6LTHM.mjs +0 -216
  450. package/dist/chunk-SQJWAIF4.mjs +0 -12
  451. package/dist/chunk-TDIK6NTV.mjs +0 -10
  452. package/dist/chunk-TQXHPFWT.mjs +0 -177
  453. package/dist/chunk-U55XJEIG.mjs +0 -18
  454. package/dist/chunk-UD5HIS4U.mjs +0 -382
  455. package/dist/chunk-UHICS3JI.mjs +0 -83
  456. package/dist/chunk-UJDJB3NE.mjs +0 -913
  457. package/dist/chunk-VD7J552I.mjs +0 -28
  458. package/dist/chunk-VSLSGASN.mjs +0 -30
  459. package/dist/chunk-VZ2ONHEE.mjs +0 -3715
  460. package/dist/chunk-W7OSSKH5.mjs +0 -1427
  461. package/dist/chunk-WZ7YV54N.mjs +0 -152
  462. package/dist/chunk-X5CV2E2B.mjs +0 -1
  463. package/dist/chunk-Y4WPNHMC.mjs +0 -50
  464. package/dist/chunk-YEKL4JBK.mjs +0 -96
  465. package/dist/chunk-ZXNQEW43.mjs +0 -18
  466. package/dist/chunk-ZYAY26KG.mjs +0 -29
  467. package/dist/direct-executor-V5DRIECI.mjs +0 -34
  468. package/dist/durability-wiring-3TD62MBI.mjs +0 -4072
  469. package/dist/engine-hooks-HA3SBOBR.mjs +0 -54
  470. package/dist/gzip-F4HMIEEQ.mjs +0 -1
  471. package/dist/import-GPKLTSKA.mjs +0 -56
  472. package/dist/methods-7IOINERF.mjs +0 -30
  473. package/dist/module-JYHFTVPS.mjs +0 -63
  474. package/dist/operations-G5EP6JPX.mjs +0 -5
  475. package/dist/phrase-module-XW5MKWP5.mjs +0 -330
  476. package/dist/pool-Y4A46T36.mjs +0 -298
  477. package/dist/react-native/chunks/analysis-change-QUVDNABS.mjs +0 -934
  478. package/dist/react-native/chunks/analysis-change-entry-OC4XASBF.mjs +0 -1
  479. package/dist/react-native/chunks/archive-history-MJKUQLSU.mjs +0 -29
  480. package/dist/react-native/chunks/chunk-2PRHILX3.mjs +0 -529
  481. package/dist/react-native/chunks/chunk-3NQOVO3K.mjs +0 -1
  482. package/dist/react-native/chunks/chunk-3WI35N75.mjs +0 -226
  483. package/dist/react-native/chunks/chunk-4DII3ZSM.mjs +0 -73
  484. package/dist/react-native/chunks/chunk-4J74WHQM.mjs +0 -177
  485. package/dist/react-native/chunks/chunk-4NWR3NGS.mjs +0 -151
  486. package/dist/react-native/chunks/chunk-5CQX3XXV.mjs +0 -17
  487. package/dist/react-native/chunks/chunk-5MSX5FRT.mjs +0 -858
  488. package/dist/react-native/chunks/chunk-6OFDUYVX.mjs +0 -92
  489. package/dist/react-native/chunks/chunk-6VBXNFLE.mjs +0 -9
  490. package/dist/react-native/chunks/chunk-6ZELFCK5.mjs +0 -315
  491. package/dist/react-native/chunks/chunk-AAAETEJK.mjs +0 -27
  492. package/dist/react-native/chunks/chunk-AM7RSBPF.mjs +0 -32
  493. package/dist/react-native/chunks/chunk-BITYFZSZ.mjs +0 -242
  494. package/dist/react-native/chunks/chunk-C5G6TJ7A.mjs +0 -140
  495. package/dist/react-native/chunks/chunk-CHPTFFJM.mjs +0 -43
  496. package/dist/react-native/chunks/chunk-DIYDOIM4.mjs +0 -10
  497. package/dist/react-native/chunks/chunk-DMIFXZ3Q.mjs +0 -432
  498. package/dist/react-native/chunks/chunk-DVDX4SDR.mjs +0 -375
  499. package/dist/react-native/chunks/chunk-DXRSEWA2.mjs +0 -12
  500. package/dist/react-native/chunks/chunk-F753DA54.mjs +0 -23
  501. package/dist/react-native/chunks/chunk-FHPW7P2O.mjs +0 -40
  502. package/dist/react-native/chunks/chunk-FTZHMRD2.mjs +0 -18
  503. package/dist/react-native/chunks/chunk-HJBJBH26.mjs +0 -34
  504. package/dist/react-native/chunks/chunk-IAENKE2D.mjs +0 -21
  505. package/dist/react-native/chunks/chunk-IO2SMUQD.mjs +0 -10
  506. package/dist/react-native/chunks/chunk-IYNT75CE.mjs +0 -111
  507. package/dist/react-native/chunks/chunk-JDOEEMEF.mjs +0 -38
  508. package/dist/react-native/chunks/chunk-JWUCMVRK.mjs +0 -15
  509. package/dist/react-native/chunks/chunk-KSBYEMCB.mjs +0 -100
  510. package/dist/react-native/chunks/chunk-KU646XXT.mjs +0 -164
  511. package/dist/react-native/chunks/chunk-LU3Y3P63.mjs +0 -68
  512. package/dist/react-native/chunks/chunk-LVIKKAA6.mjs +0 -93
  513. package/dist/react-native/chunks/chunk-LZSLNSUN.mjs +0 -193
  514. package/dist/react-native/chunks/chunk-MJDQU5MW.mjs +0 -1225
  515. package/dist/react-native/chunks/chunk-NNE4VDMM.mjs +0 -8
  516. package/dist/react-native/chunks/chunk-NVVRSRX5.mjs +0 -78
  517. package/dist/react-native/chunks/chunk-OCSLSH27.mjs +0 -22723
  518. package/dist/react-native/chunks/chunk-PB2YXVQW.mjs +0 -29
  519. package/dist/react-native/chunks/chunk-PJHAB5ZY.mjs +0 -79
  520. package/dist/react-native/chunks/chunk-PSWKQVVT.mjs +0 -9
  521. package/dist/react-native/chunks/chunk-RDWG33MM.mjs +0 -79
  522. package/dist/react-native/chunks/chunk-SK2PWYDH.mjs +0 -654
  523. package/dist/react-native/chunks/chunk-SUPXRTMD.mjs +0 -96
  524. package/dist/react-native/chunks/chunk-TORU2OHQ.mjs +0 -218
  525. package/dist/react-native/chunks/chunk-U4KSKA4Z.mjs +0 -23
  526. package/dist/react-native/chunks/chunk-UNUFZ2QY.mjs +0 -68
  527. package/dist/react-native/chunks/chunk-WBYQRHDY.mjs +0 -86
  528. package/dist/react-native/chunks/chunk-WVWJNUMN.mjs +0 -2562
  529. package/dist/react-native/chunks/chunk-YH7QY657.mjs +0 -288
  530. package/dist/react-native/chunks/chunk-YZW4CHM6.mjs +0 -562
  531. package/dist/react-native/chunks/chunk-ZNZ6CPAZ.mjs +0 -562
  532. package/dist/react-native/chunks/chunk-ZVQMBF2H.mjs +0 -63
  533. package/dist/react-native/chunks/chunk-ZWAIN4GG.mjs +0 -2636
  534. package/dist/react-native/chunks/chunk-ZZT7CEN7.mjs +0 -92
  535. package/dist/react-native/chunks/cold-module-N6KFNNZA.mjs +0 -773
  536. package/dist/react-native/chunks/durability-wiring-RN64S2TL.mjs +0 -4984
  537. package/dist/react-native/chunks/engine-hooks-CWA5CKCA.mjs +0 -30
  538. package/dist/react-native/chunks/gzip.react-native-HLEPDKZ3.mjs +0 -3
  539. package/dist/react-native/chunks/import-5UAWBRJP.mjs +0 -32
  540. package/dist/react-native/chunks/methods-6W5U4C3H.mjs +0 -30
  541. package/dist/react-native/chunks/module-6RP4ZCBW.mjs +0 -37
  542. package/dist/react-native/chunks/operations-GNRMD6EX.mjs +0 -5
  543. package/dist/react-native/chunks/phrase-module-QK22HSQ2.mjs +0 -329
  544. package/dist/react-native/chunks/pool-JR3NJCAF.mjs +0 -297
  545. package/dist/react-native/chunks/release-2SIHZWDO.mjs +0 -32
  546. package/dist/react-native/chunks/restore-hooks-ENFMDTWC.mjs +0 -127
  547. package/dist/react-native/chunks/restore-queue-4R6AASE5.mjs +0 -55
  548. package/dist/react-native/chunks/retention-5GTFT3GR.mjs +0 -223
  549. package/dist/react-native/chunks/snapshot-5LGPQXFS.mjs +0 -976
  550. package/dist/react-native/chunks/start-ZQJVZ5DN.mjs +0 -38
  551. package/dist/react-native/chunks/sync-state-PASCIHHT.mjs +0 -15
  552. package/dist/react-native/chunks/write-6BRYIIZ7.mjs +0 -208
  553. package/dist/release-SMR4WJD4.mjs +0 -32
  554. package/dist/restore-hooks-HXLIEB4A.mjs +0 -153
  555. package/dist/restore-queue-LZH7LMLR.mjs +0 -55
  556. package/dist/retention-3K7ARGAG.mjs +0 -66
  557. package/dist/runtime-COIUORCN.mjs +0 -177
  558. package/dist/snapshot-O7ICP7NJ.mjs +0 -783
  559. package/dist/start-S4PFP6RS.mjs +0 -66
  560. package/dist/sync-state-ZWHLAFLE.mjs +0 -15
  561. package/dist/thread-YLJ3ZPQ3.mjs +0 -50
  562. package/dist/write-2OBJGLFS.mjs +0 -233
  563. /package/dist/browser/{chunk-M3MQE2X7.mjs → digest-leaf-64IPRPZD.mjs} +0 -0
  564. /package/dist/browser/{chunk-JNOAKDUM.mjs → gzip-JF3S6PPT.mjs} +0 -0
  565. /package/dist/{chunk-ZXNNR73R.mjs → gzip-2UCZW2MM.mjs} +0 -0
@@ -0,0 +1,2627 @@
1
+ import { buildSegmentPayloads, freezeSegmentShared, isValidWorkerResponse, createRequestId, mergeSegmentContents, freezeEncodedSegmentShared, toError, isDeterministicFailure, workerIneligibility, reportIneligible, selectionKeeps, alreadyPresentOnWorker, eligibleIndexNames } from './chunk-AQOWRH2C.mjs';
2
+ import { growPartitionsTo, isCompositePartition, createSharedFrozenSegment, loadStorageCode, resolvePartitionInsertOptions, DEFAULT_COPY_THRESHOLD, workersEnabledByDefault, POOL_RESTART_DELAY_MS, SegmentRefusal, NO_REMOVED_SPELLINGS, DEFAULT_COPY_IDLE_TIMEOUT_MS, LONG_QUERY_THREADS, prepareDocumentVectors, MAX_PENDING_REPLICATION_DOCUMENTS, MAIN_COPY_LONE_QUERY_DOCUMENTS, returnsEveryMatch, oversampledFacetConfig, afterCurrentTurn, LOAD_BUFFER_YIELD_INTERVAL, yieldToEventLoop, IDLE_MERGE_DELAY_MS, REPLICATION_WINDOW, RETIRED_THREADS_EXIT_WAIT_MS, createWorkerFactory, availablePatternSearch, patternHost, kWayMerge, mergeFacets, generateId, encodeSegmentState, POOL_RESTART_DELAY_MAX_MS, COMPACTION_SEGMENT_TRIGGER, LIVE_TAIL_FREEZE_FLOOR, requireLoadedVectorCode, LIVE_TAIL_FLUSH_DOCUMENTS, SEGMENTS_PER_MERGE } from './chunk-L4R5ITVG.mjs';
3
+ import './chunk-52QD7CBJ.mjs';
4
+ import { resolveWorkerCount, DEFAULT_BACKPRESSURE_LIMIT, DEFAULT_REQUEST_TIMEOUT_MS, splitWorkerBudget, SHUTDOWN_TIMEOUT_MS, MAX_SWEEP_INTERVAL_MS, MIN_SWEEP_INTERVAL_MS, resolveRequestThreadCount, CONSECUTIVE_TIMEOUTS_BEFORE_DEATH, fnv1a, noteThreadSlotGone, threadSlotOfWorker } from './chunk-XEGKQKLY.mjs';
5
+ import { MATCHER_STATE_BUDGET_BYTES } from './chunk-4EXKQ2IB.mjs';
6
+ import './chunk-G3CUQ333.mjs';
7
+ import './chunk-PVMEHBQO.mjs';
8
+ import './chunk-YOTP2FPE.mjs';
9
+ import { schemaFieldsOf } from './chunk-FTQ7JNMU.mjs';
10
+ import './chunk-HRDMIQ63.mjs';
11
+ import './chunk-VUJCSQG5.mjs';
12
+ import { NarsilError, ErrorCodes } from './chunk-TQ26BKBH.mjs';
13
+
14
+ // src/engine/orchestration/worker-copies-host.ts
15
+ function bindWorkerCopiesHost(_host, _segmentCodec, _segmentMerge) {
16
+ }
17
+
18
+ // src/workers/pool-memory.ts
19
+ function toFinite(value) {
20
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
21
+ }
22
+ function toHeapLimit(value) {
23
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : null;
24
+ }
25
+ async function memoryStatsOf(slots) {
26
+ const indices = [];
27
+ const pending = [];
28
+ for (const [slotIndex, slot] of slots) {
29
+ indices.push(slotIndex);
30
+ pending.push(
31
+ slot.executor.execute({ type: "memoryReport", requestId: createRequestId() }).catch(() => null)
32
+ );
33
+ }
34
+ const reports = await Promise.all(pending);
35
+ const stats = [];
36
+ for (let n = 0; n < indices.length; n++) {
37
+ const report = reports[n];
38
+ stats.push({
39
+ workerId: indices[n],
40
+ heapUsed: toFinite(report?.heapUsed),
41
+ heapTotal: toFinite(report?.heapTotal),
42
+ heapLimit: toHeapLimit(report?.heapLimit),
43
+ external: toFinite(report?.external)
44
+ });
45
+ }
46
+ return stats;
47
+ }
48
+
49
+ // src/workers/pool.ts
50
+ function createWorkerPool(config) {
51
+ const workerCount = resolveWorkerCount(config.count);
52
+ const queryWorkerCount = workerCount - Math.min(config.longQueryWorkers ?? 0, workerCount - 1);
53
+ const workers = /* @__PURE__ */ new Map();
54
+ const deadSlots = /* @__PURE__ */ new Set();
55
+ const goneSlots = /* @__PURE__ */ new Set();
56
+ const threadsStillRunning = /* @__PURE__ */ new Map();
57
+ const waitingForEveryThread = [];
58
+ const indexAssignment = /* @__PURE__ */ new Map();
59
+ let isShutdown = false;
60
+ function handleWorkerDeath(slotIndex, error) {
61
+ if (isShutdown) {
62
+ return;
63
+ }
64
+ const slot = workers.get(slotIndex);
65
+ if (slot !== void 0 && !slot.serving) {
66
+ workers.delete(slotIndex);
67
+ goneSlots.delete(slotIndex);
68
+ return;
69
+ }
70
+ if (deadSlots.has(slotIndex)) {
71
+ return;
72
+ }
73
+ deadSlots.add(slotIndex);
74
+ const indexNames = slot ? [...slot.indexes].sort() : [];
75
+ workers.delete(slotIndex);
76
+ config.onWorkerCrash?.(slotIndex, indexNames, error);
77
+ }
78
+ function handleWorkerGone(slotIndex) {
79
+ if (!deadSlots.has(slotIndex) || workers.has(slotIndex)) {
80
+ return;
81
+ }
82
+ goneSlots.add(slotIndex);
83
+ config.onWorkerGone?.(slotIndex);
84
+ }
85
+ function spawnSlot(slotIndex, serving) {
86
+ const slot = {
87
+ executor: config.workerFactory(
88
+ slotIndex,
89
+ (error) => handleWorkerDeath(slotIndex, error),
90
+ () => {
91
+ noteThreadGone(slot.executor);
92
+ handleWorkerGone(slotIndex);
93
+ }
94
+ ),
95
+ indexes: /* @__PURE__ */ new Set(),
96
+ inFlight: 0,
97
+ serving
98
+ };
99
+ let finish = () => void 0;
100
+ const gone = new Promise((resolve) => {
101
+ finish = resolve;
102
+ });
103
+ threadsStillRunning.set(slot.executor, { indexes: slot.indexes, gone, finish });
104
+ workers.set(slotIndex, slot);
105
+ return slot;
106
+ }
107
+ function noteThreadGone(executor) {
108
+ threadsStillRunning.get(executor)?.finish();
109
+ threadsStillRunning.delete(executor);
110
+ if (threadsStillRunning.size > 0) return;
111
+ for (const resume of waitingForEveryThread.splice(0)) resume();
112
+ }
113
+ function whenEveryThreadIsGone() {
114
+ if (threadsStillRunning.size === 0) return Promise.resolve();
115
+ return new Promise((resolve) => {
116
+ waitingForEveryThread.push(resolve);
117
+ });
118
+ }
119
+ async function whenRetiredCopiesAreGone(indexName) {
120
+ const active = new Set(getAllExecutors());
121
+ const pending = [];
122
+ for (const [executor, lifetime] of threadsStillRunning) {
123
+ if (!active.has(executor) && lifetime.indexes.has(indexName)) pending.push(lifetime.gone);
124
+ }
125
+ await Promise.all(pending);
126
+ }
127
+ function shutDownUntilTheThreadIsGone(executor) {
128
+ return executor.shutdown().then(() => noteThreadGone(executor));
129
+ }
130
+ function ensureWorker(slotIndex) {
131
+ const slot = workers.get(slotIndex);
132
+ if (slot !== void 0) return slot;
133
+ if (deadSlots.has(slotIndex)) return void 0;
134
+ return spawnSlot(slotIndex, true);
135
+ }
136
+ function deadWorkerIds() {
137
+ return [...deadSlots].filter((slotIndex) => goneSlots.has(slotIndex) && !workers.has(slotIndex)).sort((a, b) => a - b);
138
+ }
139
+ function spawnReplacement(slotIndex) {
140
+ if (isShutdown || !deadSlots.has(slotIndex) || !goneSlots.has(slotIndex) || workers.has(slotIndex)) return null;
141
+ const slot = spawnSlot(slotIndex, false);
142
+ return {
143
+ workerId: slotIndex,
144
+ executor: slot.executor,
145
+ hold(indexName) {
146
+ slot.indexes.add(indexName);
147
+ },
148
+ admit() {
149
+ if (workers.get(slotIndex) !== slot) return;
150
+ deadSlots.delete(slotIndex);
151
+ goneSlots.delete(slotIndex);
152
+ slot.serving = true;
153
+ },
154
+ abandon() {
155
+ if (workers.get(slotIndex) === slot) workers.delete(slotIndex);
156
+ void shutDownUntilTheThreadIsGone(slot.executor).catch(() => void 0);
157
+ }
158
+ };
159
+ }
160
+ function isLongQueryWorker(workerId) {
161
+ return workerId >= queryWorkerCount;
162
+ }
163
+ function assignSlot(indexName) {
164
+ const preferred = fnv1a(indexName) % queryWorkerCount;
165
+ for (let probe = 0; probe < queryWorkerCount; probe++) {
166
+ const candidate = (preferred + probe) % queryWorkerCount;
167
+ if (!deadSlots.has(candidate)) {
168
+ return candidate;
169
+ }
170
+ }
171
+ throw new NarsilError(ErrorCodes.WORKER_CRASHED, `Every worker in the pool has crashed`);
172
+ }
173
+ function addIndex(indexName) {
174
+ if (isShutdown) {
175
+ throw new NarsilError(ErrorCodes.WORKER_CRASHED, "Worker pool has been shut down");
176
+ }
177
+ if (indexAssignment.has(indexName)) {
178
+ return;
179
+ }
180
+ const slotIndex = assignSlot(indexName);
181
+ const slot = ensureWorker(slotIndex);
182
+ if (!slot) {
183
+ throw new NarsilError(ErrorCodes.WORKER_CRASHED, `Worker ${slotIndex} for index "${indexName}" has crashed`);
184
+ }
185
+ slot.indexes.add(indexName);
186
+ indexAssignment.set(indexName, slotIndex);
187
+ }
188
+ function removeIndex(indexName) {
189
+ for (const slot of workers.values()) slot.indexes.delete(indexName);
190
+ indexAssignment.delete(indexName);
191
+ }
192
+ function getExecutor(indexName) {
193
+ if (isShutdown) {
194
+ throw new NarsilError(ErrorCodes.WORKER_CRASHED, "Worker pool has been shut down");
195
+ }
196
+ const slotIndex = indexAssignment.get(indexName);
197
+ if (slotIndex === void 0) {
198
+ throw new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Index "${indexName}" is not registered in the worker pool`);
199
+ }
200
+ const slot = workers.get(slotIndex);
201
+ if (slot?.serving) {
202
+ return slot.executor;
203
+ }
204
+ for (const survivor of workers.values()) {
205
+ if (survivor.serving && survivor.indexes.has(indexName)) {
206
+ return survivor.executor;
207
+ }
208
+ }
209
+ throw new NarsilError(ErrorCodes.WORKER_CRASHED, `Every worker holding index "${indexName}" has crashed`);
210
+ }
211
+ async function shutdown() {
212
+ if (isShutdown) {
213
+ return;
214
+ }
215
+ isShutdown = true;
216
+ const shutdownPromises = [...threadsStillRunning.keys()].map((executor) => {
217
+ let timeoutId;
218
+ const timeoutPromise = new Promise((resolve) => {
219
+ timeoutId = setTimeout(resolve, SHUTDOWN_TIMEOUT_MS);
220
+ });
221
+ return Promise.race([shutDownUntilTheThreadIsGone(executor), timeoutPromise]).finally(
222
+ () => clearTimeout(timeoutId)
223
+ );
224
+ });
225
+ await Promise.allSettled(shutdownPromises);
226
+ indexAssignment.clear();
227
+ }
228
+ function getAllExecutors() {
229
+ return [...workers.values()].map((slot) => slot.executor);
230
+ }
231
+ function executorEntries() {
232
+ return [...workers].map(([workerId, slot]) => ({ workerId, executor: slot.executor }));
233
+ }
234
+ function executorsHolding(indexName) {
235
+ const holders = [];
236
+ for (const slot of workers.values()) {
237
+ if (slot.indexes.has(indexName)) holders.push(slot.executor);
238
+ }
239
+ return holders;
240
+ }
241
+ function lease(workerId, slot) {
242
+ slot.inFlight += 1;
243
+ let released = false;
244
+ return {
245
+ workerId,
246
+ executor: slot.executor,
247
+ release() {
248
+ if (released) return;
249
+ released = true;
250
+ slot.inFlight -= 1;
251
+ }
252
+ };
253
+ }
254
+ function leaseLeastBusy(indexName) {
255
+ let chosenId = -1;
256
+ let chosen = null;
257
+ for (const [workerId, slot] of workers) {
258
+ if (!slot.serving || isLongQueryWorker(workerId)) continue;
259
+ if (indexName !== void 0 && !slot.indexes.has(indexName)) continue;
260
+ if (chosen === null || slot.inFlight < chosen.inFlight) {
261
+ chosenId = workerId;
262
+ chosen = slot;
263
+ }
264
+ if (chosen.inFlight === 0) break;
265
+ }
266
+ return chosen === null ? null : lease(chosenId, chosen);
267
+ }
268
+ function queriesInFlight() {
269
+ let total = 0;
270
+ for (const [workerId, slot] of workers) {
271
+ if (slot.serving && !isLongQueryWorker(workerId)) total += slot.inFlight;
272
+ }
273
+ return total;
274
+ }
275
+ function leaseIdle(limit) {
276
+ const leases = [];
277
+ for (const [workerId, slot] of workers) {
278
+ if (leases.length >= limit) break;
279
+ if (slot.serving && slot.inFlight === 0 && !isLongQueryWorker(workerId)) leases.push(lease(workerId, slot));
280
+ }
281
+ return leases;
282
+ }
283
+ function addIndexToAll(indexName) {
284
+ if (isShutdown) {
285
+ throw new NarsilError(ErrorCodes.WORKER_CRASHED, "Worker pool has been shut down");
286
+ }
287
+ for (let i = 0; i < workerCount; i++) {
288
+ ensureWorker(i)?.indexes.add(indexName);
289
+ }
290
+ indexAssignment.set(indexName, assignSlot(indexName));
291
+ }
292
+ function spawnAll() {
293
+ if (isShutdown) {
294
+ throw new NarsilError(ErrorCodes.WORKER_CRASHED, "Worker pool has been shut down");
295
+ }
296
+ for (let i = 0; i < workerCount; i++) ensureWorker(i);
297
+ }
298
+ function retire(executor, error) {
299
+ for (const [workerId, slot] of workers) {
300
+ if (slot.executor !== executor) continue;
301
+ handleWorkerDeath(workerId, error);
302
+ void shutDownUntilTheThreadIsGone(executor).catch(() => void 0);
303
+ return;
304
+ }
305
+ }
306
+ return {
307
+ getExecutor,
308
+ getAllExecutors,
309
+ executorEntries,
310
+ executorsHolding,
311
+ leaseLeastBusy,
312
+ leaseIdle,
313
+ isLongQueryWorker,
314
+ queriesInFlight,
315
+ spawnAll,
316
+ deadWorkerIds,
317
+ spawnReplacement,
318
+ retire,
319
+ get workerCount() {
320
+ return workerCount;
321
+ },
322
+ addIndex,
323
+ addIndexToAll,
324
+ removeIndex,
325
+ getMemoryStats: () => memoryStatsOf(workers.entries()),
326
+ shutdown,
327
+ whenEveryThreadIsGone,
328
+ whenRetiredCopiesAreGone,
329
+ whenExecutorIsGone: (executor) => threadsStillRunning.get(executor)?.gone ?? Promise.resolve()
330
+ };
331
+ }
332
+
333
+ // src/core/partition/frozen/merge-split.ts
334
+ function splitMergeSelections(selections) {
335
+ let count = 0;
336
+ for (const selection of selections) {
337
+ for (let ordinal = 0; ordinal < selection.segment.arrays.documentCount; ordinal++) {
338
+ if (selectionKeeps(selection, ordinal)) count++;
339
+ }
340
+ }
341
+ if (count < 2) throw new SegmentRefusal(8);
342
+ const middle = Math.ceil(count / 2);
343
+ const halves = [[], []];
344
+ let at = 0;
345
+ for (const selection of selections) {
346
+ const marks = new Uint8Array(selection.segment.arrays.documentCount);
347
+ let first = false;
348
+ let second = false;
349
+ for (let ordinal = 0; ordinal < marks.length; ordinal++) {
350
+ if (!selectionKeeps(selection, ordinal)) continue;
351
+ if (at++ < middle) {
352
+ marks[ordinal] = 1;
353
+ first = true;
354
+ } else {
355
+ marks[ordinal] = 2;
356
+ second = true;
357
+ }
358
+ }
359
+ if (first) halves[0].push({ segment: selection.segment, keep: (ordinal) => marks[ordinal] === 1 });
360
+ if (second) halves[1].push({ segment: selection.segment, keep: (ordinal) => marks[ordinal] === 2 });
361
+ }
362
+ return halves;
363
+ }
364
+ function mergeFrozenSegmentOutputs(segments, analysis, textAnalysis) {
365
+ if (typeof SharedArrayBuffer !== "function") return null;
366
+ const outputs = [];
367
+ const pending = [segments.map((segment) => ({ segment }))];
368
+ while (pending.length > 0) {
369
+ const selections = pending.pop();
370
+ if (selections === void 0) break;
371
+ try {
372
+ const merged = mergeSegmentContents(selections, analysis, textAnalysis);
373
+ const snapshot = freezeEncodedSegmentShared(
374
+ merged.payload,
375
+ merged.documents,
376
+ void 0,
377
+ analysis,
378
+ merged.reusedPartialWords
379
+ );
380
+ if (snapshot === null) return null;
381
+ outputs.push(snapshot);
382
+ } catch (error) {
383
+ if (!(error instanceof SegmentRefusal) || error.status !== 8) throw error;
384
+ const [first, second] = splitMergeSelections(selections);
385
+ pending.push(second, first);
386
+ }
387
+ }
388
+ return outputs;
389
+ }
390
+
391
+ // src/workers/segment-actions.ts
392
+ function isPagedSnapshot(snapshot) {
393
+ return "path" in snapshot;
394
+ }
395
+ async function attachSegments(entry, action) {
396
+ for (const segment of action.segments) {
397
+ growPartitionsTo(entry.manager, segment.partitionId);
398
+ const surfaceForms = segment.survivingSurfaceForms;
399
+ const spellings = surfaceForms === void 0 ? void 0 : NO_REMOVED_SPELLINGS;
400
+ const frozen = isPagedSnapshot(segment.snapshot) ? (await loadStorageCode()).createPagedFrozenSegment(segment.snapshot) : createSharedFrozenSegment(segment.snapshot, surfaceForms);
401
+ for (const docId of segment.tombstonedDocIds ?? []) frozen.tombstoneDocument(docId, spellings);
402
+ entry.manager.attachFrozenSegment(segment.partitionId, frozen);
403
+ }
404
+ }
405
+ function requireComposite(entry, indexName, partitionId, verb) {
406
+ const partition = entry.manager.getPartition(partitionId);
407
+ if (!isCompositePartition(partition)) {
408
+ throw new NarsilError(
409
+ ErrorCodes.PARTITION_CORRUPTED,
410
+ `Partition ${partitionId} of "${indexName}" holds no frozen segments to ${verb}`,
411
+ { indexName, partitionId }
412
+ );
413
+ }
414
+ return partition;
415
+ }
416
+ function runSegmentAction(entry, action) {
417
+ switch (action.type) {
418
+ case "buildSegment": {
419
+ const options = resolvePartitionInsertOptions(entry.config, entry.manager.analysis, action.options);
420
+ const outputs = buildSegmentPayloads(
421
+ action.documents,
422
+ entry.config.schema,
423
+ entry.language,
424
+ options,
425
+ entry.config.trackPositions ?? true
426
+ );
427
+ return outputs.map(({ payload, documents }, index) => {
428
+ const snapshot = freezeSegmentShared(
429
+ payload,
430
+ documents.map((doc) => doc.document),
431
+ index === 0 ? action.segmentId : void 0
432
+ );
433
+ return snapshot === null ? { kind: "payload", payload } : { kind: "shared", snapshot };
434
+ });
435
+ }
436
+ case "mergeSegments": {
437
+ for (const segment of action.segments) {
438
+ if (segment.payload.docIds.some((docId) => entry.manager.has(docId))) {
439
+ console.warn(
440
+ `Skipping replicated segment for index "${action.indexName}": its documents already exist on this copy`
441
+ );
442
+ continue;
443
+ }
444
+ entry.manager.mergeSegment(segment.partitionId, segment.payload, segment.documents);
445
+ }
446
+ return void 0;
447
+ }
448
+ case "attachSegments":
449
+ return attachSegments(entry, action);
450
+ case "adoptDiskSegments":
451
+ return loadStorageCode().then((code) => code.adoptSegmentsOnWorker(entry.manager, action));
452
+ case "compactSegments": {
453
+ const partition = requireComposite(entry, action.indexName, action.partitionId, "compact");
454
+ return mergeFrozenSegmentOutputs(
455
+ partition.frozenSegmentsById(action.segmentIds),
456
+ entry.manager.partialWordAnalysis,
457
+ entry.manager.textAnalysis
458
+ );
459
+ }
460
+ case "swapSegments": {
461
+ requireComposite(entry, action.indexName, action.partitionId, "swap");
462
+ entry.manager.swapFrozenSegments(
463
+ action.partitionId,
464
+ action.dropSegmentIds,
465
+ action.snapshots.map((snapshot) => createSharedFrozenSegment(snapshot)),
466
+ action.removedSpellings
467
+ );
468
+ return void 0;
469
+ }
470
+ case "freezeLiveTail": {
471
+ growPartitionsTo(entry.manager, action.partitionId);
472
+ const tail = entry.manager.getPartition(action.partitionId);
473
+ const held = isCompositePartition(tail) ? tail.live.count() : tail.count();
474
+ if (held !== action.snapshot.documentCount) {
475
+ console.warn(
476
+ `Partition ${action.partitionId} of "${action.indexName}" held ${held} live documents where the frozen tail holds ${action.snapshot.documentCount}`
477
+ );
478
+ }
479
+ entry.manager.replaceLiveTail(action.partitionId, createSharedFrozenSegment(action.snapshot));
480
+ return void 0;
481
+ }
482
+ }
483
+ }
484
+
485
+ // src/workers/worker-executor.ts
486
+ function errorFromEventLike(event) {
487
+ if (event instanceof Error) {
488
+ return event;
489
+ }
490
+ if (typeof event === "object" && event !== null) {
491
+ const detail = event;
492
+ if (detail.error instanceof Error) {
493
+ return detail.error;
494
+ }
495
+ if (typeof detail.message === "string" && detail.message.length > 0) {
496
+ return new Error(detail.message);
497
+ }
498
+ }
499
+ return new Error("The worker reported an error event carrying no detail");
500
+ }
501
+ function createWorkerExecutor(worker, config) {
502
+ const backpressureLimit = config?.backpressureLimit ?? DEFAULT_BACKPRESSURE_LIMIT;
503
+ const requestTimeout = config?.requestTimeout ?? DEFAULT_REQUEST_TIMEOUT_MS;
504
+ const pending = /* @__PURE__ */ new Map();
505
+ const retained = /* @__PURE__ */ new Map();
506
+ let deathError = null;
507
+ let shutdownRequested = false;
508
+ let consecutiveTimeouts = 0;
509
+ let gone = false;
510
+ const waitingForTheThreadToGo = [];
511
+ const reportsItsExit = typeof worker.on === "function";
512
+ function processResponse(msg) {
513
+ if (!isValidWorkerResponse(msg)) {
514
+ return;
515
+ }
516
+ consecutiveTimeouts = 0;
517
+ const response = msg;
518
+ retained.get(response.requestId)?.();
519
+ retained.delete(response.requestId);
520
+ const entry = pending.get(response.requestId);
521
+ if (!entry) {
522
+ return;
523
+ }
524
+ clearTimeout(entry.timeoutId);
525
+ pending.delete(response.requestId);
526
+ if (response.type === "error") {
527
+ entry.reject(new NarsilError(response.code, response.message, response.details));
528
+ } else {
529
+ entry.resolve(response.data);
530
+ }
531
+ }
532
+ function handleDeath(cause) {
533
+ if (deathError !== null) {
534
+ return;
535
+ }
536
+ const message = cause instanceof Error ? cause.message : String(cause);
537
+ deathError = new NarsilError(ErrorCodes.WORKER_CRASHED, `Worker died: ${message}`);
538
+ for (const [id, entry] of pending) {
539
+ clearTimeout(entry.timeoutId);
540
+ entry.reject(deathError);
541
+ pending.delete(id);
542
+ }
543
+ if (!shutdownRequested) {
544
+ config?.onDeath?.(cause instanceof Error ? cause : new Error(message));
545
+ }
546
+ }
547
+ function reportGone() {
548
+ if (gone) {
549
+ return;
550
+ }
551
+ gone = true;
552
+ for (const release of retained.values()) release();
553
+ retained.clear();
554
+ for (const resume of waitingForTheThreadToGo.splice(0)) resume();
555
+ config?.onGone?.();
556
+ }
557
+ function threadGoneWithin(timeoutMs) {
558
+ if (gone) return Promise.resolve(true);
559
+ return new Promise((resolve) => {
560
+ const timeoutId = setTimeout(() => resolve(false), timeoutMs);
561
+ waitingForTheThreadToGo.push(() => {
562
+ clearTimeout(timeoutId);
563
+ resolve(true);
564
+ });
565
+ });
566
+ }
567
+ function stopTheThread() {
568
+ try {
569
+ const stopping = worker.terminate?.();
570
+ if (stopping instanceof Promise) stopping.catch(() => void 0);
571
+ } catch {
572
+ }
573
+ }
574
+ function stopAWorkerThatReportsNoExit() {
575
+ stopTheThread();
576
+ reportGone();
577
+ }
578
+ function askTheThreadToLeave(requestId) {
579
+ try {
580
+ worker.postMessage({ type: "shutdown", requestId });
581
+ } catch {
582
+ }
583
+ }
584
+ function retireUnansweringWorker(cause) {
585
+ handleDeath(cause);
586
+ if (reportsItsExit) {
587
+ askTheThreadToLeave(createRequestId());
588
+ return;
589
+ }
590
+ stopAWorkerThatReportsNoExit();
591
+ }
592
+ if (typeof worker.on === "function") {
593
+ worker.on("message", (msg) => processResponse(msg));
594
+ worker.on("error", (cause) => {
595
+ handleDeath(cause);
596
+ });
597
+ worker.on("exit", (code) => {
598
+ handleDeath(new Error(`Worker exited with code ${String(code)}`));
599
+ reportGone();
600
+ });
601
+ } else if (typeof worker.addEventListener === "function") {
602
+ worker.addEventListener("message", (event) => {
603
+ const msg = event.data;
604
+ processResponse(msg);
605
+ });
606
+ worker.addEventListener("error", (event) => {
607
+ handleDeath(errorFromEventLike(event));
608
+ stopAWorkerThatReportsNoExit();
609
+ });
610
+ }
611
+ function declareTimedOut(requestId, entry) {
612
+ if (pending.get(requestId) !== entry) return;
613
+ pending.delete(requestId);
614
+ entry.reject(new NarsilError(ErrorCodes.WORKER_TIMEOUT, `Request ${requestId} timed out after ${requestTimeout}ms`));
615
+ consecutiveTimeouts += 1;
616
+ if (consecutiveTimeouts >= CONSECUTIVE_TIMEOUTS_BEFORE_DEATH) {
617
+ retireUnansweringWorker(new Error(`no answer to ${CONSECUTIVE_TIMEOUTS_BEFORE_DEATH} consecutive requests`));
618
+ }
619
+ }
620
+ function execute(action, transfer, settled2) {
621
+ if (deathError !== null) {
622
+ settled2?.();
623
+ return Promise.reject(deathError);
624
+ }
625
+ if (pending.size >= backpressureLimit) {
626
+ settled2?.();
627
+ return Promise.reject(
628
+ new NarsilError(ErrorCodes.WORKER_BUSY, `Backpressure limit of ${backpressureLimit} pending requests reached`)
629
+ );
630
+ }
631
+ const requestId = createRequestId();
632
+ const taggedAction = { ...action, requestId };
633
+ return new Promise((resolve, reject) => {
634
+ const entry = {
635
+ resolve,
636
+ reject,
637
+ timeoutId: setTimeout(() => {
638
+ entry.timeoutId = setTimeout(() => declareTimedOut(requestId, entry), 0);
639
+ }, requestTimeout)
640
+ };
641
+ pending.set(requestId, entry);
642
+ if (settled2 !== void 0) retained.set(requestId, settled2);
643
+ try {
644
+ if (transfer === void 0) worker.postMessage(taggedAction);
645
+ else worker.postMessage(taggedAction, transfer);
646
+ } catch (error) {
647
+ clearTimeout(entry.timeoutId);
648
+ pending.delete(requestId);
649
+ retained.delete(requestId);
650
+ settled2?.();
651
+ reject(error);
652
+ }
653
+ });
654
+ }
655
+ function stopOnceTheWorkerAcknowledges() {
656
+ const requestId = createRequestId();
657
+ const stop = () => {
658
+ pending.delete(requestId);
659
+ if (!reportsItsExit) stopAWorkerThatReportsNoExit();
660
+ };
661
+ pending.set(requestId, { resolve: stop, reject: () => void 0, timeoutId: void 0 });
662
+ askTheThreadToLeave(requestId);
663
+ }
664
+ async function shutdown() {
665
+ shutdownRequested = true;
666
+ if (deathError === null && !gone) stopOnceTheWorkerAcknowledges();
667
+ try {
668
+ if (await threadGoneWithin(SHUTDOWN_TIMEOUT_MS)) return;
669
+ if (!reportsItsExit) {
670
+ stopAWorkerThatReportsNoExit();
671
+ return;
672
+ }
673
+ worker.unref?.();
674
+ throw new NarsilError(ErrorCodes.WORKER_TIMEOUT, "Shutdown timed out");
675
+ } finally {
676
+ for (const [id, entry] of pending) {
677
+ clearTimeout(entry.timeoutId);
678
+ entry.reject(new NarsilError(ErrorCodes.WORKER_CRASHED, "Worker is shutting down"));
679
+ pending.delete(id);
680
+ }
681
+ }
682
+ }
683
+ return { execute, executeRetained: (action, settled2) => execute(action, void 0, settled2), shutdown };
684
+ }
685
+
686
+ // src/core/partition/frozen/share.ts
687
+ function shareFrozenSegment(segment, analysis) {
688
+ segment.settleRemovals();
689
+ if (segment.paged !== null) {
690
+ return { snapshot: segment.paged.snapshot, tombstonedDocIds: segment.tombstonedDocIds() };
691
+ }
692
+ if (segment.sharedSnapshot !== null) {
693
+ const tombstonedDocIds = segment.tombstonedDocIds();
694
+ if (tombstonedDocIds.length === 0 || !segment.removals.settled) {
695
+ return { snapshot: segment.sharedSnapshot, tombstonedDocIds };
696
+ }
697
+ return {
698
+ snapshot: segment.sharedSnapshot,
699
+ tombstonedDocIds,
700
+ survivingSurfaceForms: segment.surfaces.reader.serialize()
701
+ };
702
+ }
703
+ const payload = encodeSegmentState(segment);
704
+ const documents = payload.docIds.map((docId) => {
705
+ const stored = segment.docStore.get(docId);
706
+ if (stored === void 0) {
707
+ throw new NarsilError(ErrorCodes.PARTITION_CORRUPTED, `Document "${docId}" vanished while sharing a segment`, {
708
+ docId
709
+ });
710
+ }
711
+ return stored.fields;
712
+ });
713
+ const snapshot = freezeSegmentShared(payload, documents, segment.segmentId, analysis);
714
+ return snapshot === null ? null : { snapshot, tombstonedDocIds: [] };
715
+ }
716
+
717
+ // src/workers/copy-generation.ts
718
+ var generations = /* @__PURE__ */ new WeakMap();
719
+ function copyGenerationOf(manager) {
720
+ return generations.get(manager) ?? 0;
721
+ }
722
+ function advanceCopyGeneration(manager) {
723
+ const generation = copyGenerationOf(manager) + 1;
724
+ generations.set(manager, generation);
725
+ return generation;
726
+ }
727
+
728
+ // src/workers/copy-publication.ts
729
+ function holdCopyPublication(manager, advances) {
730
+ const held = [];
731
+ try {
732
+ const fields = [];
733
+ for (const [fieldName, vector] of manager.getVectorIndexes()) {
734
+ const pin = vector.holdCopyPublication();
735
+ held.push(pin);
736
+ fields.push({ fieldName, vector: pin.field });
737
+ }
738
+ const generation = advances ? advanceCopyGeneration(manager) : copyGenerationOf(manager);
739
+ let references = 0;
740
+ const retain = () => {
741
+ references++;
742
+ let released = false;
743
+ return () => {
744
+ if (released) return;
745
+ released = true;
746
+ if (--references === 0) for (const pin of held.splice(0)) pin.release();
747
+ };
748
+ };
749
+ return {
750
+ publication: { generation, fields },
751
+ release: retain(),
752
+ retain
753
+ };
754
+ } catch (error) {
755
+ for (const pin of held) pin.release();
756
+ throw error;
757
+ }
758
+ }
759
+
760
+ // src/workers/executor.ts
761
+ function executeRetained(executor, action, settled2) {
762
+ try {
763
+ return executor.executeRetained === void 0 ? executor.execute(action).finally(settled2) : executor.executeRetained(action, settled2);
764
+ } catch (error) {
765
+ settled2();
766
+ return Promise.reject(error);
767
+ }
768
+ }
769
+
770
+ // src/engine/worker-resync.ts
771
+ var copyLoadSequence = 0;
772
+ function emptySerializablePartition(manager, partitionId) {
773
+ return {
774
+ indexName: manager.indexName,
775
+ partitionId,
776
+ totalPartitions: manager.partitionCount,
777
+ language: manager.language.name,
778
+ schema: {},
779
+ docCount: 0,
780
+ avgDocLength: 0,
781
+ documents: {},
782
+ invertedIndex: {},
783
+ fieldIndexes: { numeric: {}, boolean: {}, enum: {}, geopoint: {} },
784
+ statistics: { totalDocuments: 0, totalFieldLengths: {}, averageFieldLengths: {}, docFrequencies: {} }
785
+ };
786
+ }
787
+ function captureLiveTail(partition) {
788
+ if (partition.count() === 0) return null;
789
+ const payload = partition.encodeSegment();
790
+ const documents = payload.docIds.map((docId) => {
791
+ const document = partition.getRef(docId);
792
+ if (document === void 0) {
793
+ throw new NarsilError(ErrorCodes.PARTITION_CORRUPTED, `Document "${docId}" vanished while copying a partition`, {
794
+ docId
795
+ });
796
+ }
797
+ return document;
798
+ });
799
+ return { payload, documents };
800
+ }
801
+ function captureComposite(manager, partitionId) {
802
+ const partition = manager.getPartition(partitionId);
803
+ if (!isCompositePartition(partition)) return null;
804
+ const segmentIds = partition.frozenSegmentSizes().map((size) => size.segmentId);
805
+ const frozen = [];
806
+ for (const segment of partition.frozenSegmentsById(segmentIds)) {
807
+ const shared = shareFrozenSegment(segment, manager.partialWordAnalysis);
808
+ if (shared === null) return null;
809
+ frozen.push(shared);
810
+ }
811
+ return {
812
+ partitionId,
813
+ empty: emptySerializablePartition(manager, partitionId),
814
+ live: captureLiveTail(partition.live),
815
+ frozen
816
+ };
817
+ }
818
+ function capturePartitions(manager) {
819
+ const captured = [];
820
+ for (let partitionId = 0; partitionId < manager.partitionCount; partitionId++) {
821
+ captured.push(
822
+ captureComposite(manager, partitionId) ?? {
823
+ partitionId,
824
+ empty: emptySerializablePartition(manager, partitionId),
825
+ live: captureLiveTail(manager.getPartition(partitionId)),
826
+ frozen: []
827
+ }
828
+ );
829
+ }
830
+ return captured;
831
+ }
832
+ async function captureCopy(manager) {
833
+ for (; ; ) {
834
+ const vectors = [...manager.getVectorIndexes()];
835
+ await Promise.all(vectors.map(([, vector]) => vector.prepareCopyPublication()));
836
+ const captured = await manager.publication.write(() => {
837
+ const current = manager.getVectorIndexes();
838
+ if (current.size !== vectors.length || vectors.some(([name, vector]) => current.get(name) !== vector)) return null;
839
+ if (vectors.some(([, vector]) => vector.maintenanceStatus().loadingFromFiles)) return null;
840
+ const partitions = capturePartitions(manager);
841
+ const { embedding, ...settings } = manager.config;
842
+ const config = structuredClone({
843
+ ...settings,
844
+ ...embedding === void 0 ? {} : {
845
+ embedding: {
846
+ fields: embedding.fields,
847
+ ...typeof embedding.adapter === "string" ? { adapter: embedding.adapter } : {}
848
+ }
849
+ }
850
+ });
851
+ const synonyms = manager.synonyms?.encodedRules.slice() ?? null;
852
+ return { captured: partitions, config, synonyms, held: holdCopyPublication(manager, false) };
853
+ });
854
+ if (captured !== null) return captured;
855
+ }
856
+ }
857
+ async function sendPartition(manager, executors, captured) {
858
+ const { indexName } = manager;
859
+ await Promise.all(
860
+ executors.map(
861
+ (workerExecutor) => workerExecutor.execute({
862
+ type: "deserialize",
863
+ indexName,
864
+ partitionId: captured.partitionId,
865
+ data: captured.empty,
866
+ requestId: `resync-${indexName}-${captured.partitionId}`
867
+ })
868
+ )
869
+ );
870
+ const live = captured.live;
871
+ if (live !== null) {
872
+ await Promise.all(
873
+ executors.map(
874
+ (workerExecutor) => workerExecutor.execute({
875
+ type: "mergeSegments",
876
+ indexName,
877
+ segments: [{ partitionId: captured.partitionId, payload: live.payload, documents: live.documents }],
878
+ requestId: `resync-live-${indexName}-${captured.partitionId}`
879
+ })
880
+ )
881
+ );
882
+ }
883
+ if (captured.frozen.length === 0) return;
884
+ const segments = captured.frozen.map((shared) => ({
885
+ partitionId: captured.partitionId,
886
+ snapshot: shared.snapshot,
887
+ tombstonedDocIds: shared.tombstonedDocIds,
888
+ survivingSurfaceForms: shared.survivingSurfaceForms
889
+ }));
890
+ await Promise.all(
891
+ executors.map(
892
+ (workerExecutor) => workerExecutor.execute({
893
+ type: "attachSegments",
894
+ indexName,
895
+ segments,
896
+ requestId: `resync-attach-${indexName}-${captured.partitionId}`
897
+ })
898
+ )
899
+ );
900
+ }
901
+ function transferIndexToPool(indexName, pool, source, manager, analysisStale = false) {
902
+ return loadIndexOntoWorkers(
903
+ indexName,
904
+ pool.getAllExecutors(),
905
+ source,
906
+ manager,
907
+ () => pool.addIndexToAll(indexName),
908
+ analysisStale
909
+ );
910
+ }
911
+ async function loadIndexOntoWorkers(indexName, executors, source, manager, onDropped, analysisStale = false) {
912
+ copyLoadSequence += 1;
913
+ const copyLoad = `${indexName}#${copyLoadSequence}`;
914
+ const { captured, held, config, synonyms } = await captureCopy(manager);
915
+ try {
916
+ source.onPublication?.(held.publication);
917
+ await Promise.allSettled(
918
+ executors.map(
919
+ (workerExecutor) => workerExecutor.execute({ type: "dropIndex", indexName, requestId: `resync-drop-${indexName}` })
920
+ )
921
+ );
922
+ onDropped?.();
923
+ const created = await Promise.allSettled(
924
+ executors.map(
925
+ (workerExecutor) => executeRetained(
926
+ workerExecutor,
927
+ {
928
+ type: "createIndex",
929
+ indexName,
930
+ config,
931
+ requestId: `resync-create-${indexName}`,
932
+ analysisStale,
933
+ copyLoad,
934
+ publication: held.publication,
935
+ ...synonyms !== null ? { synonyms } : {}
936
+ },
937
+ held.retain()
938
+ )
939
+ )
940
+ );
941
+ const failure = created.find((result) => result.status === "rejected");
942
+ if (failure?.status === "rejected") throw failure.reason;
943
+ for (const partition of captured) {
944
+ await sendPartition(manager, executors, partition);
945
+ }
946
+ return copyLoad;
947
+ } catch (error) {
948
+ await Promise.allSettled(
949
+ executors.map(
950
+ (executor) => executor.execute({
951
+ type: "dropIndex",
952
+ indexName,
953
+ requestId: `failed-load-${copyLoad}`
954
+ })
955
+ )
956
+ );
957
+ throw error;
958
+ } finally {
959
+ held.release();
960
+ }
961
+ }
962
+
963
+ // src/engine/orchestration/publication-stores.ts
964
+ function trackPublicationStores(state, publication) {
965
+ for (const { vector } of publication.fields) {
966
+ state.sharedVectorFields.set(vector.handle, vector.handles);
967
+ state.publishedVectorStores.add(vector.handle);
968
+ }
969
+ }
970
+ function prunePublicationStores(state) {
971
+ for (const handle of state.publishedVectorStores) {
972
+ const field = state.sharedVectorFields.get(handle);
973
+ if (field !== void 0) {
974
+ let pinned = false;
975
+ for (let slot = 0; slot < field.store.pins.length; slot++) {
976
+ if (Atomics.load(field.store.pins, slot) !== 0n) {
977
+ pinned = true;
978
+ break;
979
+ }
980
+ }
981
+ if (pinned) continue;
982
+ }
983
+ state.sharedVectorFields.delete(handle);
984
+ state.publishedVectorStores.delete(handle);
985
+ }
986
+ }
987
+
988
+ // src/engine/orchestration/replication.ts
989
+ async function dispatchToWorkers(state, action) {
990
+ const pool = state.workerPool;
991
+ if (!pool) return;
992
+ const executors = "indexName" in action ? pool.executorsHolding(action.indexName) : pool.getAllExecutors();
993
+ if ("indexName" in action && !state.scaledOutIndexes.has(action.indexName)) return;
994
+ const results = await Promise.allSettled(
995
+ executors.map((workerExecutor) => {
996
+ const release = retainPublicationOf(action);
997
+ return release === void 0 ? workerExecutor.execute(action) : executeRetained(workerExecutor, action, release);
998
+ })
999
+ );
1000
+ for (let i = 0; i < results.length; i++) {
1001
+ const result = results[i];
1002
+ if (result.status === "rejected") {
1003
+ if (action.publication === void 0 && action.type === "insert" && alreadyPresentOnWorker(result.reason)) {
1004
+ continue;
1005
+ }
1006
+ if (action.publication !== void 0) pool.retire(executors[i], toError(result.reason));
1007
+ console.warn("Worker replication failed:", result.reason);
1008
+ }
1009
+ }
1010
+ }
1011
+ function documentWeight(action) {
1012
+ if (action.type === "mergeSegments") {
1013
+ let total = 0;
1014
+ for (const segment of action.segments) {
1015
+ total += segment.documents.length;
1016
+ }
1017
+ return total;
1018
+ }
1019
+ if (action.type === "attachSegments") {
1020
+ let total = 0;
1021
+ for (const segment of action.segments) {
1022
+ total += segment.snapshot.documentCount;
1023
+ }
1024
+ return total;
1025
+ }
1026
+ if (action.type === "swapSegments") {
1027
+ let total = 0;
1028
+ for (const snapshot of action.snapshots) total += snapshot.documentCount;
1029
+ return total;
1030
+ }
1031
+ return 1;
1032
+ }
1033
+ var NO_VECTOR_FIELDS = /* @__PURE__ */ new Set();
1034
+ function vectorFieldPathsOf(state, indexName) {
1035
+ return state.indexRegistry.get(indexName)?.vectorFieldPaths ?? NO_VECTOR_FIELDS;
1036
+ }
1037
+ function withoutVectors(document, fieldPaths) {
1038
+ if (fieldPaths.size === 0) return document;
1039
+ return prepareDocumentVectors(document, fieldPaths).partitionDoc;
1040
+ }
1041
+ function detachCallerDocuments(state, action) {
1042
+ if (action.type !== "insert" && action.type !== "update" && action.type !== "mergeSegments") return action;
1043
+ const fieldPaths = vectorFieldPathsOf(state, action.indexName);
1044
+ if (action.type === "insert") {
1045
+ const stripped = withoutVectors(action.document, fieldPaths);
1046
+ const document = action.skipClone === true || stripped !== action.document ? stripped : structuredClone(stripped);
1047
+ return { ...action, document };
1048
+ }
1049
+ if (action.type === "update") {
1050
+ const stripped = withoutVectors(action.document, fieldPaths);
1051
+ return { ...action, document: stripped !== action.document ? stripped : structuredClone(stripped) };
1052
+ }
1053
+ return {
1054
+ ...action,
1055
+ segments: action.segments.map((segment) => {
1056
+ const documents = segment.documents.map((document) => withoutVectors(document, fieldPaths));
1057
+ const cloned = action.skipClone === true || fieldPaths.size > 0 ? documents : structuredClone(documents);
1058
+ return { ...segment, documents: cloned };
1059
+ })
1060
+ };
1061
+ }
1062
+ function partitionLedger(state, indexName, partitionId) {
1063
+ let byPartition = state.segmentLedger.get(indexName);
1064
+ if (byPartition === void 0) {
1065
+ byPartition = /* @__PURE__ */ new Map();
1066
+ state.segmentLedger.set(indexName, byPartition);
1067
+ }
1068
+ let entries = byPartition.get(partitionId);
1069
+ if (entries === void 0) {
1070
+ entries = [];
1071
+ byPartition.set(partitionId, entries);
1072
+ }
1073
+ return entries;
1074
+ }
1075
+ function recordSegmentBroadcast(state, action) {
1076
+ if (action.type === "clear") {
1077
+ state.segmentLedger.delete(action.indexName);
1078
+ return;
1079
+ }
1080
+ if (action.type === "attachSegments") {
1081
+ for (const segment of action.segments) {
1082
+ partitionLedger(state, action.indexName, segment.partitionId).push({
1083
+ segmentId: segment.snapshot.segmentId,
1084
+ documentCount: segment.snapshot.documentCount
1085
+ });
1086
+ }
1087
+ return;
1088
+ }
1089
+ if (action.type === "freezeLiveTail") {
1090
+ partitionLedger(state, action.indexName, action.partitionId).push({
1091
+ segmentId: action.snapshot.segmentId,
1092
+ documentCount: action.snapshot.documentCount
1093
+ });
1094
+ return;
1095
+ }
1096
+ if (action.type === "swapSegments") {
1097
+ const entries = partitionLedger(state, action.indexName, action.partitionId);
1098
+ const dropped = new Set(action.dropSegmentIds);
1099
+ const kept = entries.filter((entry) => !dropped.has(entry.segmentId));
1100
+ for (const snapshot of action.snapshots) {
1101
+ kept.push({ segmentId: snapshot.segmentId, documentCount: snapshot.documentCount });
1102
+ }
1103
+ entries.length = 0;
1104
+ entries.push(...kept);
1105
+ }
1106
+ }
1107
+ function queueFor(state, indexName) {
1108
+ let queue = state.replicationQueues.get(indexName);
1109
+ if (queue === void 0) {
1110
+ queue = {
1111
+ tail: Promise.resolve(),
1112
+ oldestRun: void 0,
1113
+ newestRun: void 0,
1114
+ runsInFlight: 0,
1115
+ pendingActions: 0,
1116
+ pendingDocuments: 0,
1117
+ drainWaiters: []
1118
+ };
1119
+ state.replicationQueues.set(indexName, queue);
1120
+ }
1121
+ return queue;
1122
+ }
1123
+ function dispatchGate(queue) {
1124
+ if (queue.runsInFlight < REPLICATION_WINDOW) return Promise.resolve();
1125
+ let run = queue.newestRun;
1126
+ for (let step = 1; step < REPLICATION_WINDOW && run !== void 0; step++) run = run.older;
1127
+ return run?.done ?? Promise.resolve();
1128
+ }
1129
+ function appendRun(queue, done) {
1130
+ const run = { done, older: queue.newestRun, newer: void 0 };
1131
+ if (queue.newestRun === void 0) queue.oldestRun = run;
1132
+ else queue.newestRun.newer = run;
1133
+ queue.newestRun = run;
1134
+ queue.runsInFlight += 1;
1135
+ return run;
1136
+ }
1137
+ function removeRun(queue, run) {
1138
+ if (run.older === void 0) queue.oldestRun = run.newer;
1139
+ else run.older.newer = run.newer;
1140
+ if (run.newer === void 0) queue.newestRun = run.older;
1141
+ else run.newer.older = run.older;
1142
+ run.older = void 0;
1143
+ run.newer = void 0;
1144
+ queue.runsInFlight -= 1;
1145
+ }
1146
+ function enqueueReplication(state, indexName, action) {
1147
+ const weight = documentWeight(action);
1148
+ recordSegmentBroadcast(state, action);
1149
+ const queue = queueFor(state, indexName);
1150
+ queue.pendingActions += 1;
1151
+ queue.pendingDocuments += weight;
1152
+ const gate = dispatchGate(queue);
1153
+ const entry = appendRun(queue, gate);
1154
+ const run = gate.then(async () => {
1155
+ try {
1156
+ if (action.publication !== void 0) {
1157
+ const manager = state.executor.getManager(indexName);
1158
+ await manager?.publication.afterQueuedWrites();
1159
+ if (state.executor.getManager(indexName) !== manager) return;
1160
+ }
1161
+ await dispatchToWorkers(state, action);
1162
+ } catch (err) {
1163
+ console.warn("Worker replication failed:", err);
1164
+ } finally {
1165
+ releasePublicationOf(action);
1166
+ removeRun(queue, entry);
1167
+ queue.pendingActions -= 1;
1168
+ queue.pendingDocuments -= weight;
1169
+ if (queue.pendingDocuments < MAX_PENDING_REPLICATION_DOCUMENTS && queue.drainWaiters.length > 0) {
1170
+ for (const resume of queue.drainWaiters.splice(0)) resume();
1171
+ }
1172
+ if (queue.pendingActions === 0 && state.replicationQueues.get(indexName) === queue) {
1173
+ state.replicationQueues.delete(indexName);
1174
+ }
1175
+ }
1176
+ });
1177
+ entry.done = run;
1178
+ queue.tail = run;
1179
+ }
1180
+ function markCopiesLoaded(state, indexName, copyLoad) {
1181
+ enqueueReplication(state, indexName, {
1182
+ type: "copyLoaded",
1183
+ indexName,
1184
+ copyLoad,
1185
+ requestId: `copy-loaded-${copyLoad}`
1186
+ });
1187
+ }
1188
+ function copiesWillReceive(state, indexName) {
1189
+ if (state.copyLoadBuffers.has(indexName) || state.replicationQueues.has(indexName)) return true;
1190
+ return state.workerPool !== null && state.scaledOutIndexes.has(indexName);
1191
+ }
1192
+ async function replicateToWorkers(state, action) {
1193
+ if (!("indexName" in action)) {
1194
+ if (state.workerPool === null) return;
1195
+ await awaitReplicationIdle(state);
1196
+ await dispatchToWorkers(state, action);
1197
+ return;
1198
+ }
1199
+ recordMainWrite(state, action);
1200
+ const waiting = replicationRoomWait(state, action.indexName);
1201
+ if (waiting !== null) await waiting;
1202
+ }
1203
+ function queueForCopies(state, action) {
1204
+ if (!("indexName" in action)) return;
1205
+ const loading = state.copyLoadBuffers.get(action.indexName);
1206
+ if (loading !== void 0) {
1207
+ loading.push(action);
1208
+ return;
1209
+ }
1210
+ if (state.workerPool === null || !state.scaledOutIndexes.has(action.indexName)) {
1211
+ releasePublicationOf(action);
1212
+ return;
1213
+ }
1214
+ enqueueReplication(state, action.indexName, action);
1215
+ }
1216
+ function recordMainWrite(state, action) {
1217
+ if (!("indexName" in action) || !copiesWillReceive(state, action.indexName)) return;
1218
+ queueForCopies(state, detachCallerDocuments(state, action));
1219
+ }
1220
+ function replicationRoomWait(state, indexName) {
1221
+ const loading = state.copyLoadBuffers.get(indexName);
1222
+ if (loading !== void 0) {
1223
+ const lastYield = state.copyLoadBufferYieldMarks.get(loading) ?? 0;
1224
+ if (loading.length - lastYield < LOAD_BUFFER_YIELD_INTERVAL) return null;
1225
+ state.copyLoadBufferYieldMarks.set(loading, loading.length);
1226
+ return yieldToEventLoop();
1227
+ }
1228
+ const queue = state.replicationQueues.get(indexName);
1229
+ if (queue === void 0 || queue.pendingDocuments < MAX_PENDING_REPLICATION_DOCUMENTS) return null;
1230
+ return new Promise((resume) => queue.drainWaiters.push(resume));
1231
+ }
1232
+ function settled(queue) {
1233
+ const runs = [queue.tail];
1234
+ for (let run = queue.oldestRun; run !== void 0; run = run.newer) runs.push(run.done);
1235
+ return Promise.all(runs).then(() => void 0);
1236
+ }
1237
+ function publicationCapacityWait(state, indexName) {
1238
+ const waiting = replicationRoomWait(state, indexName);
1239
+ return waiting === null ? null : awaitPublicationCapacity(state, indexName, waiting);
1240
+ }
1241
+ async function awaitPublicationCapacity(state, indexName, first) {
1242
+ await first;
1243
+ for (; ; ) {
1244
+ const waiting = replicationRoomWait(state, indexName);
1245
+ if (waiting === null) return;
1246
+ await waiting;
1247
+ }
1248
+ }
1249
+ async function awaitReplicationIdle(state, indexName) {
1250
+ if (indexName !== void 0) {
1251
+ const queue = state.replicationQueues.get(indexName);
1252
+ if (queue !== void 0) await settled(queue);
1253
+ return;
1254
+ }
1255
+ while (state.replicationQueues.size > 0) {
1256
+ await Promise.all(Array.from(state.replicationQueues.values(), settled));
1257
+ }
1258
+ }
1259
+ async function awaitWritesApplied(state, indexName) {
1260
+ let awaitedTransition;
1261
+ let awaitedRepair = null;
1262
+ for (; ; ) {
1263
+ const transition = state.copyTransitions.get(indexName);
1264
+ if (transition !== void 0 && transition !== awaitedTransition) {
1265
+ awaitedTransition = transition;
1266
+ await transition.done;
1267
+ continue;
1268
+ }
1269
+ const repair = state.poolRepair;
1270
+ if (repair !== null && repair !== awaitedRepair) {
1271
+ awaitedRepair = repair;
1272
+ await repair;
1273
+ continue;
1274
+ }
1275
+ await awaitReplicationIdle(state, indexName);
1276
+ return;
1277
+ }
1278
+ }
1279
+
1280
+ // src/engine/orchestration/publication.ts
1281
+ var heldPublications = /* @__PURE__ */ new WeakMap();
1282
+ function retainPublicationOf2(action) {
1283
+ return heldPublications.get(action)?.retain();
1284
+ }
1285
+ function releasePublicationOf2(action) {
1286
+ heldPublications.get(action)?.release();
1287
+ heldPublications.delete(action);
1288
+ }
1289
+ function publishToWorkers(state, action) {
1290
+ if (!("indexName" in action)) return;
1291
+ if (!state.copyLoadBuffers.has(action.indexName) && !state.scaledOutIndexes.has(action.indexName)) return;
1292
+ const manager = state.executor.getManager(action.indexName);
1293
+ if (manager === void 0) return;
1294
+ if (!state.scaledOutIndexes.has(action.indexName)) {
1295
+ for (const vector of manager.getVectorIndexes().values()) {
1296
+ if (vector.maintenanceStatus().loadingFromFiles) return;
1297
+ }
1298
+ }
1299
+ const detached = detachCallerDocuments(state, action);
1300
+ const held = holdCopyPublication(manager, true);
1301
+ const publication = { ...detached, publication: held.publication };
1302
+ heldPublications.set(publication, held);
1303
+ trackPublicationStores(state, held.publication);
1304
+ try {
1305
+ queueForCopies(state, publication);
1306
+ } catch (error) {
1307
+ releasePublicationOf2(publication);
1308
+ throw error;
1309
+ }
1310
+ }
1311
+
1312
+ // src/engine/orchestration/publication-code.ts
1313
+ var publicationLoaded = false;
1314
+ async function loadPublicationCode() {
1315
+ publicationLoaded = true;
1316
+ }
1317
+ function publishToWorkers2(state, action) {
1318
+ return publicationLoaded ? publishToWorkers(state, action) : void 0;
1319
+ }
1320
+ function releasePublicationOf(action) {
1321
+ if (publicationLoaded) releasePublicationOf2(action);
1322
+ }
1323
+ function retainPublicationOf(action) {
1324
+ return publicationLoaded ? retainPublicationOf2(action) : void 0;
1325
+ }
1326
+
1327
+ // src/engine/orchestration/live-tail.ts
1328
+ function liveTailCount(manager, partitionId) {
1329
+ if (partitionId >= manager.partitionCount) return 0;
1330
+ const partition = manager.getPartition(partitionId);
1331
+ return isCompositePartition(partition) ? partition.live.count() : partition.count();
1332
+ }
1333
+ function freezeIntoSharedSegment(manager, partitionId) {
1334
+ return manager.freezeLiveTail(partitionId, (payload, documents) => {
1335
+ const snapshot = freezeSegmentShared(payload, documents, void 0, manager.partialWordAnalysis);
1336
+ return snapshot === null ? null : createSharedFrozenSegment(snapshot);
1337
+ });
1338
+ }
1339
+ async function freezeLiveTailsBeforeCopiesLoad(manager) {
1340
+ for (let partitionId = 0; partitionId < manager.partitionCount; partitionId++) {
1341
+ try {
1342
+ await manager.publication.write(() => {
1343
+ if (liveTailCount(manager, partitionId) >= LIVE_TAIL_FREEZE_FLOOR) freezeIntoSharedSegment(manager, partitionId);
1344
+ });
1345
+ } catch (err) {
1346
+ console.warn(`Freezing the live tail of partition ${partitionId} of "${manager.indexName}" failed:`, err);
1347
+ }
1348
+ }
1349
+ }
1350
+ function freezeTail(state, indexName, manager, partitionId) {
1351
+ const segment = freezeIntoSharedSegment(manager, partitionId);
1352
+ if (segment === null || segment.sharedSnapshot === null) return;
1353
+ publishToWorkers2(state, {
1354
+ type: "freezeLiveTail",
1355
+ indexName,
1356
+ partitionId,
1357
+ snapshot: segment.sharedSnapshot,
1358
+ requestId: createRequestId()
1359
+ });
1360
+ }
1361
+ async function freezeLiveTail(state, indexName, manager, partitionId, floor) {
1362
+ if (!state.scaledOutIndexes.has(indexName)) return;
1363
+ if (liveTailCount(manager, partitionId) < floor) return;
1364
+ try {
1365
+ await manager.publication.write(() => {
1366
+ if (state.executor.getManager(indexName) !== manager || liveTailCount(manager, partitionId) < floor) return;
1367
+ freezeTail(state, indexName, manager, partitionId);
1368
+ });
1369
+ } catch (err) {
1370
+ console.warn(`Freezing the live tail of partition ${partitionId} of "${indexName}" failed:`, err);
1371
+ }
1372
+ }
1373
+ async function flushGrownTails(state, indexName) {
1374
+ if (!state.scaledOutIndexes.has(indexName)) return;
1375
+ const manager = state.executor.getManager(indexName);
1376
+ if (manager === void 0) return;
1377
+ for (let partitionId = 0; partitionId < manager.partitionCount; partitionId++) {
1378
+ await freezeLiveTail(state, indexName, manager, partitionId, LIVE_TAIL_FLUSH_DOCUMENTS);
1379
+ }
1380
+ }
1381
+
1382
+ // src/engine/orchestration/compaction.ts
1383
+ function smallestSegmentIds(entries, limit) {
1384
+ return [...entries].sort((a, b) => a.documentCount - b.documentCount).slice(0, limit).map((entry) => entry.segmentId);
1385
+ }
1386
+ function pickSegmentIds(entries, policy) {
1387
+ if (policy === "idle") {
1388
+ return entries.length >= 2 ? smallestSegmentIds(entries, SEGMENTS_PER_MERGE) : null;
1389
+ }
1390
+ return entries.length >= COMPACTION_SEGMENT_TRIGGER ? smallestSegmentIds(entries, COMPACTION_SEGMENT_TRIGGER) : null;
1391
+ }
1392
+ function mainCompositeOf(manager, partitionId) {
1393
+ if (partitionId >= manager.partitionCount) return null;
1394
+ const partition = manager.getPartition(partitionId);
1395
+ return isCompositePartition(partition) ? partition : null;
1396
+ }
1397
+ function candidateSegmentIds(state, indexName, manager, partitionId, policy) {
1398
+ const composite = mainCompositeOf(manager, partitionId);
1399
+ if (composite !== null) {
1400
+ const sizes = composite.frozenSegmentSizes().map((size) => ({ segmentId: size.segmentId, documentCount: size.liveDocumentCount }));
1401
+ const picks = pickSegmentIds(sizes, policy);
1402
+ if (picks !== null) return picks;
1403
+ }
1404
+ const ledger = state.segmentLedger.get(indexName)?.get(partitionId);
1405
+ return ledger === void 0 ? null : pickSegmentIds(ledger, policy);
1406
+ }
1407
+ function mainHoldsAll(composite, segmentIds) {
1408
+ if (composite === null) return false;
1409
+ const held = new Set(composite.frozenSegmentSizes().map((size) => size.segmentId));
1410
+ return segmentIds.every((segmentId) => held.has(segmentId));
1411
+ }
1412
+ async function compactOnWorker(state, indexName, partitionId, segmentIds) {
1413
+ const pool = state.workerPool;
1414
+ if (pool === null || !state.scaledOutIndexes.has(indexName)) return null;
1415
+ const lease = pool.leaseLeastBusy(indexName);
1416
+ if (lease === null) return null;
1417
+ try {
1418
+ return await lease.executor.execute({
1419
+ type: "compactSegments",
1420
+ indexName,
1421
+ partitionId,
1422
+ segmentIds: [...segmentIds],
1423
+ requestId: createRequestId()
1424
+ });
1425
+ } catch (err) {
1426
+ if (!(err instanceof NarsilError) || err.code !== ErrorCodes.PARTITION_CORRUPTED) {
1427
+ console.warn("Worker segment compaction failed:", err);
1428
+ }
1429
+ return null;
1430
+ } finally {
1431
+ lease.release();
1432
+ }
1433
+ }
1434
+ function ledgerHoldsAll(state, indexName, partitionId, segmentIds) {
1435
+ const entries = state.segmentLedger.get(indexName)?.get(partitionId);
1436
+ if (entries === void 0) return false;
1437
+ const held = new Set(entries.map((entry) => entry.segmentId));
1438
+ return segmentIds.every((segmentId) => held.has(segmentId));
1439
+ }
1440
+ function segmentsStillHeld(state, indexName, manager, partitionId, segmentIds, mergedOnMain) {
1441
+ if (state.executor.getManager(indexName) !== manager) return false;
1442
+ if (mergedOnMain === null) return ledgerHoldsAll(state, indexName, partitionId, segmentIds);
1443
+ return mainCompositeOf(manager, partitionId) === mergedOnMain && mainHoldsAll(mergedOnMain, segmentIds);
1444
+ }
1445
+ function forgetLedgerSegments(state, indexName, partitionId, segmentIds) {
1446
+ const entries = state.segmentLedger.get(indexName)?.get(partitionId);
1447
+ if (entries === void 0) return;
1448
+ const stale = new Set(segmentIds);
1449
+ const kept = entries.filter((entry) => !stale.has(entry.segmentId));
1450
+ entries.length = 0;
1451
+ entries.push(...kept);
1452
+ }
1453
+ async function compactPartitionSegments(state, indexName, manager, partitionId, segmentIds, policy) {
1454
+ await awaitReplicationIdle(state, indexName);
1455
+ if (policy === "idle" && !state.scaledOutIndexes.has(indexName)) return false;
1456
+ const composite = mainCompositeOf(manager, partitionId);
1457
+ const mainCanMerge = composite !== null && mainHoldsAll(composite, segmentIds);
1458
+ let snapshots = await compactOnWorker(state, indexName, partitionId, segmentIds);
1459
+ if (snapshots === null && mainCanMerge && composite !== null) {
1460
+ snapshots = mergeFrozenSegmentOutputs(
1461
+ composite.frozenSegmentsById(segmentIds),
1462
+ manager.partialWordAnalysis,
1463
+ manager.textAnalysis
1464
+ );
1465
+ }
1466
+ if (snapshots === null) {
1467
+ if (!mainCanMerge) forgetLedgerSegments(state, indexName, partitionId, segmentIds);
1468
+ return false;
1469
+ }
1470
+ const mergedOnMain = mainCanMerge ? composite : null;
1471
+ const replacement = snapshots.map((snapshot) => createSharedFrozenSegment(snapshot));
1472
+ const publication = await manager.publication.write(() => {
1473
+ if (!segmentsStillHeld(state, indexName, manager, partitionId, segmentIds, mergedOnMain)) return null;
1474
+ const removedSpellings = mainCanMerge ? manager.swapFrozenSegments(partitionId, segmentIds, replacement) : void 0;
1475
+ publishToWorkers2(state, {
1476
+ type: "swapSegments",
1477
+ indexName,
1478
+ partitionId,
1479
+ dropSegmentIds: [...segmentIds],
1480
+ snapshots,
1481
+ removedSpellings,
1482
+ requestId: createRequestId()
1483
+ });
1484
+ return true;
1485
+ });
1486
+ if (publication === null) return false;
1487
+ return snapshots.length < segmentIds.length;
1488
+ }
1489
+ async function compactIndexSegments(state, indexName, policy) {
1490
+ const manager = state.executor.getManager(indexName);
1491
+ if (!manager || manager.config.textStorage === "disk") return;
1492
+ const partitionIds = /* @__PURE__ */ new Set();
1493
+ for (let partitionId = 0; partitionId < manager.partitionCount; partitionId++) partitionIds.add(partitionId);
1494
+ for (const partitionId of state.segmentLedger.get(indexName)?.keys() ?? []) partitionIds.add(partitionId);
1495
+ for (const partitionId of partitionIds) {
1496
+ if (policy === "idle") {
1497
+ await awaitReplicationIdle(state, indexName);
1498
+ await freezeLiveTail(state, indexName, manager, partitionId, LIVE_TAIL_FREEZE_FLOOR);
1499
+ }
1500
+ let picks = candidateSegmentIds(state, indexName, manager, partitionId, policy);
1501
+ while (picks !== null) {
1502
+ const compacted = await compactPartitionSegments(state, indexName, manager, partitionId, picks, policy);
1503
+ if (!compacted) break;
1504
+ picks = candidateSegmentIds(state, indexName, manager, partitionId, policy);
1505
+ }
1506
+ }
1507
+ }
1508
+ function maybeCompactSegments(state, indexName, policy = "ingest") {
1509
+ if (state.compactionsInFlight.has(indexName)) {
1510
+ if (policy === "idle") scheduleIdleMerge(state, indexName);
1511
+ return;
1512
+ }
1513
+ const run = compactIndexSegments(state, indexName, policy).catch((err) => {
1514
+ console.warn("Segment compaction failed:", err);
1515
+ }).finally(() => {
1516
+ state.compactionsInFlight.delete(indexName);
1517
+ });
1518
+ state.compactionsInFlight.set(indexName, run);
1519
+ }
1520
+ function armIdleMerge(state, indexName, dueAt, delayMs) {
1521
+ const timer = setTimeout(() => {
1522
+ const pending = state.idleMergeTimers.get(indexName);
1523
+ if (pending === void 0 || pending.timer !== timer) return;
1524
+ const remainingMs = pending.dueAt - Date.now();
1525
+ if (remainingMs > 0) {
1526
+ armIdleMerge(state, indexName, pending.dueAt, remainingMs);
1527
+ return;
1528
+ }
1529
+ state.idleMergeTimers.delete(indexName);
1530
+ maybeCompactSegments(state, indexName, "idle");
1531
+ }, delayMs);
1532
+ if (typeof timer.unref === "function") timer.unref();
1533
+ state.idleMergeTimers.set(indexName, { timer, dueAt });
1534
+ }
1535
+ function scheduleIdleMerge(state, indexName) {
1536
+ const dueAt = Date.now() + IDLE_MERGE_DELAY_MS;
1537
+ const pending = state.idleMergeTimers.get(indexName);
1538
+ if (pending !== void 0) {
1539
+ pending.dueAt = dueAt;
1540
+ return;
1541
+ }
1542
+ armIdleMerge(state, indexName, dueAt, IDLE_MERGE_DELAY_MS);
1543
+ }
1544
+ function cancelIdleMerge(state, indexName) {
1545
+ const pending = state.idleMergeTimers.get(indexName);
1546
+ if (pending === void 0) return;
1547
+ clearTimeout(pending.timer);
1548
+ state.idleMergeTimers.delete(indexName);
1549
+ }
1550
+ async function awaitCompactions(state) {
1551
+ while (state.compactionsInFlight.size > 0) {
1552
+ await Promise.all([...state.compactionsInFlight.values()]);
1553
+ }
1554
+ }
1555
+
1556
+ // src/engine/orchestration/vector-copies.ts
1557
+ function holdsIndex(state, indexName) {
1558
+ return state.scaledOutIndexes.has(indexName) || state.copyLoadBuffers.has(indexName);
1559
+ }
1560
+ async function insertOnLease(lease, indexName, fieldName, handle, ordinals) {
1561
+ try {
1562
+ const outcome = await lease.executor.execute({
1563
+ type: "insertVectorOrdinals",
1564
+ indexName,
1565
+ fieldName,
1566
+ handle,
1567
+ ordinals,
1568
+ requestId: createRequestId()
1569
+ });
1570
+ return outcome ?? null;
1571
+ } catch {
1572
+ return null;
1573
+ } finally {
1574
+ lease.release();
1575
+ }
1576
+ }
1577
+ function sharedCopyHostOf(state) {
1578
+ return {
1579
+ get workerCount() {
1580
+ return state.workerPool?.workerCount ?? state.keywordWorkerCount + state.longQueryWorkerCount;
1581
+ },
1582
+ holdsIndex: (indexName) => holdsIndex(state, indexName),
1583
+ resolvePartition: (indexName, docId) => state.executor.getManager(indexName)?.partitionIdOf(docId),
1584
+ async loadShared(indexName, fieldName, handle, handles) {
1585
+ if (!holdsIndex(state, indexName)) return false;
1586
+ const publication = state.executor.getManager(indexName)?.getVectorIndexes().get(fieldName)?.holdCopyPublication();
1587
+ if (publication === void 0) return false;
1588
+ state.sharedVectorFields.set(handle, handles);
1589
+ try {
1590
+ await replicateToWorkers(state, {
1591
+ type: "loadVectorCopy",
1592
+ indexName,
1593
+ fieldName,
1594
+ handle,
1595
+ storeHandle: publication.field.handle,
1596
+ handles,
1597
+ requestId: createRequestId()
1598
+ });
1599
+ await awaitWritesApplied(state, indexName);
1600
+ return state.scaledOutIndexes.has(indexName);
1601
+ } finally {
1602
+ publication.release();
1603
+ }
1604
+ },
1605
+ async drop(indexName, fieldName, handle) {
1606
+ state.sharedVectorFields.delete(handle);
1607
+ if (!holdsIndex(state, indexName)) return;
1608
+ await replicateToWorkers(state, {
1609
+ type: "dropVectorCopy",
1610
+ indexName,
1611
+ fieldName,
1612
+ handle,
1613
+ requestId: createRequestId()
1614
+ });
1615
+ },
1616
+ async insertOrdinals(indexName, fieldName, handle, ordinals) {
1617
+ const pool = state.workerPool;
1618
+ if (pool === null || !state.scaledOutIndexes.has(indexName)) return null;
1619
+ for (let attempt = 0; attempt < pool.workerCount; attempt++) {
1620
+ const lease = pool.leaseLeastBusy();
1621
+ if (lease === null) return null;
1622
+ const outcome = await insertOnLease(lease, indexName, fieldName, handle, ordinals);
1623
+ if (outcome !== null) return outcome;
1624
+ if (!state.scaledOutIndexes.has(indexName)) return null;
1625
+ }
1626
+ return null;
1627
+ }
1628
+ };
1629
+ }
1630
+ function releaseVectorLocksOf(state, workerId) {
1631
+ noteThreadSlotGone(threadSlotOfWorker(workerId));
1632
+ if (state.sharedVectorFields.size === 0) return;
1633
+ const { releaseLocksHeldBy, releasePinsHeldBy } = requireLoadedVectorCode();
1634
+ const threadSlot = threadSlotOfWorker(workerId);
1635
+ for (const handles of state.sharedVectorFields.values()) {
1636
+ releasePinsHeldBy(handles.store.pins, threadSlot);
1637
+ if (handles.graph !== null) releaseLocksHeldBy(handles.graph, threadSlot);
1638
+ }
1639
+ }
1640
+ function refreshVectorCopies(state, indexName) {
1641
+ if (state.vectorCopyPolicy?.host === void 0) return;
1642
+ const manager = state.executor.getManager(indexName);
1643
+ if (manager === void 0) return;
1644
+ for (const vectorIndex of manager.getVectorIndexes().values()) vectorIndex.refreshWorkerCopies();
1645
+ }
1646
+
1647
+ // src/engine/orchestration/request-threads.ts
1648
+ async function announceRequestThreads(state, pool) {
1649
+ const listener = state.requestThreads;
1650
+ if (listener === null) return;
1651
+ const entries = pool.executorEntries();
1652
+ for (const { workerId, executor } of entries) {
1653
+ if (pool.isLongQueryWorker(workerId)) await listener.onLongQueryWorkerReady(workerId, executor);
1654
+ }
1655
+ for (const { workerId, executor } of entries) {
1656
+ if (!pool.isLongQueryWorker(workerId)) await listener.onWorkerReady(workerId, executor);
1657
+ }
1658
+ }
1659
+ function scheduleRequestThreadPoolRestart(state) {
1660
+ if (state.requestThreads === null) return;
1661
+ const timer = setTimeout(
1662
+ () => {
1663
+ if (state.requestThreads === null || state.workerPool !== null) return;
1664
+ void ensurePool(state).then(() => scaleOutReadyIndexes(state)).catch((err) => {
1665
+ console.warn("Restarting the request threads failed:", err);
1666
+ scheduleRequestThreadPoolRestart(state);
1667
+ });
1668
+ },
1669
+ Math.max(0, state.poolRetryAt - Date.now())
1670
+ );
1671
+ if (typeof timer.unref === "function") timer.unref();
1672
+ }
1673
+ async function serveRequestsOnWorkers(state, listener) {
1674
+ if (!state.workersEnabled) return 0;
1675
+ state.requestThreads = listener;
1676
+ state.keywordWorkerCount = resolveRequestThreadCount(state.config?.workers?.count);
1677
+ state.longQueryWorkerCount = LONG_QUERY_THREADS;
1678
+ if (state.vectorCopyPolicy !== void 0) {
1679
+ state.vectorCopyPolicy.host = sharedCopyHostOf(state);
1680
+ state.vectorCopyPolicy.enabled = state.workersEnabled;
1681
+ }
1682
+ if (state.poolStart !== null) await state.poolStart.catch(() => void 0);
1683
+ const running = state.workerPool;
1684
+ if (running !== null && running.workerCount !== resolveWorkerCount(state.keywordWorkerCount + LONG_QUERY_THREADS)) {
1685
+ retirePool(state, running);
1686
+ state.poolRetryAt = 0;
1687
+ }
1688
+ const pool = await ensurePool(state);
1689
+ for (const indexName of state.indexRegistry.keys()) {
1690
+ refreshVectorCopies(state, indexName);
1691
+ if (indexReadyForCopies(state, indexName)) {
1692
+ await scaleOutIndex(state, indexName, copyThresholdReason(state, indexName));
1693
+ }
1694
+ }
1695
+ return pool.workerCount;
1696
+ }
1697
+ function requestThreadCountOf(state) {
1698
+ if (!state.workersEnabled) return 0;
1699
+ return resolveRequestThreadCount(state.config?.workers?.count);
1700
+ }
1701
+ function stopRequestThreads(state) {
1702
+ const listener = state.requestThreads;
1703
+ if (listener === null) return;
1704
+ state.requestThreads = null;
1705
+ const pool = state.workerPool;
1706
+ if (pool === null) return;
1707
+ for (const { workerId } of pool.executorEntries()) listener.onWorkerGone(workerId);
1708
+ }
1709
+
1710
+ // src/engine/orchestration/repair.ts
1711
+ var COPY_RESTART_REASON = "A request arrived after every worker crashed and the restart delay passed";
1712
+ function nextRestartDelay(state) {
1713
+ const delay = state.poolRetryDelayMs;
1714
+ state.poolRetryDelayMs = Math.min(delay * 2, POOL_RESTART_DELAY_MAX_MS);
1715
+ return delay;
1716
+ }
1717
+ function deferPoolRestart(state) {
1718
+ state.poolRetryAt = Date.now() + nextRestartDelay(state);
1719
+ }
1720
+ function retirePool(state, pool) {
1721
+ if (state.workerPool !== pool) return;
1722
+ state.workerPool = null;
1723
+ cancelRepair(state);
1724
+ deferPoolRestart(state);
1725
+ for (const indexName of state.scaledOutIndexes) state.droppedCopies.set(indexName, COPY_RESTART_REASON);
1726
+ state.scaledOutIndexes.clear();
1727
+ state.segmentLedger.clear();
1728
+ for (const { workerId } of pool.executorEntries()) state.requestThreads?.onWorkerGone(workerId);
1729
+ const earlierThreadsGone = state.retiredThreadsGone;
1730
+ state.retiredThreadsGone = pool.whenEveryThreadIsGone().then(() => earlierThreadsGone);
1731
+ void pool.shutdown().catch(() => void 0);
1732
+ scheduleRequestThreadPoolRestart(state);
1733
+ }
1734
+ function handleWorkerCrash(state, pool, workerId, indexNames, error) {
1735
+ state.callbacks?.onWorkerCrash?.(workerId, indexNames, error);
1736
+ state.requestThreads?.onWorkerGone(workerId);
1737
+ if (pool.getAllExecutors().length === 0) retirePool(state, pool);
1738
+ }
1739
+ function handleWorkerThreadGone(state, pool, workerId) {
1740
+ releaseVectorLocksOf(state, workerId);
1741
+ scheduleRepair(state, pool);
1742
+ }
1743
+ function scheduleRepair(state, pool) {
1744
+ if (state.workerPool !== pool || state.repairTimer !== null || state.poolRepair !== null) return;
1745
+ const timer = setTimeout(() => {
1746
+ state.repairTimer = null;
1747
+ void repairPool(state, pool);
1748
+ }, state.poolRetryDelayMs);
1749
+ if (typeof timer.unref === "function") timer.unref();
1750
+ state.repairTimer = timer;
1751
+ }
1752
+ function cancelRepair(state) {
1753
+ if (state.repairTimer === null) return;
1754
+ clearTimeout(state.repairTimer);
1755
+ state.repairTimer = null;
1756
+ }
1757
+ async function loadOntoReplacement(state, replacement, indexName) {
1758
+ const entry = state.indexRegistry.get(indexName);
1759
+ const manager = state.executor.getManager(indexName);
1760
+ if (entry === void 0 || manager === void 0 || !state.scaledOutIndexes.has(indexName)) return;
1761
+ const buffered = [];
1762
+ state.copyLoadBuffers.set(indexName, buffered);
1763
+ let copyLoad;
1764
+ try {
1765
+ copyLoad = await loadIndexOntoWorkers(
1766
+ indexName,
1767
+ [replacement.executor],
1768
+ { ...entry, onPublication: (publication) => trackPublicationStores(state, publication) },
1769
+ manager,
1770
+ void 0,
1771
+ state.callbacks?.isAnalysisStale?.(indexName)
1772
+ );
1773
+ replacement.hold(indexName);
1774
+ } finally {
1775
+ state.copyLoadBuffers.delete(indexName);
1776
+ for (const action of buffered) enqueueReplication(state, indexName, action);
1777
+ }
1778
+ markCopiesLoaded(state, indexName, copyLoad);
1779
+ refreshVectorCopies(state, indexName);
1780
+ }
1781
+ async function replaceWorker(state, pool, workerId) {
1782
+ const replacement = pool.spawnReplacement(workerId);
1783
+ if (replacement === null) return;
1784
+ try {
1785
+ if (state.bootstrapModule !== void 0) {
1786
+ await replacement.executor.execute({
1787
+ type: "bootstrap",
1788
+ moduleUrl: state.bootstrapModule,
1789
+ requestId: createRequestId()
1790
+ });
1791
+ }
1792
+ for (const indexName of [...state.scaledOutIndexes]) {
1793
+ await loadOntoReplacement(state, replacement, indexName);
1794
+ }
1795
+ replacement.admit();
1796
+ if (pool.isLongQueryWorker(workerId))
1797
+ await state.requestThreads?.onLongQueryWorkerReady(workerId, replacement.executor);
1798
+ else await state.requestThreads?.onWorkerReady(workerId, replacement.executor);
1799
+ } catch (err) {
1800
+ replacement.abandon();
1801
+ throw err;
1802
+ }
1803
+ }
1804
+ async function replaceDeadWorkers(state, pool) {
1805
+ for (const workerId of pool.deadWorkerIds()) {
1806
+ if (state.workerPool !== pool) return;
1807
+ await replaceWorker(state, pool, workerId);
1808
+ }
1809
+ state.poolRetryDelayMs = POOL_RESTART_DELAY_MS;
1810
+ }
1811
+ function repairPool(state, pool) {
1812
+ if (state.workerPool !== pool) return Promise.resolve();
1813
+ if (state.poolRepair !== null) return state.poolRepair;
1814
+ const run = replaceDeadWorkers(state, pool).catch((err) => {
1815
+ nextRestartDelay(state);
1816
+ console.warn("Replacing a crashed worker failed:", err);
1817
+ }).finally(() => {
1818
+ state.poolRepair = null;
1819
+ if (pool.deadWorkerIds().length > 0) scheduleRepair(state, pool);
1820
+ });
1821
+ state.poolRepair = run;
1822
+ return run;
1823
+ }
1824
+
1825
+ // src/engine/orchestration/scale-out.ts
1826
+ var COPY_RELOAD_REASON = "A request arrived after an idle spell dropped the worker copies";
1827
+ function copiesAllowed(state) {
1828
+ if (!state.workersEnabled || state.scaleOutBlocked || state.shuttingDown) return false;
1829
+ return state.workerPool !== null || Date.now() >= state.poolRetryAt;
1830
+ }
1831
+ function retiredThreadsGoneWithin(state, timeoutMs) {
1832
+ return new Promise((resolve) => {
1833
+ const timeoutId = setTimeout(() => resolve(false), timeoutMs);
1834
+ const settle = () => {
1835
+ clearTimeout(timeoutId);
1836
+ resolve(true);
1837
+ };
1838
+ void state.retiredThreadsGone.then(settle);
1839
+ void state.shutdownStarted.then(settle);
1840
+ });
1841
+ }
1842
+ async function startPool(state) {
1843
+ const everyRetiredThreadIsGone = await retiredThreadsGoneWithin(state, RETIRED_THREADS_EXIT_WAIT_MS);
1844
+ if (state.shuttingDown) {
1845
+ throw new NarsilError(ErrorCodes.WORKER_CRASHED, "The engine is shutting down, so no worker pool starts");
1846
+ }
1847
+ if (!everyRetiredThreadIsGone) {
1848
+ throw new NarsilError(
1849
+ ErrorCodes.WORKER_TIMEOUT,
1850
+ `A thread of the retired worker pool still runs after ${RETIRED_THREADS_EXIT_WAIT_MS}ms, so no new pool starts yet`
1851
+ );
1852
+ }
1853
+ eligibleIndexNames(state);
1854
+ const factory = await createWorkerFactory();
1855
+ let started = null;
1856
+ const pool = createWorkerPool({
1857
+ count: state.keywordWorkerCount + state.longQueryWorkerCount,
1858
+ longQueryWorkers: state.longQueryWorkerCount,
1859
+ workerFactory: factory,
1860
+ onWorkerCrash(workerId, indexNames, error) {
1861
+ if (started !== null) handleWorkerCrash(state, started, workerId, indexNames, error);
1862
+ },
1863
+ onWorkerGone(workerId) {
1864
+ if (started !== null) handleWorkerThreadGone(state, started, workerId);
1865
+ }
1866
+ });
1867
+ started = pool;
1868
+ try {
1869
+ pool.spawnAll();
1870
+ if (state.bootstrapModule !== void 0) {
1871
+ const moduleUrl = state.bootstrapModule;
1872
+ await Promise.all(
1873
+ pool.getAllExecutors().map(
1874
+ (workerExecutor) => workerExecutor.execute({ type: "bootstrap", moduleUrl, requestId: "copies-bootstrap" })
1875
+ )
1876
+ );
1877
+ }
1878
+ } catch (err) {
1879
+ await pool.shutdown().catch(() => void 0);
1880
+ throw err;
1881
+ }
1882
+ return pool;
1883
+ }
1884
+ async function ensurePool(state) {
1885
+ await loadPublicationCode();
1886
+ if (state.workerPool !== null) return state.workerPool;
1887
+ if (state.poolStart === null) {
1888
+ state.poolStart = startPool(state).then(
1889
+ async (pool) => {
1890
+ state.workerPool = pool;
1891
+ state.poolStart = null;
1892
+ try {
1893
+ await announceRequestThreads(state, pool);
1894
+ } catch (err) {
1895
+ retirePool(state, pool);
1896
+ throw err;
1897
+ }
1898
+ return pool;
1899
+ },
1900
+ (err) => {
1901
+ state.poolStart = null;
1902
+ deferPoolRestart(state);
1903
+ throw err;
1904
+ }
1905
+ );
1906
+ }
1907
+ return state.poolStart;
1908
+ }
1909
+ function documentCountOf(state, indexName) {
1910
+ return state.executor.getManager(indexName)?.countDocuments() ?? 0;
1911
+ }
1912
+ function copyThresholdReason(state, indexName, incomingCount = 0) {
1913
+ const held = documentCountOf(state, indexName);
1914
+ if (incomingCount <= 0) {
1915
+ return `Index "${indexName}" holds ${held} documents, reaching the copy threshold of ${state.copyThreshold}`;
1916
+ }
1917
+ return `Index "${indexName}" holds ${held} documents, and the batch that the engine is writing adds ${incomingCount}, for ${held + incomingCount} against the copy threshold of ${state.copyThreshold}`;
1918
+ }
1919
+ function indexReadyForCopies(state, indexName, incomingCount = 0) {
1920
+ if (!copiesAllowed(state)) return false;
1921
+ if (state.scaledOutIndexes.has(indexName) || state.desyncedIndexes.has(indexName)) return false;
1922
+ if (state.copyTransitions.has(indexName)) return false;
1923
+ if (state.callbacks?.shouldDeferCopies?.()) return false;
1924
+ const entry = state.indexRegistry.get(indexName);
1925
+ if (entry === void 0 || state.executor.getManager(indexName) === void 0) return false;
1926
+ if (documentCountOf(state, indexName) + incomingCount < state.copyThreshold) return false;
1927
+ const ineligibility = workerIneligibility(indexName, entry.config, state.bootstrapModule);
1928
+ if (ineligibility !== null) {
1929
+ reportIneligible(state, indexName, ineligibility);
1930
+ return false;
1931
+ }
1932
+ return true;
1933
+ }
1934
+ function discardLoadedCopies(state, pool, indexName) {
1935
+ const holders = pool.executorsHolding(indexName);
1936
+ pool.removeIndex(indexName);
1937
+ state.segmentLedger.delete(indexName);
1938
+ void Promise.allSettled(
1939
+ holders.map((worker) => worker.execute({ type: "dropIndex", indexName, requestId: `stale-load-drop-${indexName}` }))
1940
+ );
1941
+ }
1942
+ async function loadCopies(state, indexName, reason) {
1943
+ const entry = state.indexRegistry.get(indexName);
1944
+ const manager = state.executor.getManager(indexName);
1945
+ if (entry === void 0 || manager === void 0) return;
1946
+ const ineligibility = workerIneligibility(indexName, entry.config, state.bootstrapModule);
1947
+ if (ineligibility !== null) {
1948
+ reportIneligible(state, indexName, ineligibility);
1949
+ return;
1950
+ }
1951
+ const pool = await ensurePool(state);
1952
+ if (state.executor.getManager(indexName) === void 0) return;
1953
+ const dropReason = state.droppedCopies.get(indexName);
1954
+ const reload = state.droppedCopies.delete(indexName);
1955
+ const buffered = [];
1956
+ let dispatched = false;
1957
+ state.copyLoadBuffers.set(indexName, buffered);
1958
+ try {
1959
+ await freezeLiveTailsBeforeCopiesLoad(manager);
1960
+ const copyLoad = await transferIndexToPool(
1961
+ indexName,
1962
+ pool,
1963
+ { ...entry, onPublication: (publication) => trackPublicationStores(state, publication) },
1964
+ manager,
1965
+ state.callbacks?.isAnalysisStale?.(indexName)
1966
+ );
1967
+ if (state.desyncedIndexes.has(indexName)) {
1968
+ discardLoadedCopies(state, pool, indexName);
1969
+ return;
1970
+ }
1971
+ state.scaledOutIndexes.add(indexName);
1972
+ state.lastAccessAt.set(indexName, Date.now());
1973
+ state.poolRetryDelayMs = POOL_RESTART_DELAY_MS;
1974
+ for (const action of buffered) enqueueReplication(state, indexName, action);
1975
+ dispatched = true;
1976
+ markCopiesLoaded(state, indexName, copyLoad);
1977
+ } catch (err) {
1978
+ if (dropReason !== void 0) state.droppedCopies.set(indexName, dropReason);
1979
+ throw err;
1980
+ } finally {
1981
+ state.copyLoadBuffers.delete(indexName);
1982
+ if (!dispatched) for (const action of buffered) releasePublicationOf(action);
1983
+ }
1984
+ if (reload) state.copyReloadCounts.set(indexName, (state.copyReloadCounts.get(indexName) ?? 0) + 1);
1985
+ refreshVectorCopies(state, indexName);
1986
+ scheduleIdleMerge(state, indexName);
1987
+ state.callbacks?.onCopiesLoaded?.(pool.workerCount, reason);
1988
+ }
1989
+ async function loadAfter(previous, state, indexName, reason) {
1990
+ if (previous !== void 0) await previous.done;
1991
+ if (state.poolRepair !== null) await state.poolRepair;
1992
+ if (!copiesAllowed(state)) return;
1993
+ if (state.scaledOutIndexes.has(indexName) || state.desyncedIndexes.has(indexName)) return;
1994
+ await loadCopies(state, indexName, reason);
1995
+ }
1996
+ function scaleOutIndex(state, indexName, reason) {
1997
+ const previous = state.copyTransitions.get(indexName);
1998
+ if (previous !== void 0 && previous.kind !== "drop") return previous.done;
1999
+ const kind = state.droppedCopies.has(indexName) ? "reload" : "load";
2000
+ const done = loadAfter(previous, state, indexName, reason).catch((err) => {
2001
+ const error = toError(err);
2002
+ state.scaleOutBlocked = isDeterministicFailure(error);
2003
+ state.callbacks?.onCopyLoadFailure?.(reason, error, !state.scaleOutBlocked);
2004
+ }).finally(() => {
2005
+ if (state.copyTransitions.get(indexName)?.done === done) state.copyTransitions.delete(indexName);
2006
+ });
2007
+ state.copyTransitions.set(indexName, { kind, done });
2008
+ return done;
2009
+ }
2010
+ async function scaleOutReadyIndexes(state) {
2011
+ if (!copiesAllowed(state)) return;
2012
+ for (const indexName of state.indexRegistry.keys()) {
2013
+ if (!indexReadyForCopies(state, indexName)) continue;
2014
+ const reason = copyThresholdReason(state, indexName);
2015
+ setTimeout(() => {
2016
+ void scaleOutIndex(state, indexName, reason);
2017
+ }, 0);
2018
+ }
2019
+ }
2020
+ async function scaleOutBeforeBatch(state, indexName, incomingCount) {
2021
+ if (incomingCount <= 0) return;
2022
+ const pending = state.copyTransitions.get(indexName);
2023
+ if (pending !== void 0) {
2024
+ if (pending.kind === "load") await pending.done;
2025
+ return;
2026
+ }
2027
+ if (!indexReadyForCopies(state, indexName, incomingCount)) return;
2028
+ await scaleOutIndex(state, indexName, copyThresholdReason(state, indexName, incomingCount));
2029
+ }
2030
+
2031
+ // src/engine/orchestration/idle.ts
2032
+ function isIndexBusy(state, indexName) {
2033
+ return state.poolRepair !== null || state.copyTransitions.has(indexName) || state.replicationQueues.has(indexName) || state.compactionsInFlight.has(indexName);
2034
+ }
2035
+ function noteAccess(state, indexName) {
2036
+ state.lastAccessAt.set(indexName, Date.now());
2037
+ if (!copiesAllowed(state)) return;
2038
+ const reloadReason = state.droppedCopies.get(indexName);
2039
+ if (reloadReason !== void 0) {
2040
+ const transition = state.copyTransitions.get(indexName);
2041
+ if (transition !== void 0 && transition.kind !== "drop") return;
2042
+ void scaleOutIndex(state, indexName, reloadReason);
2043
+ return;
2044
+ }
2045
+ if (indexReadyForCopies(state, indexName)) {
2046
+ void scaleOutIndex(state, indexName, copyThresholdReason(state, indexName));
2047
+ }
2048
+ }
2049
+ async function releaseCopies(state, indexName) {
2050
+ const pool = state.workerPool;
2051
+ if (pool === null) return;
2052
+ await awaitReplicationIdle(state, indexName);
2053
+ const cutoff = Date.now() - state.copyIdleTimeoutMs;
2054
+ if ((state.lastAccessAt.get(indexName) ?? Date.now()) > cutoff) return;
2055
+ if (!state.scaledOutIndexes.has(indexName) || state.compactionsInFlight.has(indexName)) return;
2056
+ state.scaledOutIndexes.delete(indexName);
2057
+ state.droppedCopies.set(indexName, COPY_RELOAD_REASON);
2058
+ cancelIdleMerge(state, indexName);
2059
+ const outcomes = await Promise.allSettled(
2060
+ pool.executorsHolding(indexName).map((worker) => worker.execute({ type: "dropIndex", indexName, requestId: `idle-drop-${indexName}` }))
2061
+ );
2062
+ for (const outcome of outcomes) {
2063
+ if (outcome.status === "rejected") console.warn("Dropping an idle worker copy failed:", outcome.reason);
2064
+ }
2065
+ pool.removeIndex(indexName);
2066
+ state.segmentLedger.delete(indexName);
2067
+ }
2068
+ async function dropIdleCopies(state, indexName) {
2069
+ const previous = state.copyTransitions.get(indexName);
2070
+ if (previous !== void 0) return;
2071
+ const done = releaseCopies(state, indexName).catch((err) => {
2072
+ console.warn("Dropping idle worker copies failed:", err);
2073
+ }).finally(() => {
2074
+ if (state.copyTransitions.get(indexName)?.done === done) state.copyTransitions.delete(indexName);
2075
+ });
2076
+ state.copyTransitions.set(indexName, { kind: "drop", done });
2077
+ await done;
2078
+ }
2079
+ async function sweepIdleCopies(state) {
2080
+ prunePublicationStores(state);
2081
+ const cutoff = Date.now() - state.copyIdleTimeoutMs;
2082
+ for (const indexName of [...state.scaledOutIndexes]) {
2083
+ if ((state.lastAccessAt.get(indexName) ?? Date.now()) > cutoff) continue;
2084
+ if (isIndexBusy(state, indexName)) continue;
2085
+ await dropIdleCopies(state, indexName);
2086
+ }
2087
+ }
2088
+ function startIdleSweep(state) {
2089
+ if (!state.workersEnabled || state.idleSweep !== null) return;
2090
+ const interval = Math.min(MAX_SWEEP_INTERVAL_MS, Math.max(MIN_SWEEP_INTERVAL_MS, state.copyIdleTimeoutMs / 2));
2091
+ const timer = setInterval(() => void sweepIdleCopies(state).catch(() => void 0), interval);
2092
+ if (typeof timer.unref === "function") timer.unref();
2093
+ state.idleSweep = timer;
2094
+ }
2095
+ function stopIdleSweep(state) {
2096
+ if (state.idleSweep === null) return;
2097
+ clearInterval(state.idleSweep);
2098
+ state.idleSweep = null;
2099
+ }
2100
+
2101
+ // src/engine/orchestration/release.ts
2102
+ async function releaseIndexCopies(state, indexName, steps) {
2103
+ const wasScaledOut = state.scaledOutIndexes.delete(indexName);
2104
+ const loading = state.copyTransitions.get(indexName);
2105
+ if (loading !== void 0) await loading.done.catch(() => void 0);
2106
+ const previousPool = state.workerPool;
2107
+ await steps.awaitReplicationIdle(state, indexName);
2108
+ await previousPool?.whenRetiredCopiesAreGone(indexName);
2109
+ await state.retiredThreadsGone;
2110
+ steps.cancelIdleMerge(state, indexName);
2111
+ const reloads = wasScaledOut || loading !== void 0 && loading.kind !== "drop";
2112
+ const pool = state.workerPool;
2113
+ if (pool === null) return reloads;
2114
+ pool.removeIndex(indexName);
2115
+ state.segmentLedger.delete(indexName);
2116
+ await Promise.all(
2117
+ pool.executorEntries().map(
2118
+ ({ executor }) => executor.execute({ type: "dropIndex", indexName, requestId: `restore-release-${indexName}` }).catch(async (error) => {
2119
+ if (steps.heldNowhere(error)) return;
2120
+ if (pool.getAllExecutors().includes(executor)) throw error;
2121
+ await pool.whenExecutorIsGone(executor);
2122
+ })
2123
+ )
2124
+ );
2125
+ return reloads;
2126
+ }
2127
+ async function endCopyRelease(state, indexName, reloads, steps) {
2128
+ const hold = state.copyReleaseHolds.get(indexName);
2129
+ if (hold === void 0) return steps.resyncIndex(indexName, reloads);
2130
+ hold.holders -= 1;
2131
+ hold.reloads ||= reloads;
2132
+ if (hold.holders > 0) return;
2133
+ state.copyReleaseHolds.delete(indexName);
2134
+ await steps.resyncIndex(indexName, hold.reloads);
2135
+ }
2136
+
2137
+ // src/core/pattern-index/matcher-budget.ts
2138
+ function widestPatternSplit(footprintBytes) {
2139
+ return Math.max(1, Math.floor(MATCHER_STATE_BUDGET_BYTES / Math.max(1, footprintBytes)));
2140
+ }
2141
+
2142
+ // src/core/pattern-index/query-positions.ts
2143
+ function largestPatternFootprint(expression, schema) {
2144
+ if (expression === void 0) return null;
2145
+ const module = availablePatternSearch();
2146
+ if (module === null) return null;
2147
+ const patternFields = /* @__PURE__ */ new Set();
2148
+ for (const { path, pattern } of schemaFieldsOf(schema)) if (pattern) patternFields.add(path);
2149
+ return patternFields.size === 0 ? null : module.largestFootprint(expression, patternFields, patternHost());
2150
+ }
2151
+
2152
+ // src/engine/orchestration/search.ts
2153
+ function partitionsPerLease(scope, leaseCount) {
2154
+ const assignments = Array.from({ length: leaseCount }, () => []);
2155
+ for (let at = 0; at < scope.length; at++) {
2156
+ assignments[at % leaseCount].push(scope[at]);
2157
+ }
2158
+ return assignments;
2159
+ }
2160
+ function mergedDocuments(results) {
2161
+ let documents;
2162
+ for (const result of results) {
2163
+ if (result.documents === void 0) continue;
2164
+ documents ??= /* @__PURE__ */ new Map();
2165
+ for (const [docId, document] of result.documents) documents.set(docId, document);
2166
+ }
2167
+ return documents;
2168
+ }
2169
+ function mergeWorkerResults(results, facetConfig) {
2170
+ const merged = kWayMerge(results.map((result) => result.scored));
2171
+ let totalMatched = 0;
2172
+ let patternWorkSpent = 0;
2173
+ const workerFacets = [];
2174
+ for (const result of results) {
2175
+ totalMatched += result.totalMatched;
2176
+ patternWorkSpent += result.patternWorkSpent ?? 0;
2177
+ if (result.facets !== void 0) workerFacets.push(result.facets);
2178
+ }
2179
+ const documents = mergedDocuments(results);
2180
+ return {
2181
+ scored: merged,
2182
+ totalMatched,
2183
+ facets: facetConfig !== void 0 && workerFacets.length > 0 ? mergeFacets(workerFacets, facetConfig) : void 0,
2184
+ patternWorkSpent,
2185
+ ...documents !== void 0 ? { documents } : {}
2186
+ };
2187
+ }
2188
+ function splitReadsOf(params, reads) {
2189
+ if (reads === void 0 || reads.kind === "documents") return reads;
2190
+ return returnsEveryMatch(params) ? null : { kind: "documents" };
2191
+ }
2192
+ function scoresPerPartition(state, indexName, params, globalStats) {
2193
+ const mode = params.scoring ?? state.indexRegistry.get(indexName)?.config.defaultScoring ?? "local";
2194
+ if (mode === "local") return true;
2195
+ return mode === "broadcast" && globalStats !== void 0;
2196
+ }
2197
+ function queryAction(indexName, params, globalStats, partitionIds, patternWork, bm25, reads, readGeneration) {
2198
+ const workerParams = params.group?.reduce === void 0 ? params : { ...params, group: { ...params.group, reduce: void 0 } };
2199
+ const workerReads = params.group?.reduce === void 0 ? reads : { kind: "documents" };
2200
+ return {
2201
+ type: "query",
2202
+ readGeneration,
2203
+ indexName,
2204
+ params: workerParams,
2205
+ requestId: createRequestId(),
2206
+ patternWork,
2207
+ bm25: bm25 ?? null,
2208
+ ...partitionIds !== void 0 ? { partitionIds } : {},
2209
+ ...globalStats !== void 0 ? { globalStats } : {},
2210
+ ...workerReads !== void 0 ? { reads: workerReads } : {}
2211
+ };
2212
+ }
2213
+ async function runSplit(leases, scope, indexName, params, globalStats, patternWork, bm25, reads, readGeneration) {
2214
+ const assignments = partitionsPerLease(scope, leases.length);
2215
+ const workerParams = params.facets !== void 0 ? { ...params, facets: oversampledFacetConfig(params.facets) } : params;
2216
+ const outcomes = await Promise.allSettled(
2217
+ assignments.map(
2218
+ (partitionIds, at) => leases[at].executor.execute(
2219
+ queryAction(indexName, workerParams, globalStats, partitionIds, patternWork, bm25, reads, readGeneration)
2220
+ ).finally(() => leases[at].release())
2221
+ )
2222
+ );
2223
+ const results = [];
2224
+ const failures = [];
2225
+ for (const outcome of outcomes) {
2226
+ if (outcome.status === "fulfilled") results.push(outcome.value);
2227
+ else failures.push(outcome.reason);
2228
+ }
2229
+ if (failures.length > 0) throw failures.find(failsTheSearch) ?? failures[0];
2230
+ return mergeWorkerResults(results, params.facets);
2231
+ }
2232
+ function failsTheSearch(err) {
2233
+ if (!(err instanceof NarsilError)) return false;
2234
+ return err.code === ErrorCodes.SEARCH_WORK_CAP_EXCEEDED || err.code === ErrorCodes.SEARCH_STATE_MOVED;
2235
+ }
2236
+ function patternSplitWidth(state, indexName, params) {
2237
+ const schema = state.indexRegistry.get(indexName)?.config.schema;
2238
+ if (schema === void 0) return Number.POSITIVE_INFINITY;
2239
+ const largest = largestPatternFootprint(params.filters, schema);
2240
+ if (largest === null) return Number.POSITIVE_INFINITY;
2241
+ if (typeof SharedArrayBuffer !== "function") return 1;
2242
+ return widestPatternSplit(largest);
2243
+ }
2244
+ function takeMainCopyTurn(state) {
2245
+ if (state.mainCopyQueries === "none" || state.mainCopyTurnTaken) return false;
2246
+ state.mainCopyTurnTaken = true;
2247
+ afterCurrentTurn(() => {
2248
+ state.mainCopyTurnTaken = false;
2249
+ });
2250
+ return true;
2251
+ }
2252
+ function answersFasterOnMainCopy(state, pool, manager) {
2253
+ if (pool.queriesInFlight() > 0) return false;
2254
+ if (manager.countDocuments() > MAIN_COPY_LONE_QUERY_DOCUMENTS) return false;
2255
+ return takeMainCopyTurn(state);
2256
+ }
2257
+ async function searchViaWorker(state, indexName, params, globalStats, partitionIds, patternWork, bm25, reads) {
2258
+ const pool = state.workerPool;
2259
+ if (!pool) return null;
2260
+ if (!state.scaledOutIndexes.has(indexName) || state.copyLoadBuffers.has(indexName)) return null;
2261
+ const manager = state.executor.getManager(indexName);
2262
+ if (!manager) return null;
2263
+ if (answersFasterOnMainCopy(state, pool, manager)) return null;
2264
+ const readGeneration = copyGenerationOf(manager);
2265
+ await awaitReplicationIdle(state, indexName);
2266
+ const scope = partitionIds ?? Array.from({ length: manager.partitionCount }, (_, partitionId) => partitionId);
2267
+ const idle = pool.leaseIdle(Math.min(scope.length, patternSplitWidth(state, indexName, params)));
2268
+ const leases = [];
2269
+ let fork = null;
2270
+ const splitReads = splitReadsOf(params, reads);
2271
+ try {
2272
+ if (idle.length >= 2 && splitReads !== null && scoresPerPartition(state, indexName, params, globalStats)) {
2273
+ leases.push(...idle);
2274
+ fork = patternWork.fork(leases.length);
2275
+ const merged = await runSplit(
2276
+ leases,
2277
+ scope,
2278
+ indexName,
2279
+ params,
2280
+ globalStats,
2281
+ fork.shared,
2282
+ bm25,
2283
+ splitReads,
2284
+ readGeneration
2285
+ );
2286
+ fork.settle(merged.patternWorkSpent ?? 0);
2287
+ return merged;
2288
+ }
2289
+ for (const lease2 of idle.slice(1)) lease2.release();
2290
+ const lease = idle[0] ?? (takeMainCopyTurn(state) ? null : pool.leaseLeastBusy());
2291
+ if (lease === null) return null;
2292
+ leases.push(lease);
2293
+ fork = patternWork.fork(1);
2294
+ const result = await lease.executor.execute(
2295
+ queryAction(indexName, params, globalStats, partitionIds, fork.shared, bm25, reads, readGeneration)
2296
+ );
2297
+ fork.settle(result.patternWorkSpent ?? 0);
2298
+ return result;
2299
+ } catch (err) {
2300
+ if (failsTheSearch(err)) throw err;
2301
+ fork?.settle(0);
2302
+ console.warn("Worker search failed, falling back to local:", err);
2303
+ return null;
2304
+ } finally {
2305
+ for (const lease of leases) lease.release();
2306
+ }
2307
+ }
2308
+
2309
+ // src/engine/orchestration/segments.ts
2310
+ function segmentBuildConcurrency(state, indexName) {
2311
+ if (!state.scaledOutIndexes.has(indexName)) return 0;
2312
+ return state.workerPool?.workerCount ?? 0;
2313
+ }
2314
+ function builtSegmentsOf(request, results) {
2315
+ let start = 0;
2316
+ return results.map((result) => {
2317
+ const count = result.kind === "shared" ? result.snapshot.documentCount : result.payload.documentCount;
2318
+ const segmentId = result.kind === "shared" ? result.snapshot.segmentId : start === 0 ? request.action.segmentId : generateId();
2319
+ const base = {
2320
+ partitionId: request.partitionId,
2321
+ segmentId,
2322
+ documents: request.documents.slice(start, start + count),
2323
+ docIds: request.action.documents.slice(start, start + count).map((document) => document.docId)
2324
+ };
2325
+ start += count;
2326
+ if (result.kind === "shared") return { ...base, snapshot: result.snapshot, payload: null };
2327
+ return { ...base, snapshot: null, payload: result.payload };
2328
+ });
2329
+ }
2330
+ async function buildSegments(state, requests) {
2331
+ const pool = state.workerPool;
2332
+ if (!pool || requests.length === 0) return null;
2333
+ const leases = requests.map(() => pool.leaseLeastBusy());
2334
+ if (leases.some((lease) => lease === null)) {
2335
+ for (const lease of leases) lease?.release();
2336
+ return null;
2337
+ }
2338
+ try {
2339
+ const outputs = await Promise.all(
2340
+ requests.map((request, i) => {
2341
+ const lease = leases[i];
2342
+ if (lease === null) throw new Error("Segment build lease missing");
2343
+ return lease.executor.execute(request.action).then((result) => builtSegmentsOf(request, result));
2344
+ })
2345
+ );
2346
+ return outputs.flat();
2347
+ } finally {
2348
+ for (const lease of leases) lease?.release();
2349
+ }
2350
+ }
2351
+
2352
+ // src/engine/orchestration/index.ts
2353
+ function holdsCopies(state, indexName) {
2354
+ return state.scaledOutIndexes.has(indexName) || state.copyLoadBuffers.has(indexName);
2355
+ }
2356
+ async function settleCopiedPublications(state, indexName, growth, room) {
2357
+ if (room !== null) await room;
2358
+ if (growth === "segments") maybeCompactSegments(state, indexName);
2359
+ if (!holdsCopies(state, indexName)) return;
2360
+ if (growth === "liveTail") await flushGrownTails(state, indexName);
2361
+ scheduleIdleMerge(state, indexName);
2362
+ }
2363
+ function copyIdleTimeoutBeforeClose(closeAfterMs) {
2364
+ return closeAfterMs === void 0 ? DEFAULT_COPY_IDLE_TIMEOUT_MS : Math.min(DEFAULT_COPY_IDLE_TIMEOUT_MS, closeAfterMs);
2365
+ }
2366
+ function createWorkerOrchestrator(config, executor, indexRegistry, callbacks, vectorCopyPolicy) {
2367
+ let announceShutdown = () => void 0;
2368
+ const shutdownStarted = new Promise((resolve) => {
2369
+ announceShutdown = resolve;
2370
+ });
2371
+ const state = {
2372
+ config,
2373
+ executor,
2374
+ indexRegistry,
2375
+ callbacks,
2376
+ vectorCopyPolicy,
2377
+ workersEnabled: config?.workers?.enabled ?? workersEnabledByDefault(),
2378
+ keywordWorkerCount: splitWorkerBudget(resolveWorkerCount(config?.workers?.count)).keyword,
2379
+ longQueryWorkerCount: 0,
2380
+ requestThreads: null,
2381
+ copyThreshold: config?.workers?.promotionThreshold ?? DEFAULT_COPY_THRESHOLD,
2382
+ copyIdleTimeoutMs: config?.workers?.idleTimeoutMs ?? copyIdleTimeoutBeforeClose(config?.lifecycle?.idleTimeoutMs),
2383
+ bootstrapModule: config?.workers?.bootstrapModule,
2384
+ reportedIneligible: /* @__PURE__ */ new Set(),
2385
+ scaledOutIndexes: /* @__PURE__ */ new Set(),
2386
+ desyncedIndexes: /* @__PURE__ */ new Set(),
2387
+ copyReleaseHolds: /* @__PURE__ */ new Map(),
2388
+ copyLoadBuffers: /* @__PURE__ */ new Map(),
2389
+ copyLoadBufferYieldMarks: /* @__PURE__ */ new WeakMap(),
2390
+ copyTransitions: /* @__PURE__ */ new Map(),
2391
+ droppedCopies: /* @__PURE__ */ new Map(),
2392
+ lastAccessAt: /* @__PURE__ */ new Map(),
2393
+ copyReloadCounts: /* @__PURE__ */ new Map(),
2394
+ replicationQueues: /* @__PURE__ */ new Map(),
2395
+ segmentLedger: /* @__PURE__ */ new Map(),
2396
+ compactionsInFlight: /* @__PURE__ */ new Map(),
2397
+ idleMergeTimers: /* @__PURE__ */ new Map(),
2398
+ sharedVectorFields: /* @__PURE__ */ new Map(),
2399
+ publishedVectorStores: /* @__PURE__ */ new Set(),
2400
+ workerPool: null,
2401
+ retiredThreadsGone: Promise.resolve(),
2402
+ shutdownStarted,
2403
+ announceShutdown,
2404
+ poolStart: null,
2405
+ poolRetryAt: 0,
2406
+ poolRetryDelayMs: POOL_RESTART_DELAY_MS,
2407
+ poolRepair: null,
2408
+ mainCopyTurnTaken: false,
2409
+ mainCopyQueries: config?.workers?.mainCopyQueries,
2410
+ repairTimer: null,
2411
+ scaleOutBlocked: false,
2412
+ shuttingDown: false,
2413
+ idleSweep: null
2414
+ };
2415
+ startIdleSweep(state);
2416
+ function settlingCopyWork(indexName) {
2417
+ return state.copyTransitions.get(indexName)?.done ?? state.compactionsInFlight.get(indexName);
2418
+ }
2419
+ async function resyncIndex(indexName, wasScaledOut) {
2420
+ for (let pending = settlingCopyWork(indexName); pending !== void 0; pending = settlingCopyWork(indexName)) {
2421
+ await pending;
2422
+ }
2423
+ const hold = state.copyReleaseHolds.get(indexName);
2424
+ if (hold !== void 0) {
2425
+ hold.reloads ||= wasScaledOut;
2426
+ return;
2427
+ }
2428
+ state.desyncedIndexes.delete(indexName);
2429
+ if (!wasScaledOut) return;
2430
+ await scaleOutIndex(state, indexName, copyThresholdReason(state, indexName));
2431
+ }
2432
+ const copyReleaseSteps = {
2433
+ awaitReplicationIdle,
2434
+ cancelIdleMerge,
2435
+ heldNowhere: (error) => error instanceof NarsilError && error.code === ErrorCodes.INDEX_NOT_FOUND,
2436
+ resyncIndex
2437
+ };
2438
+ async function openIndex(indexName) {
2439
+ state.droppedCopies.delete(indexName);
2440
+ if (!indexReadyForCopies(state, indexName)) return;
2441
+ await scaleOutIndex(state, indexName, copyThresholdReason(state, indexName));
2442
+ }
2443
+ async function getWorkerMemoryStats() {
2444
+ if (!state.workerPool) return [];
2445
+ const reports = await state.workerPool.getMemoryStats();
2446
+ return reports.map((report) => ({
2447
+ workerId: report.workerId,
2448
+ heapUsed: report.heapUsed,
2449
+ heapTotal: report.heapTotal,
2450
+ heapLimit: report.heapLimit,
2451
+ external: report.external
2452
+ }));
2453
+ }
2454
+ function workerCopies() {
2455
+ const reports = [];
2456
+ for (const indexName of indexRegistry.keys()) {
2457
+ reports.push({
2458
+ indexName,
2459
+ scaledOut: state.scaledOutIndexes.has(indexName),
2460
+ reloadCount: state.copyReloadCounts.get(indexName) ?? 0
2461
+ });
2462
+ }
2463
+ return reports;
2464
+ }
2465
+ async function shutdown() {
2466
+ state.shuttingDown = true;
2467
+ state.announceShutdown();
2468
+ stopIdleSweep(state);
2469
+ cancelRepair(state);
2470
+ stopRequestThreads(state);
2471
+ for (const indexName of [...state.idleMergeTimers.keys()]) cancelIdleMerge(state, indexName);
2472
+ await Promise.allSettled([...state.copyTransitions.values()].map((transition) => transition.done));
2473
+ if (state.poolRepair !== null) await state.poolRepair;
2474
+ if (state.poolStart !== null) await state.poolStart.catch(() => void 0);
2475
+ let threadsGone = state.retiredThreadsGone;
2476
+ if (state.workerPool) {
2477
+ await awaitCompactions(state);
2478
+ await awaitReplicationIdle(state);
2479
+ await state.workerPool.shutdown();
2480
+ threadsGone = Promise.all([threadsGone, state.workerPool.whenEveryThreadIsGone()]).then(() => void 0);
2481
+ state.workerPool = null;
2482
+ state.scaledOutIndexes.clear();
2483
+ state.replicationQueues.clear();
2484
+ state.segmentLedger.clear();
2485
+ }
2486
+ void threadsGone.then(() => {
2487
+ state.sharedVectorFields.clear();
2488
+ state.publishedVectorStores.clear();
2489
+ });
2490
+ }
2491
+ async function restoreAfterFailedClose(indexName, failure) {
2492
+ const pool = state.workerPool;
2493
+ const entry = indexRegistry.get(indexName);
2494
+ const manager = executor.getManager(indexName);
2495
+ if (pool !== null && entry !== void 0 && manager !== void 0) {
2496
+ try {
2497
+ const copyLoad = await transferIndexToPool(
2498
+ indexName,
2499
+ pool,
2500
+ { ...entry, onPublication: (publication) => trackPublicationStores(state, publication) },
2501
+ manager,
2502
+ callbacks?.isAnalysisStale?.(indexName)
2503
+ );
2504
+ state.scaledOutIndexes.add(indexName);
2505
+ markCopiesLoaded(state, indexName, copyLoad);
2506
+ refreshVectorCopies(state, indexName);
2507
+ } catch (repairError) {
2508
+ throw new NarsilError(
2509
+ ErrorCodes.WORKER_CRASHED,
2510
+ `Worker copies for index "${indexName}" could not be restored after close failed`,
2511
+ {
2512
+ indexName,
2513
+ closeError: failure.reason instanceof Error ? failure.reason.message : String(failure.reason),
2514
+ repairError: repairError instanceof Error ? repairError.message : String(repairError)
2515
+ }
2516
+ );
2517
+ }
2518
+ }
2519
+ if (failure.reason instanceof NarsilError) throw failure.reason;
2520
+ throw new NarsilError(ErrorCodes.WORKER_CRASHED, `A worker could not close index "${indexName}"`, {
2521
+ indexName,
2522
+ cause: failure.reason instanceof Error ? failure.reason.message : String(failure.reason)
2523
+ });
2524
+ }
2525
+ function withdrawCopies(indexName) {
2526
+ const pool = state.workerPool;
2527
+ if (pool === null) return;
2528
+ const holders = pool.executorsHolding(indexName);
2529
+ pool.removeIndex(indexName);
2530
+ state.segmentLedger.delete(indexName);
2531
+ void Promise.allSettled(
2532
+ holders.map((worker) => worker.execute({ type: "dropIndex", indexName, requestId: `desync-drop-${indexName}` }))
2533
+ );
2534
+ }
2535
+ async function closeIndex(indexName) {
2536
+ if (state.poolRepair !== null) await state.poolRepair;
2537
+ const transition = state.copyTransitions.get(indexName);
2538
+ if (transition !== void 0) await transition.done;
2539
+ await awaitReplicationIdle(state, indexName);
2540
+ await awaitCompactions(state);
2541
+ cancelIdleMerge(state, indexName);
2542
+ const pool = state.workerPool;
2543
+ if (pool !== null && state.scaledOutIndexes.has(indexName)) {
2544
+ state.scaledOutIndexes.delete(indexName);
2545
+ const outcomes = await Promise.allSettled(
2546
+ pool.executorsHolding(indexName).map((worker) => worker.execute({ type: "dropIndex", indexName, requestId: `close-${indexName}` }))
2547
+ );
2548
+ const failure = outcomes.find((outcome) => outcome.status === "rejected");
2549
+ if (failure?.status === "rejected") await restoreAfterFailedClose(indexName, failure);
2550
+ pool.removeIndex(indexName);
2551
+ }
2552
+ state.droppedCopies.delete(indexName);
2553
+ if (!state.copyReleaseHolds.has(indexName)) state.desyncedIndexes.delete(indexName);
2554
+ state.lastAccessAt.delete(indexName);
2555
+ state.replicationQueues.delete(indexName);
2556
+ state.segmentLedger.delete(indexName);
2557
+ }
2558
+ async function replicate(action) {
2559
+ await replicateToWorkers(state, action);
2560
+ if (!("indexName" in action)) return;
2561
+ if (action.type === "attachSegments") maybeCompactSegments(state, action.indexName);
2562
+ if (action.type === "insert" || action.type === "update") await flushGrownTails(state, action.indexName);
2563
+ if (state.scaledOutIndexes.has(action.indexName) || state.copyLoadBuffers.has(action.indexName)) {
2564
+ scheduleIdleMerge(state, action.indexName);
2565
+ }
2566
+ }
2567
+ return {
2568
+ scaleOutReadyIndexes: () => scaleOutReadyIndexes(state),
2569
+ scaleOutBeforeBatch: (indexName, incomingCount) => scaleOutBeforeBatch(state, indexName, incomingCount),
2570
+ replicateToWorkers: replicate,
2571
+ publishToWorkers: (action) => publishToWorkers2(state, action),
2572
+ settlePublications(indexName, growth = "liveTail") {
2573
+ const room = publicationCapacityWait(state, indexName);
2574
+ if (room === null && (growth === "none" || !holdsCopies(state, indexName))) {
2575
+ if (growth === "segments") maybeCompactSegments(state, indexName);
2576
+ if (holdsCopies(state, indexName)) scheduleIdleMerge(state, indexName);
2577
+ return void 0;
2578
+ }
2579
+ return settleCopiedPublications(state, indexName, growth, room);
2580
+ },
2581
+ awaitReplication: (indexName) => awaitReplicationIdle(state, indexName),
2582
+ queueDiskAdoption: (action) => {
2583
+ publishToWorkers2(state, action);
2584
+ },
2585
+ awaitWrites: (indexName) => awaitWritesApplied(state, indexName),
2586
+ awaitCompactions: () => awaitCompactions(state),
2587
+ openIndex,
2588
+ closeIndex,
2589
+ isIndexBusy: (indexName) => isIndexBusy(state, indexName),
2590
+ buildSegments: (requests) => buildSegments(state, requests),
2591
+ segmentBuildConcurrency: (indexName) => segmentBuildConcurrency(state, indexName),
2592
+ searchViaWorker: (indexName, params, globalStats, partitionIds, patternWork, bm25, reads) => searchViaWorker(state, indexName, params, globalStats, partitionIds, patternWork, bm25, reads),
2593
+ hasWorkerPool: () => state.workerPool !== null,
2594
+ mainCopyQueries: () => state.mainCopyQueries ?? "lone",
2595
+ shareMainThread: () => {
2596
+ if (state.mainCopyQueries === void 0) state.mainCopyQueries = "none";
2597
+ },
2598
+ serveRequestsOnWorkers: (listener) => serveRequestsOnWorkers(state, listener),
2599
+ requestThreadCount: () => requestThreadCountOf(state),
2600
+ copyIdleTimeoutMs: () => state.copyIdleTimeoutMs,
2601
+ stopRequestThreads: () => stopRequestThreads(state),
2602
+ desyncIndex: (indexName) => {
2603
+ state.desyncedIndexes.add(indexName);
2604
+ const loading = state.copyTransitions.get(indexName);
2605
+ const wasScaledOut = state.scaledOutIndexes.delete(indexName);
2606
+ if (wasScaledOut) withdrawCopies(indexName);
2607
+ return wasScaledOut || loading !== void 0 && loading.kind !== "drop";
2608
+ },
2609
+ async releaseIndexCopies(indexName) {
2610
+ const hold = state.copyReleaseHolds.get(indexName);
2611
+ if (hold === void 0) state.copyReleaseHolds.set(indexName, { holders: 1, reloads: false });
2612
+ else hold.holders += 1;
2613
+ state.desyncedIndexes.add(indexName);
2614
+ return releaseIndexCopies(state, indexName, copyReleaseSteps);
2615
+ },
2616
+ async endCopyRelease(indexName, reloads) {
2617
+ await endCopyRelease(state, indexName, reloads, copyReleaseSteps);
2618
+ },
2619
+ resyncIndex,
2620
+ noteAccess: (indexName) => noteAccess(state, indexName),
2621
+ workerCopies,
2622
+ getWorkerMemoryStats,
2623
+ shutdown
2624
+ };
2625
+ }
2626
+
2627
+ export { bindWorkerCopiesHost, createWorkerExecutor, createWorkerOrchestrator, createWorkerPool, emptySerializablePartition, endCopyRelease, loadIndexOntoWorkers, publishToWorkers, releaseIndexCopies, releasePublicationOf2 as releasePublicationOf, retainPublicationOf2 as retainPublicationOf, runSegmentAction, transferIndexToPool };