@delali/narsil 0.3.1-next.83 → 0.3.1-next.85

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.
@@ -4875,10 +4875,10 @@ function firstAtOrAfter2(sorted, key) {
4875
4875
  }
4876
4876
  return lo;
4877
4877
  }
4878
- function settledColumn(column, holds) {
4878
+ function settledColumn(column, holds2) {
4879
4879
  const { keys, values } = column;
4880
4880
  const order = [];
4881
- for (let at = 0; at < keys.length; at++) if (holds(values[at])) order.push(at);
4881
+ for (let at = 0; at < keys.length; at++) if (holds2(values[at])) order.push(at);
4882
4882
  order.sort((left, right) => compareUnits(keys[left], keys[right]) || compareUnits(values[left], values[right]));
4883
4883
  const settled2 = { keys: [], values: [] };
4884
4884
  for (const at of order) {
@@ -4890,8 +4890,8 @@ function settledColumn(column, holds) {
4890
4890
  return settled2;
4891
4891
  }
4892
4892
  var ValueAffixes = class {
4893
- constructor(holds, values) {
4894
- this.holds = holds;
4893
+ constructor(holds2, values) {
4894
+ this.holds = holds2;
4895
4895
  for (const value of values) this.add(value);
4896
4896
  }
4897
4897
  forward = { keys: [], values: [] };
@@ -6792,7 +6792,7 @@ function withinBounds(value, bounds) {
6792
6792
  }
6793
6793
  return true;
6794
6794
  }
6795
- function selectTextRange(column, requested, capacity, isLive2) {
6795
+ function selectTextRange(column, requested, capacity, isLive3) {
6796
6796
  const bounds = {
6797
6797
  ...requested,
6798
6798
  lower: requested.lower === null ? null : truncateSortString(requested.lower),
@@ -6813,7 +6813,7 @@ function selectTextRange(column, requested, capacity, isLive2) {
6813
6813
  }
6814
6814
  for (let position = start; position < end; position++) {
6815
6815
  const internalId = ordered[position];
6816
- if (!column.isDirty(internalId) && isLive2(internalId)) bitsetSet(result, internalId);
6816
+ if (!column.isDirty(internalId) && isLive3(internalId)) bitsetSet(result, internalId);
6817
6817
  }
6818
6818
  for (const internalId of column.dirtyStream().present) {
6819
6819
  if (withinBounds(column.valueOf(internalId), bounds)) bitsetSet(result, internalId);
@@ -7612,65 +7612,6 @@ function mergeSegmentState(target, source) {
7612
7612
  target.scoreBuffer = null;
7613
7613
  }
7614
7614
 
7615
- // src/core/partition/rebuild.ts
7616
- var EMPTY_STATS = {
7617
- totalDocuments: 0,
7618
- totalFieldLengths: {},
7619
- averageFieldLengths: {},
7620
- docFrequencies: {}
7621
- };
7622
- function textFieldsOf(fields) {
7623
- return fields.filter((field) => isWordIndexedBase(field.base));
7624
- }
7625
- function rebuildTextIndex(state, schema, language, options) {
7626
- const textFields = textFieldsOf(schemaFieldsOf(schema));
7627
- state.invertedIdx.clear();
7628
- state.surfaceRegistry.clear();
7629
- state.stats.deserialize(EMPTY_STATS);
7630
- state.invertedIdx.beginBatch();
7631
- try {
7632
- for (const [docId, stored] of state.docStore.all()) {
7633
- const internalId = state.docStore.getInternalId(docId);
7634
- if (internalId === void 0) continue;
7635
- const fields = stored.fields;
7636
- const fieldLengths = {};
7637
- const tokensByField = {};
7638
- for (const { path: fieldPath, base } of textFields) {
7639
- const value = getNestedValue(fields, fieldPath);
7640
- if (value === void 0 || value === null) continue;
7641
- if (base === "string") {
7642
- indexStringField(
7643
- state,
7644
- internalId,
7645
- fieldPath,
7646
- value,
7647
- language,
7648
- options,
7649
- fieldLengths,
7650
- tokensByField
7651
- );
7652
- } else {
7653
- indexStringArrayField(
7654
- state,
7655
- internalId,
7656
- fieldPath,
7657
- value,
7658
- language,
7659
- options,
7660
- fieldLengths,
7661
- tokensByField
7662
- );
7663
- }
7664
- }
7665
- state.docStore.storeRef(docId, stored.fields, fieldLengths);
7666
- state.stats.addDocument(fieldLengths, tokensByField);
7667
- }
7668
- } finally {
7669
- state.invertedIdx.endBatch();
7670
- state.partialWords.forgetSpellings();
7671
- }
7672
- }
7673
-
7674
7615
  // src/core/partition/block-bounds.ts
7675
7616
  var ABSENT_FIELD_LENGTH_BOUND = 0;
7676
7617
  var boundsByList = /* @__PURE__ */ new WeakMap();
@@ -9163,9 +9104,6 @@ function createPartitionIndex(partitionId, trackPositions = true) {
9163
9104
  if (internalId !== void 0) recordSortValues(state, internalId, document2);
9164
9105
  }
9165
9106
  },
9166
- rebuildTextIndex(schema, language, options) {
9167
- rebuildTextIndex(state, schema, language, options);
9168
- },
9169
9107
  get(docId, projection) {
9170
9108
  const stored = state.docStore.get(docId);
9171
9109
  if (!stored) return void 0;
@@ -9259,6 +9197,8 @@ function yieldToEventLoop() {
9259
9197
  // src/partitioning/constants.ts
9260
9198
  var REBALANCE_CHUNK_SIZE = 1e3;
9261
9199
  var DEFAULT_WRITE_AHEAD_QUEUE_MAX_SIZE = 1e4;
9200
+ var TEXT_REBUILD_CHUNK_DOCUMENTS = 1e3;
9201
+ var TEXT_REBUILD_SWITCH_BACKLOG_DOCUMENTS = 1e3;
9262
9202
 
9263
9203
  // src/partitioning/rebalancer.ts
9264
9204
  function createRebalancer() {
@@ -11162,18 +11102,6 @@ function createCompositePartition(partitionId, trackPositions = true, existingLi
11162
11102
  if (frozen.length === 0) return live.encodeSegment();
11163
11103
  return composedScratch().encodeSegment();
11164
11104
  },
11165
- rebuildTextIndex(schema, language, options) {
11166
- invalidateDocFrequencies();
11167
- if (frozen.length > 0) {
11168
- for (const segment of frozen) {
11169
- const payload = encodeSegmentState(segment);
11170
- if (payload.documentCount === 0) continue;
11171
- live.mergeSegmentPayload(payload, survivorDocumentsOf(segment, payload));
11172
- }
11173
- frozen.length = 0;
11174
- }
11175
- live.rebuildTextIndex(schema, language, options);
11176
- },
11177
11105
  serialize(indexName, totalPartitions, language, schema) {
11178
11106
  if (frozen.length === 0) return live.serialize(indexName, totalPartitions, language, schema);
11179
11107
  return composedScratch().serialize(indexName, totalPartitions, language, schema);
@@ -11417,6 +11345,81 @@ function setNestedValue(obj, path, value) {
11417
11345
  current[segments[segments.length - 1]] = value;
11418
11346
  }
11419
11347
 
11348
+ // src/partitioning/manager/stats.ts
11349
+ function aggregateStatsOf(partitions) {
11350
+ let totalDocuments = 0;
11351
+ const docFrequencies = /* @__PURE__ */ Object.create(null);
11352
+ const totalFieldLengths = /* @__PURE__ */ Object.create(null);
11353
+ for (let i = 0; i < partitions.length; i++) {
11354
+ const stats = partitions[i].stats;
11355
+ totalDocuments += stats.totalDocuments;
11356
+ for (const [term, freq] of Object.entries(stats.docFrequencies)) {
11357
+ docFrequencies[term] = (docFrequencies[term] ?? 0) + freq;
11358
+ }
11359
+ for (const [field, length] of Object.entries(stats.totalFieldLengths)) {
11360
+ totalFieldLengths[field] = (totalFieldLengths[field] ?? 0) + length;
11361
+ }
11362
+ }
11363
+ return { totalDocuments, docFrequencies, totalFieldLengths };
11364
+ }
11365
+ function estimateBytesOf(partitions, vecIndexes) {
11366
+ let total = 0;
11367
+ for (let i = 0; i < partitions.length; i++) {
11368
+ total += partitions[i].estimateMemoryBytes();
11369
+ }
11370
+ for (const [, vecIndex] of vecIndexes) {
11371
+ total += vecIndex.estimateMemoryBytes();
11372
+ }
11373
+ return total;
11374
+ }
11375
+ function partitionStatsOf(partitions) {
11376
+ return partitions.map((p, i) => ({
11377
+ partitionId: i,
11378
+ documentCount: p.count(),
11379
+ estimatedMemoryBytes: p.estimateMemoryBytes()
11380
+ }));
11381
+ }
11382
+
11383
+ // src/partitioning/manager/write-log.ts
11384
+ function createWriteLogRecorder() {
11385
+ let current = null;
11386
+ return {
11387
+ open() {
11388
+ const log = { docIds: /* @__PURE__ */ new Set(), replaced: false };
11389
+ current = log;
11390
+ return {
11391
+ get partitionsReplaced() {
11392
+ return log.replaced;
11393
+ },
11394
+ get pendingDocuments() {
11395
+ return log.docIds.size;
11396
+ },
11397
+ takeWrittenDocIds() {
11398
+ const taken = log.docIds;
11399
+ log.docIds = /* @__PURE__ */ new Set();
11400
+ return taken;
11401
+ },
11402
+ close() {
11403
+ if (current === log) current = null;
11404
+ }
11405
+ };
11406
+ },
11407
+ documentWritten(docId) {
11408
+ if (current !== null) current.docIds.add(docId);
11409
+ },
11410
+ segmentWritten(segment) {
11411
+ if (current === null) return;
11412
+ for (const internalId of segment.docStore.allInternalIds()) {
11413
+ const docId = segment.docStore.getExternalId(internalId);
11414
+ if (docId !== void 0) current.docIds.add(docId);
11415
+ }
11416
+ },
11417
+ partitionsReplaced() {
11418
+ if (current !== null) current.replaced = true;
11419
+ }
11420
+ };
11421
+ }
11422
+
11420
11423
  // src/partitioning/manager/index.ts
11421
11424
  function createPartitionManager(indexName, config, language, router, initialPartitionCount, vectorIndexes) {
11422
11425
  requirePatternSearchFor(config.schema);
@@ -11428,6 +11431,7 @@ function createPartitionManager(indexName, config, language, router, initialPart
11428
11431
  const vecIndexes = vectorIndexes ?? /* @__PURE__ */ new Map();
11429
11432
  let partitions = [];
11430
11433
  const docPartitionMap = /* @__PURE__ */ new Map();
11434
+ const writeLog = createWriteLogRecorder();
11431
11435
  for (let i = 0; i < count; i++) {
11432
11436
  partitions.push(createPartitionIndex(i, trackPositions));
11433
11437
  }
@@ -11515,9 +11519,23 @@ function createPartitionManager(indexName, config, language, router, initialPart
11515
11519
  return [...partitions];
11516
11520
  },
11517
11521
  setPartitions(newPartitions) {
11522
+ writeLog.partitionsReplaced();
11518
11523
  partitions = [...newPartitions];
11519
11524
  rebuildDocPartitionMap();
11520
11525
  },
11526
+ openWriteLog() {
11527
+ return writeLog.open();
11528
+ },
11529
+ adoptRebuiltPartitions(rebuilt) {
11530
+ if (rebuilt.length !== partitions.length) {
11531
+ throw new NarsilError(
11532
+ ErrorCodes.PARTITION_CORRUPTED,
11533
+ `Index "${indexName}" holds ${partitions.length} partitions, but the rebuild produced ${rebuilt.length}`,
11534
+ { indexName, partitionCount: partitions.length, rebuiltCount: rebuilt.length }
11535
+ );
11536
+ }
11537
+ partitions = [...rebuilt];
11538
+ },
11521
11539
  addPartition() {
11522
11540
  const currentMaxPartitions = config.partitions?.maxPartitions;
11523
11541
  if (currentMaxPartitions !== void 0 && partitions.length >= currentMaxPartitions) {
@@ -11528,11 +11546,13 @@ function createPartitionManager(indexName, config, language, router, initialPart
11528
11546
  );
11529
11547
  }
11530
11548
  const partition = createPartitionIndex(partitions.length, trackPositions);
11549
+ writeLog.partitionsReplaced();
11531
11550
  partitions.push(partition);
11532
11551
  return partition;
11533
11552
  },
11534
11553
  removePartition(partitionId) {
11535
11554
  validatePartitionId(partitionId);
11555
+ writeLog.partitionsReplaced();
11536
11556
  partitions.splice(partitionId, 1);
11537
11557
  rebuildDocPartitionMap();
11538
11558
  },
@@ -11546,6 +11566,7 @@ function createPartitionManager(indexName, config, language, router, initialPart
11546
11566
  if (newCount >= partitions.length) {
11547
11567
  return;
11548
11568
  }
11569
+ writeLog.partitionsReplaced();
11549
11570
  for (const [docId, pid] of docPartitionMap) {
11550
11571
  if (pid >= newCount) {
11551
11572
  docPartitionMap.delete(docId);
@@ -11568,6 +11589,7 @@ function createPartitionManager(indexName, config, language, router, initialPart
11568
11589
  const insertOpts = resolveInsertOptions(options);
11569
11590
  partitions[pid].insert(docId, document2, config.schema, language, insertOpts);
11570
11591
  docPartitionMap.set(docId, pid);
11592
+ writeLog.documentWritten(docId);
11571
11593
  },
11572
11594
  remove(docId) {
11573
11595
  const pid = locateDocument(docId);
@@ -11576,11 +11598,7 @@ function createPartitionManager(indexName, config, language, router, initialPart
11576
11598
  }
11577
11599
  partitions[pid].remove(docId, config.schema, language, resolveInsertOptions());
11578
11600
  docPartitionMap.delete(docId);
11579
- },
11580
- rebuildTextIndex(partitionId) {
11581
- validatePartitionId(partitionId);
11582
- partitions[partitionId].rebuildTextIndex(config.schema, language, resolveInsertOptions());
11583
- preparePartialWords(partitionId);
11601
+ writeLog.documentWritten(docId);
11584
11602
  },
11585
11603
  beginBatchRemove() {
11586
11604
  for (let i = 0; i < partitions.length; i++) {
@@ -11600,6 +11618,7 @@ function createPartitionManager(indexName, config, language, router, initialPart
11600
11618
  const updateOpts = resolveInsertOptions(options);
11601
11619
  partitions[pid].update(docId, document2, config.schema, language, updateOpts);
11602
11620
  docPartitionMap.set(docId, pid);
11621
+ writeLog.documentWritten(docId);
11603
11622
  },
11604
11623
  get(docId, projection) {
11605
11624
  const pid = locateDocument(docId);
@@ -11650,6 +11669,7 @@ function createPartitionManager(indexName, config, language, router, initialPart
11650
11669
  partition.endBatch();
11651
11670
  for (const docId of payload.docIds) {
11652
11671
  docPartitionMap.set(docId, partitionId);
11672
+ writeLog.documentWritten(docId);
11653
11673
  }
11654
11674
  },
11655
11675
  attachFrozenSegment(partitionId, segment) {
@@ -11664,6 +11684,7 @@ function createPartitionManager(indexName, config, language, router, initialPart
11664
11684
  }
11665
11685
  asCompositePartition(partitionId).attachFrozenSegment(segment);
11666
11686
  registerSegmentDocuments(partitionId, segment);
11687
+ writeLog.segmentWritten(segment);
11667
11688
  },
11668
11689
  swapFrozenSegments(partitionId, dropSegmentIds, replacement) {
11669
11690
  validatePartitionId(partitionId);
@@ -11681,42 +11702,19 @@ function createPartitionManager(indexName, config, language, router, initialPart
11681
11702
  },
11682
11703
  deserializePartition(partitionId, data) {
11683
11704
  validatePartitionId(partitionId);
11705
+ writeLog.partitionsReplaced();
11684
11706
  partitions[partitionId].deserialize(data, config.schema);
11685
11707
  preparePartialWords(partitionId);
11686
11708
  rebuildDocPartitionMapForPartition(partitionId);
11687
11709
  },
11688
11710
  getAggregateStats() {
11689
- let totalDocuments = 0;
11690
- const docFrequencies = /* @__PURE__ */ Object.create(null);
11691
- const totalFieldLengths = /* @__PURE__ */ Object.create(null);
11692
- for (let i = 0; i < partitions.length; i++) {
11693
- const stats = partitions[i].stats;
11694
- totalDocuments += stats.totalDocuments;
11695
- for (const [term, freq] of Object.entries(stats.docFrequencies)) {
11696
- docFrequencies[term] = (docFrequencies[term] ?? 0) + freq;
11697
- }
11698
- for (const [field, length] of Object.entries(stats.totalFieldLengths)) {
11699
- totalFieldLengths[field] = (totalFieldLengths[field] ?? 0) + length;
11700
- }
11701
- }
11702
- return { totalDocuments, docFrequencies, totalFieldLengths };
11711
+ return aggregateStatsOf(partitions);
11703
11712
  },
11704
11713
  estimateMemoryBytes() {
11705
- let total = 0;
11706
- for (let i = 0; i < partitions.length; i++) {
11707
- total += partitions[i].estimateMemoryBytes();
11708
- }
11709
- for (const [, vecIndex] of vecIndexes) {
11710
- total += vecIndex.estimateMemoryBytes();
11711
- }
11712
- return total;
11714
+ return estimateBytesOf(partitions, vecIndexes);
11713
11715
  },
11714
11716
  getPartitionStats() {
11715
- return partitions.map((p, i) => ({
11716
- partitionId: i,
11717
- documentCount: p.count(),
11718
- estimatedMemoryBytes: p.estimateMemoryBytes()
11719
- }));
11717
+ return partitionStatsOf(partitions);
11720
11718
  },
11721
11719
  getVectorIndexes() {
11722
11720
  return vecIndexes;
@@ -11755,6 +11753,7 @@ var VECTOR_STORE_INITIAL_CAPACITY = 16;
11755
11753
  var VECTOR_BLOCK_INITIAL_SLOTS = 256;
11756
11754
  var DISK_STORAGE_BLOCK_BYTES = 67108864;
11757
11755
  var MAX_VECTOR_ORDINALS = 16777216;
11756
+ var UNPLACED_FLAG_CHUNK_ORDINALS = 1024;
11758
11757
  var VECTOR_SCRATCH_SLOTS = 64;
11759
11758
  var SCRATCH_SLOTS_PER_THREAD_POOL = 31;
11760
11759
  var MAX_DOC_ID_TABLE_BYTES = 1073741824;
@@ -12300,6 +12299,8 @@ var STORE_DOC_ID_BYTES = 5;
12300
12299
  var STORE_CODE_BLOCK_COUNT = 6;
12301
12300
  var STORE_CALIBRATION_GENERATION = 7;
12302
12301
  var STORE_RELEASED_VECTORS_TO_DISK = 8;
12302
+ var STORE_UNPLACED_COUNT = 9;
12303
+ var STORE_REMOVED_COUNT = 10;
12303
12304
  var STORE_HEADER_WORDS = 16;
12304
12305
  var IN_MEMORY = -1;
12305
12306
  function fixedInt32(words) {
@@ -12308,6 +12309,9 @@ function fixedInt32(words) {
12308
12309
  function fixedFloat32(length) {
12309
12310
  return new Float32Array(createFixedBuffer(length * 4), 0, length);
12310
12311
  }
12312
+ function unplacedChunkCount(ordinals) {
12313
+ return Math.floor(ordinals / UNPLACED_FLAG_CHUNK_ORDINALS) + 1;
12314
+ }
12311
12315
  function perOrdinal(bytesPerOrdinal) {
12312
12316
  return createGrowableBuffer(
12313
12317
  VECTOR_STORE_INITIAL_CAPACITY * bytesPerOrdinal,
@@ -12332,6 +12336,9 @@ function createSharedVectorStoreHandles(dimension, codeBits, blockBytes2 = BLOCK
12332
12336
  vectorFiles: [],
12333
12337
  magnitudes: perOrdinal(8),
12334
12338
  present: perOrdinal(1),
12339
+ unplaced: perOrdinal(1),
12340
+ unplacedChunks: createGrowableBuffer(4, unplacedChunkCount(MAX_VECTOR_ORDINALS) * 4),
12341
+ removed: perOrdinal(1),
12335
12342
  partitions: perOrdinal(4),
12336
12343
  docIdBytes: createGrowableBuffer(VECTOR_STORE_INITIAL_CAPACITY * 16, MAX_DOC_ID_TABLE_BYTES),
12337
12344
  docIdOffsets: perOrdinal(4),
@@ -12349,6 +12356,9 @@ function sharedVectorStoreBytes(handles) {
12349
12356
  for (const block of handles.codeBlocks) bytes += blockBuffer(block).byteLength;
12350
12357
  bytes += handles.magnitudes.byteLength;
12351
12358
  bytes += handles.present.byteLength;
12359
+ bytes += handles.unplaced.byteLength;
12360
+ bytes += handles.unplacedChunks.byteLength;
12361
+ bytes += handles.removed.byteLength;
12352
12362
  bytes += handles.partitions.byteLength;
12353
12363
  bytes += handles.docIdBytes.byteLength;
12354
12364
  bytes += handles.docIdOffsets.byteLength;
@@ -12602,7 +12612,7 @@ function openSharedQuantizer(store, layout, metric) {
12602
12612
  let centroidGeneration = -1;
12603
12613
  let currentQuery = null;
12604
12614
  const singleOrdinal = new Int32Array(1);
12605
- function reach(ordinal) {
12615
+ function reach2(ordinal) {
12606
12616
  if (ordinal < codePresent.length) return true;
12607
12617
  if (ordinal >= store.handles.codePresent.byteLength) return false;
12608
12618
  codePresent = fixedView(store.handles.codePresent, Uint8Array);
@@ -12623,8 +12633,8 @@ function openSharedQuantizer(store, layout, metric) {
12623
12633
  if (generation !== centroidGeneration) recomputeCentroidDot(generation);
12624
12634
  return centroidDot;
12625
12635
  }
12626
- function holds(ordinal) {
12627
- return ordinal >= 0 && reach(ordinal) && codePresent[ordinal] === 1;
12636
+ function holds2(ordinal) {
12637
+ return ordinal >= 0 && reach2(ordinal) && codePresent[ordinal] === 1;
12628
12638
  }
12629
12639
  function recordOffset(ordinal) {
12630
12640
  return slotByteOffset(layout, ordinal % capacity);
@@ -12645,7 +12655,7 @@ function openSharedQuantizer(store, layout, metric) {
12645
12655
  }
12646
12656
  }
12647
12657
  function writeRecordOf(ordinal) {
12648
- if (!reach(ordinal) || !store.holdsOrdinal(ordinal)) return;
12658
+ if (!reach2(ordinal) || !store.holdsOrdinal(ordinal)) return;
12649
12659
  const code = osqQuantize(store.vectorAt(ordinal), store.handles.centroid, bits, metric, scratch);
12650
12660
  const block = store.codeBlockOf(ordinal);
12651
12661
  const offset = recordOffset(ordinal);
@@ -12715,7 +12725,7 @@ function openSharedQuantizer(store, layout, metric) {
12715
12725
  return currentQuery;
12716
12726
  }
12717
12727
  function distanceBetween(ordA, ordB) {
12718
- if (!holds(ordA) || !holds(ordB)) return Number.POSITIVE_INFINITY;
12728
+ if (!holds2(ordA) || !holds2(ordB)) return Number.POSITIVE_INFINITY;
12719
12729
  const blockA = store.codeBlockOf(ordA);
12720
12730
  const blockB = store.codeBlockOf(ordB);
12721
12731
  const offsetA = recordOffset(ordA);
@@ -12743,7 +12753,7 @@ function openSharedQuantizer(store, layout, metric) {
12743
12753
  return isCalibrated() ? store.handles.centroid : null;
12744
12754
  },
12745
12755
  isCalibrated,
12746
- holdsOrdinal: holds,
12756
+ holdsOrdinal: holds2,
12747
12757
  writeCodes(ordinal) {
12748
12758
  singleOrdinal[0] = ordinal;
12749
12759
  writeCodesOf(singleOrdinal);
@@ -12757,7 +12767,7 @@ function openSharedQuantizer(store, layout, metric) {
12757
12767
  return true;
12758
12768
  },
12759
12769
  restoreRecord(ordinal, record) {
12760
- if (!reach(ordinal) || record.length !== layout.slotStride) return;
12770
+ if (!reach2(ordinal) || record.length !== layout.slotStride) return;
12761
12771
  const block = store.codeBlockOf(ordinal);
12762
12772
  const offset = recordOffset(ordinal);
12763
12773
  block.bytes(offset + layout.slotStride).set(record, offset);
@@ -12769,7 +12779,7 @@ function openSharedQuantizer(store, layout, metric) {
12769
12779
  return block.bytes(offset + layout.slotStride).subarray(offset, offset + layout.slotStride);
12770
12780
  },
12771
12781
  clearCodes(ordinal) {
12772
- if (ordinal < 0 || !reach(ordinal)) return;
12782
+ if (ordinal < 0 || !reach2(ordinal)) return;
12773
12783
  if (Atomics.exchange(codePresent, ordinal, 0) === 1) Atomics.sub(store.handles.header, STORE_CODE_COUNT, 1);
12774
12784
  },
12775
12785
  writeCentroid,
@@ -12785,11 +12795,11 @@ function openSharedQuantizer(store, layout, metric) {
12785
12795
  },
12786
12796
  prepareQuery,
12787
12797
  distanceFromPreparedByOrdinal(prepared, ordinal) {
12788
- if (!holds(ordinal)) return Number.POSITIVE_INFINITY;
12798
+ if (!holds2(ordinal)) return Number.POSITIVE_INFINITY;
12789
12799
  return estimateDistance(ordinal, prepared);
12790
12800
  },
12791
12801
  preparedDistance(prepared) {
12792
- const generic = (ordinal) => holds(ordinal) ? estimateDistance(ordinal, prepared) : Number.POSITIVE_INFINITY;
12802
+ const generic = (ordinal) => holds2(ordinal) ? estimateDistance(ordinal, prepared) : Number.POSITIVE_INFINITY;
12793
12803
  if (store.handles.codeBlocks.length === 0) return generic;
12794
12804
  const block = store.codeBlockOf(0);
12795
12805
  const simd = block.simd;
@@ -13263,7 +13273,7 @@ function openSharedVectorStore(initial, threadSlot) {
13263
13273
  const index = Math.floor(ordinal / capacity);
13264
13274
  return index < handles.blocks.length && handles.blocks[index] !== null ? HELD_HOT : NOT_HELD;
13265
13275
  }
13266
- function holds(ordinal) {
13276
+ function holds2(ordinal) {
13267
13277
  return holdState(ordinal) !== NOT_HELD;
13268
13278
  }
13269
13279
  function readFromFile(file, offset, target) {
@@ -13343,7 +13353,7 @@ function openSharedVectorStore(initial, threadSlot) {
13343
13353
  reader?.retainOnly(new Set(next.vectorFiles.filter((path) => path.length > 0)));
13344
13354
  },
13345
13355
  rebind,
13346
- holdsOrdinal: holds,
13356
+ holdsOrdinal: holds2,
13347
13357
  isCold,
13348
13358
  readColdInto,
13349
13359
  readFromFile,
@@ -13384,7 +13394,7 @@ function openSharedVectorStore(initial, threadSlot) {
13384
13394
  return magnitudes[ordinal];
13385
13395
  },
13386
13396
  entryForOrdinal(ordinal) {
13387
- if (!holds(ordinal)) return void 0;
13397
+ if (!holds2(ordinal)) return void 0;
13388
13398
  return { vector: view.vectorAt(ordinal), magnitude: magnitudes[ordinal] };
13389
13399
  },
13390
13400
  docIdAt(ordinal) {
@@ -13406,7 +13416,7 @@ function openSharedVectorStore(initial, threadSlot) {
13406
13416
  return partition < 0 ? void 0 : partition;
13407
13417
  },
13408
13418
  distanceByOrdinal(ordA, ordB, metric) {
13409
- if (!holds(ordA) || !holds(ordB)) return Number.POSITIVE_INFINITY;
13419
+ if (!holds2(ordA) || !holds2(ordB)) return Number.POSITIVE_INFINITY;
13410
13420
  if (!isCold(ordB)) {
13411
13421
  const indexA = ordA / capacity | 0;
13412
13422
  const indexB = ordB / capacity | 0;
@@ -13543,6 +13553,9 @@ function ensureOrdinal(store, ordinal, cold) {
13543
13553
  const { handles } = store;
13544
13554
  growBufferTo(handles.magnitudes, (ordinal + 1) * 8);
13545
13555
  growBufferTo(handles.present, ordinal + 1);
13556
+ growBufferTo(handles.unplaced, ordinal + 1);
13557
+ growBufferTo(handles.unplacedChunks, unplacedChunkCount(ordinal) * 4);
13558
+ growBufferTo(handles.removed, ordinal + 1);
13546
13559
  growBufferTo(handles.partitions, (ordinal + 1) * 4);
13547
13560
  growBufferTo(handles.codePresent, ordinal + 1);
13548
13561
  const diskFileBytes = handles.diskFile.byteLength;
@@ -13919,8 +13932,11 @@ var GRAPH_M = 2;
13919
13932
  var GRAPH_MMAX0 = 3;
13920
13933
  var GRAPH_EF_CONSTRUCTION = 4;
13921
13934
  var GRAPH_METRIC = 5;
13935
+ var GRAPH_RETIRED = 6;
13922
13936
  var GRAPH_NODE_COUNT = 32;
13923
13937
  var GRAPH_TOMBSTONE_COUNT = 33;
13938
+ var GRAPH_UNLINKED_NODES = 34;
13939
+ var GRAPH_INCOMING_UNCOUNTED = 35;
13924
13940
  var GRAPH_UPPER_USED = 64;
13925
13941
  var GRAPH_SLOTS = 65;
13926
13942
  var GRAPH_UPPER_CAPACITY = 66;
@@ -13958,6 +13974,7 @@ function createSharedGraphHandles(shape) {
13958
13974
  upper: perOrdinal2(upperStride * 4, INITIAL_UPPER_SLOTS),
13959
13975
  locks: perOrdinal2(4, VECTOR_STORE_INITIAL_CAPACITY),
13960
13976
  tombstones: perOrdinal2(1, VECTOR_STORE_INITIAL_CAPACITY),
13977
+ incoming: perOrdinal2(4, VECTOR_STORE_INITIAL_CAPACITY),
13961
13978
  heldLocks: new Int32Array(createFixedBuffer(heldWords * 4), 0, heldWords)
13962
13979
  };
13963
13980
  publishGraphCapacity(handles);
@@ -13977,7 +13994,8 @@ function publishGraphCapacity(handles) {
13977
13994
  Math.floor(handles.level0.byteLength / (level0Stride * 4)),
13978
13995
  Math.floor(handles.upperBase.byteLength / 4),
13979
13996
  Math.floor(handles.locks.byteLength / 4),
13980
- handles.tombstones.byteLength
13997
+ handles.tombstones.byteLength,
13998
+ Math.floor(handles.incoming.byteLength / 4)
13981
13999
  );
13982
14000
  raiseHeaderWord(handles.header, GRAPH_SLOT_CAPACITY, slotCapacity);
13983
14001
  raiseHeaderWord(handles.header, GRAPH_UPPER_CAPACITY, Math.floor(handles.upper.byteLength / 4));
@@ -14004,11 +14022,13 @@ function openAdjacency(handles, m, mMax0) {
14004
14022
  nodeLevels: fixedView(handles.nodeLevels, Uint8Array),
14005
14023
  level0: fixedView(handles.level0, Int32Array),
14006
14024
  upperBase: fixedView(handles.upperBase, Int32Array),
14007
- upper: fixedView(handles.upper, Int32Array)
14025
+ upper: fixedView(handles.upper, Int32Array),
14026
+ incoming: fixedView(handles.incoming, Int32Array)
14008
14027
  };
14009
14028
  }
14010
14029
  function rebindNodes(adj) {
14011
14030
  adj.nodeLevels = fixedView(adj.handles.nodeLevels, Uint8Array);
14031
+ adj.incoming = fixedView(adj.handles.incoming, Int32Array);
14012
14032
  adj.level0 = fixedView(adj.handles.level0, Int32Array);
14013
14033
  adj.upperBase = fixedView(adj.handles.upperBase, Int32Array);
14014
14034
  }
@@ -14045,6 +14065,7 @@ function ensureAdjacencyCapacity(adj, needed) {
14045
14065
  growBufferToExactly(handles.upperBase, capacity * 4);
14046
14066
  growBufferToExactly(handles.locks, capacity * 4);
14047
14067
  growBufferToExactly(handles.tombstones, capacity);
14068
+ growBufferToExactly(handles.incoming, capacity * 4);
14048
14069
  growBufferToExactly(handles.nodeLevels, capacity);
14049
14070
  publishGraphCapacity(handles);
14050
14071
  rebindNodes(adj);
@@ -14091,10 +14112,12 @@ function deleteNode(adj, ord) {
14091
14112
  Atomics.store(adj.nodeLevels, ord, 0);
14092
14113
  adj.upperBase[ord] = 0;
14093
14114
  adj.level0[ord * adj.level0Stride] = 0;
14115
+ Atomics.store(adj.incoming, ord, 0);
14094
14116
  }
14095
14117
  function resetAdjacency(adj) {
14096
14118
  rebindNodes(adj);
14097
14119
  rebindUpper(adj);
14120
+ adj.incoming.fill(0);
14098
14121
  adj.nodeLevels.fill(0);
14099
14122
  adj.level0.fill(0);
14100
14123
  adj.upperBase.fill(0);
@@ -14111,7 +14134,51 @@ function hasNode(adj, ord) {
14111
14134
  }
14112
14135
  function graphBytes(adj) {
14113
14136
  const handles = adj.handles;
14114
- return handles.header.byteLength + handles.nodeLevels.byteLength + handles.level0.byteLength + handles.upperBase.byteLength + handles.upper.byteLength + handles.locks.byteLength + handles.tombstones.byteLength + handles.heldLocks.byteLength;
14137
+ return handles.header.byteLength + handles.nodeLevels.byteLength + handles.level0.byteLength + handles.upperBase.byteLength + handles.upper.byteLength + handles.locks.byteLength + handles.tombstones.byteLength + handles.incoming.byteLength + handles.heldLocks.byteLength;
14138
+ }
14139
+
14140
+ // src/vector/hnsw/adjacency/incoming.ts
14141
+ function incomingCount(adj, ord) {
14142
+ if (ord < 0 || !reachNode(adj, ord)) return 0;
14143
+ return Atomics.load(adj.incoming, ord);
14144
+ }
14145
+ function noteLinked(adj, ord) {
14146
+ if (ord < 0 || !reachNode(adj, ord)) return;
14147
+ Atomics.add(adj.incoming, ord, 1);
14148
+ }
14149
+ function noteUnlinked(adj, ord) {
14150
+ if (ord < 0 || !reachNode(adj, ord)) return 0;
14151
+ for (; ; ) {
14152
+ const seen = Atomics.load(adj.incoming, ord);
14153
+ if (seen <= 0) return 0;
14154
+ if (Atomics.compareExchange(adj.incoming, ord, seen, seen - 1) === seen) return seen - 1;
14155
+ }
14156
+ }
14157
+ function releaseUnlessLast(adj, ord) {
14158
+ if (ord < 0 || !reachNode(adj, ord)) return true;
14159
+ for (; ; ) {
14160
+ const seen = Atomics.load(adj.incoming, ord);
14161
+ if (seen <= 1) return false;
14162
+ if (Atomics.compareExchange(adj.incoming, ord, seen, seen - 1) === seen) return true;
14163
+ }
14164
+ }
14165
+ function holdsNodeAt(adj, ord) {
14166
+ return ord >= 0 && ord < adj.nodeLevels.length && adj.nodeLevels[ord] !== 0;
14167
+ }
14168
+ function countLinksOutOf(adj, ord, step) {
14169
+ if (!holdsNodeAt(adj, ord)) return;
14170
+ const incoming = adj.incoming;
14171
+ const level0 = adj.level0;
14172
+ const base = ord * adj.level0Stride;
14173
+ const count = level0[base];
14174
+ for (let i = 1; i <= count; i++) {
14175
+ const neighbour = level0[base + i];
14176
+ if (neighbour === ord || !holdsNodeAt(adj, neighbour)) continue;
14177
+ if (incoming[neighbour] + step >= 0) incoming[neighbour] += step;
14178
+ }
14179
+ }
14180
+ function countEveryLink(adj, slots) {
14181
+ for (let ord = 0; ord < slots; ord++) countLinksOutOf(adj, ord, 1);
14115
14182
  }
14116
14183
 
14117
14184
  // src/vector/hnsw/adjacency/neighbors.ts
@@ -14161,6 +14228,7 @@ function addNeighbor(adj, ord, layer, neighborOrd) {
14161
14228
  if (count + 1 > stride - 1) return;
14162
14229
  array[base + count + 1] = neighborOrd;
14163
14230
  array[base] = count + 1;
14231
+ if (layer === 0) noteLinked(adj, neighborOrd);
14164
14232
  }
14165
14233
  function removeNeighbor(adj, ord, layer, neighborOrd) {
14166
14234
  const base = layerBase(adj, ord, layer);
@@ -14173,10 +14241,11 @@ function removeNeighbor(adj, ord, layer, neighborOrd) {
14173
14241
  array[base + j] = array[base + j + 1];
14174
14242
  }
14175
14243
  array[base] = count - 1;
14244
+ if (layer === 0) noteUnlinked(adj, neighborOrd);
14176
14245
  return;
14177
14246
  }
14178
14247
  }
14179
- function replaceNeighbors(adj, ord, layer, neighbors, count) {
14248
+ function writeNeighbors(adj, ord, layer, neighbors, count) {
14180
14249
  const base = layerBase(adj, ord, layer);
14181
14250
  if (base === ABSENT) return;
14182
14251
  const array = layerArray(adj, layer);
@@ -14187,6 +14256,24 @@ function replaceNeighbors(adj, ord, layer, neighbors, count) {
14187
14256
  }
14188
14257
  array[base] = limit;
14189
14258
  }
14259
+ function holds(neighbors, count, ord) {
14260
+ for (let i = 0; i < count; i++) if (neighbors[i] === ord) return true;
14261
+ return false;
14262
+ }
14263
+ function replaceNeighbors(adj, ord, layer, neighbors, count) {
14264
+ const base = layerBase(adj, ord, layer);
14265
+ if (base === ABSENT) return;
14266
+ if (layer > 0) {
14267
+ writeNeighbors(adj, ord, layer, neighbors, count);
14268
+ return;
14269
+ }
14270
+ const array = adj.level0;
14271
+ const held = array.slice(base + 1, base + 1 + array[base]);
14272
+ const limit = Math.min(count, adj.level0Stride - 1);
14273
+ for (const former of held) if (!holds(neighbors, limit, former)) noteUnlinked(adj, former);
14274
+ for (let i = 0; i < limit; i++) if (!holds(held, held.length, neighbors[i])) noteLinked(adj, neighbors[i]);
14275
+ writeNeighbors(adj, ord, 0, neighbors, count);
14276
+ }
14190
14277
 
14191
14278
  // src/vector/hnsw/locks.ts
14192
14279
  var FREE = 0;
@@ -14417,6 +14504,17 @@ function nativeCompact(state, field, metric) {
14417
14504
  const compact2 = () => field.core.compact(field.graph, field.store.handle, nativeMetricCode(metric), state.locks.threadSlot, field.wakes);
14418
14505
  return writeToTheGraph(state, field, compact2) === NATIVE_OK;
14419
14506
  }
14507
+ function nativeConnect(state, field, metric, nearby) {
14508
+ const connect = () => field.core.connectUnreached(
14509
+ field.graph,
14510
+ field.store.handle,
14511
+ nativeMetricCode(metric),
14512
+ nearby,
14513
+ state.locks.threadSlot,
14514
+ field.wakes
14515
+ );
14516
+ return writeToTheGraph(state, field, connect) === NATIVE_OK;
14517
+ }
14420
14518
 
14421
14519
  // src/vector/hnsw/shared.ts
14422
14520
  function entryPointOf(state) {
@@ -14556,7 +14654,8 @@ function createHNSWWorkspace() {
14556
14654
  pruneCandidates: createList(INITIAL_LIST_CAPACITY),
14557
14655
  pruneSelection: createList(INITIAL_LIST_CAPACITY),
14558
14656
  repairCandidates: createList(INITIAL_LIST_CAPACITY),
14559
- repairSelection: createList(INITIAL_LIST_CAPACITY)
14657
+ repairSelection: createList(INITIAL_LIST_CAPACITY),
14658
+ droppedLinks: createList(INITIAL_LIST_CAPACITY)
14560
14659
  };
14561
14660
  }
14562
14661
  function linkSelectionsFor(workspace, layers) {
@@ -14714,6 +14813,260 @@ function setEntryPointsFromList(workspace, list) {
14714
14813
  workspace.entryPointCount = list.size;
14715
14814
  }
14716
14815
 
14816
+ // src/vector/hnsw/last-links.ts
14817
+ function isLive(state, ord) {
14818
+ return nodeExists(state, ord) && !isTombstoned(state, ord);
14819
+ }
14820
+ function noteNodeLeftUnlinked(state) {
14821
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 1);
14822
+ }
14823
+ function listHolds(list, ord) {
14824
+ for (let i = 0; i < list.size; i++) if (list.ords[i] === ord) return true;
14825
+ return false;
14826
+ }
14827
+ function collectDropped(sorted, kept, dropped) {
14828
+ dropped.size = 0;
14829
+ let next = 0;
14830
+ for (let i = 0; i < sorted.size; i++) {
14831
+ const ord = sorted.ords[i];
14832
+ if (next < kept.size && kept.ords[next] === ord) {
14833
+ next += 1;
14834
+ continue;
14835
+ }
14836
+ if (listHolds(kept, ord)) continue;
14837
+ appendToList(dropped, ord, sorted.distances[i]);
14838
+ }
14839
+ }
14840
+ function replaceFarthestShared(state, kept, held, ord, distance) {
14841
+ const tried = /* @__PURE__ */ new Set();
14842
+ for (; ; ) {
14843
+ let farthest = -1;
14844
+ for (let k = 0; k < kept.size; k++) {
14845
+ if (tried.has(k) || incomingCount(state.adjacency, kept.ords[k]) < 2) continue;
14846
+ if (farthest === -1 || kept.distances[k] > kept.distances[farthest]) farthest = k;
14847
+ }
14848
+ if (farthest === -1) return false;
14849
+ tried.add(farthest);
14850
+ const replaced = kept.ords[farthest];
14851
+ if (held(replaced) && !releaseUnlessLast(state.adjacency, replaced)) continue;
14852
+ kept.ords[farthest] = ord;
14853
+ kept.distances[farthest] = distance;
14854
+ return true;
14855
+ }
14856
+ }
14857
+ function writeKeepingLastLinks(state, ord, sorted, kept, held) {
14858
+ const adjacency = state.adjacency;
14859
+ const limit = state.Mmax0;
14860
+ const dropped = state.workspace.droppedLinks;
14861
+ collectDropped(sorted, kept, dropped);
14862
+ ensureListCapacity(kept, limit);
14863
+ for (let i = 0; i < dropped.size; i++) {
14864
+ const candidate = dropped.ords[i];
14865
+ if (!held(candidate)) continue;
14866
+ if (!isLive(state, candidate)) {
14867
+ noteUnlinked(adjacency, candidate);
14868
+ continue;
14869
+ }
14870
+ if (releaseUnlessLast(adjacency, candidate)) continue;
14871
+ if (kept.size < limit) {
14872
+ appendToList(kept, candidate, dropped.distances[i]);
14873
+ continue;
14874
+ }
14875
+ if (replaceFarthestShared(state, kept, held, candidate, dropped.distances[i])) continue;
14876
+ noteUnlinked(adjacency, candidate);
14877
+ noteNodeLeftUnlinked(state);
14878
+ }
14879
+ for (let k = 0; k < kept.size; k++) if (!held(kept.ords[k])) noteLinked(adjacency, kept.ords[k]);
14880
+ writeNeighbors(adjacency, ord, 0, kept.ords, kept.size);
14881
+ }
14882
+
14883
+ // src/vector/hnsw/connect-unreached.ts
14884
+ var NOT_REACHED_THROUGH_A_NODE = -1;
14885
+ function reach(walk, node, through) {
14886
+ walk.reached[node] = 1;
14887
+ walk.firstReachedThrough[node] = through;
14888
+ walk.queue[walk.tail++] = node;
14889
+ }
14890
+ function walkFrom(state, walk, start, through) {
14891
+ const adjacency = state.adjacency;
14892
+ if (walk.reached[start] === 1) return;
14893
+ reach(walk, start, through);
14894
+ while (walk.head < walk.tail) {
14895
+ const ord = walk.queue[walk.head++];
14896
+ const base = layerBase(adjacency, ord, 0);
14897
+ if (base === ABSENT) continue;
14898
+ const count = adjacency.level0[base];
14899
+ for (let i = 1; i <= count; i++) {
14900
+ const next = adjacency.level0[base + i];
14901
+ if (next < 0 || next >= walk.reached.length || walk.reached[next] === 1 || !nodeExists(state, next)) continue;
14902
+ reach(walk, next, ord);
14903
+ }
14904
+ }
14905
+ }
14906
+ function hasRoom(state, _walk, host) {
14907
+ const base = layerBase(state.adjacency, host, 0);
14908
+ return base !== ABSENT && state.adjacency.level0[base] < state.Mmax0;
14909
+ }
14910
+ function reachedThroughAnotherNode(walk, host, neighbour) {
14911
+ if (neighbour < 0 || neighbour >= walk.reached.length || walk.reached[neighbour] !== 1) return false;
14912
+ const through = walk.firstReachedThrough[neighbour];
14913
+ return through !== NOT_REACHED_THROUGH_A_NODE && through !== host;
14914
+ }
14915
+ function holdsLinkOutsideTheWalk(state, walk, host) {
14916
+ const adjacency = state.adjacency;
14917
+ const base = layerBase(adjacency, host, 0);
14918
+ if (base === ABSENT) return false;
14919
+ const count = adjacency.level0[base];
14920
+ for (let i = 1; i <= count; i++) if (reachedThroughAnotherNode(walk, host, adjacency.level0[base + i])) return true;
14921
+ return false;
14922
+ }
14923
+ function listOf(state, ord) {
14924
+ const adjacency = state.adjacency;
14925
+ const base = layerBase(adjacency, ord, 0);
14926
+ if (base === ABSENT) return new Int32Array(0);
14927
+ return adjacency.level0.slice(base + 1, base + 1 + adjacency.level0[base]);
14928
+ }
14929
+ function nearestHost(state, walk, orphan, nearby, test) {
14930
+ const distance = nodeDistanceFunction(state, state.buildMetric);
14931
+ let best = -1;
14932
+ let bestDistance = Number.POSITIVE_INFINITY;
14933
+ const consider = (host) => {
14934
+ if (host === orphan || walk.reached[host] !== 1 || !isLive(state, host) || !test(state, walk, host)) return;
14935
+ const away = distance(orphan, host);
14936
+ if (best === -1 || away < bestDistance) {
14937
+ best = host;
14938
+ bestDistance = away;
14939
+ }
14940
+ };
14941
+ for (const host of nearby.list) consider(host);
14942
+ for (const host of listOf(state, orphan))
14943
+ if (host >= 0 && host < walk.reached.length && nearby.holds[host] !== 1) consider(host);
14944
+ if (best !== -1) return best;
14945
+ for (let host = 0; host < walk.reached.length; host++) consider(host);
14946
+ return best;
14947
+ }
14948
+ function takeOutFarthestLinkOutsideTheWalk(state, walk, host) {
14949
+ const adjacency = state.adjacency;
14950
+ const base = layerBase(adjacency, host, 0);
14951
+ if (base === ABSENT) return;
14952
+ const distance = nodeDistanceFunction(state, state.buildMetric);
14953
+ const count = adjacency.level0[base];
14954
+ let farthest = -1;
14955
+ let farthestDistance = Number.NEGATIVE_INFINITY;
14956
+ for (let i = 1; i <= count; i++) {
14957
+ const neighbour = adjacency.level0[base + i];
14958
+ if (!reachedThroughAnotherNode(walk, host, neighbour)) continue;
14959
+ const away = distance(host, neighbour);
14960
+ if (farthest === -1 || away > farthestDistance) {
14961
+ farthest = i;
14962
+ farthestDistance = away;
14963
+ }
14964
+ }
14965
+ if (farthest === -1) return;
14966
+ const taken = adjacency.level0[base + farthest];
14967
+ for (let i = farthest; i < count; i++) adjacency.level0[base + i] = adjacency.level0[base + i + 1];
14968
+ adjacency.level0[base] = count - 1;
14969
+ noteUnlinked(adjacency, taken);
14970
+ }
14971
+ function appendTo(state, host, orphan) {
14972
+ const adjacency = state.adjacency;
14973
+ const base = layerBase(adjacency, host, 0);
14974
+ if (base === ABSENT) return;
14975
+ const count = adjacency.level0[base];
14976
+ adjacency.level0[base + count + 1] = orphan;
14977
+ adjacency.level0[base] = count + 1;
14978
+ noteLinked(adjacency, orphan);
14979
+ }
14980
+ function connectUnreached(state, nearby) {
14981
+ const slots = adjacencySlots(state.adjacency);
14982
+ const entry = entryPointOf(state);
14983
+ if (entry < 0 || entry >= slots || !nodeExists(state, entry)) return;
14984
+ const walk = {
14985
+ reached: new Uint8Array(slots),
14986
+ firstReachedThrough: new Int32Array(slots),
14987
+ queue: new Int32Array(slots),
14988
+ head: 0,
14989
+ tail: 0
14990
+ };
14991
+ walkFrom(state, walk, entry, NOT_REACHED_THROUGH_A_NODE);
14992
+ const held = { list: [], holds: new Uint8Array(slots) };
14993
+ for (let i = 0; i < nearby.length; i++) {
14994
+ const ord = nearby[i];
14995
+ if (ord < 0 || ord >= slots || held.holds[ord] === 1) continue;
14996
+ held.holds[ord] = 1;
14997
+ held.list.push(ord);
14998
+ }
14999
+ for (let orphan = 0; orphan < slots; orphan++) {
15000
+ if (walk.reached[orphan] === 1 || !isLive(state, orphan)) continue;
15001
+ let host = nearestHost(state, walk, orphan, held, hasRoom);
15002
+ if (host === -1) {
15003
+ host = nearestHost(state, walk, orphan, held, holdsLinkOutsideTheWalk);
15004
+ if (host === -1) continue;
15005
+ takeOutFarthestLinkOutsideTheWalk(state, walk, host);
15006
+ }
15007
+ appendTo(state, host, orphan);
15008
+ walkFrom(state, walk, orphan, host);
15009
+ }
15010
+ }
15011
+ function holdsUnlinkedNode(state) {
15012
+ const slots = adjacencySlots(state.adjacency);
15013
+ const entry = entryPointOf(state);
15014
+ const incoming = state.adjacency.incoming;
15015
+ for (let ord = 0; ord < slots; ord++) {
15016
+ if (incoming[ord] === 0 && ord !== entry && isLive(state, ord)) return true;
15017
+ }
15018
+ return false;
15019
+ }
15020
+ function connectThroughEitherCore(state, nearby) {
15021
+ const field = nativeFieldFor();
15022
+ if (field !== null && nativeConnect(state, field, state.buildMetric, nearby)) return;
15023
+ connectUnreached(state, nearby);
15024
+ }
15025
+ function connectAfterRemoval(state, nearby) {
15026
+ connectThroughEitherCore(state, nearby);
15027
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
15028
+ }
15029
+ function connectAfterPlacement(state, holdsGraphLock) {
15030
+ if (Atomics.load(state.header, GRAPH_UNLINKED_NODES) === 0) return;
15031
+ if (!holdsGraphLock) lockGraphExclusive(state.locks);
15032
+ try {
15033
+ if (Atomics.load(state.header, GRAPH_UNLINKED_NODES) === 0) return;
15034
+ connectThroughEitherCore(state, new Int32Array(0));
15035
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
15036
+ } finally {
15037
+ if (!holdsGraphLock) unlockGraphExclusive(state.locks);
15038
+ }
15039
+ }
15040
+ function baseNeighboursOfRemoved(state, removed) {
15041
+ const leaving = new Set(removed);
15042
+ const nearby = [];
15043
+ for (const ord of removed) {
15044
+ for (const neighbour of listOf(state, ord)) if (!leaving.has(neighbour)) nearby.push(neighbour);
15045
+ }
15046
+ return Int32Array.from(nearby);
15047
+ }
15048
+ function leaveIncomingUncounted(state) {
15049
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
15050
+ Atomics.store(state.header, GRAPH_INCOMING_UNCOUNTED, 1);
15051
+ }
15052
+ function countIncomingBeforeWriting(state, holdsGraphLock) {
15053
+ if (Atomics.load(state.header, GRAPH_INCOMING_UNCOUNTED) === 0) return;
15054
+ if (!holdsGraphLock) lockGraphExclusive(state.locks);
15055
+ try {
15056
+ if (Atomics.load(state.header, GRAPH_INCOMING_UNCOUNTED) === 0) return;
15057
+ const adjacency = state.adjacency;
15058
+ const slots = adjacencySlots(adjacency);
15059
+ if (slots > 0) reachNode(adjacency, slots - 1);
15060
+ adjacency.incoming.fill(0);
15061
+ countEveryLink(adjacency, slots);
15062
+ if (holdsUnlinkedNode(state)) connectThroughEitherCore(state, new Int32Array(0));
15063
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
15064
+ Atomics.store(state.header, GRAPH_INCOMING_UNCOUNTED, 0);
15065
+ } finally {
15066
+ if (!holdsGraphLock) unlockGraphExclusive(state.locks);
15067
+ }
15068
+ }
15069
+
14717
15070
  // src/vector/hnsw/graph-ops.ts
14718
15071
  function readNeighborsLocked(state, ord, layer) {
14719
15072
  for (; ; ) {
@@ -14810,14 +15163,24 @@ function pruneConnections(state, ord, layer, distance) {
14810
15163
  for (let i = 1; i <= count; i++) {
14811
15164
  const connOrd = neighbors[base + i];
14812
15165
  const dist = distance(ord, connOrd);
14813
- if (dist === Number.POSITIVE_INFINITY) continue;
15166
+ if (dist === Number.POSITIVE_INFINITY) {
15167
+ if (layer === 0) noteUnlinked(adjacency, connOrd);
15168
+ continue;
15169
+ }
14814
15170
  candidates.ords[candidates.size] = connOrd;
14815
15171
  candidates.distances[candidates.size] = dist;
14816
15172
  candidates.size += 1;
14817
15173
  }
14818
15174
  const kept = state.workspace.pruneSelection;
14819
15175
  selectNeighborsHeuristic(state, candidates, mc, distance, kept);
14820
- replaceNeighbors(adjacency, ord, layer, kept.ords, kept.size);
15176
+ if (layer === 0) {
15177
+ writeKeepingLastLinks(state, ord, state.workspace.working, kept, everyCandidateWasHeld);
15178
+ return;
15179
+ }
15180
+ writeNeighbors(adjacency, ord, layer, kept.ords, kept.size);
15181
+ }
15182
+ function everyCandidateWasHeld() {
15183
+ return true;
14821
15184
  }
14822
15185
 
14823
15186
  // src/vector/hnsw/mutation.ts
@@ -14931,11 +15294,15 @@ function linkNeighborsBack(state, ord, linkTop) {
14931
15294
  }
14932
15295
  function linkNode(state, ord, level2) {
14933
15296
  const topLayer = topLayerOf(state);
15297
+ const formerEntry = entryPointOf(state);
14934
15298
  const linkTop = selectNeighborsPerLayer(state, ord, level2);
14935
15299
  if (linkTop < 0) return;
14936
15300
  writeOwnLists(state, ord, linkTop);
14937
15301
  linkNeighborsBack(state, ord, linkTop);
14938
- if (level2 > topLayer) raiseEntry(state, ord, level2);
15302
+ if (level2 <= topLayer) return;
15303
+ raiseEntry(state, ord, level2);
15304
+ const demoted = entryPointOf(state) !== formerEntry && isLive(state, formerEntry);
15305
+ if (demoted && incomingCount(state.adjacency, formerEntry) === 0) noteNodeLeftUnlinked(state);
14939
15306
  }
14940
15307
  function writeRecordBeforePlacement(state, ord) {
14941
15308
  const quantizer = state.quantizer;
@@ -14961,8 +15328,12 @@ function insertNode(state, ord, holdsGraphLock = false) {
14961
15328
  if (!state.store.holdsOrdinal(ord)) return false;
14962
15329
  ensureCapacity(state, ord + 1);
14963
15330
  if (nodeExists(state, ord)) return false;
15331
+ countIncomingBeforeWriting(state, holdsGraphLock);
14964
15332
  writeRecordBeforePlacement(state, ord);
14965
- if (placeThroughTheCore(state, ord, holdsGraphLock)) return true;
15333
+ if (placeThroughTheCore(state, ord, holdsGraphLock)) {
15334
+ connectAfterPlacement(state, holdsGraphLock);
15335
+ return true;
15336
+ }
14966
15337
  if (nodeExists(state, ord)) return false;
14967
15338
  if (!holdsGraphLock) lockGraphShared(state.locks);
14968
15339
  try {
@@ -14970,11 +15341,11 @@ function insertNode(state, ord, holdsGraphLock = false) {
14970
15341
  const level2 = randomLevel(state.mL);
14971
15342
  createNode(state.adjacency, ord, level2);
14972
15343
  Atomics.add(state.header, GRAPH_NODE_COUNT, 1);
14973
- if (claimEntryIfEmpty(state, ord, level2)) return true;
14974
- linkNode(state, ord, level2);
15344
+ if (!claimEntryIfEmpty(state, ord, level2)) linkNode(state, ord, level2);
14975
15345
  } finally {
14976
15346
  if (!holdsGraphLock) unlockGraphShared(state.locks);
14977
15347
  }
15348
+ connectAfterPlacement(state, holdsGraphLock);
14978
15349
  return true;
14979
15350
  }
14980
15351
  function removeNodeEager(state, ord, excludeOrds) {
@@ -14992,7 +15363,8 @@ function removeNodeEager(state, ord, excludeOrds) {
14992
15363
  if (layer > nodeMaxLayer(state, neighborOrd)) continue;
14993
15364
  const mc = maxConns(state, layer);
14994
15365
  if (neighborCount(state.adjacency, neighborOrd, layer) >= mc) continue;
14995
- const candidateOrds = new Set(collectNeighbors(state.adjacency, neighborOrd, layer));
15366
+ const heldNow = new Set(collectNeighbors(state.adjacency, neighborOrd, layer));
15367
+ const candidateOrds = new Set(heldNow);
14996
15368
  for (const otherOrd of formerNeighbors) {
14997
15369
  if (otherOrd !== neighborOrd && otherOrd !== ord) {
14998
15370
  if (excludeOrds?.has(otherOrd)) continue;
@@ -15003,12 +15375,19 @@ function removeNodeEager(state, ord, excludeOrds) {
15003
15375
  candidates.size = 0;
15004
15376
  for (const candOrd of candidateOrds) {
15005
15377
  const dist = distance(neighborOrd, candOrd);
15006
- if (dist === Number.POSITIVE_INFINITY) continue;
15378
+ if (dist === Number.POSITIVE_INFINITY) {
15379
+ if (layer === 0 && heldNow.has(candOrd)) noteUnlinked(state.adjacency, candOrd);
15380
+ continue;
15381
+ }
15007
15382
  appendToList(candidates, candOrd, dist);
15008
15383
  }
15009
15384
  const selected = state.workspace.repairSelection;
15010
15385
  selectNeighborsHeuristic(state, candidates, mc, distance, selected);
15011
- replaceNeighbors(state.adjacency, neighborOrd, layer, selected.ords, selected.size);
15386
+ if (layer === 0) {
15387
+ writeKeepingLastLinks(state, neighborOrd, state.workspace.working, selected, (held) => heldNow.has(held));
15388
+ } else {
15389
+ replaceNeighbors(state.adjacency, neighborOrd, layer, selected.ords, selected.size);
15390
+ }
15012
15391
  for (let i = 0; i < selected.size; i++) {
15013
15392
  const newConnOrd = selected.ords[i];
15014
15393
  if (layer <= nodeMaxLayer(state, newConnOrd)) {
@@ -15018,6 +15397,8 @@ function removeNodeEager(state, ord, excludeOrds) {
15018
15397
  }
15019
15398
  }
15020
15399
  }
15400
+ const formerBaseNeighbors = collectNeighbors(state.adjacency, ord, 0);
15401
+ for (const neighborOrd of formerBaseNeighbors) noteUnlinked(state.adjacency, neighborOrd);
15021
15402
  deleteNode(state.adjacency, ord);
15022
15403
  Atomics.sub(state.header, GRAPH_NODE_COUNT, 1);
15023
15404
  clearTombstone(state, ord);
@@ -15040,8 +15421,7 @@ function markTombstone(state, ord) {
15040
15421
  }
15041
15422
  function compactTombstones(state) {
15042
15423
  if (Atomics.load(state.header, GRAPH_TOMBSTONE_COUNT) === 0) return;
15043
- const field = nativeFieldFor();
15044
- if (field !== null && nativeCompact(state, field, state.buildMetric)) return;
15424
+ countIncomingBeforeWriting(state, true);
15045
15425
  const tombstonedOrds = [];
15046
15426
  const slots = adjacencySlots(state.adjacency);
15047
15427
  for (let ord = 0; ord < slots; ord++) {
@@ -15049,10 +15429,15 @@ function compactTombstones(state) {
15049
15429
  tombstonedOrds.push(ord);
15050
15430
  }
15051
15431
  }
15052
- const excl = new Set(tombstonedOrds);
15053
- for (const ord of tombstonedOrds) {
15054
- removeNodeEager(state, ord, excl);
15432
+ const nearby = baseNeighboursOfRemoved(state, tombstonedOrds);
15433
+ const field = nativeFieldFor();
15434
+ if (field === null || !nativeCompact(state, field, state.buildMetric)) {
15435
+ const excl = new Set(tombstonedOrds);
15436
+ for (const ord of tombstonedOrds) {
15437
+ removeNodeEager(state, ord, excl);
15438
+ }
15055
15439
  }
15440
+ connectAfterRemoval(state, nearby);
15056
15441
  }
15057
15442
  function resetGraph(state) {
15058
15443
  resetAdjacency(state.adjacency);
@@ -15062,6 +15447,7 @@ function resetGraph(state) {
15062
15447
  Atomics.store(state.header, GRAPH_NODE_COUNT, 0);
15063
15448
  Atomics.store(state.header, GRAPH_ENTRY_POINT, -1);
15064
15449
  Atomics.store(state.header, GRAPH_TOP_LAYER, -1);
15450
+ Atomics.store(state.header, GRAPH_INCOMING_UNCOUNTED, 0);
15065
15451
  }
15066
15452
  function rebuild(state) {
15067
15453
  const nodeCount = Atomics.load(state.header, GRAPH_NODE_COUNT);
@@ -15271,7 +15657,7 @@ function deserializeNumberedGraph(state, graph, ordinalOfNumber) {
15271
15657
  }
15272
15658
  cursor += count + 1;
15273
15659
  if (ordinal !== NO_ORDINAL && layer <= keptTop) {
15274
- replaceNeighbors(state.adjacency, ordinal, layer, kept, kept.length);
15660
+ writeNeighbors(state.adjacency, ordinal, layer, kept, kept.length);
15275
15661
  }
15276
15662
  }
15277
15663
  }
@@ -15283,6 +15669,7 @@ function deserializeNumberedGraph(state, graph, ordinalOfNumber) {
15283
15669
  const entryIsUsable = entryOrdinal !== NO_ORDINAL && entryTop >= 0;
15284
15670
  Atomics.store(state.header, GRAPH_ENTRY_POINT, entryIsUsable ? entryOrdinal : nodeOnHighestLayer);
15285
15671
  Atomics.store(state.header, GRAPH_TOP_LAYER, entryIsUsable ? entryTop : highestLayer);
15672
+ leaveIncomingUncounted(state);
15286
15673
  }
15287
15674
 
15288
15675
  // src/vector/hnsw/persistence.ts
@@ -15321,7 +15708,7 @@ function deserializeGraph(state, store, data) {
15321
15708
  for (const node of nodes) {
15322
15709
  createNode(state.adjacency, node.ord, node.maxLayer);
15323
15710
  for (const [layer, neighbors] of node.layers) {
15324
- replaceNeighbors(state.adjacency, node.ord, layer, neighbors, neighbors.length);
15711
+ writeNeighbors(state.adjacency, node.ord, layer, neighbors, neighbors.length);
15325
15712
  }
15326
15713
  Atomics.add(state.header, GRAPH_NODE_COUNT, 1);
15327
15714
  }
@@ -15347,6 +15734,7 @@ function deserializeGraph(state, store, data) {
15347
15734
  Atomics.store(state.header, GRAPH_ENTRY_POINT, bestOrd);
15348
15735
  Atomics.store(state.header, GRAPH_TOP_LAYER, bestLayer);
15349
15736
  }
15737
+ leaveIncomingUncounted(state);
15350
15738
  }
15351
15739
 
15352
15740
  // src/vector/hnsw/scratch-bytes.ts
@@ -15646,6 +16034,7 @@ function restoreSnapshot(state, snapshot) {
15646
16034
  Atomics.store(state.header, GRAPH_NODE_COUNT, snapshot.nodeCount);
15647
16035
  Atomics.store(state.header, GRAPH_ENTRY_POINT, snapshot.entryPointOrd);
15648
16036
  Atomics.store(state.header, GRAPH_TOP_LAYER, snapshot.topLayer);
16037
+ leaveIncomingUncounted(state);
15649
16038
  }
15650
16039
 
15651
16040
  // src/vector/hnsw/state.ts
@@ -15781,6 +16170,7 @@ function createHNSWIndex(dimension, store, config, quantizer, handles) {
15781
16170
  },
15782
16171
  search: (query, k, searchMetric, minSimilarity, options) => search(state, docIdOf, query, k, searchMetric, minSimilarity, options),
15783
16172
  clear: () => exclusively(() => resetGraph(state)),
16173
+ retire: () => Atomics.store(shared2.header, GRAPH_RETIRED, 1),
15784
16174
  entries: entriesIterator,
15785
16175
  compactionNeeded,
15786
16176
  compactTombstones: () => exclusively(() => compactTombstones(state)),
@@ -15844,6 +16234,7 @@ function releaseReplacedVectors(state) {
15844
16234
  for (const ordinal of state.store.releaseReplacedVectors()) state.osq?.removeOrdinal(ordinal);
15845
16235
  }
15846
16236
  function adoptGraph(state, graph) {
16237
+ if (state.hnsw !== null && state.hnsw !== graph) state.hnsw.retire();
15847
16238
  if (graph === null && state.hnsw !== null) {
15848
16239
  state.hnsw.clear();
15849
16240
  }
@@ -16609,7 +17000,7 @@ async function placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fa
16609
17000
  continue;
16610
17001
  }
16611
17002
  if (!state.tombstones.has(docId) && !graph.has(docId) && graph.insertOrdinal(ordinal)) placedHere += 1;
16612
- state.buffer.delete(docId);
17003
+ if (graph === state.hnsw) state.buffer.delete(docId);
16613
17004
  }
16614
17005
  if (placedHere > 0 && outcome !== null && !fallback.shortAnswerWarned) {
16615
17006
  fallback.shortAnswerWarned = true;
@@ -16626,7 +17017,7 @@ async function insertLocally(state, graph, docIds, admit) {
16626
17017
  const ordinal = state.store.getOrdinal(docId);
16627
17018
  if (ordinal === void 0) continue;
16628
17019
  graph.insertOrdinal(ordinal);
16629
- state.buffer.delete(docId);
17020
+ if (graph === state.hnsw) state.buffer.delete(docId);
16630
17021
  count += 1;
16631
17022
  if (count % BUILD_CHUNK_SIZE === 0) {
16632
17023
  noteWrite(state);
@@ -16743,6 +17134,10 @@ async function buildGraphFromStore(state) {
16743
17134
  }
16744
17135
  const previous = state.hnsw;
16745
17136
  adoptGraph(state, graph);
17137
+ for (const docId of [...state.buffer]) {
17138
+ const ordinal = state.store.getOrdinal(docId);
17139
+ if (ordinal !== void 0 && graph.holdsNode(ordinal) && !graph.holdsTombstone(ordinal)) state.buffer.delete(docId);
17140
+ }
16746
17141
  if (previous !== null && previous !== graph) dropSharedGraph(state, previous);
16747
17142
  await releasePendingLocations(state);
16748
17143
  }
@@ -17251,7 +17646,7 @@ function registryMatches(state, listedKeys) {
17251
17646
  if (listed.length !== state.savedFiles.length) return false;
17252
17647
  return state.savedFiles.every((file, index) => file.key === listed[index]);
17253
17648
  }
17254
- function isLive(state, ordinal) {
17649
+ function isLive2(state, ordinal) {
17255
17650
  if (ordinal === NO_LIVE_VECTOR_AT_POSITION) return false;
17256
17651
  const docId = state.store.docIdForOrdinal(ordinal);
17257
17652
  return docId !== void 0 && !state.tombstones.has(docId);
@@ -17260,7 +17655,7 @@ function isGraphTombstone(state, ordinal) {
17260
17655
  return ordinal !== NO_LIVE_VECTOR_AT_POSITION && state.hnsw !== null && state.hnsw.holdsTombstone(ordinal);
17261
17656
  }
17262
17657
  function isCarried(state, ordinal) {
17263
- return isLive(state, ordinal) || isGraphTombstone(state, ordinal);
17658
+ return isLive2(state, ordinal) || isGraphTombstone(state, ordinal);
17264
17659
  }
17265
17660
  function carriedCountOf(state, file) {
17266
17661
  let carried = 0;
@@ -17268,7 +17663,7 @@ function carriedCountOf(state, file) {
17268
17663
  return carried;
17269
17664
  }
17270
17665
  function deadBitsOf(state, file) {
17271
- return deadBitsWhere(file.ordinals.length, (position) => !isLive(state, file.ordinals[position]));
17666
+ return deadBitsWhere(file.ordinals.length, (position) => !isLive2(state, file.ordinals[position]));
17272
17667
  }
17273
17668
  function filesWorthKeeping(state, listed, calibration, carriedCounts) {
17274
17669
  const kept = [];
@@ -17350,7 +17745,7 @@ function graphPartsWithoutNodes(parts, numbers) {
17350
17745
  }
17351
17746
  function plannedVectorOf(state, ordinal) {
17352
17747
  const docId = state.store.docIdForOrdinal(ordinal) ?? state.store.view.docIdAt(ordinal) ?? "";
17353
- return { ordinal, docId, tombstoned: !isLive(state, ordinal) };
17748
+ return { ordinal, docId, tombstoned: !isLive2(state, ordinal) };
17354
17749
  }
17355
17750
  function readFile(state, planned, centroid) {
17356
17751
  const dimension = state.dimension;
@@ -17592,6 +17987,87 @@ function beginCheckpointRestore(state, shape) {
17592
17987
  return { addFile, finish };
17593
17988
  }
17594
17989
 
17990
+ // src/vector/vector-index/flagged-documents.ts
17991
+ var FlaggedDocuments = class extends Set {
17992
+ constructor(store, layout) {
17993
+ super();
17994
+ this.store = store;
17995
+ this.layout = layout;
17996
+ }
17997
+ ordinals = /* @__PURE__ */ new Map();
17998
+ handles = null;
17999
+ flags = new Uint8Array(0);
18000
+ chunks = null;
18001
+ add(docId) {
18002
+ super.add(docId);
18003
+ const handles = this.openHandles();
18004
+ if (handles === null) return this;
18005
+ this.flagCurrentOrdinal(docId);
18006
+ Atomics.store(handles.header, this.layout.countWord, this.size);
18007
+ return this;
18008
+ }
18009
+ delete(docId) {
18010
+ const held = super.delete(docId);
18011
+ const handles = this.openHandles();
18012
+ if (handles === null) return held;
18013
+ const ordinal = this.ordinals.get(docId);
18014
+ if (ordinal !== void 0) {
18015
+ this.ordinals.delete(docId);
18016
+ this.unflag(ordinal);
18017
+ }
18018
+ Atomics.store(handles.header, this.layout.countWord, this.size);
18019
+ return held;
18020
+ }
18021
+ clear() {
18022
+ super.clear();
18023
+ const handles = this.openHandles();
18024
+ if (handles !== null) {
18025
+ for (const ordinal of this.ordinals.values()) this.unflag(ordinal);
18026
+ Atomics.store(handles.header, this.layout.countWord, 0);
18027
+ }
18028
+ this.ordinals.clear();
18029
+ }
18030
+ flagCurrentOrdinal(docId) {
18031
+ const ordinal = this.store.getOrdinal(docId);
18032
+ const previous = this.ordinals.get(docId);
18033
+ if (ordinal === void 0 || ordinal === previous) return;
18034
+ if (this.chunks !== null) Atomics.add(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
18035
+ Atomics.store(this.flags, ordinal, 1);
18036
+ this.ordinals.set(docId, ordinal);
18037
+ if (previous !== void 0) this.unflag(previous);
18038
+ }
18039
+ unflag(ordinal) {
18040
+ Atomics.store(this.flags, ordinal, 0);
18041
+ if (this.chunks !== null) Atomics.sub(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
18042
+ }
18043
+ openHandles() {
18044
+ if (this.store.dimension === 0) return null;
18045
+ const handles = this.store.handles;
18046
+ if (handles === this.handles) return handles;
18047
+ this.handles = handles;
18048
+ this.flags = new Uint8Array(this.layout.flagsOf(handles));
18049
+ const chunksOf2 = this.layout.chunksOf;
18050
+ this.chunks = chunksOf2 === null ? null : new Int32Array(chunksOf2(handles));
18051
+ this.ordinals.clear();
18052
+ for (const docId of this) this.flagCurrentOrdinal(docId);
18053
+ return handles;
18054
+ }
18055
+ };
18056
+ function createUnplacedVectors(store) {
18057
+ return new FlaggedDocuments(store, {
18058
+ flagsOf: (handles) => handles.unplaced,
18059
+ chunksOf: (handles) => handles.unplacedChunks,
18060
+ countWord: STORE_UNPLACED_COUNT
18061
+ });
18062
+ }
18063
+ function createRemovedDocuments(store) {
18064
+ return new FlaggedDocuments(store, {
18065
+ flagsOf: (handles) => handles.removed,
18066
+ chunksOf: null,
18067
+ countWord: STORE_REMOVED_COUNT
18068
+ });
18069
+ }
18070
+
17595
18071
  // src/vector/vector-index/maintenance.ts
17596
18072
  function compact(state) {
17597
18073
  if (state.tombstones.size === 0 && state.store.replacedVectorCount === 0) return;
@@ -17789,6 +18265,43 @@ function createHeap2(compare) {
17789
18265
  };
17790
18266
  }
17791
18267
 
18268
+ // src/vector/vector-index/merge-results.ts
18269
+ function highScoreFirst(a, b) {
18270
+ return b.score - a.score || compareCodePoints(a.docId, b.docId);
18271
+ }
18272
+ function mergeResults(hnswResults, bufferResults, k) {
18273
+ const seen = /* @__PURE__ */ new Set();
18274
+ const merged = [];
18275
+ let hi = 0;
18276
+ let bi = 0;
18277
+ while (merged.length < k && (hi < hnswResults.length || bi < bufferResults.length)) {
18278
+ const h = hi < hnswResults.length ? hnswResults[hi] : void 0;
18279
+ const b = bi < bufferResults.length ? bufferResults[bi] : void 0;
18280
+ let pick;
18281
+ if (h && b) {
18282
+ if (h.score > b.score || h.score === b.score && compareCodePoints(h.docId, b.docId) < 0) {
18283
+ pick = h;
18284
+ hi++;
18285
+ } else {
18286
+ pick = b;
18287
+ bi++;
18288
+ }
18289
+ } else if (h) {
18290
+ pick = h;
18291
+ hi++;
18292
+ } else if (b) {
18293
+ pick = b;
18294
+ bi++;
18295
+ } else {
18296
+ break;
18297
+ }
18298
+ if (seen.has(pick.docId)) continue;
18299
+ seen.add(pick.docId);
18300
+ merged.push(pick);
18301
+ }
18302
+ return merged;
18303
+ }
18304
+
17792
18305
  // src/vector/vector-index/search.ts
17793
18306
  function* bufferCandidates(state, filter) {
17794
18307
  for (const docId of state.buffer) {
@@ -17818,9 +18331,6 @@ function fetchedOutcome(state, results, minSimilarity, filter) {
17818
18331
  function scannedOutcome(scan2) {
17819
18332
  return { results: scan2.results, matched: scan2.matched, matchedExact: true };
17820
18333
  }
17821
- function highScoreFirst(a, b) {
17822
- return b.score - a.score || compareCodePoints(a.docId, b.docId);
17823
- }
17824
18334
  function liveCandidates(state, candidates) {
17825
18335
  const docIds = [];
17826
18336
  const ordinals = [];
@@ -17885,38 +18395,6 @@ function bruteForceSearch(state, query, k, metric, minSimilarity, candidates) {
17885
18395
  }
17886
18396
  return { results: heap.toSortedArray().reverse(), matched };
17887
18397
  }
17888
- function mergeResults(hnswResults, bufferResults, k) {
17889
- const seen = /* @__PURE__ */ new Set();
17890
- const merged = [];
17891
- let hi = 0;
17892
- let bi = 0;
17893
- while (merged.length < k && (hi < hnswResults.length || bi < bufferResults.length)) {
17894
- const h = hi < hnswResults.length ? hnswResults[hi] : void 0;
17895
- const b = bi < bufferResults.length ? bufferResults[bi] : void 0;
17896
- let pick;
17897
- if (h && b) {
17898
- if (h.score > b.score || h.score === b.score && compareCodePoints(h.docId, b.docId) < 0) {
17899
- pick = h;
17900
- hi++;
17901
- } else {
17902
- pick = b;
17903
- bi++;
17904
- }
17905
- } else if (h) {
17906
- pick = h;
17907
- hi++;
17908
- } else if (b) {
17909
- pick = b;
17910
- bi++;
17911
- } else {
17912
- break;
17913
- }
17914
- if (seen.has(pick.docId)) continue;
17915
- seen.add(pick.docId);
17916
- merged.push(pick);
17917
- }
17918
- return merged;
17919
- }
17920
18398
  function search2(state, query, k, options) {
17921
18399
  return searchWithFilter(state, query, k, options, filterForOptions(state, options));
17922
18400
  }
@@ -18002,8 +18480,8 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
18002
18480
  hnswConfig,
18003
18481
  workerCopies,
18004
18482
  store,
18005
- tombstones: /* @__PURE__ */ new Set(),
18006
- buffer: /* @__PURE__ */ new Set(),
18483
+ tombstones: createRemovedDocuments(store),
18484
+ buffer: createUnplacedVectors(store),
18007
18485
  pendingLocations: /* @__PURE__ */ new Map(),
18008
18486
  savedFiles: [],
18009
18487
  savedSignature: null,
@@ -20479,55 +20957,93 @@ function runSegmentAction(entry, action) {
20479
20957
  }
20480
20958
  }
20481
20959
 
20482
- // src/vector/shared-field/searcher.ts
20483
- function bestFirst2(a, b) {
20484
- return b.score - a.score || (a.docId < b.docId ? -1 : a.docId > b.docId ? 1 : 0);
20960
+ // src/vector/shared-field/store-flags.ts
20961
+ function bytesOf(flags, source) {
20962
+ if (flags.source !== source) {
20963
+ flags.source = source;
20964
+ flags.view = new Uint8Array(source);
20965
+ }
20966
+ return flags.view;
20485
20967
  }
20486
- function distancesInTypeScript(state, query, metric, ordinals) {
20487
- const arenaQuery = state.store.prepareQueryArena(query);
20968
+ function countsOf(chunks, source) {
20969
+ if (chunks.source !== source) {
20970
+ chunks.source = source;
20971
+ chunks.view = new Int32Array(source);
20972
+ }
20973
+ return chunks.view;
20974
+ }
20975
+ function readStoreFlags(store) {
20976
+ const unplaced = { source: null, view: new Uint8Array(0) };
20977
+ const unplacedChunks = { source: null, view: new Int32Array(0) };
20978
+ const removed = { source: null, view: new Uint8Array(0) };
20979
+ return {
20980
+ liveDocuments() {
20981
+ const header2 = store.handles.header;
20982
+ return Atomics.load(header2, STORE_LIVE_COUNT) - Atomics.load(header2, STORE_REMOVED_COUNT);
20983
+ },
20984
+ isRemoved(ordinal) {
20985
+ const bytes = bytesOf(removed, store.handles.removed);
20986
+ return ordinal < bytes.length && Atomics.load(bytes, ordinal) === 1;
20987
+ },
20988
+ unplacedOrdinals() {
20989
+ const handles = store.handles;
20990
+ if (Atomics.load(handles.header, STORE_UNPLACED_COUNT) === 0) return [];
20991
+ const bytes = bytesOf(unplaced, handles.unplaced);
20992
+ const counts = countsOf(unplacedChunks, handles.unplacedChunks);
20993
+ const end = Math.min(store.slots, bytes.length);
20994
+ const ordinals = [];
20995
+ for (let chunk = 0; chunk < counts.length; chunk++) {
20996
+ const from = chunk * UNPLACED_FLAG_CHUNK_ORDINALS;
20997
+ if (from >= end) break;
20998
+ if (Atomics.load(counts, chunk) === 0) continue;
20999
+ const to = Math.min(end, from + UNPLACED_FLAG_CHUNK_ORDINALS);
21000
+ for (let ordinal = from; ordinal < to; ordinal++) {
21001
+ if (Atomics.load(bytes, ordinal) === 1) ordinals.push(ordinal);
21002
+ }
21003
+ }
21004
+ return ordinals;
21005
+ }
21006
+ };
21007
+ }
21008
+
21009
+ // src/vector/shared-field/searcher.ts
21010
+ function distancesInTypeScript(store, query, metric, ordinals) {
21011
+ const arenaQuery = store.prepareQueryArena(query);
20488
21012
  const queryMagnitude = arenaQuery ? arenaQuery.magnitude : magnitude(query);
20489
21013
  const distances = new Float64Array(ordinals.length).fill(Number.POSITIVE_INFINITY);
20490
21014
  for (let i = 0; i < ordinals.length; i++) {
20491
21015
  if (arenaQuery) {
20492
- distances[i] = state.store.distanceFromArena(arenaQuery, ordinals[i], metric);
21016
+ distances[i] = store.distanceFromArena(arenaQuery, ordinals[i], metric);
20493
21017
  continue;
20494
21018
  }
20495
- const entry = entryForOrd(state, ordinals[i]);
21019
+ const entry = store.entryForOrdinal(ordinals[i]);
20496
21020
  if (entry) distances[i] = toDistance(query, entry.vector, queryMagnitude, entry.magnitude, metric);
20497
21021
  }
20498
21022
  return distances;
20499
21023
  }
20500
- function bruteForceOrdinals(state, docIdOf, query, k, metric, minSimilarity, filter) {
20501
- const ordinals = [];
20502
- const docIds = [];
20503
- for (const ord of ordinalFilterValues(filter)) {
20504
- if (isTombstoned(state, ord)) continue;
20505
- const docId = docIdOf(ord);
20506
- if (docId === void 0) continue;
20507
- ordinals.push(ord);
20508
- docIds.push(docId);
20509
- }
20510
- const distances = nativeScoresOf(state.store.handles, query, metric, ordinals) ?? distancesInTypeScript(state, query, metric, ordinals);
20511
- const heap = createBoundedMaxHeap(bestFirst2, k);
21024
+ function scoreCandidates(store, candidates, query, k, metric, minSimilarity) {
21025
+ const { ordinals, docIds } = candidates;
21026
+ if (ordinals.length === 0) return { results: [], matched: 0 };
21027
+ const distances = nativeScoresOf(store.handles, query, metric, ordinals) ?? distancesInTypeScript(store, query, metric, ordinals);
21028
+ const heap = createBoundedMaxHeap(highScoreFirst, k);
20512
21029
  let matched = 0;
20513
21030
  for (let i = 0; i < ordinals.length; i++) {
20514
21031
  if (distances[i] === Number.POSITIVE_INFINITY) continue;
20515
21032
  const score = toScore(distances[i], metric);
20516
21033
  if (score < minSimilarity) continue;
20517
21034
  matched++;
20518
- heap.push({ ord: ordinals[i], score, docId: docIds[i] });
21035
+ heap.push({ docId: docIds[i], score });
20519
21036
  }
20520
- return { hits: heap.toSortedArray().reverse(), matched, matchedExact: true };
21037
+ return { results: heap.toSortedArray().reverse(), matched };
20521
21038
  }
20522
21039
  function createSharedVectorSearcher(options) {
20523
21040
  const { fieldName, view, holdsDocument } = options;
21041
+ const flags = readStoreFlags(view.store);
20524
21042
  function filterForDocIds(allowed) {
20525
21043
  const filter = createOrdinalFilter(view.store.slots);
20526
- const graph = view.graph;
20527
21044
  for (const docId of allowed) {
20528
21045
  const ordinal = view.ordinalOf(docId);
20529
- if (ordinal === void 0) continue;
20530
- if (graph !== null && isTombstoned(graph, ordinal)) continue;
21046
+ if (ordinal === void 0 || flags.isRemoved(ordinal)) continue;
20531
21047
  addToOrdinalFilter(filter, ordinal);
20532
21048
  }
20533
21049
  return filter;
@@ -20537,9 +21053,9 @@ function createSharedVectorSearcher(options) {
20537
21053
  const filter = createOrdinalFilter(slots);
20538
21054
  for (let ordinal = 0; ordinal < slots; ordinal++) {
20539
21055
  const partition = view.store.partitionAt(ordinal);
20540
- if (partition !== void 0 && allowed.has(partition) && view.store.holdsOrdinal(ordinal)) {
20541
- addToOrdinalFilter(filter, ordinal);
20542
- }
21056
+ if (partition === void 0 || !allowed.has(partition)) continue;
21057
+ if (!view.store.holdsOrdinal(ordinal) || flags.isRemoved(ordinal)) continue;
21058
+ addToOrdinalFilter(filter, ordinal);
20543
21059
  }
20544
21060
  return filter;
20545
21061
  }
@@ -20548,18 +21064,62 @@ function createSharedVectorSearcher(options) {
20548
21064
  if (searchOptions.filterPartitions !== void 0) return filterForPartitions(searchOptions.filterPartitions);
20549
21065
  return void 0;
20550
21066
  }
20551
- function hitsFor(query, k, searchOptions, filter) {
21067
+ function candidatesOf(ordinals, filter) {
21068
+ const candidates = { ordinals: [], docIds: [] };
21069
+ for (const ordinal of ordinals) {
21070
+ if (filter !== void 0 && !ordinalFilterHas(filter, ordinal)) continue;
21071
+ if (flags.isRemoved(ordinal)) continue;
21072
+ const docId = view.docIdOf(ordinal);
21073
+ if (docId === void 0 || view.ordinalOf(docId) !== ordinal) continue;
21074
+ candidates.ordinals.push(ordinal);
21075
+ candidates.docIds.push(docId);
21076
+ }
21077
+ return candidates;
21078
+ }
21079
+ function scanned(scan2) {
21080
+ return { results: scan2.results, matched: scan2.matched, matchedExact: true, resolvedEveryHit: true };
21081
+ }
21082
+ function search3(query, k, searchOptions, filter) {
20552
21083
  const { metric, minSimilarity, efSearch, oversample } = searchOptions;
20553
- const liveSize2 = view.liveSize;
21084
+ const empty = { results: [], matched: 0, matchedExact: true, resolvedEveryHit: true };
21085
+ const liveDocuments = flags.liveDocuments();
21086
+ if (liveDocuments <= 0) return empty;
21087
+ if (filter !== void 0 && filter.count === 0) return empty;
21088
+ function fetched(results, resolvedEveryHit2) {
21089
+ if (minSimilarity === Number.NEGATIVE_INFINITY) {
21090
+ const matched = filter === void 0 ? liveDocuments : filter.count;
21091
+ return { results, matched, matchedExact: true, resolvedEveryHit: resolvedEveryHit2 };
21092
+ }
21093
+ return { results, matched: results.length, matchedExact: false, resolvedEveryHit: resolvedEveryHit2 };
21094
+ }
21095
+ if (k <= 0) return fetched([], true);
20554
21096
  const graph = view.graph;
20555
- if (graph !== null && filter !== void 0 && liveSize2 > 0 && filter.count / liveSize2 < view.handles.filterThreshold) {
20556
- return bruteForceOrdinals(graph, view.docIdOf, query, k, metric, minSimilarity, filter);
21097
+ if (graph === null) {
21098
+ const ordinals = filter === void 0 ? flags.unplacedOrdinals() : ordinalFilterValues(filter);
21099
+ return scanned(scoreCandidates(view.store, candidatesOf(ordinals, filter), query, k, metric, minSimilarity));
20557
21100
  }
20558
- const hits = view.searchOrdinals(query, k, metric, minSimilarity, { filter, efSearch, oversample });
20559
- if (minSimilarity === Number.NEGATIVE_INFINITY) {
20560
- return { hits, matched: filter === void 0 ? liveSize2 : filter.count, matchedExact: true };
21101
+ if (filter !== void 0) {
21102
+ const graphLiveSize = view.liveSize;
21103
+ const selectivity = graphLiveSize > 0 ? filter.count / graphLiveSize : 1;
21104
+ if (selectivity < view.handles.filterThreshold) {
21105
+ const candidates = candidatesOf(ordinalFilterValues(filter), filter);
21106
+ return scanned(scoreCandidates(view.store, candidates, query, k, metric, minSimilarity));
21107
+ }
21108
+ }
21109
+ let resolvedEveryHit = true;
21110
+ const graphResults = [];
21111
+ for (const hit of view.searchOrdinals(query, k, metric, minSimilarity, { filter, efSearch, oversample })) {
21112
+ const docId = view.docIdOf(hit.ord);
21113
+ if (docId === void 0) {
21114
+ resolvedEveryHit = false;
21115
+ continue;
21116
+ }
21117
+ graphResults.push({ docId, score: hit.score });
20561
21118
  }
20562
- return { hits, matched: hits.length, matchedExact: false };
21119
+ const unplaced = flags.unplacedOrdinals();
21120
+ if (unplaced.length === 0) return fetched(graphResults, resolvedEveryHit);
21121
+ const scan2 = scoreCandidates(view.store, candidatesOf(unplaced, filter), query, k, metric, minSimilarity);
21122
+ return fetched(mergeResults(graphResults, scan2.results, k), resolvedEveryHit);
20563
21123
  }
20564
21124
  return {
20565
21125
  fieldName,
@@ -20574,22 +21134,20 @@ function createSharedVectorSearcher(options) {
20574
21134
  { expected: view.handles.dimension, received: query.length }
20575
21135
  );
20576
21136
  }
20577
- const filter = filterFor(searchOptions);
20578
- if (filter !== void 0 && filter.count === 0) {
20579
- return Promise.resolve({ results: [], matched: 0, matchedExact: true });
20580
- }
20581
- const scanned = hitsFor(query, k, searchOptions, filter);
20582
- const results = [];
20583
- for (const hit of scanned.hits) {
20584
- const docId = view.docIdOf(hit.ord);
20585
- if (docId === void 0 || !holdsDocument(docId)) continue;
20586
- results.push({ docId, score: hit.score });
21137
+ const outcome = search3(query, k, searchOptions, filterFor(searchOptions));
21138
+ if (view.graphRetired) {
21139
+ throw new NarsilError(
21140
+ ErrorCodes.INDEX_NOT_FOUND,
21141
+ `The main thread replaced the graph of vector field "${fieldName}" while this thread searched it`,
21142
+ { fieldName }
21143
+ );
20587
21144
  }
20588
- const holdsEveryHit = results.length === scanned.hits.length;
21145
+ const results = outcome.results.filter((result) => holdsDocument(result.docId));
21146
+ const holdsEveryHit = outcome.resolvedEveryHit && results.length === outcome.results.length;
20589
21147
  return Promise.resolve({
20590
21148
  results,
20591
- matched: scanned.matched,
20592
- matchedExact: scanned.matchedExact && holdsEveryHit
21149
+ matched: outcome.matched,
21150
+ matchedExact: outcome.matchedExact && holdsEveryHit
20593
21151
  });
20594
21152
  }
20595
21153
  };
@@ -20638,6 +21196,9 @@ function openSharedVectorField(initial, threadSlot) {
20638
21196
  get readsEveryVector() {
20639
21197
  return store.holdsEveryBlock;
20640
21198
  },
21199
+ get graphRetired() {
21200
+ return graph !== null && Atomics.load(graph.header, GRAPH_RETIRED) === 1;
21201
+ },
20641
21202
  docIdOf,
20642
21203
  ordinalOf,
20643
21204
  vectorOf(docId) {
@@ -20699,7 +21260,8 @@ function loadHeldVectorCopy(held, manager, fieldName, handle, handles, threadSlo
20699
21260
  );
20700
21261
  }
20701
21262
  function holdsVectorField(held, fieldName) {
20702
- return held.fields.get(fieldName)?.view.readsEveryVector === true;
21263
+ const view = held.fields.get(fieldName)?.view;
21264
+ return view?.readsEveryVector === true && !view.graphRetired;
20703
21265
  }
20704
21266
  function dropHeldVectorCopy(held, fieldName, handle) {
20705
21267
  held.builds.get(handle)?.view.close();
@@ -20770,6 +21332,9 @@ function createDirectExecutor(options) {
20770
21332
  return vectorIndexes;
20771
21333
  }
20772
21334
  function createIndex(indexName, config, language, analysisStale = false) {
21335
+ addIndex(indexName, config, language, analysisStale, null);
21336
+ }
21337
+ function addIndex(indexName, config, language, analysisStale, awaitedCopyLoad) {
20773
21338
  if (indexes.has(indexName)) {
20774
21339
  throw new NarsilError(ErrorCodes.INDEX_ALREADY_EXISTS, `Index "${indexName}" already exists`, {
20775
21340
  indexName
@@ -20790,7 +21355,8 @@ function createDirectExecutor(options) {
20790
21355
  },
20791
21356
  vectorIndexes,
20792
21357
  vectorCopies: createHeldVectorCopies(),
20793
- analysisStale
21358
+ analysisStale,
21359
+ awaitedCopyLoad
20794
21360
  });
20795
21361
  }
20796
21362
  function dropIndex(indexName) {
@@ -20812,7 +21378,7 @@ function createDirectExecutor(options) {
20812
21378
  }
20813
21379
  function queryContextOf(indexName) {
20814
21380
  const entry = indexes.get(indexName);
20815
- if (entry === void 0) return void 0;
21381
+ if (entry === void 0 || entry.awaitedCopyLoad !== null) return void 0;
20816
21382
  return {
20817
21383
  manager: entry.manager,
20818
21384
  config: entry.config,
@@ -20842,7 +21408,12 @@ function createDirectExecutor(options) {
20842
21408
  }
20843
21409
  case "createIndex": {
20844
21410
  const language = getLanguage(action.config.language ?? "english");
20845
- createIndex(action.indexName, action.config, language, action.analysisStale ?? false);
21411
+ addIndex(action.indexName, action.config, language, action.analysisStale ?? false, action.copyLoad ?? null);
21412
+ return void 0;
21413
+ }
21414
+ case "copyLoaded": {
21415
+ const entry = indexes.get(action.indexName);
21416
+ if (entry?.awaitedCopyLoad === action.copyLoad) entry.awaitedCopyLoad = null;
20846
21417
  return void 0;
20847
21418
  }
20848
21419
  case "dropIndex": {
@@ -21004,6 +21575,74 @@ function createDirectExecutor(options) {
21004
21575
  };
21005
21576
  }
21006
21577
 
21578
+ // src/partitioning/text-rebuild.ts
21579
+ function interrupted(pass) {
21580
+ return pass.log.partitionsReplaced || !pass.hooks.isCurrent();
21581
+ }
21582
+ async function buildPartition(pass, source, partitionId) {
21583
+ const { manager, options } = pass;
21584
+ const target = createPartitionIndex(partitionId, manager.config.trackPositions ?? true);
21585
+ const docIds = [...source.docIds()];
21586
+ target.beginBatch();
21587
+ for (let start = 0; start < docIds.length; start += TEXT_REBUILD_CHUNK_DOCUMENTS) {
21588
+ const end = Math.min(start + TEXT_REBUILD_CHUNK_DOCUMENTS, docIds.length);
21589
+ for (let i = start; i < end; i++) {
21590
+ const document2 = manager.getRef(docIds[i]);
21591
+ if (document2 !== void 0) target.insert(docIds[i], document2, manager.schema, manager.language, options);
21592
+ }
21593
+ await yieldToEventLoop();
21594
+ if (interrupted(pass)) return null;
21595
+ }
21596
+ target.endBatch();
21597
+ if (manager.partialWordAnalysis !== null) target.preparePartialWords(manager.partialWordAnalysis);
21598
+ return target;
21599
+ }
21600
+ function applyWrittenDocuments(pass, built) {
21601
+ const { manager, options } = pass;
21602
+ for (const docId of pass.log.takeWrittenDocIds()) {
21603
+ for (const partition of built) {
21604
+ if (!partition.has(docId)) continue;
21605
+ partition.remove(docId, manager.schema, manager.language, options);
21606
+ break;
21607
+ }
21608
+ const partitionId = manager.partitionIdOf(docId);
21609
+ const document2 = manager.getRef(docId);
21610
+ if (partitionId === void 0 || document2 === void 0) continue;
21611
+ built[partitionId].insert(docId, document2, manager.schema, manager.language, options);
21612
+ }
21613
+ }
21614
+ async function rebuildOnce(pass) {
21615
+ const partitionCount = pass.manager.partitionCount;
21616
+ const built = [];
21617
+ for (let partitionId = 0; partitionId < partitionCount; partitionId++) {
21618
+ const partition = await buildPartition(pass, pass.manager.getPartition(partitionId), partitionId);
21619
+ if (partition === null) return false;
21620
+ built.push(partition);
21621
+ pass.hooks.onPartitionBuilt(built.length);
21622
+ }
21623
+ while (pass.log.pendingDocuments > TEXT_REBUILD_SWITCH_BACKLOG_DOCUMENTS) {
21624
+ applyWrittenDocuments(pass, built);
21625
+ await yieldToEventLoop();
21626
+ if (interrupted(pass)) return false;
21627
+ }
21628
+ applyWrittenDocuments(pass, built);
21629
+ pass.manager.adoptRebuiltPartitions(built);
21630
+ pass.hooks.onSwitch();
21631
+ return true;
21632
+ }
21633
+ async function rebuildTextIndexes(manager, hooks) {
21634
+ const options = resolvePartitionInsertOptions(manager.config, manager.analysis, { validate: false, skipClone: true });
21635
+ while (hooks.isCurrent()) {
21636
+ const log = manager.openWriteLog();
21637
+ try {
21638
+ if (await rebuildOnce({ manager, log, hooks, options })) return true;
21639
+ } finally {
21640
+ log.close();
21641
+ }
21642
+ }
21643
+ return false;
21644
+ }
21645
+
21007
21646
  // src/engine/events.ts
21008
21647
  function eventHandlersOf(events) {
21009
21648
  const handlers = /* @__PURE__ */ new Map();
@@ -21026,11 +21665,6 @@ function emitEngineEvent(eventHandlers, event, payload) {
21026
21665
  }
21027
21666
 
21028
21667
  // src/engine/analysis-rebuild.ts
21029
- function yieldToEventLoop4() {
21030
- return new Promise((resolve) => {
21031
- setTimeout(resolve, 0);
21032
- });
21033
- }
21034
21668
  function toError(value) {
21035
21669
  return value instanceof Error ? value : new Error(String(value));
21036
21670
  }
@@ -21041,9 +21675,13 @@ function wireAnalysisRebuild(wiring) {
21041
21675
  resyncIndex: wiring.resyncIndex,
21042
21676
  async persistAnalysisRevision(indexName) {
21043
21677
  const durability = wiring.durabilityManager;
21044
- if (durability === null) return;
21045
- await durability.persistMetadata(indexName);
21678
+ if (durability === null) {
21679
+ wiring.recordCurrentAnalysis(indexName);
21680
+ return;
21681
+ }
21046
21682
  await (durability.checkpointFromMemory?.(indexName) ?? durability.checkpoint(indexName));
21683
+ wiring.recordCurrentAnalysis(indexName);
21684
+ await durability.persistMetadata(indexName);
21047
21685
  },
21048
21686
  emit(payload) {
21049
21687
  emitEngineEvent(wiring.eventHandlers, "analysisRebuild", payload);
@@ -21054,9 +21692,10 @@ function createAnalysisRebuildCoordinator(config, deps) {
21054
21692
  const stale = /* @__PURE__ */ new Map();
21055
21693
  const rebuildMode = config?.rebuild ?? "auto";
21056
21694
  let queue = Promise.resolve();
21695
+ let active = null;
21057
21696
  async function rebuildNow(indexName, entry) {
21058
21697
  const manager = deps.getManager(indexName);
21059
- if (manager === void 0) {
21698
+ if (manager === void 0 || entry.abandoned) {
21060
21699
  entry.run = null;
21061
21700
  return;
21062
21701
  }
@@ -21064,12 +21703,20 @@ function createAnalysisRebuildCoordinator(config, deps) {
21064
21703
  deps.emit({ indexName, status: "started", partitionsRebuilt: 0, partitionCount });
21065
21704
  const wasPromoted = deps.desyncIndex(indexName);
21066
21705
  try {
21067
- for (let partitionId = 0; partitionId < manager.partitionCount; partitionId += 1) {
21068
- manager.rebuildTextIndex(partitionId);
21069
- entry.partitionsRebuilt = partitionId + 1;
21070
- await yieldToEventLoop4();
21706
+ const rebuilt = await rebuildTextIndexes(manager, {
21707
+ isCurrent: () => deps.getManager(indexName) === manager && stale.get(indexName) === entry && !entry.abandoned,
21708
+ onPartitionBuilt(partitionsBuilt) {
21709
+ entry.partitionsRebuilt = partitionsBuilt;
21710
+ },
21711
+ onSwitch() {
21712
+ stale.delete(indexName);
21713
+ }
21714
+ });
21715
+ if (!rebuilt) {
21716
+ entry.run = null;
21717
+ if (deps.getManager(indexName) === manager) await deps.resyncIndex(indexName, wasPromoted);
21718
+ return;
21071
21719
  }
21072
- stale.delete(indexName);
21073
21720
  await deps.persistAnalysisRevision(indexName);
21074
21721
  await deps.resyncIndex(indexName, wasPromoted);
21075
21722
  deps.emit({
@@ -21080,7 +21727,6 @@ function createAnalysisRebuildCoordinator(config, deps) {
21080
21727
  });
21081
21728
  } catch (err) {
21082
21729
  const error = toError(err);
21083
- stale.set(indexName, entry);
21084
21730
  entry.run = null;
21085
21731
  await deps.resyncIndex(indexName, wasPromoted).catch(() => void 0);
21086
21732
  deps.emit({
@@ -21093,11 +21739,20 @@ function createAnalysisRebuildCoordinator(config, deps) {
21093
21739
  throw error;
21094
21740
  }
21095
21741
  }
21742
+ function runTracked(indexName, entry) {
21743
+ const done = rebuildNow(indexName, entry);
21744
+ const tracked = { indexName, entry, done };
21745
+ active = tracked;
21746
+ return done.finally(() => {
21747
+ if (active === tracked) active = null;
21748
+ });
21749
+ }
21096
21750
  function enqueue(indexName, entry) {
21097
21751
  if (entry.run !== null) {
21098
21752
  return entry.run;
21099
21753
  }
21100
- const run = queue.then(() => rebuildNow(indexName, entry));
21754
+ entry.abandoned = false;
21755
+ const run = queue.then(() => runTracked(indexName, entry));
21101
21756
  entry.run = run;
21102
21757
  queue = run.then(
21103
21758
  () => void 0,
@@ -21132,7 +21787,7 @@ function createAnalysisRebuildCoordinator(config, deps) {
21132
21787
  if (existing !== void 0 && existing.run !== null) {
21133
21788
  return;
21134
21789
  }
21135
- stale.set(index.indexName, { index, partitionsRebuilt: 0, run: null });
21790
+ stale.set(index.indexName, { index, partitionsRebuilt: 0, run: null, abandoned: false });
21136
21791
  },
21137
21792
  clearStale(indexName) {
21138
21793
  stale.delete(indexName);
@@ -21143,6 +21798,14 @@ function createAnalysisRebuildCoordinator(config, deps) {
21143
21798
  isRunning(indexName) {
21144
21799
  return stale.get(indexName)?.run !== null && stale.get(indexName)?.run !== void 0;
21145
21800
  },
21801
+ async abandon(indexName) {
21802
+ const entry = stale.get(indexName);
21803
+ if (entry !== void 0 && entry.run !== null) entry.abandoned = true;
21804
+ const running = active;
21805
+ if (running === null || running.indexName !== indexName) return;
21806
+ running.entry.abandoned = true;
21807
+ await running.done.catch(() => void 0);
21808
+ },
21146
21809
  review(indexName) {
21147
21810
  const entry = stale.get(indexName);
21148
21811
  return entry === void 0 ? Promise.resolve() : review(indexName, entry);
@@ -21398,12 +22061,12 @@ async function lockDirectory(root) {
21398
22061
  const resolvedRoot = pathMod.resolve(root);
21399
22062
  const lockPath = pathMod.join(resolvedRoot, LOCK_FILE_NAME);
21400
22063
  const self = await describeSelf();
21401
- async function holds() {
22064
+ async function holds2() {
21402
22065
  const holder = await readHolder(fs, lockPath);
21403
22066
  return typeof holder === "object" && holder.nonce === self.nonce;
21404
22067
  }
21405
22068
  async function removeOwnLockFile() {
21406
- if (await holds().catch(() => false)) await fs.unlink(lockPath).catch(() => void 0);
22069
+ if (await holds2().catch(() => false)) await fs.unlink(lockPath).catch(() => void 0);
21407
22070
  }
21408
22071
  let directoryKey = null;
21409
22072
  let closeDirectory = () => void 0;
@@ -21429,7 +22092,7 @@ async function lockDirectory(root) {
21429
22092
  const heldKey = directoryKey;
21430
22093
  let released = false;
21431
22094
  return {
21432
- holds,
22095
+ holds: holds2,
21433
22096
  async release() {
21434
22097
  if (released) return;
21435
22098
  released = true;
@@ -21809,7 +22472,7 @@ function buildEntry(fields) {
21809
22472
  }
21810
22473
 
21811
22474
  // src/serialization/chunk-checksum.ts
21812
- function yieldToEventLoop5() {
22475
+ function yieldToEventLoop4() {
21813
22476
  if (typeof setImmediate === "function") {
21814
22477
  return new Promise((resolve) => setImmediate(resolve));
21815
22478
  }
@@ -21821,7 +22484,7 @@ async function advanceChecksum(start, payload) {
21821
22484
  const end = Math.min(offset + CHECKSUM_YIELD_CHUNK_BYTES, payload.length);
21822
22485
  state = crc32Update(state, payload.subarray(offset, end));
21823
22486
  if (end < payload.length) {
21824
- await yieldToEventLoop5();
22487
+ await yieldToEventLoop4();
21825
22488
  }
21826
22489
  }
21827
22490
  return state;
@@ -25830,7 +26493,7 @@ function removeWhatTheSegmentReplaces(manager, segment, contents) {
25830
26493
  manager.endBatchRemove();
25831
26494
  }
25832
26495
  }
25833
- function yieldToEventLoop6() {
26496
+ function yieldToEventLoop5() {
25834
26497
  return new Promise((resolve) => setTimeout(resolve, 0));
25835
26498
  }
25836
26499
  async function loadPartitionSegments(store, partition, manager, options) {
@@ -25841,7 +26504,7 @@ async function loadPartitionSegments(store, partition, manager, options) {
25841
26504
  const segment = createSharedFrozenSegment(shareSegmentArrays(contents.arrays, manager.partialWordAnalysis));
25842
26505
  removeWhatTheSegmentReplaces(manager, segment, contents);
25843
26506
  if (contents.arrays.documentCount > 0) manager.attachFrozenSegment(partition.partitionId, segment);
25844
- await yieldToEventLoop6();
26507
+ await yieldToEventLoop5();
25845
26508
  }
25846
26509
  }
25847
26510
 
@@ -28269,7 +28932,7 @@ function metadataConfigOf(entry) {
28269
28932
  b: entry.config.bm25?.b ?? 0.75,
28270
28933
  ...embedding !== void 0 ? { embedding } : {},
28271
28934
  surfaceForms: entry.config.surfaceForms !== false,
28272
- analysisRevision: entry.language.revision,
28935
+ ...entry.analysisRevision !== null ? { analysisRevision: entry.analysisRevision } : {},
28273
28936
  ...typeof entry.config.tokenizer === "string" ? { tokenizer: entry.config.tokenizer } : {},
28274
28937
  ...typeof entry.config.stopWords === "string" ? { stopWords: entry.config.stopWords } : {},
28275
28938
  ...entry.config.stopWords instanceof Set ? { stopWordList: [...entry.config.stopWords].sort(compareCodePoints) } : {},
@@ -28510,7 +29173,8 @@ async function createIndexFromMetadata(deps, metadata, loadData) {
28510
29173
  indexUuid: metadata.indexUuid ?? null,
28511
29174
  heldPartitions: metadata.heldPartitions ?? null,
28512
29175
  documentCount: metadata.documentCount ?? 0,
28513
- partitionCount: metadata.partitionCount
29176
+ partitionCount: metadata.partitionCount,
29177
+ analysisRevision: metadata.analysisRevision ?? null
28514
29178
  });
28515
29179
  if (loadData) await deps.indexState().registerOpen(metadata.indexName);
28516
29180
  else deps.indexState().registerClosed(metadata.indexName);
@@ -28633,8 +29297,14 @@ function createIndexStateCoordinator(config, callbacks) {
28633
29297
  return entry;
28634
29298
  }
28635
29299
  }
29300
+ function transitionPending(indexName, entry) {
29301
+ if (entries.get(indexName) !== entry) return true;
29302
+ if (entry.state === "dropping" && entry.dropPromise !== null) return true;
29303
+ return entry.state === "closing" && entry.closePromise !== null;
29304
+ }
28636
29305
  async function reopen(indexName, explicit) {
28637
- const entry = await awaitPendingTransitions(indexName);
29306
+ let entry = await awaitPendingTransitions(indexName);
29307
+ while (transitionPending(indexName, entry)) entry = await awaitPendingTransitions(indexName);
28638
29308
  if (entry.state === "open") return;
28639
29309
  if (explicit) {
28640
29310
  entry.reopenFailures = 0;
@@ -29223,6 +29893,7 @@ function shareFrozenSegment(segment, analysis) {
29223
29893
  }
29224
29894
 
29225
29895
  // src/engine/worker-resync.ts
29896
+ var copyLoadSequence = 0;
29226
29897
  function captureComposite(manager, partitionId) {
29227
29898
  const partition = manager.getPartition(partitionId);
29228
29899
  if (!isCompositePartition(partition)) return null;
@@ -29283,8 +29954,8 @@ async function sendPartition(indexName, executors, captured) {
29283
29954
  )
29284
29955
  );
29285
29956
  }
29286
- async function transferIndexToPool(indexName, pool, config, manager, analysisStale = false) {
29287
- await loadIndexOntoWorkers(
29957
+ function transferIndexToPool(indexName, pool, config, manager, analysisStale = false) {
29958
+ return loadIndexOntoWorkers(
29288
29959
  indexName,
29289
29960
  pool.getAllExecutors(),
29290
29961
  config,
@@ -29294,6 +29965,8 @@ async function transferIndexToPool(indexName, pool, config, manager, analysisSta
29294
29965
  );
29295
29966
  }
29296
29967
  async function loadIndexOntoWorkers(indexName, executors, config, manager, onDropped, analysisStale = false) {
29968
+ copyLoadSequence += 1;
29969
+ const copyLoad = `${indexName}#${copyLoadSequence}`;
29297
29970
  const captured = capturePartitions(manager);
29298
29971
  await Promise.allSettled(
29299
29972
  executors.map(
@@ -29308,13 +29981,15 @@ async function loadIndexOntoWorkers(indexName, executors, config, manager, onDro
29308
29981
  indexName,
29309
29982
  config,
29310
29983
  requestId: `resync-create-${indexName}`,
29311
- analysisStale
29984
+ analysisStale,
29985
+ copyLoad
29312
29986
  })
29313
29987
  )
29314
29988
  );
29315
29989
  for (const partition of captured) {
29316
29990
  await sendPartition(indexName, executors, partition);
29317
29991
  }
29992
+ return copyLoad;
29318
29993
  }
29319
29994
 
29320
29995
  // src/workers/protocol.ts
@@ -29562,6 +30237,14 @@ function enqueueReplication(state, indexName, action) {
29562
30237
  queue.inFlight.push(run);
29563
30238
  queue.tail = run;
29564
30239
  }
30240
+ function markCopiesLoaded(state, indexName, copyLoad) {
30241
+ enqueueReplication(state, indexName, {
30242
+ type: "copyLoaded",
30243
+ indexName,
30244
+ copyLoad,
30245
+ requestId: `copy-loaded-${copyLoad}`
30246
+ });
30247
+ }
29565
30248
  async function replicateToWorkers(state, action) {
29566
30249
  if (!("indexName" in action)) {
29567
30250
  if (state.workerPool === null) return;
@@ -30511,8 +31194,9 @@ async function loadOntoReplacement(state, replacement, indexName) {
30511
31194
  if (entry === void 0 || manager === void 0 || !state.scaledOutIndexes.has(indexName)) return;
30512
31195
  const buffered = [];
30513
31196
  state.copyLoadBuffers.set(indexName, buffered);
31197
+ let copyLoad;
30514
31198
  try {
30515
- await loadIndexOntoWorkers(
31199
+ copyLoad = await loadIndexOntoWorkers(
30516
31200
  indexName,
30517
31201
  [replacement.executor],
30518
31202
  entry.config,
@@ -30525,6 +31209,7 @@ async function loadOntoReplacement(state, replacement, indexName) {
30525
31209
  state.copyLoadBuffers.delete(indexName);
30526
31210
  for (const action of buffered) enqueueReplication(state, indexName, action);
30527
31211
  }
31212
+ markCopiesLoaded(state, indexName, copyLoad);
30528
31213
  refreshVectorCopies(state, indexName);
30529
31214
  }
30530
31215
  async function replaceWorker(state, pool, workerId) {
@@ -30654,21 +31339,21 @@ async function ensurePool(state) {
30654
31339
  function documentCountOf(state, indexName) {
30655
31340
  return state.executor.getManager(indexName)?.countDocuments() ?? 0;
30656
31341
  }
30657
- function copyThresholdReason(state, indexName, incomingCount = 0) {
31342
+ function copyThresholdReason(state, indexName, incomingCount2 = 0) {
30658
31343
  const held = documentCountOf(state, indexName);
30659
- if (incomingCount <= 0) {
31344
+ if (incomingCount2 <= 0) {
30660
31345
  return `Index "${indexName}" holds ${held} documents, reaching the copy threshold of ${state.copyThreshold}`;
30661
31346
  }
30662
- 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}`;
31347
+ return `Index "${indexName}" holds ${held} documents, and the batch that the engine is writing adds ${incomingCount2}, for ${held + incomingCount2} against the copy threshold of ${state.copyThreshold}`;
30663
31348
  }
30664
- function indexReadyForCopies(state, indexName, incomingCount = 0) {
31349
+ function indexReadyForCopies(state, indexName, incomingCount2 = 0) {
30665
31350
  if (!copiesAllowed(state)) return false;
30666
31351
  if (state.scaledOutIndexes.has(indexName) || state.desyncedIndexes.has(indexName)) return false;
30667
31352
  if (state.copyTransitions.has(indexName)) return false;
30668
31353
  if (state.callbacks?.shouldDeferCopies?.()) return false;
30669
31354
  const entry = state.indexRegistry.get(indexName);
30670
31355
  if (entry === void 0 || state.executor.getManager(indexName) === void 0) return false;
30671
- if (documentCountOf(state, indexName) + incomingCount < state.copyThreshold) return false;
31356
+ if (documentCountOf(state, indexName) + incomingCount2 < state.copyThreshold) return false;
30672
31357
  const ineligibility = workerIneligibility(indexName, entry.config, state.bootstrapModule);
30673
31358
  if (ineligibility !== null) {
30674
31359
  reportIneligible(state, indexName, ineligibility);
@@ -30701,7 +31386,13 @@ async function loadCopies(state, indexName, reason) {
30701
31386
  state.copyLoadBuffers.set(indexName, buffered);
30702
31387
  try {
30703
31388
  freezeLiveTailsBeforeCopiesLoad(manager);
30704
- await transferIndexToPool(indexName, pool, entry.config, manager, state.callbacks?.isAnalysisStale?.(indexName));
31389
+ const copyLoad = await transferIndexToPool(
31390
+ indexName,
31391
+ pool,
31392
+ entry.config,
31393
+ manager,
31394
+ state.callbacks?.isAnalysisStale?.(indexName)
31395
+ );
30705
31396
  if (state.desyncedIndexes.has(indexName)) {
30706
31397
  discardLoadedCopies(state, pool, indexName);
30707
31398
  return;
@@ -30710,6 +31401,7 @@ async function loadCopies(state, indexName, reason) {
30710
31401
  state.lastAccessAt.set(indexName, Date.now());
30711
31402
  state.poolRetryDelayMs = POOL_RESTART_DELAY_MS;
30712
31403
  for (const action of buffered) enqueueReplication(state, indexName, action);
31404
+ markCopiesLoaded(state, indexName, copyLoad);
30713
31405
  } catch (err) {
30714
31406
  if (dropReason !== void 0) state.droppedCopies.set(indexName, dropReason);
30715
31407
  throw err;
@@ -30752,15 +31444,15 @@ async function scaleOutReadyIndexes(state) {
30752
31444
  }, 0);
30753
31445
  }
30754
31446
  }
30755
- async function scaleOutBeforeBatch(state, indexName, incomingCount) {
30756
- if (incomingCount <= 0) return;
31447
+ async function scaleOutBeforeBatch(state, indexName, incomingCount2) {
31448
+ if (incomingCount2 <= 0) return;
30757
31449
  const pending = state.copyTransitions.get(indexName);
30758
31450
  if (pending !== void 0) {
30759
31451
  if (pending.kind === "load") await pending.done;
30760
31452
  return;
30761
31453
  }
30762
- if (!indexReadyForCopies(state, indexName, incomingCount)) return;
30763
- await scaleOutIndex(state, indexName, copyThresholdReason(state, indexName, incomingCount));
31454
+ if (!indexReadyForCopies(state, indexName, incomingCount2)) return;
31455
+ await scaleOutIndex(state, indexName, copyThresholdReason(state, indexName, incomingCount2));
30764
31456
  }
30765
31457
 
30766
31458
  // src/engine/orchestration/idle.ts
@@ -31053,7 +31745,13 @@ function createWorkerOrchestrator(config, executor, indexRegistry, callbacks, ve
31053
31745
  idleSweep: null
31054
31746
  };
31055
31747
  startIdleSweep(state);
31748
+ function settlingCopyWork(indexName) {
31749
+ return state.copyTransitions.get(indexName)?.done ?? state.compactionsInFlight.get(indexName);
31750
+ }
31056
31751
  async function resyncIndex(indexName, wasScaledOut) {
31752
+ for (let pending = settlingCopyWork(indexName); pending !== void 0; pending = settlingCopyWork(indexName)) {
31753
+ await pending;
31754
+ }
31057
31755
  state.desyncedIndexes.delete(indexName);
31058
31756
  if (!wasScaledOut) return;
31059
31757
  await scaleOutIndex(state, indexName, copyThresholdReason(state, indexName));
@@ -31111,8 +31809,15 @@ function createWorkerOrchestrator(config, executor, indexRegistry, callbacks, ve
31111
31809
  const manager = executor.getManager(indexName);
31112
31810
  if (pool !== null && entry !== void 0 && manager !== void 0) {
31113
31811
  try {
31114
- await transferIndexToPool(indexName, pool, entry.config, manager, callbacks?.isAnalysisStale?.(indexName));
31812
+ const copyLoad = await transferIndexToPool(
31813
+ indexName,
31814
+ pool,
31815
+ entry.config,
31816
+ manager,
31817
+ callbacks?.isAnalysisStale?.(indexName)
31818
+ );
31115
31819
  state.scaledOutIndexes.add(indexName);
31820
+ markCopiesLoaded(state, indexName, copyLoad);
31116
31821
  refreshVectorCopies(state, indexName);
31117
31822
  } catch (repairError) {
31118
31823
  throw new NarsilError(
@@ -31177,7 +31882,7 @@ function createWorkerOrchestrator(config, executor, indexRegistry, callbacks, ve
31177
31882
  }
31178
31883
  return {
31179
31884
  scaleOutReadyIndexes: () => scaleOutReadyIndexes(state),
31180
- scaleOutBeforeBatch: (indexName, incomingCount) => scaleOutBeforeBatch(state, indexName, incomingCount),
31885
+ scaleOutBeforeBatch: (indexName, incomingCount2) => scaleOutBeforeBatch(state, indexName, incomingCount2),
31181
31886
  replicateToWorkers: replicate,
31182
31887
  awaitReplication: (indexName) => awaitReplicationIdle(state, indexName),
31183
31888
  awaitWrites: (indexName) => awaitWritesApplied(state, indexName),
@@ -31405,6 +32110,10 @@ function createEngineCore(config, hooks, capacityScope = "index") {
31405
32110
  getManager: (indexName) => executor.getManager(indexName),
31406
32111
  desyncIndex: (indexName) => orchestrator.desyncIndex(indexName),
31407
32112
  resyncIndex: (indexName, wasPromoted) => orchestrator.resyncIndex(indexName, wasPromoted),
32113
+ recordCurrentAnalysis: (indexName) => {
32114
+ const entry = indexRegistry.get(indexName);
32115
+ if (entry !== void 0 && !analysisRebuild.isStale(indexName)) entry.analysisRevision = entry.language.revision;
32116
+ },
31408
32117
  durabilityManager: durability?.manager ?? null
31409
32118
  });
31410
32119
  const indexState = wireIndexState({
@@ -31578,11 +32287,16 @@ function refuseCode(entry) {
31578
32287
  }
31579
32288
  function indexConfigurationOf(entry) {
31580
32289
  refuseCode(entry);
31581
- return configurationPayloadOf(entry.config, entry.language, entry.embeddingAdapterName);
32290
+ return configurationPayloadOf(entry.config, entry.language, entry.embeddingAdapterName, entry.analysisRevision);
31582
32291
  }
31583
32292
  function requireConfigurationWithinSizeLimit(config, language) {
31584
32293
  const adapter = config.embedding?.adapter;
31585
- const payload = configurationPayloadOf(config, language, typeof adapter === "string" ? adapter : null);
32294
+ const payload = configurationPayloadOf(
32295
+ config,
32296
+ language,
32297
+ typeof adapter === "string" ? adapter : null,
32298
+ language.revision
32299
+ );
31586
32300
  if (messagePackUpperBoundBytes(payload) <= MAX_INDEX_CONFIGURATION_BYTES) return;
31587
32301
  const bytes = encode(payload).byteLength;
31588
32302
  if (bytes <= MAX_INDEX_CONFIGURATION_BYTES) return;
@@ -31592,12 +32306,12 @@ function requireConfigurationWithinSizeLimit(config, language) {
31592
32306
  { bytes, maximum: MAX_INDEX_CONFIGURATION_BYTES }
31593
32307
  );
31594
32308
  }
31595
- function configurationPayloadOf(config, language, adapterName) {
32309
+ function configurationPayloadOf(config, language, adapterName, analysisRevision) {
31596
32310
  const bm25 = config.bm25;
31597
32311
  return {
31598
32312
  schema: config.schema,
31599
32313
  language: language.name,
31600
- analysisRevision: language.revision,
32314
+ ...analysisRevision !== null ? { analysisRevision } : {},
31601
32315
  ...typeof config.tokenizer === "string" ? { tokenizer: config.tokenizer } : {},
31602
32316
  ...typeof config.stopWords === "string" ? { stopWords: config.stopWords } : {},
31603
32317
  ...config.stopWords instanceof Set ? { stopWordList: [...config.stopWords].sort(compareCodePoints) } : {},
@@ -31756,7 +32470,8 @@ async function createEngineIndex(core, config, name, requestedConfig, creation =
31756
32470
  indexUuid: indexUuid ?? null,
31757
32471
  heldPartitions: null,
31758
32472
  documentCount: 0,
31759
- partitionCount: indexConfig.partitions?.maxPartitions ?? 1
32473
+ partitionCount: indexConfig.partitions?.maxPartitions ?? 1,
32474
+ analysisRevision: language.revision
31760
32475
  });
31761
32476
  await core.indexState.registerOpen(name);
31762
32477
  if (core.durability) {
@@ -31768,6 +32483,7 @@ async function dropEngineIndex(core, name) {
31768
32483
  core.guardShutdown();
31769
32484
  const entry = core.requireIndex(name);
31770
32485
  await core.indexState.drop(name, async () => {
32486
+ await core.analysisRebuild.abandon(name);
31771
32487
  await core.orchestrator.closeIndex(name);
31772
32488
  if (core.durability) {
31773
32489
  try {
@@ -34115,7 +34831,7 @@ function dateOf(value, path) {
34115
34831
  refuse(path, `holds a "${DATE_TAG}" tag outside the range of a date`);
34116
34832
  return new Date(milliseconds);
34117
34833
  }
34118
- function bytesOf(value, path) {
34834
+ function bytesOf2(value, path) {
34119
34835
  const bytes = typeof value === "string" ? decodeExactBase64(value) : null;
34120
34836
  if (bytes === null) refuse(path, `holds a "${BYTES_TAG}" tag whose text is not padded base64`);
34121
34837
  return bytes;
@@ -34145,7 +34861,7 @@ function untagTag(tag, value, path, depth) {
34145
34861
  case DATE_TAG:
34146
34862
  return dateOf(value, path);
34147
34863
  case BYTES_TAG:
34148
- return bytesOf(value, path);
34864
+ return bytesOf2(value, path);
34149
34865
  case NUMBER_TAG:
34150
34866
  return numberOf(value, path);
34151
34867
  case OBJECT_TAG:
@@ -35068,7 +35784,8 @@ async function createRestoredIndex(indexName, prepared, partitionCount, deps) {
35068
35784
  indexUuid: null,
35069
35785
  heldPartitions: null,
35070
35786
  documentCount: 0,
35071
- partitionCount
35787
+ partitionCount,
35788
+ analysisRevision: prepared.storedRevision
35072
35789
  });
35073
35790
  await deps.indexState.registerOpen(indexName);
35074
35791
  const release = await deps.indexState.acquire(indexName, false);
@@ -36487,15 +37204,15 @@ function oversampleOf(vectorConfig) {
36487
37204
  return oversample;
36488
37205
  }
36489
37206
  function vectorFetchDepth(limit, offset, cursorDepth) {
36490
- const reach = cursorDepth + offset + limit;
36491
- if (reach > RESULT_WINDOW) {
37207
+ const reach2 = cursorDepth + offset + limit;
37208
+ if (reach2 > RESULT_WINDOW) {
36492
37209
  throw new NarsilError(
36493
37210
  ErrorCodes.SEARCH_RESULT_WINDOW_EXCEEDED,
36494
- `The engine pages a vector search through the first ${RESULT_WINDOW} results, and this page ends at ${reach}. Narrow the search with a filter or a similarity floor`,
37211
+ `The engine pages a vector search through the first ${RESULT_WINDOW} results, and this page ends at ${reach2}. Narrow the search with a filter or a similarity floor`,
36495
37212
  { limit, offset, cursorDepth, window: RESULT_WINDOW }
36496
37213
  );
36497
37214
  }
36498
- return Math.min(reach + 1, RESULT_WINDOW);
37215
+ return Math.min(reach2 + 1, RESULT_WINDOW);
36499
37216
  }
36500
37217
  async function executeVectorSearch(params, context, limit, offset, cursorDepth = 0) {
36501
37218
  const { manager, config, partitionIds } = context;
@@ -36965,6 +37682,15 @@ async function enterRead(core, indexName) {
36965
37682
  core.guardShutdown();
36966
37683
  if (core.rebalancingIndexes.has(indexName)) await yieldToEventLoop();
36967
37684
  }
37685
+ async function readUnderOneAnalysis(core, indexName, read) {
37686
+ let analysisStale = core.analysisRebuild.isStale(indexName);
37687
+ let result = await read();
37688
+ while (core.analysisRebuild.isStale(indexName) !== analysisStale) {
37689
+ analysisStale = core.analysisRebuild.isStale(indexName);
37690
+ result = await read();
37691
+ }
37692
+ return { result, analysisStale };
37693
+ }
36968
37694
  function scopedParams(params, options) {
36969
37695
  {
36970
37696
  return params;
@@ -36992,7 +37718,7 @@ async function runEngineQuery(core, indexName, params, options) {
36992
37718
  entry.embeddingAdapterName
36993
37719
  );
36994
37720
  const workerSearch = core.orchestrator.hasWorkerPool() ? core.orchestrator.searchViaWorker.bind(core.orchestrator) : void 0;
36995
- const result = await executeQuery(resolvedParams, {
37721
+ const context = {
36996
37722
  manager,
36997
37723
  language: entry.language,
36998
37724
  config: entry.config,
@@ -37002,7 +37728,15 @@ async function runEngineQuery(core, indexName, params, options) {
37002
37728
  partitionIds: options?.partitionIds,
37003
37729
  cursorBinding: queryBindingOf(params),
37004
37730
  patternWorkCap: core.patternWorkCap
37005
- });
37731
+ };
37732
+ const { result, analysisStale } = await readUnderOneAnalysis(
37733
+ core,
37734
+ indexName,
37735
+ () => executeQuery(resolvedParams, context)
37736
+ );
37737
+ if (analysisStale) {
37738
+ result.analysisStale = true;
37739
+ }
37006
37740
  if (core.pluginRegistry.hasHooks("afterSearch")) {
37007
37741
  const observed = { ...result, hits: [...result.hits] };
37008
37742
  try {
@@ -37011,9 +37745,6 @@ async function runEngineQuery(core, indexName, params, options) {
37011
37745
  console.warn("afterSearch plugin hook error:", err);
37012
37746
  }
37013
37747
  }
37014
- if (core.analysisRebuild.isStale(indexName)) {
37015
- result.analysisStale = true;
37016
- }
37017
37748
  return result;
37018
37749
  } finally {
37019
37750
  release();
@@ -37034,7 +37765,7 @@ async function runEnginePreflight(core, indexName, params, options) {
37034
37765
  entry.embeddingAdapterName
37035
37766
  );
37036
37767
  const workerSearch = core.orchestrator.hasWorkerPool() ? core.orchestrator.searchViaWorker.bind(core.orchestrator) : void 0;
37037
- const result = await executePreflight(resolvedParams, {
37768
+ const context = {
37038
37769
  manager,
37039
37770
  language: entry.language,
37040
37771
  config: entry.config,
@@ -37044,8 +37775,13 @@ async function runEnginePreflight(core, indexName, params, options) {
37044
37775
  partitionIds: options?.partitionIds,
37045
37776
  cursorBinding: queryBindingOf(params),
37046
37777
  patternWorkCap: core.patternWorkCap
37047
- });
37048
- if (core.analysisRebuild.isStale(indexName)) {
37778
+ };
37779
+ const { result, analysisStale } = await readUnderOneAnalysis(
37780
+ core,
37781
+ indexName,
37782
+ () => executePreflight(resolvedParams, context)
37783
+ );
37784
+ if (analysisStale) {
37049
37785
  result.analysisStale = true;
37050
37786
  }
37051
37787
  return result;