@delali/narsil 0.3.1-next.130 → 0.3.1-next.140

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 (120) hide show
  1. package/dist/adapters/filesystem.mjs +1 -1
  2. package/dist/adapters/indexeddb.mjs +1 -1
  3. package/dist/analysis-change-JN23AZQP.mjs +43 -0
  4. package/dist/analysis-change-entry-5JSIMEKG.mjs +1 -0
  5. package/dist/browser/analysis-change-57MIRJWR.mjs +499 -0
  6. package/dist/browser/analysis-change-entry-ICGRR3IN.mjs +1 -0
  7. package/dist/browser/{chunk-UDGB2MWT.mjs → chunk-35B42UFM.mjs} +4 -0
  8. package/dist/browser/{chunk-VBBLTD7O.mjs → chunk-3EYDURFD.mjs} +39 -27
  9. package/dist/browser/{chunk-AIIJDWMG.mjs → chunk-4MFH226Q.mjs} +1 -1
  10. package/dist/browser/{chunk-D7WQF6DV.mjs → chunk-5LYWZJE2.mjs} +2 -2
  11. package/dist/browser/chunk-67KSBUYR.mjs +173 -0
  12. package/dist/browser/{chunk-M5UAAXGU.mjs → chunk-6BSB4N6B.mjs} +2 -2
  13. package/dist/browser/{chunk-NSCSR6MA.mjs → chunk-CGVBGHBX.mjs} +81 -6
  14. package/dist/browser/chunk-E5SFTOQR.mjs +21 -0
  15. package/dist/browser/{chunk-VEGHFP2P.mjs → chunk-MZI3TEOW.mjs} +4 -2
  16. package/dist/browser/chunk-OIZS5KIX.mjs +6 -0
  17. package/dist/browser/chunk-QHNNULPW.mjs +265 -0
  18. package/dist/browser/chunk-XVMOQQ34.mjs +384 -0
  19. package/dist/browser/{chunk-2Y3CO4EQ.mjs → chunk-ZAIZQOTM.mjs} +256 -777
  20. package/dist/browser/{durability-wiring-FNZSRH4N.mjs → durability-wiring-MTIAN6VT.mjs} +145 -50
  21. package/dist/browser/import-Q7MXD6BS.mjs +8 -0
  22. package/dist/browser/{module-4HIMOQM5.mjs → module-5J3PEUEB.mjs} +7 -6
  23. package/dist/browser/{phrase-module-WESYVU7K.mjs → phrase-module-HGVBO24W.mjs} +1 -1
  24. package/dist/{restore-queue-JAMNRC6T.mjs → browser/restore-queue-DISFECE4.mjs} +8 -2
  25. package/dist/browser/{runtime-3X7ZJ5T3.mjs → runtime-VEUH674B.mjs} +2 -2
  26. package/dist/browser/{snapshot-V7VF6BN6.mjs → snapshot-YBJ5MF2M.mjs} +36 -167
  27. package/dist/browser/{write-YIC2MKRY.mjs → write-RTWI5MXL.mjs} +8 -6
  28. package/dist/{capabilities-Bl8G9taj.d.ts → capabilities-DXAmysqJ.d.ts} +11 -11
  29. package/dist/{chunk-NMXRXFGZ.mjs → chunk-4GCN34JV.mjs} +2 -2
  30. package/dist/{chunk-SOHMMNCH.mjs → chunk-6VHWXAOI.mjs} +4 -4
  31. package/dist/{chunk-2C3WXBQS.mjs → chunk-74XB2SXI.mjs} +1 -1
  32. package/dist/{chunk-QQTXT65X.mjs → chunk-B5I3IL4H.mjs} +12 -7
  33. package/dist/chunk-BDIDUW2B.mjs +387 -0
  34. package/dist/{chunk-5LX6MOIB.mjs → chunk-C3YDYX6C.mjs} +1 -1
  35. package/dist/{chunk-Y4BVTQ53.mjs → chunk-CCCGEPJM.mjs} +2 -2
  36. package/dist/{chunk-2JWNJSNE.mjs → chunk-CPCGEIOM.mjs} +11 -8
  37. package/dist/chunk-CZQ33DLW.mjs +78 -0
  38. package/dist/{chunk-4GDLFDPZ.mjs → chunk-DD2673TX.mjs} +3 -2
  39. package/dist/{chunk-RNSC4NFI.mjs → chunk-EEBCDP36.mjs} +3 -1
  40. package/dist/{chunk-HCGJKDZV.mjs → chunk-FFVLPCLH.mjs} +1 -1
  41. package/dist/{chunk-WVRDRLIC.mjs → chunk-FKARMUWH.mjs} +6 -6
  42. package/dist/{chunk-PATMDIMN.mjs → chunk-HJJWY6PT.mjs} +1 -1
  43. package/dist/chunk-HMDT3I63.mjs +21 -0
  44. package/dist/{chunk-CZ7ISLXA.mjs → chunk-HNHCNTVG.mjs} +4 -0
  45. package/dist/{chunk-RWESFSJI.mjs → chunk-IBATGS3J.mjs} +87 -61
  46. package/dist/{chunk-Y2U5VVWX.mjs → chunk-ICW45TTU.mjs} +1 -1
  47. package/dist/{chunk-T4OFG4IQ.mjs → chunk-IP4HJOGC.mjs} +90 -16
  48. package/dist/{chunk-EUCX5K74.mjs → chunk-J3EHATEZ.mjs} +3 -3
  49. package/dist/{chunk-X5CNUKD5.mjs → chunk-K3AQOZVM.mjs} +37 -20
  50. package/dist/chunk-LK3MOTXF.mjs +174 -0
  51. package/dist/{chunk-7OIQ2SIO.mjs → chunk-LLDFIKGP.mjs} +4 -4
  52. package/dist/{chunk-K77FJXKS.mjs → chunk-MTQTX4BP.mjs} +81 -8
  53. package/dist/{chunk-ZR4VM4A6.mjs → chunk-N37VMC62.mjs} +1 -18
  54. package/dist/{chunk-2WBY4Y4M.mjs → chunk-NOT5QOHU.mjs} +1 -1
  55. package/dist/{chunk-D5TFRB5G.mjs → chunk-NPNK57CA.mjs} +1 -1
  56. package/dist/{chunk-6SSVIW7Y.mjs → chunk-O4LJQI4G.mjs} +31 -5
  57. package/dist/{chunk-JJ6EUMXB.mjs → chunk-PNNVTKMT.mjs} +2 -2
  58. package/dist/{chunk-2SEL6SBS.mjs → chunk-RKAVHNRW.mjs} +2 -2
  59. package/dist/{chunk-XABMCBHA.mjs → chunk-SBMAWTRG.mjs} +684 -99
  60. package/dist/chunk-TCWQJL7O.mjs +188 -0
  61. package/dist/{chunk-2MKX2SCO.mjs → chunk-U4X4AWKL.mjs} +7 -7
  62. package/dist/{chunk-PU6SKH7O.mjs → chunk-UBXJQJII.mjs} +2 -2
  63. package/dist/chunk-UEV53BBO.mjs +480 -0
  64. package/dist/{chunk-NNO7CO3A.mjs → chunk-UIKN2OTQ.mjs} +1 -1
  65. package/dist/{chunk-F6AAXI7T.mjs → chunk-VSUYTPME.mjs} +3 -3
  66. package/dist/{chunk-TGHNXN7W.mjs → chunk-W2XDK66Z.mjs} +180 -541
  67. package/dist/{chunk-7X5JST5L.mjs → chunk-WNBRW4Q2.mjs} +16 -2
  68. package/dist/{chunk-6M6SDH3S.mjs → chunk-XMQNOD5V.mjs} +8 -8
  69. package/dist/{chunk-SZGRYEIC.mjs → chunk-XS2VM7UE.mjs} +3 -3
  70. package/dist/{chunk-JT3YZGCE.mjs → chunk-XXKWEG53.mjs} +3 -188
  71. package/dist/{chunk-YBWSV5DV.mjs → chunk-ZMBFWQXN.mjs} +2 -2
  72. package/dist/{chunk-7XY6VD7B.mjs → chunk-ZSWHPKGM.mjs} +1 -1
  73. package/dist/{client-Cx-TiqPE.d.ts → client-DApBeLbk.d.ts} +18 -3
  74. package/dist/client.d.ts +7 -8
  75. package/dist/client.mjs +11 -1
  76. package/dist/{events-DErop1LM.d.ts → config-B0H-vp4U.d.ts} +424 -2
  77. package/dist/{config-CzB9YvF_.d.ts → config-Brysl9NO.d.ts} +3 -3
  78. package/dist/{direct-executor-HQ4QVDX7.mjs → direct-executor-J5UDLPL3.mjs} +14 -14
  79. package/dist/distribution/coordinator/etcd.d.ts +2 -2
  80. package/dist/distribution/coordinator/etcd.mjs +1 -1
  81. package/dist/distribution/coordinator/in-memory.d.ts +2 -2
  82. package/dist/distribution/transport/grpc.mjs +1 -1
  83. package/dist/distribution.d.ts +24 -9
  84. package/dist/distribution.mjs +882 -192
  85. package/dist/{durability-wiring-S7OX2MAI.mjs → durability-wiring-AYOTAPNQ.mjs} +165 -70
  86. package/dist/embeddings/openai.mjs +2 -2
  87. package/dist/{engine-B_W9sdZK.d.ts → engine-CuLMbH1-.d.ts} +64 -7
  88. package/dist/import-QRZAQ5RH.mjs +43 -0
  89. package/dist/{index-CKI1fDEl.d.ts → index-HYnqS1SE.d.ts} +2 -2
  90. package/dist/index.browser.mjs +4 -4
  91. package/dist/index.d.ts +6 -7
  92. package/dist/index.mjs +20 -20
  93. package/dist/{long-query-thread-WVSMIBYP.mjs → long-query-thread-U7K5A72G.mjs} +14 -14
  94. package/dist/{module-2E4XYSGT.mjs → module-L2OZO5PQ.mjs} +11 -11
  95. package/dist/partial-words.mjs +5 -5
  96. package/dist/pattern.mjs +3 -3
  97. package/dist/persistence/durability/checkpoint-worker.d.ts +12 -1
  98. package/dist/persistence/durability/checkpoint-worker.mjs +23 -23
  99. package/dist/{phrase-module-JERF2IDG.mjs → phrase-module-5QHBD2VJ.mjs} +1 -1
  100. package/dist/react.d.ts +11 -8
  101. package/dist/react.mjs +4 -0
  102. package/dist/{rerank-DMMT4FZA.mjs → rerank-KTLC3E6N.mjs} +5 -5
  103. package/dist/rerankers/http.mjs +2 -2
  104. package/dist/{browser/restore-queue-GEZ54CA5.mjs → restore-queue-RYSRRFS4.mjs} +8 -2
  105. package/dist/{runtime-7MLQ7J64.mjs → runtime-DYV75DCB.mjs} +6 -6
  106. package/dist/{schema-CS-clLun.d.ts → schema-BTXbHCqA.d.ts} +4 -1
  107. package/dist/server.d.ts +200 -11
  108. package/dist/server.mjs +66 -23
  109. package/dist/{snapshot-DOQLGGEX.mjs → snapshot-BTRZ3MZK.mjs} +59 -190
  110. package/dist/synonyms.mjs +6 -4
  111. package/dist/tasks-DjGjK70Y.d.ts +720 -0
  112. package/dist/{thread-DNK5XWXG.mjs → thread-USB3GOPY.mjs} +13 -13
  113. package/dist/vector/search-worker.d.ts +1 -1
  114. package/dist/vector/search-worker.mjs +5 -5
  115. package/dist/workers/entry.mjs +7 -7
  116. package/dist/{write-ZKR7SNRT.mjs → write-GHTA56OW.mjs} +25 -22
  117. package/openapi.json +5310 -0
  118. package/package.json +16 -8
  119. package/dist/config-hOWGBeC6.d.ts +0 -414
  120. package/dist/tasks-BfF-VRhm.d.ts +0 -302
@@ -0,0 +1,480 @@
1
+ import { requireAnalysisRebuildMemory } from './chunk-LK3MOTXF.mjs';
2
+ import { requireConfigurationWithinSizeLimit, reconstructSchemaFromMetadata, prepareStoredSynonyms, rulesOfStoredSynonyms, emitEngineEvent, prepareConfiguredSynonyms, rebuildTextIndexes, textRebuildTargetOf } from './chunk-W2XDK66Z.mjs';
3
+ import { resolvePartialWordAnalysis } from './chunk-IBATGS3J.mjs';
4
+ import { ARRAY_POSITION_GAP } from './chunk-U4X4AWKL.mjs';
5
+ import { ANALYSIS_CHANGE_MEMBERS, ANALYSIS_HOLD_APPLY_INTERVAL_MS } from './chunk-EEBCDP36.mjs';
6
+ import { configUnderAnalysis, resolveIndexAnalysis, targetOfPending } from './chunk-O4LJQI4G.mjs';
7
+ import { getLanguage } from './chunk-WNBRW4Q2.mjs';
8
+ import { compareCodePoints } from './chunk-YTJCGKM4.mjs';
9
+ import { NarsilError, ErrorCodes } from './chunk-HNHCNTVG.mjs';
10
+
11
+ // src/engine/analysis-change/run.ts
12
+ function createHold(generation) {
13
+ let markReady = () => void 0;
14
+ let refuse = () => void 0;
15
+ let wake = () => void 0;
16
+ const ready = new Promise((resolve, reject) => {
17
+ markReady = resolve;
18
+ refuse = reject;
19
+ });
20
+ ready.catch(() => void 0);
21
+ return {
22
+ generation,
23
+ built: false,
24
+ released: false,
25
+ ready,
26
+ markReady,
27
+ refuse,
28
+ wake: () => wake(),
29
+ pause: () => new Promise((resolve) => {
30
+ const timer = setTimeout(resolve, ANALYSIS_HOLD_APPLY_INTERVAL_MS);
31
+ wake = () => {
32
+ clearTimeout(timer);
33
+ resolve();
34
+ };
35
+ })
36
+ };
37
+ }
38
+ var runsByCore = /* @__PURE__ */ new WeakMap();
39
+ function runsOf(core) {
40
+ let runs = runsByCore.get(core);
41
+ if (runs === void 0) {
42
+ runs = /* @__PURE__ */ new Map();
43
+ runsByCore.set(core, runs);
44
+ }
45
+ return runs;
46
+ }
47
+ function createRun(target, hold = null) {
48
+ let finish = () => void 0;
49
+ let rejectFinished = () => void 0;
50
+ let settle = () => void 0;
51
+ const finished = new Promise((resolve, reject) => {
52
+ finish = resolve;
53
+ rejectFinished = reject;
54
+ });
55
+ finished.catch(() => void 0);
56
+ const settled = new Promise((resolve) => {
57
+ settle = resolve;
58
+ });
59
+ const fail = (error) => {
60
+ rejectFinished(error);
61
+ hold?.refuse(error);
62
+ hold?.wake();
63
+ };
64
+ return { target, stopReason: null, switched: false, settled, finish, fail, finished, settle, hold };
65
+ }
66
+ async function waitForRelease(run, hold, keepCurrent) {
67
+ hold.built = true;
68
+ hold.markReady();
69
+ while (!hold.released) {
70
+ if (run.stopReason !== null || !keepCurrent()) {
71
+ hold.built = false;
72
+ return false;
73
+ }
74
+ await hold.pause();
75
+ }
76
+ return true;
77
+ }
78
+ function stopRun(run, indexName) {
79
+ run.stopReason = new NarsilError(
80
+ ErrorCodes.ANALYSIS_CHANGE_REPLACED,
81
+ `A later change or a restore replaced this analysis change of index "${indexName}"`,
82
+ { indexName }
83
+ );
84
+ run.fail(run.stopReason);
85
+ }
86
+
87
+ // src/engine/analysis-change/settings.ts
88
+ function refuseTokenizer(indexName) {
89
+ throw new NarsilError(
90
+ ErrorCodes.CONFIG_INVALID,
91
+ `The tokenizer of index "${indexName}" must change to a name registered with registerTokenizer, because an instance survives neither a worker boundary nor a restart`,
92
+ { indexName }
93
+ );
94
+ }
95
+ function refuseStopWords(indexName) {
96
+ throw new NarsilError(
97
+ ErrorCodes.CONFIG_INVALID,
98
+ `The stop words of index "${indexName}" must change to a name registered with registerStopWords or to a list of words, because a function survives neither a worker boundary nor a restart`,
99
+ { indexName }
100
+ );
101
+ }
102
+ function stopWordsOfChange(indexName, value) {
103
+ if (value === void 0 || value === null || typeof value === "string") return value;
104
+ const words = value instanceof Set ? [...value] : value;
105
+ if (!Array.isArray(words) || words.some((word) => typeof word !== "string")) refuseStopWords(indexName);
106
+ return new Set(words);
107
+ }
108
+ function tokenizerOfChange(indexName, value) {
109
+ if (value === void 0 || value === null || typeof value === "string") return value;
110
+ refuseTokenizer(indexName);
111
+ }
112
+ function requireRegisteredLanguage(name) {
113
+ if (typeof name !== "string") {
114
+ throw new NarsilError(ErrorCodes.LANGUAGE_NOT_SUPPORTED, "The language of an analysis change must be a name");
115
+ }
116
+ return getLanguage(name);
117
+ }
118
+ function resolveAnalysisTarget(indexName, base, change) {
119
+ if (change === null || typeof change !== "object" || Array.isArray(change)) {
120
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "An analysis change must be an object", { indexName });
121
+ }
122
+ const unknownMember = Object.keys(change).find((member) => !ANALYSIS_CHANGE_MEMBERS.includes(member));
123
+ if (unknownMember !== void 0) {
124
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, `"${unknownMember}" is not an analysis setting`, { indexName });
125
+ }
126
+ const language = change.language === void 0 ? getLanguage(base.language ?? "english") : requireRegisteredLanguage(change.language);
127
+ const tokenizer = tokenizerOfChange(indexName, change.tokenizer);
128
+ const stopWords = stopWordsOfChange(indexName, change.stopWords);
129
+ const config = configUnderAnalysis(
130
+ base,
131
+ language.name,
132
+ tokenizer === void 0 ? base.tokenizer : tokenizer ?? void 0,
133
+ stopWords === void 0 ? base.stopWords : stopWords ?? void 0
134
+ );
135
+ requireConfigurationWithinSizeLimit(config, language);
136
+ return { config, language, analysis: resolveIndexAnalysis(config) };
137
+ }
138
+ function currentConfigUnderTarget(current, target) {
139
+ return configUnderAnalysis(current, target.language.name, target.config.tokenizer, target.config.stopWords);
140
+ }
141
+ function sameStopWords(left, right) {
142
+ if (left === right) return true;
143
+ if (!(left instanceof Set) || !(right instanceof Set) || left.size !== right.size) return false;
144
+ for (const word of left) if (!right.has(word)) return false;
145
+ return true;
146
+ }
147
+ function holdsSettings(config, target) {
148
+ return (config.language ?? "english") === target.language.name && config.tokenizer === target.config.tokenizer && sameStopWords(config.stopWords, target.config.stopWords);
149
+ }
150
+ function namedSettingsOf(config, language) {
151
+ return {
152
+ language,
153
+ ...typeof config.tokenizer === "string" ? { tokenizer: config.tokenizer } : {},
154
+ ...typeof config.stopWords === "string" ? { stopWords: config.stopWords } : {},
155
+ ...config.stopWords instanceof Set ? { stopWordList: [...config.stopWords].sort(compareCodePoints) } : {}
156
+ };
157
+ }
158
+ function durableSettingsOf(entry) {
159
+ return {
160
+ ...namedSettingsOf(entry.config, entry.language.name),
161
+ generation: entry.analysisGeneration,
162
+ analysisRevision: entry.analysisRevision,
163
+ arrayPositionGap: entry.arrayPositionGap
164
+ };
165
+ }
166
+ function requireSynonymsUnderTarget(entry, target) {
167
+ if (entry.synonyms === null) return;
168
+ prepareConfiguredSynonyms({ ...target.config, synonyms: rulesOfStoredSynonyms(entry.synonyms) }, target.language);
169
+ }
170
+
171
+ // src/engine/analysis-change/adopt-saved.ts
172
+ function adoptSavedAnalysis(manager, entry, metadata) {
173
+ const saved = reconstructSchemaFromMetadata(metadata);
174
+ const { tokenizer: _tokenizer, stopWords: _stopWords, ...rest } = entry.config;
175
+ const config = {
176
+ ...rest,
177
+ language: saved.language,
178
+ ...saved.tokenizer !== void 0 ? { tokenizer: saved.tokenizer } : {},
179
+ ...saved.stopWords !== void 0 ? { stopWords: saved.stopWords } : {}
180
+ };
181
+ const language = getLanguage(config.language ?? "english");
182
+ const analysis = resolveIndexAnalysis(config);
183
+ const synonyms = prepareStoredSynonyms(rulesOfStoredSynonyms(entry.synonyms), config, language);
184
+ entry.config = config;
185
+ entry.language = language;
186
+ entry.analysisGeneration = metadata.analysisGeneration ?? 0;
187
+ entry.analysisRevision = metadata.analysisRevision ?? null;
188
+ entry.arrayPositionGap = metadata.arrayPositionGap ?? null;
189
+ manager?.adoptAnalysis({
190
+ config,
191
+ language,
192
+ analysis,
193
+ partialWordAnalysis: resolvePartialWordAnalysis(config, language, analysis),
194
+ synonyms: synonyms?.lookup ?? null
195
+ });
196
+ }
197
+
198
+ // src/engine/analysis-change/index.ts
199
+ function termBytesOf(core, indexName) {
200
+ let bytes = 0;
201
+ for (const partition of core.requireManager(indexName).getAllPartitions()) {
202
+ bytes += partition.estimateMemoryBytes();
203
+ for (const state of partition.readStates()) bytes -= state.docStore.contentBytes();
204
+ }
205
+ return Math.max(0, bytes);
206
+ }
207
+ function toError(value) {
208
+ return value instanceof Error ? value : new Error(String(value));
209
+ }
210
+ async function requireNoUndurableSwitch(core, indexName, entry) {
211
+ if (entry.durableAnalysis === null) return;
212
+ const durable = core.durability?.manager;
213
+ await (durable?.checkpointFromMemory?.(indexName) ?? durable?.checkpoint(indexName));
214
+ if (entry.durableAnalysis === null) return;
215
+ throw new NarsilError(
216
+ ErrorCodes.ANALYSIS_CHANGE_NOT_READY,
217
+ `The previous analysis change of index "${indexName}" has no durable checkpoint yet, so try this change again once a checkpoint succeeds`,
218
+ { indexName }
219
+ );
220
+ }
221
+ function switchIndex({ core, indexName, entry, run, generation }) {
222
+ const { language, analysis } = run.target;
223
+ const config = currentConfigUnderTarget(entry.config, run.target);
224
+ const synonyms = prepareStoredSynonyms(rulesOfStoredSynonyms(entry.synonyms), config, language);
225
+ return () => {
226
+ run.switched = true;
227
+ entry.durableAnalysis = core.durability === null ? null : durableSettingsOf(entry);
228
+ entry.config = config;
229
+ entry.language = language;
230
+ entry.analysisGeneration = generation;
231
+ entry.analysisRevision = language.revision;
232
+ entry.arrayPositionGap = ARRAY_POSITION_GAP;
233
+ entry.pendingAnalysis = null;
234
+ entry.synonyms = synonyms?.encoded ?? null;
235
+ core.requireManager(indexName).adoptAnalysis({
236
+ ...textRebuildTargetOf(config, language, analysis),
237
+ synonyms: synonyms?.lookup ?? null
238
+ });
239
+ core.analysisRebuild.clearStale(indexName);
240
+ };
241
+ }
242
+ async function buildAndSwitch(scope) {
243
+ const { core, indexName, entry, run } = scope;
244
+ const { hold } = run;
245
+ const manager = core.requireManager(indexName);
246
+ const emit = (status, partitionsRebuilt2, error) => {
247
+ emitEngineEvent(core.eventHandlers, "analysisRebuild", {
248
+ indexName,
249
+ status,
250
+ partitionsRebuilt: partitionsRebuilt2,
251
+ partitionCount: manager.partitionCount,
252
+ ...error !== void 0 ? { error } : {}
253
+ });
254
+ };
255
+ let partitionsRebuilt = 0;
256
+ let adopt = () => void 0;
257
+ const isCurrent = () => run.stopReason === null && !core.shutdownState.isShutdown && core.indexRegistry.get(indexName) === entry && core.executor.getManager(indexName) === manager;
258
+ emit("started", 0);
259
+ const wasPromoted = core.orchestrator.desyncIndex(indexName);
260
+ try {
261
+ const durable = core.durability?.manager;
262
+ const rebuilt = await rebuildTextIndexes(
263
+ manager,
264
+ {
265
+ isCurrent,
266
+ onPartitionBuilt(built) {
267
+ partitionsRebuilt = built;
268
+ },
269
+ onSwitch: () => adopt(),
270
+ ...hold === null ? {} : { holdBeforeSwitch: (keepCurrent) => waitForRelease(run, hold, keepCurrent) },
271
+ runSwitch(swap) {
272
+ const swapWithSynonyms = () => {
273
+ if (!isCurrent()) return false;
274
+ adopt = switchIndex(scope);
275
+ return swap();
276
+ };
277
+ return durable?.checkpointAfterSwitch?.(indexName, swapWithSynonyms) ?? Promise.resolve(swapWithSynonyms());
278
+ }
279
+ },
280
+ textRebuildTargetOf(run.target.config, run.target.language, run.target.analysis)
281
+ );
282
+ if (!rebuilt) throw run.stopReason ?? stoppedError(core, indexName);
283
+ if (entry.durableAnalysis !== null) {
284
+ throw new NarsilError(
285
+ ErrorCodes.PERSISTENCE_SAVE_FAILED,
286
+ `The new terms of index "${indexName}" answer queries, but they have no durable checkpoint yet`,
287
+ { indexName }
288
+ );
289
+ }
290
+ emit("completed", partitionsRebuilt);
291
+ } catch (error) {
292
+ emit("failed", partitionsRebuilt, toError(error));
293
+ throw error;
294
+ } finally {
295
+ if (core.executor.getManager(indexName) === manager) {
296
+ await core.orchestrator.resyncIndex(indexName, wasPromoted).catch(() => void 0);
297
+ }
298
+ }
299
+ }
300
+ function stoppedError(core, indexName) {
301
+ if (core.shutdownState.isShutdown) {
302
+ return new NarsilError(ErrorCodes.INSTANCE_SHUT_DOWN, "This Narsil instance has been shut down");
303
+ }
304
+ return new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Index "${indexName}" closed during its analysis change`, {
305
+ indexName
306
+ });
307
+ }
308
+ async function clearPending(core, indexName, entry) {
309
+ entry.pendingAnalysis = null;
310
+ await core.durability?.manager.persistMetadata(indexName);
311
+ }
312
+ function requireNotRebalancing(core, indexName) {
313
+ if (!core.rebalancingIndexes.has(indexName)) return;
314
+ throw new NarsilError(
315
+ ErrorCodes.PARTITION_REBALANCING_BACKPRESSURE,
316
+ `Index "${indexName}" is being rebalanced, so try its analysis change again once the rebalance ends`,
317
+ { indexName }
318
+ );
319
+ }
320
+ async function driveRun(core, indexName, entry, run, previous, resumedGeneration) {
321
+ const runs = runsOf(core);
322
+ try {
323
+ if (previous !== void 0) await previous.settled;
324
+ if (runs.get(indexName) !== run) return;
325
+ await requireNoUndurableSwitch(core, indexName, entry);
326
+ if (run.stopReason !== null) return;
327
+ if (core.indexRegistry.get(indexName) !== entry) throw stoppedError(core, indexName);
328
+ requireNotRebalancing(core, indexName);
329
+ if (resumedGeneration === null && holdsSettings(entry.config, run.target)) {
330
+ if (entry.pendingAnalysis !== null) await clearPending(core, indexName, entry);
331
+ run.finish();
332
+ return;
333
+ }
334
+ const generation = resumedGeneration ?? Math.max(entry.analysisGeneration, entry.pendingAnalysis?.generation ?? 0) + 1;
335
+ if (entry.pendingAnalysis?.generation !== generation || run.hold !== null) {
336
+ entry.pendingAnalysis = { generation, ...namedSettingsOf(run.target.config, run.target.language.name) };
337
+ await core.durability?.manager.persistMetadata(indexName);
338
+ }
339
+ if (run.stopReason !== null) return;
340
+ await core.analysisRebuild.abandon(indexName);
341
+ await core.analysisRebuild.runExclusive(
342
+ indexName,
343
+ () => buildAndSwitch({ core, indexName, entry, run, generation }),
344
+ run.hold?.ready
345
+ );
346
+ run.finish();
347
+ } catch (error) {
348
+ run.fail(error);
349
+ const keepsPending = resumedGeneration !== null || core.shutdownState.isShutdown;
350
+ const stillOwnsPending = runs.get(indexName) === run && run.stopReason === null;
351
+ if (!keepsPending && stillOwnsPending && core.indexRegistry.get(indexName) === entry) {
352
+ if (entry.pendingAnalysis !== null) await clearPending(core, indexName, entry).catch(() => void 0);
353
+ }
354
+ } finally {
355
+ if (runs.get(indexName) === run) runs.delete(indexName);
356
+ run.settle();
357
+ }
358
+ }
359
+ async function changeIndexAnalysis(core, indexName, change) {
360
+ const entry = core.requireIndex(indexName);
361
+ requireNotRebalancing(core, indexName);
362
+ const target = resolveAnalysisTarget(indexName, entry.config, change);
363
+ const runs = runsOf(core);
364
+ const previous = runs.get(indexName);
365
+ if (previous !== void 0 && holdsSettings(previous.target.config, target)) return previous.finished;
366
+ const heldAlready = holdsSettings(entry.config, target);
367
+ if (previous === void 0 && entry.durableAnalysis === null && heldAlready) return;
368
+ if (!heldAlready) {
369
+ requireSynonymsUnderTarget(entry, target);
370
+ requireAnalysisRebuildMemory(indexName, termBytesOf(core, indexName));
371
+ }
372
+ const run = createRun(target);
373
+ if (previous !== void 0 && !previous.switched) stopRun(previous, indexName);
374
+ runs.set(indexName, run);
375
+ void driveRun(core, indexName, entry, run, previous, null);
376
+ return run.finished;
377
+ }
378
+ function resumeAnalysisChange(core, indexName) {
379
+ const entry = core.requireIndex(indexName);
380
+ const runs = runsOf(core);
381
+ const running = runs.get(indexName);
382
+ if (running !== void 0) return running.finished;
383
+ const pending = entry.pendingAnalysis;
384
+ if (pending === null) return Promise.resolve();
385
+ let target;
386
+ try {
387
+ target = targetOfPending(entry, pending);
388
+ requireAnalysisRebuildMemory(indexName, termBytesOf(core, indexName));
389
+ } catch (error) {
390
+ emitEngineEvent(core.eventHandlers, "analysisRebuild", {
391
+ indexName,
392
+ status: "failed",
393
+ partitionsRebuilt: 0,
394
+ partitionCount: entry.partitionCount,
395
+ error: toError(error)
396
+ });
397
+ return Promise.reject(error);
398
+ }
399
+ const run = createRun(target);
400
+ runs.set(indexName, run);
401
+ void driveRun(core, indexName, entry, run, void 0, pending.generation);
402
+ return run.finished;
403
+ }
404
+ async function abandonAnalysisChange(core, indexName) {
405
+ const run = runsOf(core).get(indexName);
406
+ if (run === void 0) return;
407
+ stopRun(run, indexName);
408
+ await run.settled;
409
+ }
410
+ function holdServes(run, entry, pending) {
411
+ return run.hold?.generation === pending.generation && holdsSettings(run.target.config, targetOfPending(entry, pending));
412
+ }
413
+ function startHeldRun(core, indexName, pending) {
414
+ const entry = core.requireIndex(indexName);
415
+ if (entry.analysisGeneration >= pending.generation) return null;
416
+ const runs = runsOf(core);
417
+ const previous = runs.get(indexName);
418
+ if (previous !== void 0 && holdServes(previous, entry, pending)) return previous;
419
+ const target = targetOfPending(entry, pending);
420
+ requireSynonymsUnderTarget(entry, target);
421
+ requireAnalysisRebuildMemory(indexName, termBytesOf(core, indexName));
422
+ const run = createRun(target, createHold(pending.generation));
423
+ if (previous !== void 0 && !previous.switched) stopRun(previous, indexName);
424
+ runs.set(indexName, run);
425
+ void driveRun(core, indexName, entry, run, previous, pending.generation);
426
+ return run;
427
+ }
428
+ async function buildHeldAnalysisChange(core, indexName, pending) {
429
+ const running = runsOf(core).get(indexName);
430
+ const entry = core.indexRegistry.get(indexName);
431
+ if (running?.hold != null && entry !== void 0 && holdServes(running, entry, pending)) return running.hold.ready;
432
+ const release = await core.indexState.acquire(indexName, false);
433
+ let run;
434
+ try {
435
+ run = startHeldRun(core, indexName, pending);
436
+ } catch (error) {
437
+ release();
438
+ throw error;
439
+ }
440
+ if (run?.hold === void 0 || run.hold === null) {
441
+ release();
442
+ return;
443
+ }
444
+ void run.settled.finally(release);
445
+ return run.hold.ready;
446
+ }
447
+ function heldAnalysisIsBuilt(core, indexName, generation) {
448
+ const hold = runsOf(core).get(indexName)?.hold;
449
+ return hold !== void 0 && hold !== null && hold.generation === generation && hold.built;
450
+ }
451
+ async function releaseHeldAnalysisChange(core, indexName, generation) {
452
+ const run = runsOf(core).get(indexName);
453
+ if (run?.hold?.generation !== generation) return;
454
+ run.hold.released = true;
455
+ run.hold.wake();
456
+ await run.finished;
457
+ }
458
+ async function dropHeldAnalysisChange(core, indexName) {
459
+ await abandonAnalysisChange(core, indexName);
460
+ const entry = core.indexRegistry.get(indexName);
461
+ if (entry !== void 0 && entry.pendingAnalysis !== null) await clearPending(core, indexName, entry);
462
+ }
463
+ function holdsNamedSettings(entry, settings) {
464
+ return holdsSettings(entry.config, targetOfPending(entry, { generation: 0, ...settings }));
465
+ }
466
+ async function stampAnalysisGeneration(core, indexName, generation) {
467
+ const entry = core.requireIndex(indexName);
468
+ if (entry.analysisGeneration >= generation) return;
469
+ await requireNoUndurableSwitch(core, indexName, entry);
470
+ const durable = core.durability?.manager;
471
+ const stamp = () => {
472
+ entry.durableAnalysis = core.durability === null ? null : durableSettingsOf(entry);
473
+ entry.analysisGeneration = generation;
474
+ entry.pendingAnalysis = null;
475
+ return true;
476
+ };
477
+ await (durable?.checkpointAfterSwitch?.(indexName, stamp) ?? Promise.resolve(stamp()));
478
+ }
479
+
480
+ export { abandonAnalysisChange, adoptSavedAnalysis, buildHeldAnalysisChange, changeIndexAnalysis, dropHeldAnalysisChange, heldAnalysisIsBuilt, holdsNamedSettings, holdsSettings, namedSettingsOf, releaseHeldAnalysisChange, resolveAnalysisTarget, resumeAnalysisChange, stampAnalysisGeneration };
@@ -1,5 +1,5 @@
1
1
  import { SCRATCH_SLOTS_PER_THREAD_POOL } from './chunk-QSK35UC2.mjs';
2
- import { NarsilError, ErrorCodes } from './chunk-CZ7ISLXA.mjs';
2
+ import { NarsilError, ErrorCodes } from './chunk-HNHCNTVG.mjs';
3
3
 
4
4
  // src/workers/constants.ts
5
5
  var WORKER_YOUNG_GENERATION_MB = 24;
@@ -1,6 +1,6 @@
1
- import { readMetadataEnvelope } from './chunk-K77FJXKS.mjs';
2
- import { readWalSegments, durableRecordMissing, checkpointLastSeqNo, SEGMENT_HEADER_SIZE } from './chunk-2JWNJSNE.mjs';
3
- import { applyDeleteEntry, applyIndexEntry } from './chunk-7OIQ2SIO.mjs';
1
+ import { readMetadataEnvelope } from './chunk-MTQTX4BP.mjs';
2
+ import { readWalSegments, durableRecordMissing, checkpointLastSeqNo, SEGMENT_HEADER_SIZE } from './chunk-CPCGEIOM.mjs';
3
+ import { applyDeleteEntry, applyIndexEntry } from './chunk-LLDFIKGP.mjs';
4
4
 
5
5
  // src/persistence/durability/recovery.ts
6
6
  async function listPersistedIndexes(directory) {