@delali/narsil 0.3.1-next.107 → 0.3.1-next.111

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/dist/adapters/filesystem.mjs +1 -1
  2. package/dist/adapters/indexeddb.mjs +1 -1
  3. package/dist/browser/{chunk-JJKVYLL3.mjs → chunk-2CBQ2PCC.mjs} +1 -1
  4. package/dist/browser/{chunk-PA56PRWX.mjs → chunk-5BQPBG3G.mjs} +41 -4
  5. package/dist/browser/{chunk-NNQPZPTB.mjs → chunk-6YUUEMQ4.mjs} +139 -28
  6. package/dist/browser/{chunk-WZUSMF7A.mjs → chunk-HQOYKRMB.mjs} +14 -13
  7. package/dist/browser/{chunk-J3QB4LBD.mjs → chunk-LJS6PREA.mjs} +2 -0
  8. package/dist/browser/{chunk-OKZGAJEF.mjs → chunk-OVY4RPDS.mjs} +3 -3
  9. package/dist/browser/{chunk-OL7S2KLL.mjs → chunk-P7C2S5JX.mjs} +2 -2
  10. package/dist/browser/{durability-wiring-HFQULBSA.mjs → durability-wiring-CYAJBYCO.mjs} +48 -20
  11. package/dist/browser/{module-VROKZ7IZ.mjs → module-DYKR4INA.mjs} +5 -5
  12. package/dist/browser/{restore-queue-EU77FRCV.mjs → restore-queue-RTZGJT4D.mjs} +1 -1
  13. package/dist/browser/{runtime-JQYPNOK6.mjs → runtime-FL5K4JOH.mjs} +1 -1
  14. package/dist/browser/{snapshot-G77LQBZX.mjs → snapshot-BU6I25CW.mjs} +27 -14
  15. package/dist/browser/{write-RUC64JIH.mjs → write-RUPYHPBZ.mjs} +5 -5
  16. package/dist/{capabilities-ByKPha_q.d.ts → capabilities-ByaX8Arf.d.ts} +2 -2
  17. package/dist/{chunk-WCNN2PLX.mjs → chunk-2RAQKITH.mjs} +1 -1
  18. package/dist/{chunk-JSSGO4B5.mjs → chunk-542TD4BL.mjs} +1 -1
  19. package/dist/{chunk-KOCHOOS5.mjs → chunk-63HFWQEA.mjs} +5 -3
  20. package/dist/{chunk-77SO7G55.mjs → chunk-6ODIY6ZB.mjs} +1 -1
  21. package/dist/{chunk-VVDQNCOG.mjs → chunk-6OXNHLNZ.mjs} +1 -0
  22. package/dist/{chunk-CYHQNM3T.mjs → chunk-6PN3U6XW.mjs} +1 -1
  23. package/dist/{chunk-WCJJCBR4.mjs → chunk-6S7YRUPQ.mjs} +123 -35
  24. package/dist/{chunk-FEOLYA2Y.mjs → chunk-7JOONQ24.mjs} +6 -6
  25. package/dist/{chunk-MZW4V4DS.mjs → chunk-7ZZQNOIY.mjs} +1 -1
  26. package/dist/{chunk-VWFFCDOL.mjs → chunk-CUVTRLA4.mjs} +26 -13
  27. package/dist/{chunk-VVFKFHWE.mjs → chunk-DG62H7OD.mjs} +2 -2
  28. package/dist/{chunk-QALAFGF5.mjs → chunk-FUUP45IS.mjs} +1 -1
  29. package/dist/{chunk-2MNJ2HX7.mjs → chunk-HNAP2KTG.mjs} +2 -2
  30. package/dist/{chunk-RI5VNFAI.mjs → chunk-IR4KQ4WB.mjs} +2 -2
  31. package/dist/{chunk-CKNOGER5.mjs → chunk-IW6UR3AF.mjs} +23 -22
  32. package/dist/{chunk-FABI2JA7.mjs → chunk-J6YVB2XM.mjs} +1 -1
  33. package/dist/{chunk-UM6Z7AIP.mjs → chunk-JBPYFYIU.mjs} +4 -3
  34. package/dist/{chunk-WXVTQ6NO.mjs → chunk-KCQG2PJT.mjs} +3 -3
  35. package/dist/{chunk-OKZPYJZX.mjs → chunk-LOBYK2FG.mjs} +3 -3
  36. package/dist/{chunk-3BRFOMGX.mjs → chunk-N57XXGPG.mjs} +2 -2
  37. package/dist/{chunk-6JUOMMHQ.mjs → chunk-OBWUWZRA.mjs} +2 -0
  38. package/dist/{chunk-VHPVBJOI.mjs → chunk-OCB7TMUQ.mjs} +1 -1
  39. package/dist/{chunk-AEF2O7B4.mjs → chunk-OFYKBKKV.mjs} +1 -1
  40. package/dist/{chunk-IZLM2OOO.mjs → chunk-PKE2PCTS.mjs} +43 -6
  41. package/dist/{chunk-HNAQNAFS.mjs → chunk-PT7VRWS7.mjs} +3 -3
  42. package/dist/{chunk-DUWYU4D5.mjs → chunk-PWIWSUCX.mjs} +2 -2
  43. package/dist/{chunk-IA6I5U36.mjs → chunk-QI6JW4PM.mjs} +1 -1
  44. package/dist/{chunk-MJIJG4YT.mjs → chunk-QIFOXLWF.mjs} +2 -2
  45. package/dist/{chunk-M5EZVXIM.mjs → chunk-R2MRGYU5.mjs} +2 -2
  46. package/dist/{chunk-7IBX2LRA.mjs → chunk-R2MRMH6M.mjs} +11 -11
  47. package/dist/{chunk-5FPMDU54.mjs → chunk-SOADQINJ.mjs} +8 -1
  48. package/dist/{chunk-QA6F5MML.mjs → chunk-SXVDBHYJ.mjs} +39 -8
  49. package/dist/{chunk-NRARRUKN.mjs → chunk-UGKO3RG2.mjs} +7 -7
  50. package/dist/{chunk-HAQUJYMN.mjs → chunk-V7G627TA.mjs} +14 -8
  51. package/dist/{chunk-BRIOLMIX.mjs → chunk-VK45J47R.mjs} +1 -1
  52. package/dist/{chunk-RPIE4QCL.mjs → chunk-YH5OIL7B.mjs} +2 -0
  53. package/dist/{chunk-G6YATIRT.mjs → chunk-YIDSHBQS.mjs} +2 -2
  54. package/dist/{chunk-IA3H54X4.mjs → chunk-YLE75NQ5.mjs} +2 -2
  55. package/dist/{chunk-DNKM67DF.mjs → chunk-YSR5L3XJ.mjs} +2 -2
  56. package/dist/{client-DI-xIXgR.d.ts → client-BdnzDFtO.d.ts} +2 -2
  57. package/dist/client.d.ts +6 -6
  58. package/dist/client.mjs +2 -0
  59. package/dist/{config-DL_bDDD8.d.ts → config-CSgvApt0.d.ts} +1 -1
  60. package/dist/{config-BZ_e3X8w.d.ts → config-ujZESkJz.d.ts} +1 -1
  61. package/dist/{direct-executor-BPBD2PBK.mjs → direct-executor-QFAUKTCQ.mjs} +12 -12
  62. package/dist/distribution/coordinator/etcd.mjs +94 -5
  63. package/dist/distribution/transport/grpc.mjs +24 -8
  64. package/dist/distribution/transport/in-memory.mjs +4 -4
  65. package/dist/distribution/transport/tcp.mjs +6 -6
  66. package/dist/distribution.d.ts +5 -5
  67. package/dist/distribution.mjs +1800 -468
  68. package/dist/{durability-wiring-AZL4EFLO.mjs → durability-wiring-NUHTYXHA.mjs} +66 -39
  69. package/dist/embeddings/openai.mjs +2 -2
  70. package/dist/{engine-D_SYDsId.d.ts → engine-C_HSy3iz.d.ts} +1 -1
  71. package/dist/{events-C7_dboUr.d.ts → events-CJsXF1F9.d.ts} +2 -0
  72. package/dist/index.browser.mjs +3 -3
  73. package/dist/index.d.ts +5 -5
  74. package/dist/index.mjs +17 -17
  75. package/dist/{module-6EY7GW2W.mjs → module-WP4VVGV2.mjs} +9 -9
  76. package/dist/partial-words.mjs +5 -5
  77. package/dist/pattern.mjs +3 -3
  78. package/dist/persistence/durability/checkpoint-worker.d.ts +12 -0
  79. package/dist/persistence/durability/checkpoint-worker.mjs +26 -24
  80. package/dist/react.d.ts +6 -6
  81. package/dist/react.mjs +2 -0
  82. package/dist/{rerank-WVFFZV76.mjs → rerank-3POMPQNX.mjs} +7 -7
  83. package/dist/rerankers/http.mjs +2 -2
  84. package/dist/{restore-queue-DDXFDP7U.mjs → restore-queue-QSLGXZJB.mjs} +1 -1
  85. package/dist/{runtime-AVUUCJWQ.mjs → runtime-C2435MPK.mjs} +5 -5
  86. package/dist/server.d.ts +8 -8
  87. package/dist/server.mjs +16 -15
  88. package/dist/{snapshot-IDAM65JO.mjs → snapshot-IV3S2TMI.mjs} +27 -27
  89. package/dist/synonyms.mjs +3 -3
  90. package/dist/{tasks-BhjQZ8Qf.d.ts → tasks-CI3Fvkxw.d.ts} +1 -1
  91. package/dist/{thread-LMSYLCPI.mjs → thread-THLCKMEB.mjs} +13 -13
  92. package/dist/vector/search-worker.mjs +4 -4
  93. package/dist/workers/entry.mjs +6 -6
  94. package/dist/{write-3LIFIGOL.mjs → write-VGRTOS7N.mjs} +19 -19
  95. package/package.json +7 -7
@@ -1,60 +1,60 @@
1
- import { DEFAULT_NODE_LIFECYCLE_CONFIG, bindWriteBlockLookup, bindEngineWriteBlock, bindIndexReads, DEFAULT_CONTROLLER_CONFIG, getIndexMetadata, getClusterIndexConfig, withPartitionCount, validateIndexName, readIndexState, readIndexMetadataSnapshot, storeSynonymSet, deleteSynonymSet, indexSettingsOf, putIndexMetadata, isValidInteger, isRecord, HIDDEN_GENERATION_PREFIX, indexConfigFromSettings, metadataWithSynonymSet, indexConfigKey, RESHARD_RECORD_CAS_ATTEMPTS, indexUuidOfStoredMetadata, RESHARD_FORWARD_BATCH_DOCUMENTS, storedSynonymSetOf, RESHARD_RETRY_DELAY_MS, RESHARD_RECORD_CHECK_INTERVAL_MS, RESHARD_POLL_INTERVAL_MS, ALLOCATION_RETRY_DELAY_MS, TEARDOWN_CAS_ATTEMPTS, TEARDOWN_RETRY_ROUNDS, putIndexReplicationFactor, ALLOCATION_CAS_ATTEMPTS, TEARDOWN_RETRY_DELAY_MS, RESHARD_CONTROLLER_POLL_INTERVAL_MS, MAX_RESHARD_CUTOVER_ATTEMPTS, RECOVERY_CAS_ATTEMPTS, ESTIMATED_PARTITION_BYTES, REBALANCE_THRESHOLD, putGenerationMetadata, RESHARD_CUTOVER_LAG_ENTRIES, RESHARD_CUTOVER_TIMEOUT_MS, PARTITION_STORES_TIMEOUT_MS, LEADERSHIP_IMBALANCE_THRESHOLD, BOOTSTRAP_CAS_ATTEMPTS } from './chunk-OKZPYJZX.mjs';
2
- import { MAX_WAIT_FOR_ACTIVE_REPLICAS, MIN_CONTROLLER_LEASE_TTL_MS, MAX_NODE_LEFT_DELAY_MS, DEFAULT_MAX_PER_SOURCE_SNAPSHOTS, DEFAULT_MAX_STREAMS_PER_INDEX, DEFAULT_CAPACITY, CATCH_UP_TICK_MS, DEFAULT_RESUME_WRITES_BELOW, DEFAULT_BLOCK_WRITES_ABOVE, readMemoryFigures, createWritePressure, WRITE_MEMORY_SAMPLE_MAX_AGE_MS, DEFAULT_IN_FLIGHT_WRITE_FRACTION, onRefusalAfterCollection, MAX_DROP_ACKNOWLEDGE_WAIT_MS, DEFAULT_PARTITION_COUNT, DEFAULT_REPLICATION_FACTOR, DEFAULT_CREATE_INDEX_WAIT_MS, refuseEncodeWithoutRoom, readRecentMemoryFigures, DEFAULT_BOOTSTRAP_SYNC_DEADLINE_MS, MAX_SYNONYM_REPLACEMENT_ATTEMPTS, DROP_ACKNOWLEDGE_POLL_INTERVAL_MS, COORDINATOR_REQUEST_BUCKET_BOUNDS_SECONDS, COPY_FAILURE_ATTEMPTS, DECOMMISSION_RECHECK_INTERVAL_MS, withAdmittedWrite, estimateForwardedBytes, CASE_FOLD_EXPANSION_FACTOR, PATTERN_RUN_HEAP_BYTES, ENUM_VALUE_HEAP_BYTES, MAX_SOURCE_ID_LENGTH, reserveHeapBytes, reservedHeapBytes, DOCUMENT_VALUE_HEAP_BYTES, DOCUMENT_HEAP_BYTES_PER_ESTIMATED_BYTE, DOCUMENT_PROPERTY_HEAP_BYTES, VECTOR_DOCUMENT_HEAP_BYTES, VECTOR_COMPONENT_HEAP_BYTES, NORMALIZATION_EXPANSION_FACTOR, TERM_UNIT_HEAP_BYTES, PARTITION_BYTES_INDEX_OVERHEAD_BYTES, DISTINCT_TERM_HEAP_BYTES, PARTIAL_SPELLING_HEAP_BYTES, PARTITION_BYTES_ENTRY_BYTES, PARTITION_BYTES_REPORT_BUDGET_BYTES, SNAPSHOT_CHUNK_YIELD_INTERVAL_MS, CATCH_UP_FEED_WINDOW_MS, MAX_CATCH_UP_IN_FLIGHT_BYTES, SYNCED_POSITION_REPORT_TIMEOUT_MS, ADMISSION_TIMEOUT_MS, REGISTRATION_LEASE_DRIFT_SHARE, REGISTRATION_LEASE_CONFIRM_TIMEOUT_MS, WRITE_APPLY_YIELD_INTERVAL_MS, ROLLBACK_RETRY_INITIAL_DELAY_MS, ROLLBACK_RETRY_DELAY_CAP_MS, COPY_FAILURE_RETRY_DELAY_MS, FETCH_GROUP_DOCUMENT_IDS, collectYoungGenerationBeforeRefusal, collectGarbageBeforeRefusal, FULL_MEMORY_COLLECTION_INTERVAL_MS, MEMORY_SAMPLE_INTERVAL_MS, answerRefusalOf, ANSWER_DECODE_HEAP_BYTES_PER_PAYLOAD_BYTE, CLEAR_PAGE_SIZE, estimateDocumentsBytes, estimateDocumentBytes, PRIMARY_CHANGE_POLL_INTERVAL_MS, PARTITION_WRITE_CONCURRENCY, FORWARD_RETRY_INITIAL_DELAY_MS, PRIMARY_CHANGE_RETRY_WINDOW_MS, FORWARD_RETRY_DELAY_CAP_MS } from './chunk-HAQUJYMN.mjs';
3
- export { DEFAULT_CAPACITY, DEFAULT_PARTITION_COUNT, DEFAULT_REPLICATION_FACTOR } from './chunk-HAQUJYMN.mjs';
4
- import { fixedHybridDepthOf, requireWithinHybridDepth, pageByPosition } from './chunk-WCNN2PLX.mjs';
5
- import { closePartitionWriter, replaySyncedWal } from './chunk-UM6Z7AIP.mjs';
6
- import { buildArchiveFromMemory, archiveStream, memoryCheckpointStore, readSnapshotArchive } from './chunk-VWFFCDOL.mjs';
7
- import './chunk-HNAQNAFS.mjs';
8
- import { indexMetadataOf, loadPartitionSegments, readSegmentManifest, readVectorFile } from './chunk-QA6F5MML.mjs';
9
- import { decodeVectorFilePayload, bytesToVectors, isDead } from './chunk-VHPVBJOI.mjs';
1
+ import { DEFAULT_NODE_LIFECYCLE_CONFIG, bindWriteBlockLookup, bindEngineWriteBlock, bindIndexReads, DEFAULT_CONTROLLER_CONFIG, getIndexMetadata, getClusterIndexConfig, withPartitionCount, validateIndexName, readIndexState, readIndexMetadataSnapshot, storeSynonymSet, deleteSynonymSet, indexSettingsOf, putIndexMetadata, isValidInteger, isRecord, HIDDEN_GENERATION_PREFIX, indexConfigFromSettings, metadataWithSynonymSet, indexConfigKey, RESHARD_RECORD_CAS_ATTEMPTS, indexUuidOfStoredMetadata, RESHARD_FORWARD_BATCH_DOCUMENTS, storedSynonymSetOf, RESHARD_RETRY_DELAY_MS, RESHARD_RECORD_CHECK_INTERVAL_MS, RESHARD_POLL_INTERVAL_MS, ALLOCATION_RETRY_DELAY_MS, TEARDOWN_CAS_ATTEMPTS, TEARDOWN_RETRY_ROUNDS, putIndexReplicationFactor, ALLOCATION_CAS_ATTEMPTS, TEARDOWN_RETRY_DELAY_MS, RESHARD_CONTROLLER_POLL_INTERVAL_MS, MAX_RESHARD_CUTOVER_ATTEMPTS, RECOVERY_CAS_ATTEMPTS, ESTIMATED_PARTITION_BYTES, REBALANCE_THRESHOLD, putGenerationMetadata, RESHARD_CUTOVER_LAG_ENTRIES, RESHARD_CUTOVER_TIMEOUT_MS, PARTITION_STORES_TIMEOUT_MS, LEADERSHIP_IMBALANCE_THRESHOLD, BOOTSTRAP_CAS_ATTEMPTS } from './chunk-LOBYK2FG.mjs';
2
+ import { MAX_WAIT_FOR_ACTIVE_REPLICAS, MIN_CONTROLLER_LEASE_TTL_MS, MAX_NODE_LEFT_DELAY_MS, DEFAULT_MAX_PER_SOURCE_SNAPSHOTS, DEFAULT_MAX_STREAMS_PER_INDEX, DEFAULT_CAPACITY, CATCH_UP_TICK_MS, DEFAULT_RESUME_WRITES_BELOW, DEFAULT_BLOCK_WRITES_ABOVE, readMemoryFigures, createWritePressure, WRITE_MEMORY_SAMPLE_MAX_AGE_MS, DEFAULT_IN_FLIGHT_WRITE_FRACTION, onRefusalAfterCollection, MAX_DROP_ACKNOWLEDGE_WAIT_MS, DEFAULT_PARTITION_COUNT, DEFAULT_REPLICATION_FACTOR, DEFAULT_CREATE_INDEX_WAIT_MS, refuseEncodeWithoutRoom, readRecentMemoryFigures, DEFAULT_BOOTSTRAP_SYNC_DEADLINE_MS, MAX_SYNONYM_REPLACEMENT_ATTEMPTS, DROP_ACKNOWLEDGE_POLL_INTERVAL_MS, COORDINATOR_REQUEST_BUCKET_BOUNDS_SECONDS, COPY_FAILURE_ATTEMPTS, SEARCH_CONTEXT_REAPER_INTERVAL_MS, DECOMMISSION_RECHECK_INTERVAL_MS, answerRefusalOf, withAdmittedWrite, estimateForwardedBytes, CASE_FOLD_EXPANSION_FACTOR, PATTERN_RUN_HEAP_BYTES, ENUM_VALUE_HEAP_BYTES, MAX_SOURCE_ID_LENGTH, reserveHeapBytes, reservedHeapBytes, DOCUMENT_VALUE_HEAP_BYTES, DOCUMENT_HEAP_BYTES_PER_ESTIMATED_BYTE, DOCUMENT_PROPERTY_HEAP_BYTES, VECTOR_DOCUMENT_HEAP_BYTES, VECTOR_COMPONENT_HEAP_BYTES, NORMALIZATION_EXPANSION_FACTOR, TERM_UNIT_HEAP_BYTES, PARTITION_BYTES_INDEX_OVERHEAD_BYTES, DISTINCT_TERM_HEAP_BYTES, PARTIAL_SPELLING_HEAP_BYTES, PARTITION_BYTES_ENTRY_BYTES, PARTITION_BYTES_REPORT_BUDGET_BYTES, SNAPSHOT_CHUNK_YIELD_INTERVAL_MS, CATCH_UP_FEED_WINDOW_MS, MAX_CATCH_UP_IN_FLIGHT_BYTES, SYNCED_POSITION_REPORT_TIMEOUT_MS, ADMISSION_TIMEOUT_MS, REGISTRATION_LEASE_DRIFT_SHARE, REGISTRATION_LEASE_CONFIRM_TIMEOUT_MS, WRITE_APPLY_YIELD_INTERVAL_MS, ROLLBACK_RETRY_INITIAL_DELAY_MS, ROLLBACK_RETRY_DELAY_CAP_MS, COPY_FAILURE_RETRY_DELAY_MS, FETCH_GROUP_DOCUMENT_IDS, SEARCH_CONTEXT_ENTRY_HEAP_BYTES, collectYoungGenerationBeforeRefusal, collectGarbageBeforeRefusal, FULL_MEMORY_COLLECTION_INTERVAL_MS, MEMORY_SAMPLE_INTERVAL_MS, ANSWER_DECODE_HEAP_BYTES_PER_PAYLOAD_BYTE, CLEAR_PAGE_SIZE, estimateDocumentsBytes, estimateDocumentBytes, PRIMARY_CHANGE_RETRY_WINDOW_MS, PRIMARY_CHANGE_POLL_INTERVAL_MS, PARTITION_WRITE_CONCURRENCY, FORWARD_RETRY_INITIAL_DELAY_MS, FORWARD_RETRY_DELAY_CAP_MS } from './chunk-V7G627TA.mjs';
3
+ export { DEFAULT_CAPACITY, DEFAULT_PARTITION_COUNT, DEFAULT_REPLICATION_FACTOR } from './chunk-V7G627TA.mjs';
4
+ import { fixedHybridDepthOf, requireWithinHybridDepth, pageByPosition } from './chunk-2RAQKITH.mjs';
5
+ import { closePartitionWriter, replaySyncedWal } from './chunk-JBPYFYIU.mjs';
6
+ import { buildArchiveFromMemory, archiveStream, memoryCheckpointStore, readSnapshotArchive } from './chunk-CUVTRLA4.mjs';
7
+ import './chunk-PT7VRWS7.mjs';
8
+ import { indexMetadataOf, loadPartitionSegments, readSegmentManifest, readVectorFile } from './chunk-SXVDBHYJ.mjs';
9
+ import { decodeVectorFilePayload, bytesToVectors, isDead } from './chunk-OCB7TMUQ.mjs';
10
10
  import { CONTROLLER_LEASE_KEY } from './chunk-44W3CBNG.mjs';
11
11
  import { MAX_MESSAGE_SIZE_BYTES } from './chunk-UNEEFTEU.mjs';
12
- import './chunk-G6YATIRT.mjs';
12
+ import './chunk-YIDSHBQS.mjs';
13
13
  import './chunk-DJHHF2L5.mjs';
14
14
  import './chunk-37CD3OKS.mjs';
15
15
  import { getPath, getFs } from './chunk-HLPRHOSO.mjs';
16
- import { createForwardBatchResultMessage, validateEntryBatchPayload, createAckMessage, validateEntryPayload, validateForwardBatchPayload, readListedFile, optionalIndexUuid, computeEntryChecksum, createPrimaryResignMessage, validateInsyncConfirmPayload, buildEntry, createInsyncRemoveMessage, createEntryBatchMessage, createInsyncConfirmMessage, createEntryMessage, validateAckPayload, createInsyncAddMessage, directoryCheckpointStore, checkpointLastSeqNo, checkpointPrimaryTerm, createForwardBatchMessage, validateForwardBatchResultPayload, createForwardMessage } from './chunk-WCJJCBR4.mjs';
17
- import { applyIndexEntry, applyDeleteEntry, validateReplicationEntry } from './chunk-IZLM2OOO.mjs';
16
+ import { createForwardBatchResultMessage, validateEntryBatchPayload, createAckMessage, validateEntryPayload, validateForwardBatchPayload, readListedFile, optionalIndexUuid, computeEntryChecksum, createPrimaryResignMessage, validateInsyncConfirmPayload, buildEntry, createInsyncRemoveMessage, createEntryBatchMessage, createInsyncConfirmMessage, createEntryMessage, validateAckPayload, createInsyncAddMessage, directoryCheckpointStore, checkpointLastSeqNo, checkpointPrimaryTerm, createForwardBatchMessage, validateForwardBatchResultPayload, createForwardMessage } from './chunk-6S7YRUPQ.mjs';
17
+ import { applyIndexEntry, applyDeleteEntry, validateReplicationEntry } from './chunk-PKE2PCTS.mjs';
18
18
  import { MAX_PARTITION_COUNT, MAX_REPLICATION_FACTOR, WIRE_BATCH_BUDGET } from './chunk-P36MUFEJ.mjs';
19
- import { TransportErrorCodes, ReplicationMessageTypes, ClusterMessageTypes, QueryMessageTypes, TransportError, DEFAULT_TRANSPORT_CONFIG } from './chunk-RPIE4QCL.mjs';
20
- import { requireKnownConfig, resolveRunnableConfig, createEngineCore, startEngineCore, createNarsilFromCore, runEngineQueryStats, runEngineListDocuments, runEngineSuggest, runEnginePreflight, runEngineQuery, createEngineIndex, encodeIndexConfiguration, requireConfigurationWithinSizeLimit, prepareConfiguredSynonyms, createByteReader, openChecksummedEnvelope, emptySerializablePartition, crc32Init, crc32Final, crc32Update, yieldToEventLoop } from './chunk-CKNOGER5.mjs';
21
- import { runtimeHost, requireSurfaceFormsForPartialFields, queryBindingOf, now, listBindingOf, hitsKeptPerGroup, clampLimit, resolveHybridFusion, dedupePinnedEntries, foldGroupReducer } from './chunk-NRARRUKN.mjs';
22
- import { bindRerankWindowSource, rerankWindowCapOf, engineCoreOf, providedDocId } from './chunk-3BRFOMGX.mjs';
23
- import './chunk-CYHQNM3T.mjs';
19
+ import { TransportErrorCodes, ReplicationMessageTypes, ClusterMessageTypes, QueryMessageTypes, TransportError, DEFAULT_TRANSPORT_CONFIG } from './chunk-YH5OIL7B.mjs';
20
+ import { requireKnownConfig, resolveRunnableConfig, createEngineCore, startEngineCore, createNarsilFromCore, runEngineQueryStats, runEngineListDocuments, runEngineSuggest, runEnginePreflight, runEngineQuery, createEngineIndex, encodeIndexConfiguration, requireConfigurationWithinSizeLimit, prepareConfiguredSynonyms, createByteReader, openChecksummedEnvelope, emptySerializablePartition, crc32Init, crc32Final, crc32Update, yieldToEventLoop } from './chunk-IW6UR3AF.mjs';
21
+ import { runtimeHost, requireSurfaceFormsForPartialFields, now, listBindingOf, clampLimit, hitsKeptPerGroup, queryBindingOf, resolveHybridFusion, dedupePinnedEntries, foldGroupReducer } from './chunk-UGKO3RG2.mjs';
22
+ import { bindRerankWindowSource, rerankWindowCapOf, providedDocId, engineCoreOf } from './chunk-N57XXGPG.mjs';
23
+ import './chunk-6PN3U6XW.mjs';
24
24
  import './chunk-AV4IJKD2.mjs';
25
- import { synonymModule } from './chunk-7IBX2LRA.mjs';
26
- import { oversampledShardSize, normalizeSort, sortModesOf, readSortValues, resolveProjection, applyProjection, fnv1a, FILTER_EXPRESSION_KEYS, filterTestCount, FIELD_FILTER_OPERATORS, clampRowCount, sortSignatureOf, requireMatchingCursor, decodePageCursor, encodePageCursor, requireWithinResultWindow, sortSignatureEntry, fnv1aUint32s, mergePartitionStats } from './chunk-FEOLYA2Y.mjs';
27
- import { generateId, getLanguage } from './chunk-QALAFGF5.mjs';
28
- import './chunk-5FPMDU54.mjs';
25
+ import { synonymModule } from './chunk-R2MRMH6M.mjs';
26
+ import { oversampledShardSize, normalizeSort, sortModesOf, readSortValues, resolveProjection, fnv1a, applyProjection, FILTER_EXPRESSION_KEYS, filterTestCount, setNestedValue, FIELD_FILTER_OPERATORS, clampRowCount, sortSignatureOf, requireMatchingCursor, decodePageCursor, encodePageCursor, requireWithinResultWindow, sortSignatureEntry, fnv1aUint32s, mergePartitionStats } from './chunk-7JOONQ24.mjs';
27
+ import { generateId, getLanguage } from './chunk-FUUP45IS.mjs';
28
+ import './chunk-SOADQINJ.mjs';
29
29
  import './chunk-HJSN7UYG.mjs';
30
30
  import { MAX_DOC_ID_LENGTH, BATCH_CHUNK_SIZE, DEFAULT_SUGGEST_LIMIT, MAX_SUGGEST_LIMIT, MAX_SUGGEST_SCATTER_LIMIT, SUGGEST_OVERSAMPLE_FACTOR, SUGGEST_OVERSAMPLE_PADDING } from './chunk-LXWDQLTW.mjs';
31
31
  import './chunk-TGP5M5R7.mjs';
32
32
  import './chunk-U2BKYEC7.mjs';
33
33
  import './chunk-KOKM4PQN.mjs';
34
34
  import { MIN_OVERSAMPLE } from './chunk-QSK35UC2.mjs';
35
- import './chunk-BRIOLMIX.mjs';
36
- import { isPatternTolerance } from './chunk-2MNJ2HX7.mjs';
35
+ import './chunk-VK45J47R.mjs';
36
+ import { isPatternTolerance } from './chunk-HNAP2KTG.mjs';
37
37
  import { MAX_FILTER_DEPTH, MAX_FILTER_TESTS, MAX_FILTER_ARRAY_SIZE } from './chunk-WQBCBKY3.mjs';
38
- import { getNestedValue } from './chunk-WXVTQ6NO.mjs';
39
- import { validateSchema, requireCreatableSchema, encodeDocumentReply, encodeDocument, schemaFieldsOf, MIN_POLYGON_POINTS, isLatitude, MAX_LATITUDE_DEGREES, isLongitude, MAX_LONGITUDE_DEGREES } from './chunk-VVFKFHWE.mjs';
38
+ import { getNestedValue } from './chunk-KCQG2PJT.mjs';
39
+ import { encodeDocument, validateSchema, requireCreatableSchema, encodeDocumentReply, schemaFieldsOf, MIN_POLYGON_POINTS, isLatitude, MAX_LATITUDE_DEGREES, isLongitude, MAX_LONGITUDE_DEGREES } from './chunk-DG62H7OD.mjs';
40
40
  import { MAX_DOCUMENT_ENCODING_DEPTH } from './chunk-U476IXKQ.mjs';
41
41
  import { MAX_SORT_FIELDS, MAX_CURSOR_LENGTH, MAX_TOLERANCE, MAX_PREFIX_LENGTH, RESULT_WINDOW, MIN_HYBRID_DEPTH, MAX_HYBRID_DEPTH, DEFAULT_PAGE_SIZE, HYBRID_RESCORE_WINDOW } from './chunk-E74YLAIG.mjs';
42
42
  import './chunk-VJS4JFBQ.mjs';
43
43
  import './chunk-GES7U2PM.mjs';
44
- import './chunk-77SO7G55.mjs';
45
- import { resolveIndexAnalysis } from './chunk-RI5VNFAI.mjs';
44
+ import './chunk-6ODIY6ZB.mjs';
45
+ import { resolveIndexAnalysis } from './chunk-IR4KQ4WB.mjs';
46
46
  import './chunk-X5CV2E2B.mjs';
47
47
  import './chunk-7X5JST5L.mjs';
48
- import './chunk-AEF2O7B4.mjs';
48
+ import './chunk-OFYKBKKV.mjs';
49
49
  import { isSortMode, SORT_MODES, toComparableSortValue, compareComparableKeys, compareSortValues } from './chunk-R6A773WK.mjs';
50
50
  import { compareCodePoints, exceedsCodePoints } from './chunk-YTJCGKM4.mjs';
51
- import { collectAndReturnPages } from './chunk-VVDQNCOG.mjs';
51
+ import { collectAndReturnPages } from './chunk-6OXNHLNZ.mjs';
52
52
  import './chunk-CQUMNIIG.mjs';
53
- import './chunk-IA6I5U36.mjs';
53
+ import './chunk-QI6JW4PM.mjs';
54
54
  import { HEAP_RESUME_FRACTION_BELOW_V8_INEFFECTIVE_MARK_COMPACT_LINE, HEAP_BLOCK_FRACTION_BELOW_V8_INEFFECTIVE_MARK_COMPACT_LINE } from './chunk-ZXZ26WSO.mjs';
55
55
  import './chunk-RFBLQLLZ.mjs';
56
56
  import { MAX_STORED_VECTOR_DIMENSION, MAX_NESTING_DEPTH } from './chunk-L5ORED7V.mjs';
57
- import { ErrorCodes, NarsilError, describeError } from './chunk-6JUOMMHQ.mjs';
57
+ import { ErrorCodes, NarsilError, describeError, ServerErrorCodes } from './chunk-OBWUWZRA.mjs';
58
58
  import { encode, decode } from '@msgpack/msgpack';
59
59
  import { randomUUID } from 'crypto';
60
60
  import { openSync, writeSync, closeSync, rmSync, readSync } from 'fs';
@@ -456,6 +456,9 @@ function processAllocationChange(state, config, table) {
456
456
  if (config.onHoldPartition !== void 0 && alreadyHoldsData(assignment, nodeId)) {
457
457
  config.onHoldPartition(table.indexName, partitionId);
458
458
  }
459
+ if (isPrimary && assignment.state === "ACTIVE") {
460
+ config.onLeadingTerm?.(table.indexName, partitionId, assignment);
461
+ }
459
462
  }
460
463
  removeDroppedPartitions(state, config, table, currentKeys);
461
464
  releaseSupersededCopies(state, config, table, currentKeys);
@@ -2897,6 +2900,8 @@ function createCatchUpState() {
2897
2900
  pendingAdmissions: /* @__PURE__ */ new Map(),
2898
2901
  admittedReplicas: /* @__PURE__ */ new Map(),
2899
2902
  acknowledgedSeqNos: /* @__PURE__ */ new Map(),
2903
+ removedReplicas: /* @__PURE__ */ new Map(),
2904
+ startedTerms: /* @__PURE__ */ new Map(),
2900
2905
  inFlightBytes: 0,
2901
2906
  timer: null,
2902
2907
  activeTick: null,
@@ -2977,8 +2982,20 @@ function markPendingAdmission(state, indexName, partitionId, replicaNodeId) {
2977
2982
  }
2978
2983
  pending.add(replicaNodeId);
2979
2984
  state.pendingAdmissions.set(key, pending);
2985
+ state.removedReplicas.get(key)?.delete(replicaNodeId);
2980
2986
  return true;
2981
2987
  }
2988
+ function recordRemovedReplica(state, indexName, partitionId, replicaNodeId, primaryTerm) {
2989
+ const key = partitionKey2(indexName, partitionId);
2990
+ const removed = state.removedReplicas.get(key) ?? /* @__PURE__ */ new Map();
2991
+ removed.set(replicaNodeId, primaryTerm);
2992
+ state.removedReplicas.set(key, removed);
2993
+ }
2994
+ function withoutRemovedReplicas(state, indexName, partitionId, primaryTerm, targets) {
2995
+ const removed = state.removedReplicas.get(partitionKey2(indexName, partitionId));
2996
+ if (removed === void 0) return targets;
2997
+ return targets.filter((replicaNodeId) => removed.get(replicaNodeId) !== primaryTerm);
2998
+ }
2982
2999
  function clearPendingAdmission(state, indexName, partitionId, replicaNodeId) {
2983
3000
  const key = partitionKey2(indexName, partitionId);
2984
3001
  const pending = state.pendingAdmissions.get(key);
@@ -3113,18 +3130,29 @@ function chunkByBudget(items, budget) {
3113
3130
  }
3114
3131
  return chunks;
3115
3132
  }
3116
- async function replicateToReplicas(entry, inSyncReplicas, transport, sourceNodeId, resolveNodeTargets3, indexUuid) {
3117
- const message = createEntryMessage(entry, sourceNodeId, indexUuid);
3133
+ function previousTermOf(log, seqNo) {
3134
+ return seqNo <= 1 ? 0 : log.termAt(seqNo - 1);
3135
+ }
3136
+ async function replicateToReplicas(entry, inSyncReplicas, transport, sourceNodeId, resolveNodeTargets3, indexUuid, previousPrimaryTerm) {
3137
+ const message = createEntryMessage(entry, sourceNodeId, indexUuid, previousPrimaryTerm);
3118
3138
  return sendAndCollectAcks(message, entry, inSyncReplicas, transport, resolveNodeTargets3);
3119
3139
  }
3120
- async function replicateBatchToReplicas(entries, inSyncReplicas, transport, sourceNodeId, resolveNodeTargets3, indexUuid) {
3140
+ async function replicateBatchToReplicas(entries, inSyncReplicas, transport, sourceNodeId, resolveNodeTargets3, indexUuid, previousPrimaryTerm) {
3121
3141
  if (entries.length === 0) {
3122
3142
  return { acknowledged: [], failed: [] };
3123
3143
  }
3124
3144
  if (entries.length === 1) {
3125
- return replicateToReplicas(entries[0], inSyncReplicas, transport, sourceNodeId, resolveNodeTargets3, indexUuid);
3145
+ return replicateToReplicas(
3146
+ entries[0],
3147
+ inSyncReplicas,
3148
+ transport,
3149
+ sourceNodeId,
3150
+ resolveNodeTargets3,
3151
+ indexUuid,
3152
+ previousPrimaryTerm
3153
+ );
3126
3154
  }
3127
- const message = createEntryBatchMessage(entries, sourceNodeId, indexUuid);
3155
+ const message = createEntryBatchMessage(entries, sourceNodeId, indexUuid, previousPrimaryTerm);
3128
3156
  const lastEntry = entries[entries.length - 1];
3129
3157
  return sendAndCollectAcks(message, lastEntry, inSyncReplicas, transport, resolveNodeTargets3);
3130
3158
  }
@@ -3333,7 +3361,8 @@ async function sendBatch(state, deps, batch, replicaNodeId, cursor, feed) {
3333
3361
  const sendingEpoch = cursor.syncEpoch;
3334
3362
  state.inFlightBytes += reservedBytes;
3335
3363
  const delivery = (async () => {
3336
- const indexName = batch[0]?.indexName;
3364
+ const first = batch[0];
3365
+ const indexName = first?.indexName;
3337
3366
  const table = indexName === void 0 ? null : await deps.coordinator.getAllocation(indexName);
3338
3367
  if (feed.cancelled) {
3339
3368
  return false;
@@ -3344,7 +3373,8 @@ async function sendBatch(state, deps, batch, replicaNodeId, cursor, feed) {
3344
3373
  deps.transport,
3345
3374
  deps.nodeId,
3346
3375
  deps.resolveNodeTargets,
3347
- table?.indexUuid
3376
+ table?.indexUuid,
3377
+ first === void 0 ? void 0 : previousTermOf(deps.getReplicationLog(first.indexName, first.partitionId), first.seqNo)
3348
3378
  );
3349
3379
  return result.acknowledged.includes(replicaNodeId);
3350
3380
  })().finally(() => {
@@ -3374,7 +3404,7 @@ async function pumpPartition(state, deps, key, replicas, started, registered) {
3374
3404
  const separator = key.lastIndexOf(":");
3375
3405
  const indexName = key.slice(0, separator);
3376
3406
  const partitionId = Number(key.slice(separator + 1));
3377
- const table = await deps.coordinator.getAllocation(indexName);
3407
+ const table = await (deps.readLiveAllocation?.(indexName) ?? deps.coordinator.getAllocation(indexName));
3378
3408
  const assignment = table?.assignments.get(partitionId);
3379
3409
  if (assignment === void 0 || assignment.primary !== deps.nodeId) {
3380
3410
  cancelFeeds(replicas);
@@ -6555,12 +6585,36 @@ async function sendThroughTargets(targets, send, failure) {
6555
6585
  }
6556
6586
  throw lastError instanceof Error ? lastError : new Error(String(lastError));
6557
6587
  }
6558
- var UNDELIVERED_CODES = /* @__PURE__ */ new Set([
6559
- TransportErrorCodes.CONNECT_FAILED,
6560
- TransportErrorCodes.PEER_UNAVAILABLE
6561
- ]);
6562
- function forwardUndelivered(error) {
6563
- return error instanceof NarsilError && UNDELIVERED_CODES.has(error.details.transportCode) && typeof error.details.targetNodeId === "string";
6588
+ async function sendUntilMaybeDelivered(targets, send, failure) {
6589
+ let lastError;
6590
+ for (const target of targets) {
6591
+ try {
6592
+ return await send(target);
6593
+ } catch (error) {
6594
+ lastError = error;
6595
+ if (!(error instanceof TransportError && error.code === TransportErrorCodes.CONNECT_FAILED)) break;
6596
+ }
6597
+ }
6598
+ if (lastError instanceof TransportError) {
6599
+ throw narsilErrorFromTransportFailure(lastError, failure.message, failure.details ?? {});
6600
+ }
6601
+ throw lastError instanceof Error ? lastError : new Error(String(lastError));
6602
+ }
6603
+ function forwardFailedWith(error, transportCode) {
6604
+ return error instanceof NarsilError && error.details.transportCode === transportCode && typeof error.details.targetNodeId === "string";
6605
+ }
6606
+ function forwardUnsent(error) {
6607
+ if (error instanceof NarsilError && error.code === ErrorCodes.NODE_NOT_READY) return true;
6608
+ return forwardFailedWith(error, TransportErrorCodes.CONNECT_FAILED);
6609
+ }
6610
+ function forwardInterrupted(error) {
6611
+ return forwardFailedWith(error, TransportErrorCodes.PEER_UNAVAILABLE);
6612
+ }
6613
+ function unsentAfter(error) {
6614
+ return new NarsilError(error.code, error.message, {
6615
+ ...error.details,
6616
+ transportCode: TransportErrorCodes.CONNECT_FAILED
6617
+ });
6564
6618
  }
6565
6619
 
6566
6620
  // src/distribution/cluster-node/led-term.ts
@@ -6582,6 +6636,9 @@ function noteAcceptedPrimaryTerm(log, primaryTerm) {
6582
6636
  const terms = termsOf(log);
6583
6637
  if (primaryTerm > terms.accepted) terms.accepted = primaryTerm;
6584
6638
  }
6639
+ function acceptedTermOf(log) {
6640
+ return knownTerms.get(log)?.accepted ?? 0;
6641
+ }
6585
6642
  function carryKnownTerms(from, to) {
6586
6643
  const terms = from === void 0 ? void 0 : knownTerms.get(from);
6587
6644
  if (terms !== void 0) knownTerms.set(to, { ...terms });
@@ -6736,8 +6793,21 @@ async function removeEachFailedReplica(entry, failedReplicas, deps) {
6736
6793
  );
6737
6794
  }
6738
6795
  forgetAdmittedReplica(deps.catchUp, entry.indexName, entry.partitionId, replicaNodeId);
6796
+ recordRemovedReplica(deps.catchUp, entry.indexName, entry.partitionId, replicaNodeId, entry.primaryTerm);
6739
6797
  }
6740
6798
  }
6799
+ function replicaTargetsOf(entry, assignment, deps) {
6800
+ const admitted = admittedBeyondTable(deps.catchUp, entry.indexName, entry.partitionId, assignment);
6801
+ const pending = getPendingAdmissions(deps.catchUp, entry.indexName, entry.partitionId);
6802
+ const targets = withoutRemovedReplicas(
6803
+ deps.catchUp,
6804
+ entry.indexName,
6805
+ entry.partitionId,
6806
+ entry.primaryTerm,
6807
+ getInSyncReplicaTargets(assignment, deps.nodeId, [...pending, ...admitted])
6808
+ );
6809
+ return { admitted, targets };
6810
+ }
6741
6811
  async function requestInsyncRemovalWithTargets(indexName, partitionId, replicaNodeId, primaryTerm, controllerNodeId, deps) {
6742
6812
  const targets = await resolveNodeTargets(controllerNodeId, deps);
6743
6813
  const result = await sendThroughTargets(
@@ -6751,13 +6821,20 @@ async function requestInsyncRemovalWithTargets(indexName, partitionId, replicaNo
6751
6821
  return result.accepted;
6752
6822
  }
6753
6823
  async function replicateEntry(entry, assignment, deps, indexUuid) {
6754
- const admitted = admittedBeyondTable(deps.catchUp, entry.indexName, entry.partitionId, assignment);
6755
- const pending = getPendingAdmissions(deps.catchUp, entry.indexName, entry.partitionId);
6756
- const replicaTargets = getInSyncReplicaTargets(assignment, deps.nodeId, [...pending, ...admitted]);
6824
+ const { admitted, targets: replicaTargets } = replicaTargetsOf(entry, assignment, deps);
6825
+ const log = deps.getReplicationLog(entry.indexName, entry.partitionId);
6757
6826
  const result = await untilSuperseded(
6758
- deps.getReplicationLog(entry.indexName, entry.partitionId),
6827
+ log,
6759
6828
  entry,
6760
- replicateToReplicas(entry, replicaTargets, deps.transport, deps.nodeId, deps.resolveNodeTargets, indexUuid)
6829
+ replicateToReplicas(
6830
+ entry,
6831
+ replicaTargets,
6832
+ deps.transport,
6833
+ deps.nodeId,
6834
+ deps.resolveNodeTargets,
6835
+ indexUuid,
6836
+ previousTermOf(log, entry.seqNo)
6837
+ )
6761
6838
  );
6762
6839
  recordAcknowledgements(deps.catchUp, entry.indexName, entry.partitionId, result.acknowledged, entry.seqNo);
6763
6840
  const inSyncFailures = cancelAdmissionsForFailures(entry, result.failed, assignment, admitted, deps);
@@ -6781,13 +6858,20 @@ async function replicateEntryBatch(entries, assignment, deps, indexUuid) {
6781
6858
  return;
6782
6859
  }
6783
6860
  const lastEntry = entries[entries.length - 1];
6784
- const admitted = admittedBeyondTable(deps.catchUp, lastEntry.indexName, lastEntry.partitionId, assignment);
6785
- const pending = getPendingAdmissions(deps.catchUp, lastEntry.indexName, lastEntry.partitionId);
6786
- const replicaTargets = getInSyncReplicaTargets(assignment, deps.nodeId, [...pending, ...admitted]);
6861
+ const { admitted, targets: replicaTargets } = replicaTargetsOf(lastEntry, assignment, deps);
6862
+ const log = deps.getReplicationLog(lastEntry.indexName, lastEntry.partitionId);
6787
6863
  const result = await untilSuperseded(
6788
- deps.getReplicationLog(lastEntry.indexName, lastEntry.partitionId),
6864
+ log,
6789
6865
  lastEntry,
6790
- replicateBatchToReplicas(entries, replicaTargets, deps.transport, deps.nodeId, deps.resolveNodeTargets, indexUuid)
6866
+ replicateBatchToReplicas(
6867
+ entries,
6868
+ replicaTargets,
6869
+ deps.transport,
6870
+ deps.nodeId,
6871
+ deps.resolveNodeTargets,
6872
+ indexUuid,
6873
+ previousTermOf(log, entries[0].seqNo)
6874
+ )
6791
6875
  );
6792
6876
  recordAcknowledgements(deps.catchUp, lastEntry.indexName, lastEntry.partitionId, result.acknowledged, lastEntry.seqNo);
6793
6877
  const inSyncFailures = cancelAdmissionsForFailures(lastEntry, result.failed, assignment, admitted, deps);
@@ -7113,9 +7197,12 @@ async function withPartitionHeld(core, indexName, work) {
7113
7197
  release();
7114
7198
  }
7115
7199
  }
7116
- async function emptyPartitionCopy(core, indexName, partitionId) {
7200
+ async function emptyPartitionCopy(core, indexName, partitionId, writeRecords) {
7117
7201
  if (!core.indexRegistry.has(indexName)) return;
7118
- await withPartitionHeld(core, indexName, async () => clearPartition(core.requireManager(indexName), partitionId));
7202
+ await withPartitionHeld(core, indexName, async () => {
7203
+ clearPartition(core.requireManager(indexName), partitionId);
7204
+ writeRecords.resetPartition(indexName, partitionId);
7205
+ });
7119
7206
  const durable = core.durability?.manager;
7120
7207
  if (durable !== void 0) await restartPartitionLog(durable, indexName, partitionId, { seqNo: 0, primaryTerm: 0 });
7121
7208
  }
@@ -7139,7 +7226,7 @@ async function loadPartitionVectors(store, manifest, manager, partitionId) {
7139
7226
  vectorIndex.scheduleBuild();
7140
7227
  }
7141
7228
  }
7142
- async function reloadPartitionCopy(core, indexName, partitionId) {
7229
+ async function reloadPartitionCopy(core, indexName, partitionId, writeRecords) {
7143
7230
  const durable = core.durability?.manager;
7144
7231
  const registry = durable?.partitionRegistry;
7145
7232
  if (durable === void 0 || registry === void 0) {
@@ -7152,6 +7239,7 @@ async function reloadPartitionCopy(core, indexName, partitionId) {
7152
7239
  await withPartitionHeld(core, indexName, async () => {
7153
7240
  const manager = core.requireManager(indexName);
7154
7241
  clearPartition(manager, partitionId);
7242
+ writeRecords.resetPartition(indexName, partitionId);
7155
7243
  const store = directoryCheckpointStore(registry.directory);
7156
7244
  const decoded = await readSegmentManifest(store, indexName);
7157
7245
  if (decoded !== null && decoded.kind !== "current") {
@@ -7164,10 +7252,19 @@ async function reloadPartitionCopy(core, indexName, partitionId) {
7164
7252
  const manifest = decoded?.manifest ?? null;
7165
7253
  const listed = manifest?.partitions.find((entry) => entry.partitionId === partitionId);
7166
7254
  if (manifest !== null && listed !== void 0) {
7167
- await loadPartitionSegments(store, listed, manager, { schema: manifest.schema, decodesEveryDocument: true });
7255
+ await loadPartitionSegments(store, listed, manager, {
7256
+ schema: manifest.schema,
7257
+ decodesEveryDocument: true,
7258
+ writeRecords
7259
+ });
7168
7260
  await loadPartitionVectors(store, manifest, manager, partitionId);
7169
7261
  }
7170
7262
  const checkpoint = manifest?.checkpoint ?? [];
7263
+ writeRecords.loadTermHistory(
7264
+ indexName,
7265
+ partitionId,
7266
+ checkpoint.find((entry) => entry.partitionId === partitionId)
7267
+ );
7171
7268
  await reopenPartitionLogAfterReplay(durable, indexName, partitionId, async () => {
7172
7269
  const replayed = await replaySyncedWal(
7173
7270
  registry.directory,
@@ -7177,7 +7274,8 @@ async function reloadPartitionCopy(core, indexName, partitionId) {
7177
7274
  {
7178
7275
  manager,
7179
7276
  vectorFieldPaths: core.indexRegistry.get(indexName)?.vectorFieldPaths ?? /* @__PURE__ */ new Set(),
7180
- vectorIndexes: manager.getVectorIndexes()
7277
+ vectorIndexes: manager.getVectorIndexes(),
7278
+ writeRecords
7181
7279
  }
7182
7280
  );
7183
7281
  return {
@@ -7519,11 +7617,11 @@ async function seedReopenedPrimaryLogs(input) {
7519
7617
  );
7520
7618
  }
7521
7619
  }
7522
- async function applyLocalReplicationEntry(core, entry) {
7523
- const { applied, error } = await applyLocalReplicationEntries(core, [entry]);
7620
+ async function applyLocalReplicationEntry(core, entry, writeRecords) {
7621
+ const { applied, error } = await applyLocalReplicationEntries(core, [entry], writeRecords);
7524
7622
  if (applied === 0) throw error;
7525
7623
  }
7526
- async function applyLocalReplicationEntries(core, entries) {
7624
+ async function applyLocalReplicationEntries(core, entries, writeRecords) {
7527
7625
  const first = entries[0];
7528
7626
  if (first === void 0) return { applied: 0, error: null };
7529
7627
  core.guardShutdown();
@@ -7544,6 +7642,7 @@ async function applyLocalReplicationEntries(core, entries) {
7544
7642
  removedSpellings[position] = manager.removedSpellingsOf(entry.documentId);
7545
7643
  applyDeleteEntry(entry, manager, vecIndexes, removedSpellings[position]);
7546
7644
  }
7645
+ writeRecords.applied(entry);
7547
7646
  } catch (err) {
7548
7647
  stopped = err;
7549
7648
  throw err;
@@ -7700,7 +7799,7 @@ async function ensureReplicationIndex(core, engine, indexName, coordinatorConfig
7700
7799
 
7701
7800
  // src/distribution/cluster-node/partition-archive.ts
7702
7801
  var VECTOR_FILE_ENVELOPE_VERSION = 2;
7703
- async function archiveReplicationPartition(core, indexName, partitionId, lastSeqNo) {
7802
+ async function archiveReplicationPartition(core, indexName, partitionId, lastSeqNo, writeRecords) {
7704
7803
  core.guardShutdown();
7705
7804
  const release = await core.indexState.acquire(indexName, false);
7706
7805
  try {
@@ -7717,7 +7816,8 @@ async function archiveReplicationPartition(core, indexName, partitionId, lastSeq
7717
7816
  captured = lastSeqNo();
7718
7817
  return captured;
7719
7818
  },
7720
- partitionId
7819
+ partitionId,
7820
+ writeRecords
7721
7821
  });
7722
7822
  return { stream: archiveStream({ ...checkpoint, config, sourceIndexName: indexName }), lastSeqNo: captured };
7723
7823
  } finally {
@@ -7785,20 +7885,27 @@ async function receiveArchive(indexName, partitionId, source, schema) {
7785
7885
  if (received === null) restoreFailed("partition archive holds no manifest", { indexName, partitionId });
7786
7886
  return { language, analysisRevision, manifest: received, store };
7787
7887
  }
7788
- async function replacePartition(manager, partitionId, archive) {
7888
+ async function replacePartition(manager, partitionId, archive, writeRecords) {
7789
7889
  const vectorIndexes = manager.getVectorIndexes();
7790
7890
  const held = [...manager.getPartition(partitionId).docIds()];
7791
7891
  for (const vectorIndex of vectorIndexes.values()) {
7792
7892
  for (const docId of held) if (vectorIndex.has(docId)) vectorIndex.remove(docId);
7793
7893
  }
7794
7894
  manager.deserializePartition(partitionId, emptySerializablePartition(manager, partitionId));
7895
+ writeRecords.resetPartition(manager.indexName, partitionId);
7795
7896
  const entry = archive.manifest.partitions[0];
7796
7897
  if (entry !== void 0) {
7797
7898
  await loadPartitionSegments(archive.store, entry, manager, {
7798
7899
  schema: archive.manifest.schema,
7799
- decodesEveryDocument: true
7900
+ decodesEveryDocument: true,
7901
+ writeRecords
7800
7902
  });
7801
7903
  }
7904
+ writeRecords.loadTermHistory(
7905
+ manager.indexName,
7906
+ partitionId,
7907
+ archive.manifest.checkpoint.find((checkpoint) => checkpoint.partitionId === partitionId)
7908
+ );
7802
7909
  for (const field of archive.manifest.vectors) {
7803
7910
  const vectorIndex = vectorIndexes.get(field.fieldPath);
7804
7911
  if (vectorIndex === void 0) continue;
@@ -7832,7 +7939,8 @@ function recordReceivedAnalysis(core, indexName, receivedRevision) {
7832
7939
  currentRevision: current
7833
7940
  });
7834
7941
  }
7835
- async function restoreReplicationPartition(core, engine, indexName, partitionId, source, indexConfig, partitionCount) {
7942
+ async function restoreReplicationPartition(core, engine, indexName, partitionId, source, target) {
7943
+ const { config: indexConfig, partitionCount, writeRecords } = target;
7836
7944
  core.guardShutdown();
7837
7945
  requireRestoreTarget(indexName, partitionId, partitionCount);
7838
7946
  const archive = await receiveArchive(indexName, partitionId, source, flattenSchema(indexConfig.schema));
@@ -7859,7 +7967,7 @@ async function restoreReplicationPartition(core, engine, indexName, partitionId,
7859
7967
  }
7860
7968
  const wasScaledOut = core.orchestrator.desyncIndex(indexName);
7861
7969
  try {
7862
- await replacePartition(core.requireManager(indexName), partitionId, archive);
7970
+ await replacePartition(core.requireManager(indexName), partitionId, archive, writeRecords);
7863
7971
  recordReceivedAnalysis(core, indexName, archive.analysisRevision);
7864
7972
  } finally {
7865
7973
  await core.orchestrator.resyncIndex(indexName, wasScaledOut);
@@ -7869,6 +7977,29 @@ async function restoreReplicationPartition(core, engine, indexName, partitionId,
7869
7977
  }
7870
7978
  }
7871
7979
 
7980
+ // src/distribution/cluster-node/search-contexts/snapshots.ts
7981
+ function snapshotOf(manager, docId) {
7982
+ const fields = manager.getRef(docId);
7983
+ if (fields === void 0) return void 0;
7984
+ const vectors = [];
7985
+ for (const [fieldPath2, vectorIndex] of manager.getVectorIndexes()) {
7986
+ const vector = vectorIndex.getVector(docId);
7987
+ if (vector !== null) vectors.push([fieldPath2, vector]);
7988
+ }
7989
+ return { fields, vectors };
7990
+ }
7991
+ function snapshotRankedDocuments(manager, result, into) {
7992
+ const take = (docId) => {
7993
+ if (into.has(docId)) return;
7994
+ const snapshot = snapshotOf(manager, docId);
7995
+ if (snapshot !== void 0) into.set(docId, snapshot);
7996
+ };
7997
+ for (const hit of result.hits) take(hit.id);
7998
+ for (const group of result.groups ?? []) {
7999
+ for (const hit of group.hits) take(hit.id);
8000
+ }
8001
+ }
8002
+
7872
8003
  // src/distribution/cluster-node/write-admission.ts
7873
8004
  function createAdmittingRecorder(core, admission, durable) {
7874
8005
  function admit(indexName, docId, document) {
@@ -7926,18 +8057,251 @@ function createAdmittingRecorder(core, admission, durable) {
7926
8057
  };
7927
8058
  }
7928
8059
 
8060
+ // src/distribution/cluster-node/write-records/constants.ts
8061
+ var INITIAL_WRITE_RECORD_SLOTS = 64;
8062
+
8063
+ // src/distribution/cluster-node/write-records/table.ts
8064
+ function createWriteRecordTable() {
8065
+ const slots = /* @__PURE__ */ new Map();
8066
+ let seqNos = new Float64Array(0);
8067
+ let primaryTerms = new Float64Array(0);
8068
+ let freeSlots = [];
8069
+ let nextSlot = 0;
8070
+ function grow() {
8071
+ const capacity = Math.max(INITIAL_WRITE_RECORD_SLOTS, seqNos.length * 2);
8072
+ const grownSeqNos = new Float64Array(capacity);
8073
+ const grownTerms = new Float64Array(capacity);
8074
+ grownSeqNos.set(seqNos);
8075
+ grownTerms.set(primaryTerms);
8076
+ seqNos = grownSeqNos;
8077
+ primaryTerms = grownTerms;
8078
+ }
8079
+ function claimSlot() {
8080
+ const reused = freeSlots.pop();
8081
+ if (reused !== void 0) return reused;
8082
+ if (nextSlot === seqNos.length) grow();
8083
+ return nextSlot++;
8084
+ }
8085
+ return {
8086
+ get count() {
8087
+ return slots.size;
8088
+ },
8089
+ set(documentId, seqNo, primaryTerm) {
8090
+ let slot = slots.get(documentId);
8091
+ if (slot === void 0) {
8092
+ slot = claimSlot();
8093
+ slots.set(documentId, slot);
8094
+ }
8095
+ seqNos[slot] = seqNo;
8096
+ primaryTerms[slot] = primaryTerm;
8097
+ },
8098
+ remove(documentId) {
8099
+ const slot = slots.get(documentId);
8100
+ if (slot === void 0) return;
8101
+ slots.delete(documentId);
8102
+ seqNos[slot] = 0;
8103
+ primaryTerms[slot] = 0;
8104
+ freeSlots.push(slot);
8105
+ },
8106
+ recordOf(documentId) {
8107
+ const slot = slots.get(documentId);
8108
+ if (slot === void 0) return null;
8109
+ return { seqNo: seqNos[slot], primaryTerm: primaryTerms[slot] };
8110
+ },
8111
+ seqNoOf(documentId) {
8112
+ const slot = slots.get(documentId);
8113
+ return slot === void 0 ? 0 : seqNos[slot];
8114
+ },
8115
+ primaryTermOf(documentId) {
8116
+ const slot = slots.get(documentId);
8117
+ return slot === void 0 ? 0 : primaryTerms[slot];
8118
+ },
8119
+ clear() {
8120
+ slots.clear();
8121
+ seqNos = new Float64Array(0);
8122
+ primaryTerms = new Float64Array(0);
8123
+ freeSlots = [];
8124
+ nextSlot = 0;
8125
+ }
8126
+ };
8127
+ }
8128
+
8129
+ // src/distribution/cluster-node/write-records/term-history.ts
8130
+ function createTermHistory() {
8131
+ let starts = [];
8132
+ let highestSeqNo = 0;
8133
+ return {
8134
+ get highestSeqNo() {
8135
+ return highestSeqNo;
8136
+ },
8137
+ noteEntry(seqNo, primaryTerm) {
8138
+ const last = starts.at(-1);
8139
+ if (last === void 0 || primaryTerm > last.primaryTerm) starts.push({ primaryTerm, firstSeqNo: seqNo });
8140
+ if (seqNo > highestSeqNo) highestSeqNo = seqNo;
8141
+ },
8142
+ termOf(seqNo) {
8143
+ if (seqNo > highestSeqNo) return null;
8144
+ let term = null;
8145
+ for (const start of starts) {
8146
+ if (start.firstSeqNo > seqNo) break;
8147
+ term = start.primaryTerm;
8148
+ }
8149
+ return term;
8150
+ },
8151
+ starts(upToSeqNo) {
8152
+ return starts.filter((start) => start.firstSeqNo <= upToSeqNo).map((start) => ({ ...start }));
8153
+ },
8154
+ replace(next, nextHighest) {
8155
+ starts = next.map((start) => ({ ...start }));
8156
+ highestSeqNo = nextHighest;
8157
+ },
8158
+ prune(floorSeqNo) {
8159
+ while (starts.length > 1 && starts[1].firstSeqNo <= floorSeqNo) starts.shift();
8160
+ },
8161
+ clear() {
8162
+ starts = [];
8163
+ highestSeqNo = 0;
8164
+ }
8165
+ };
8166
+ }
8167
+
8168
+ // src/distribution/cluster-node/write-records/keeper.ts
8169
+ function createClusterWriteRecords() {
8170
+ const indexes = /* @__PURE__ */ new Map();
8171
+ let oldestRetainedSeqNo = () => void 0;
8172
+ function partition(indexName, partitionId) {
8173
+ let partitions = indexes.get(indexName);
8174
+ if (partitions === void 0) {
8175
+ partitions = /* @__PURE__ */ new Map();
8176
+ indexes.set(indexName, partitions);
8177
+ }
8178
+ let held = partitions.get(partitionId);
8179
+ if (held === void 0) {
8180
+ held = { table: createWriteRecordTable(), history: createTermHistory() };
8181
+ partitions.set(partitionId, held);
8182
+ }
8183
+ return held;
8184
+ }
8185
+ function applied(entry) {
8186
+ const held = partition(entry.indexName, entry.partitionId);
8187
+ if (entry.operation === "INDEX") held.table.set(entry.documentId, entry.seqNo, entry.primaryTerm);
8188
+ else held.table.remove(entry.documentId);
8189
+ held.history.noteEntry(entry.seqNo, entry.primaryTerm);
8190
+ }
8191
+ function captureSegment(table, segment) {
8192
+ const count2 = segment.arrays.documentCount;
8193
+ const seqNos = new Float64Array(count2);
8194
+ const primaryTerms = new Float64Array(count2);
8195
+ for (let ordinal = 0; ordinal < count2; ordinal++) {
8196
+ if (segment.isTombstoned(ordinal)) continue;
8197
+ const documentId = segment.arrays.idTable.idAt(ordinal);
8198
+ seqNos[ordinal] = table.seqNoOf(documentId);
8199
+ primaryTerms[ordinal] = table.primaryTermOf(documentId);
8200
+ }
8201
+ return { seqNos, primaryTerms };
8202
+ }
8203
+ return {
8204
+ readRetainedLogWith(read) {
8205
+ oldestRetainedSeqNo = read;
8206
+ },
8207
+ applied,
8208
+ replayed: applied,
8209
+ partition,
8210
+ recordOf(indexName, partitionId, documentId) {
8211
+ return indexes.get(indexName)?.get(partitionId)?.table.recordOf(documentId) ?? null;
8212
+ },
8213
+ heldEntry(indexName, partitionId, seqNo, primaryTerm) {
8214
+ const history = indexes.get(indexName)?.get(partitionId)?.history;
8215
+ if (history === void 0) return "uncovered";
8216
+ if (seqNo > history.highestSeqNo) return "absent";
8217
+ const term = history.termOf(seqNo);
8218
+ if (term === null) return "uncovered";
8219
+ return term === primaryTerm ? "held" : "absent";
8220
+ },
8221
+ resetPartition(indexName, partitionId) {
8222
+ const held = indexes.get(indexName)?.get(partitionId);
8223
+ if (held === void 0) return;
8224
+ held.table.clear();
8225
+ held.history.clear();
8226
+ },
8227
+ loadSegment(indexName, partitionId, segment, contents) {
8228
+ const { table } = partition(indexName, partitionId);
8229
+ for (const documentId of contents.tombstones) table.remove(documentId);
8230
+ const records = contents.writeRecords;
8231
+ for (let ordinal = 0; ordinal < segment.arrays.documentCount; ordinal++) {
8232
+ const documentId = segment.arrays.idTable.idAt(ordinal);
8233
+ const seqNo = records?.seqNos[ordinal] ?? 0;
8234
+ if (seqNo === 0 || records === void 0) table.remove(documentId);
8235
+ else table.set(documentId, seqNo, records.primaryTerms[ordinal]);
8236
+ }
8237
+ },
8238
+ captureSegments(indexName, partitionId, segments) {
8239
+ const captured = /* @__PURE__ */ new Map();
8240
+ const table = indexes.get(indexName)?.get(partitionId)?.table;
8241
+ if (table === void 0) return captured;
8242
+ for (const segment of segments) captured.set(segment, captureSegment(table, segment));
8243
+ return captured;
8244
+ },
8245
+ termHistoryOf(indexName, partitionId, lastSeqNo) {
8246
+ const history = indexes.get(indexName)?.get(partitionId)?.history;
8247
+ if (history === void 0) return [];
8248
+ const oldest = oldestRetainedSeqNo(indexName, partitionId);
8249
+ if (oldest !== void 0) history.prune(Math.min(oldest, lastSeqNo));
8250
+ return history.starts(lastSeqNo);
8251
+ },
8252
+ loadTermHistory(indexName, partitionId, checkpoint) {
8253
+ partition(indexName, partitionId).history.replace(checkpoint?.termHistory ?? [], checkpoint?.lastSeqNo ?? 0);
8254
+ },
8255
+ dropIndex(indexName) {
8256
+ indexes.delete(indexName);
8257
+ }
8258
+ };
8259
+ }
8260
+
7929
8261
  // src/distribution/cluster-node/local-engine.ts
7930
8262
  async function createClusterLocalEngine(config, hooks) {
7931
8263
  requireKnownConfig(config);
7932
8264
  const runnableConfig = await resolveRunnableConfig(config);
7933
- const core = await createEngineCore(runnableConfig, hooks, "partition");
8265
+ const writeRecords = createClusterWriteRecords();
8266
+ const core = await createEngineCore(
8267
+ runnableConfig,
8268
+ {
8269
+ ...hooks,
8270
+ onIndexClose: (indexName) => {
8271
+ writeRecords.dropIndex(indexName);
8272
+ return hooks?.onIndexClose?.(indexName);
8273
+ }
8274
+ },
8275
+ "partition"
8276
+ );
8277
+ core.durability?.manager.keepWriteRecordsWith?.(writeRecords);
7934
8278
  await startEngineCore(core, runnableConfig);
7935
8279
  core.orchestrator.shareMainThread();
7936
8280
  const engine = createNarsilFromCore(core, runnableConfig);
7937
8281
  const heldPartitions = createHeldPartitionRecord(core);
8282
+ const dropIndex = engine.dropIndex.bind(engine);
8283
+ const restore = engine.restore.bind(engine);
7938
8284
  return Object.assign(engine, {
7939
- createIndex: (name, indexConfig) => createEngineIndex(core, runnableConfig, name, indexConfig, { adapterBinding: "deferred" }),
7940
- createIndexWithUuid: (name, indexConfig, indexUuid) => createEngineIndex(core, runnableConfig, name, indexConfig, { indexUuid }),
8285
+ writeRecords,
8286
+ createIndex: async (name, indexConfig) => {
8287
+ await createEngineIndex(core, runnableConfig, name, indexConfig, { adapterBinding: "deferred" });
8288
+ writeRecords.dropIndex(name);
8289
+ },
8290
+ createIndexWithUuid: async (name, indexConfig, indexUuid) => {
8291
+ await createEngineIndex(core, runnableConfig, name, indexConfig, { indexUuid });
8292
+ writeRecords.dropIndex(name);
8293
+ },
8294
+ dropIndex: async (name) => {
8295
+ await dropIndex(name);
8296
+ writeRecords.dropIndex(name);
8297
+ },
8298
+ restore: async (name, data) => {
8299
+ try {
8300
+ await restore(name, data);
8301
+ } finally {
8302
+ writeRecords.dropIndex(name);
8303
+ }
8304
+ },
7941
8305
  acquireIndexForReplication: (indexName) => core.indexState.acquire(indexName, false),
7942
8306
  indexUuidOf: (indexName) => core.indexRegistry.get(indexName)?.indexUuid,
7943
8307
  stampIndexUuid: (indexName, indexUuid) => stampIndexUuid(core, indexName, indexUuid),
@@ -7954,16 +8318,25 @@ async function createClusterLocalEngine(config, hooks) {
7954
8318
  writeAnalysisOf: (indexName) => writeAnalysisOf(core, indexName),
7955
8319
  restartLocalLog: (indexName, partitionId, position) => restartLocalLog(core, indexName, partitionId, position),
7956
8320
  isolateCopyFailures: (onFailed) => core.durability?.manager.isolatePartitionFailures?.(onFailed),
7957
- emptyPartitionCopy: (indexName, partitionId) => emptyPartitionCopy(core, indexName, partitionId),
7958
- reloadPartitionCopy: (indexName, partitionId) => reloadPartitionCopy(core, indexName, partitionId),
8321
+ emptyPartitionCopy: (indexName, partitionId) => emptyPartitionCopy(core, indexName, partitionId, writeRecords),
8322
+ reloadPartitionCopy: (indexName, partitionId) => reloadPartitionCopy(core, indexName, partitionId, writeRecords),
7959
8323
  heldPartitionsOf: (indexName) => heldPartitions.held(indexName),
7960
8324
  recordHeldPartition: (indexName, partitionId) => heldPartitions.record(indexName, partitionId),
7961
8325
  forgetHeldPartition: (indexName, partitionId) => heldPartitions.forget(indexName, partitionId),
7962
- applyReplicationEntry: (entry) => applyLocalReplicationEntry(core, entry),
7963
- applyReplicationEntries: (entries) => applyLocalReplicationEntries(core, entries),
7964
- archiveReplicationPartition: (indexName, partitionId, lastSeqNo) => archiveReplicationPartition(core, indexName, partitionId, lastSeqNo),
7965
- restoreReplicationPartition: (indexName, partitionId, source, indexConfig, partitionCount) => restoreReplicationPartition(core, engine, indexName, partitionId, source, indexConfig, partitionCount),
7966
- queryPartitions: (indexName, params, partitionIds, globalStats, rescoreWindow) => runEngineQuery(core, indexName, params, { partitionIds, globalStats, rescoreWindow }),
8326
+ applyReplicationEntry: (entry) => applyLocalReplicationEntry(core, entry, writeRecords),
8327
+ applyReplicationEntries: (entries) => applyLocalReplicationEntries(core, entries, writeRecords),
8328
+ archiveReplicationPartition: (indexName, partitionId, lastSeqNo) => archiveReplicationPartition(core, indexName, partitionId, lastSeqNo, writeRecords),
8329
+ restoreReplicationPartition: (indexName, partitionId, source, indexConfig, partitionCount) => restoreReplicationPartition(core, engine, indexName, partitionId, source, {
8330
+ config: indexConfig,
8331
+ partitionCount,
8332
+ writeRecords
8333
+ }),
8334
+ queryPartitions: (indexName, params, partitionIds, globalStats, rescoreWindow, snapshots) => runEngineQuery(core, indexName, params, {
8335
+ partitionIds,
8336
+ globalStats,
8337
+ rescoreWindow,
8338
+ onRanked: snapshots === void 0 ? void 0 : (manager, result) => snapshotRankedDocuments(manager, result, snapshots)
8339
+ }),
7967
8340
  preflightPartitions: (indexName, params, partitionIds) => runEnginePreflight(core, indexName, params, { partitionIds }),
7968
8341
  suggestPartitions: (indexName, params, partitionIds) => runEngineSuggest(core, indexName, params, partitionIds),
7969
8342
  listPartitions: (indexName, params, partitionIds) => runEngineListDocuments(core, indexName, params, partitionIds),
@@ -8073,6 +8446,9 @@ var MAX_GROUPS_PER_RESPONSE = 65536;
8073
8446
  var MAX_FACET_SIZE = 1e3;
8074
8447
  var DEFAULT_MAX_FACET_BUCKETS = 100;
8075
8448
  var MAX_VECTOR_TEXT_LENGTH = 16384;
8449
+ var SEARCH_CONTEXT_KEEP_ALIVE_MS = 6e4;
8450
+ var SEARCH_CONTEXT_KEEP_ALIVE_LIMIT_MS = 864e5;
8451
+ var MAX_SEARCH_CONTEXT_ID_LENGTH = 64;
8076
8452
 
8077
8453
  // src/distribution/query/validators/common.ts
8078
8454
  var NULL_BYTE = String.fromCharCode(0);
@@ -8251,6 +8627,12 @@ function validateFetchPayload(decoded) {
8251
8627
  if (decoded.highlight !== null) {
8252
8628
  validateHighlightConfig(decoded.highlight);
8253
8629
  }
8630
+ if (decoded.contextId !== null && decoded.contextId !== void 0) {
8631
+ validateStringField(decoded.contextId, "contextId", MAX_SEARCH_CONTEXT_ID_LENGTH, CONFIG_INVALID);
8632
+ }
8633
+ if (decoded.releaseContext !== null && decoded.releaseContext !== void 0 && typeof decoded.releaseContext !== "boolean") {
8634
+ throwInvalid(CONFIG_INVALID, 'Invalid FetchPayload: "releaseContext" must be a boolean, or null');
8635
+ }
8254
8636
  return decoded;
8255
8637
  }
8256
8638
  function validateFetchResultPayload(decoded) {
@@ -9082,6 +9464,11 @@ function validateSearchPayload(decoded) {
9082
9464
  );
9083
9465
  }
9084
9466
  }
9467
+ if (decoded.keepAliveMs !== null && decoded.keepAliveMs !== void 0) {
9468
+ if (!Number.isInteger(decoded.keepAliveMs) || decoded.keepAliveMs <= 0) {
9469
+ throwInvalid(CONFIG_INVALID, 'Invalid SearchPayload: "keepAliveMs" must be a positive integer, or null');
9470
+ }
9471
+ }
9085
9472
  return decoded;
9086
9473
  }
9087
9474
 
@@ -9484,6 +9871,14 @@ function validateSearchResultPayload(decoded) {
9484
9871
  if (decoded.groups !== null && decoded.groups !== void 0) {
9485
9872
  validateGroupEntries(decoded.groups);
9486
9873
  }
9874
+ if (decoded.contextId !== null && decoded.contextId !== void 0) {
9875
+ if (typeof decoded.contextId !== "string" || decoded.contextId.length === 0 || decoded.contextId.length > MAX_SEARCH_CONTEXT_ID_LENGTH) {
9876
+ throwInvalid(
9877
+ CONFIG_INVALID,
9878
+ `Invalid SearchResultPayload: "contextId" must be a string of 1 to ${MAX_SEARCH_CONTEXT_ID_LENGTH} characters, or null`
9879
+ );
9880
+ }
9881
+ }
9487
9882
  return decoded;
9488
9883
  }
9489
9884
 
@@ -9843,21 +10238,23 @@ function transportableDetails(error) {
9843
10238
  return void 0;
9844
10239
  }
9845
10240
  }
9846
- function encodeErrorReply(code, message, details) {
10241
+ function encodeErrorReply(code, message, details, abandonedWrite) {
10242
+ const reply2 = abandonedWrite === void 0 ? { error: true, code, message } : { error: true, code, message, abandonedWrite };
9847
10243
  if (details !== void 0) {
9848
10244
  try {
9849
- return encode({ error: true, code, message, details });
10245
+ return encode({ ...reply2, details });
9850
10246
  } catch {
9851
- return encode({ error: true, code, message });
10247
+ return encode(reply2);
9852
10248
  }
9853
10249
  }
9854
- return encode({ error: true, code, message });
10250
+ return encode(reply2);
9855
10251
  }
9856
- function encodeErrorReplyFrom(error, fallbackCode) {
10252
+ function encodeErrorReplyFrom(error, fallbackCode, abandonedWrite) {
9857
10253
  return encodeErrorReply(
9858
10254
  error instanceof NarsilError ? error.code : fallbackCode,
9859
10255
  error instanceof Error ? error.message : String(error),
9860
- error instanceof NarsilError ? error.details : void 0
10256
+ error instanceof NarsilError ? error.details : void 0,
10257
+ abandonedWrite
9861
10258
  );
9862
10259
  }
9863
10260
  function isPlainRecord(value) {
@@ -10022,71 +10419,6 @@ function requireWritableIndex(table, view) {
10022
10419
  throw block;
10023
10420
  }
10024
10421
  }
10025
- function assertForwardResponse(response, indexName, primaryNodeId) {
10026
- const decoded = decode(response.payload);
10027
- if (response.type.endsWith(".error") || response.type === "error" || decoded.error === true) {
10028
- throw new NarsilError(
10029
- typeof decoded.code === "string" ? decoded.code : ErrorCodes.QUERY_ROUTING_FAILED,
10030
- typeof decoded.message === "string" ? decoded.message : `Remote primary rejected a forwarded write for index '${indexName}'`,
10031
- { ...remoteErrorDetails(decoded.details), indexName, primaryNodeId }
10032
- );
10033
- }
10034
- return decoded;
10035
- }
10036
- async function forwardInsertToRemote(indexName, document, docId, primaryNodeId, deps, indexUuid) {
10037
- const payload = {
10038
- indexName,
10039
- documentId: docId,
10040
- operation: "insert",
10041
- document: encodeDocument(document),
10042
- updateFields: null
10043
- };
10044
- if (indexUuid !== void 0) payload.indexUuid = indexUuid;
10045
- const message = createForwardMessage(payload, deps.nodeId);
10046
- const response = await sendToNode(primaryNodeId, message, deps);
10047
- const decoded = assertForwardResponse(response, indexName, primaryNodeId);
10048
- if (typeof decoded.documentId !== "string") {
10049
- throw new NarsilError(
10050
- ErrorCodes.QUERY_ROUTING_FAILED,
10051
- `Remote primary returned invalid forward response for index '${indexName}'`,
10052
- { indexName, primaryNodeId }
10053
- );
10054
- }
10055
- return decoded.documentId;
10056
- }
10057
- async function forwardUpdateToRemote(indexName, document, docId, primaryNodeId, deps, indexUuid) {
10058
- const payload = {
10059
- indexName,
10060
- documentId: docId,
10061
- operation: "update",
10062
- document: encodeDocument(document),
10063
- updateFields: null
10064
- };
10065
- if (indexUuid !== void 0) payload.indexUuid = indexUuid;
10066
- const message = createForwardMessage(payload, deps.nodeId);
10067
- const response = await sendToNode(primaryNodeId, message, deps);
10068
- assertForwardResponse(response, indexName, primaryNodeId);
10069
- }
10070
- async function forwardRemoveToRemote(indexName, docId, primaryNodeId, deps, indexUuid) {
10071
- const payload = {
10072
- indexName,
10073
- documentId: docId,
10074
- operation: "remove",
10075
- document: null,
10076
- updateFields: null
10077
- };
10078
- if (indexUuid !== void 0) payload.indexUuid = indexUuid;
10079
- const message = createForwardMessage(payload, deps.nodeId);
10080
- const response = await sendToNode(primaryNodeId, message, deps);
10081
- assertForwardResponse(response, indexName, primaryNodeId);
10082
- }
10083
- async function sendToNode(nodeId, message, deps) {
10084
- const targets = await resolveNodeTargets(nodeId, deps);
10085
- return sendThroughTargets(targets, (target) => deps.transport.send(target, message), {
10086
- message: `Node '${nodeId}' did not answer a forwarded write`,
10087
- details: { targetNodeId: nodeId }
10088
- });
10089
- }
10090
10422
 
10091
10423
  // src/distribution/cluster-node/write-routing/rollback.ts
10092
10424
  function outcomeUnknown(scope, originalError, reason, because) {
@@ -10194,82 +10526,329 @@ async function settleRollback(scope, fromSeqNo, originalError, failedAt) {
10194
10526
  return outcomeUnknown(scope, originalError, "rollback_open", because);
10195
10527
  }
10196
10528
 
10197
- // src/distribution/cluster-node/write-routing/sent-fields.ts
10198
- function isRecord3(value) {
10199
- return typeof value === "object" && value !== null && !Array.isArray(value) && !ArrayBuffer.isView(value);
10200
- }
10201
- function numbersOf(value) {
10202
- if (value instanceof Float32Array || value instanceof Float64Array) return value;
10203
- if (Array.isArray(value) && value.every((element) => typeof element === "number")) return value;
10204
- return null;
10205
- }
10206
- function sameNumber(sent, stored) {
10207
- return sent === stored || Number.isNaN(sent) && Number.isNaN(stored);
10208
- }
10209
- function sameVector(sent, stored) {
10210
- if (sent.length !== stored.length) return false;
10211
- for (let position = 0; position < sent.length; position++) {
10212
- const sentValue = sent[position];
10213
- const storedValue = stored[position];
10214
- if (!sameNumber(sentValue, storedValue) && !sameNumber(Math.fround(sentValue), Math.fround(storedValue))) {
10215
- return false;
10216
- }
10217
- }
10218
- return true;
10219
- }
10220
- function fieldPath(parent, key) {
10221
- return parent.length === 0 ? key : `${parent}.${key}`;
10222
- }
10223
- function matchesSent(sent, stored, path, derivedFields) {
10224
- const sentNumbers = numbersOf(sent);
10225
- const storedNumbers = numbersOf(stored);
10226
- if (sentNumbers !== null && storedNumbers !== null) return sameVector(sentNumbers, storedNumbers);
10227
- if (Array.isArray(sent)) {
10228
- if (!Array.isArray(stored) || sent.length !== stored.length) return false;
10229
- return sent.every((element, position) => matchesSent(element, stored[position], path, /* @__PURE__ */ new Set()));
10230
- }
10231
- if (isRecord3(sent)) {
10232
- if (!isRecord3(stored)) return false;
10233
- for (const [key, value] of Object.entries(sent)) {
10234
- if (value === void 0) continue;
10235
- if (!matchesSent(value, stored[key], fieldPath(path, key), derivedFields)) return false;
10236
- }
10237
- for (const [key, value] of Object.entries(stored)) {
10238
- if (value === void 0 || sent[key] !== void 0) continue;
10239
- if (!derivedFields.has(fieldPath(path, key))) return false;
10240
- }
10241
- return true;
10529
+ // src/distribution/cluster-node/write-routing/abandoned-write.ts
10530
+ var abandonedWrites = /* @__PURE__ */ new WeakMap();
10531
+ function abandonedWriteOf(error) {
10532
+ return error instanceof NarsilError ? abandonedWrites.get(error) : void 0;
10533
+ }
10534
+ function withAbandonedWrite(error, write) {
10535
+ const marked = new NarsilError(error.code, error.message, error.details);
10536
+ abandonedWrites.set(marked, write);
10537
+ return marked;
10538
+ }
10539
+ function isSequenceNumber(value) {
10540
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
10541
+ }
10542
+ function abandonedWriteFrom(value) {
10543
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return void 0;
10544
+ const record = value;
10545
+ const { seqNo, primaryTerm, compensationSeqNo, previousSeqNo, previousPrimaryTerm } = record;
10546
+ if (!isSequenceNumber(seqNo) || !isSequenceNumber(primaryTerm) || !isSequenceNumber(compensationSeqNo) || !isSequenceNumber(previousSeqNo) || !isSequenceNumber(previousPrimaryTerm)) {
10547
+ return void 0;
10242
10548
  }
10243
- if (typeof sent === "number" && typeof stored === "number") return sameNumber(sent, stored);
10244
- return sent === stored;
10245
- }
10246
- function storedCopyMatchesSent(sent, stored, derivedFields) {
10247
- return matchesSent(sent, stored, "", derivedFields);
10248
- }
10249
-
10250
- // src/distribution/cluster-node/write-routing/primary-change.ts
10251
- function refusedByPrimaryChange(error, retriesEndedTerm = false) {
10252
- if (error instanceof NarsilError && error.code === ErrorCodes.PARTITION_NOT_PRIMARY) return true;
10253
- return retriesEndedTerm && abandonedWithTerm(error);
10254
- }
10255
- function refusedForAnotherIndex(error) {
10256
- return refusedByPrimaryChange(error) && error instanceof NarsilError && typeof error.details.heldIndexUuid === "string";
10549
+ return { seqNo, primaryTerm, compensationSeqNo, previousSeqNo, previousPrimaryTerm };
10257
10550
  }
10258
- function retriedAfterDelay(error) {
10259
- return refusedForAnotherIndex(error) || forwardUndelivered(error);
10551
+ function previousRecordOf(scope, documentId) {
10552
+ return scope.deps.writeRecords?.recordOf(scope.indexName, scope.partitionId, documentId) ?? null;
10260
10553
  }
10261
- function hasCode(error, code) {
10262
- return error instanceof NarsilError && error.code === code;
10554
+ function markAbandoned(answer, failed2, compensation, previous) {
10555
+ if (!(answer instanceof NarsilError) || !abandonedWithTerm(answer)) return answer;
10556
+ if (compensation === null || previous === null || previous === void 0) return answer;
10557
+ return withAbandonedWrite(answer, {
10558
+ seqNo: failed2.seqNo,
10559
+ primaryTerm: failed2.primaryTerm,
10560
+ compensationSeqNo: compensation.seqNo,
10561
+ previousSeqNo: previous.seqNo,
10562
+ previousPrimaryTerm: previous.primaryTerm
10563
+ });
10263
10564
  }
10264
- function primaryFor(table, docId) {
10265
- if (table === null || table.assignments.size === 0) return null;
10266
- const partitionId = resolvePartitionId(docId, table.assignments.size);
10267
- const assignment = table.assignments.get(partitionId);
10565
+ function conditionOf(write) {
10268
10566
  return {
10269
- partitionCount: table.assignments.size,
10270
- partitionId,
10271
- primary: assignment?.primary ?? null,
10272
- primaryTerm: assignment?.primaryTerm ?? 0
10567
+ ifSeqNo: write.previousSeqNo,
10568
+ ifPrimaryTerm: write.previousPrimaryTerm,
10569
+ abandonedSeqNo: write.seqNo,
10570
+ abandonedPrimaryTerm: write.primaryTerm,
10571
+ compensationSeqNo: write.compensationSeqNo
10572
+ };
10573
+ }
10574
+ function conditionFields(condition) {
10575
+ if (condition === void 0) return {};
10576
+ return {
10577
+ ifSeqNo: condition.ifSeqNo,
10578
+ ifPrimaryTerm: condition.ifPrimaryTerm,
10579
+ abandonedSeqNo: condition.abandonedSeqNo,
10580
+ abandonedPrimaryTerm: condition.abandonedPrimaryTerm,
10581
+ compensationSeqNo: condition.compensationSeqNo
10582
+ };
10583
+ }
10584
+
10585
+ // src/distribution/cluster-node/write-routing/forwarding.ts
10586
+ function assertForwardResponse(response, indexName, primaryNodeId) {
10587
+ const decoded = decode(response.payload);
10588
+ if (response.type.endsWith(".error") || response.type === "error" || decoded.error === true) {
10589
+ const refusal = new NarsilError(
10590
+ typeof decoded.code === "string" ? decoded.code : ErrorCodes.QUERY_ROUTING_FAILED,
10591
+ typeof decoded.message === "string" ? decoded.message : `Remote primary rejected a forwarded write for index '${indexName}'`,
10592
+ { ...remoteErrorDetails(decoded.details), indexName, primaryNodeId }
10593
+ );
10594
+ const abandonedWrite = abandonedWriteFrom(decoded.abandonedWrite);
10595
+ throw abandonedWrite === void 0 ? refusal : withAbandonedWrite(refusal, abandonedWrite);
10596
+ }
10597
+ return decoded;
10598
+ }
10599
+ async function forwardInsertToRemote(indexName, document, docId, primaryNodeId, deps, indexUuid) {
10600
+ const payload = {
10601
+ indexName,
10602
+ documentId: docId,
10603
+ operation: "insert",
10604
+ document: encodeDocument(document),
10605
+ updateFields: null
10606
+ };
10607
+ if (indexUuid !== void 0) payload.indexUuid = indexUuid;
10608
+ const message = createForwardMessage(payload, deps.nodeId);
10609
+ const response = await sendToNode(primaryNodeId, message, deps);
10610
+ const decoded = assertForwardResponse(response, indexName, primaryNodeId);
10611
+ if (typeof decoded.documentId !== "string") {
10612
+ throw new NarsilError(
10613
+ ErrorCodes.QUERY_ROUTING_FAILED,
10614
+ `Remote primary returned invalid forward response for index '${indexName}'`,
10615
+ { indexName, primaryNodeId }
10616
+ );
10617
+ }
10618
+ return decoded.documentId;
10619
+ }
10620
+ async function forwardUpdateToRemote(indexName, document, docId, primaryNodeId, deps, indexUuid, condition) {
10621
+ const payload = {
10622
+ indexName,
10623
+ documentId: docId,
10624
+ operation: "update",
10625
+ document: encodeDocument(document),
10626
+ updateFields: null,
10627
+ ...conditionFields(condition)
10628
+ };
10629
+ if (indexUuid !== void 0) payload.indexUuid = indexUuid;
10630
+ const message = createForwardMessage(payload, deps.nodeId);
10631
+ const response = await sendToNode(primaryNodeId, message, deps);
10632
+ assertForwardResponse(response, indexName, primaryNodeId);
10633
+ }
10634
+ async function forwardRemoveToRemote(indexName, docId, primaryNodeId, deps, indexUuid, condition) {
10635
+ const payload = {
10636
+ indexName,
10637
+ documentId: docId,
10638
+ operation: "remove",
10639
+ document: null,
10640
+ updateFields: null,
10641
+ ...conditionFields(condition)
10642
+ };
10643
+ if (indexUuid !== void 0) payload.indexUuid = indexUuid;
10644
+ const message = createForwardMessage(payload, deps.nodeId);
10645
+ const response = await sendToNode(primaryNodeId, message, deps);
10646
+ assertForwardResponse(response, indexName, primaryNodeId);
10647
+ }
10648
+ async function sendToNode(nodeId, message, deps) {
10649
+ const targets = await resolveNodeTargets(nodeId, deps);
10650
+ return sendUntilMaybeDelivered(targets, (target) => deps.transport.send(target, message), {
10651
+ message: `Node '${nodeId}' did not answer a forwarded write`,
10652
+ details: { targetNodeId: nodeId }
10653
+ });
10654
+ }
10655
+
10656
+ // src/distribution/cluster-node/write-routing/numbered-write.ts
10657
+ async function numberedWrite(scope, write, appendOutsideTheLog, deferCommit = false) {
10658
+ refuseEndedTerm(scope);
10659
+ const writes = scope.deps.numbering?.open(
10660
+ scope.indexName,
10661
+ scope.partitionId,
10662
+ () => scope.deps.getReplicationLog(scope.indexName, scope.partitionId),
10663
+ scope.assignment.primaryTerm,
10664
+ deferCommit && scope.deps.commitPartitionLog !== void 0
10665
+ );
10666
+ let value;
10667
+ try {
10668
+ value = await write();
10669
+ } finally {
10670
+ writes?.close();
10671
+ }
10672
+ const recorded = writes?.entries.at(-1);
10673
+ if (recorded !== void 0) return { value, entry: recorded };
10674
+ const admitted = (documentId) => writes?.admittedDocument(documentId);
10675
+ const entry = await appendOutsideTheLog(value, admitted);
10676
+ scope.deps.writeRecords?.applied(entry);
10677
+ return { value, entry };
10678
+ }
10679
+ function refuseEndedTerm(scope) {
10680
+ const accepted = acceptedTermOf(scope.deps.getReplicationLog(scope.indexName, scope.partitionId));
10681
+ if (accepted <= scope.assignment.primaryTerm) return;
10682
+ throw new NarsilError(
10683
+ ErrorCodes.PARTITION_NOT_PRIMARY,
10684
+ `Node '${scope.deps.nodeId}' has accepted term ${accepted} for partition ${scope.partitionId} of index '${scope.indexName}', so it appends nothing under term ${scope.assignment.primaryTerm}`,
10685
+ {
10686
+ indexName: scope.indexName,
10687
+ partitionId: scope.partitionId,
10688
+ localNodeId: scope.deps.nodeId,
10689
+ expectedPrimaryTerm: scope.assignment.primaryTerm,
10690
+ currentPrimaryTerm: accepted
10691
+ }
10692
+ );
10693
+ }
10694
+ async function writeLocalOnly(deps, indexName, write) {
10695
+ try {
10696
+ return await write();
10697
+ } finally {
10698
+ deps.writeRecords?.dropIndex(indexName);
10699
+ }
10700
+ }
10701
+ async function storedCopyOf(scope, documentId) {
10702
+ const stored = await scope.deps.engine.get(scope.indexName, documentId);
10703
+ if (stored === void 0) {
10704
+ throw new NarsilError(
10705
+ ErrorCodes.REPLICATION_ENTRY_INVALID,
10706
+ `Document '${documentId}' could not be read back for replication`,
10707
+ { indexName: scope.indexName, documentId, partitionId: scope.partitionId }
10708
+ );
10709
+ }
10710
+ return stored;
10711
+ }
10712
+ async function appendStoredCopy(scope, documentId, admitted) {
10713
+ const admittedDocument = admitted(documentId);
10714
+ if (admittedDocument instanceof Uint8Array) {
10715
+ return scope.deps.getReplicationLog(scope.indexName, scope.partitionId).append({
10716
+ primaryTerm: scope.assignment.primaryTerm,
10717
+ operation: "INDEX",
10718
+ partitionId: scope.partitionId,
10719
+ indexName: scope.indexName,
10720
+ documentId,
10721
+ document: admittedDocument
10722
+ });
10723
+ }
10724
+ const stored = await storedCopyOf(scope, documentId);
10725
+ return appendIndexReplicationEntry(
10726
+ scope.indexName,
10727
+ scope.partitionId,
10728
+ scope.assignment,
10729
+ documentId,
10730
+ stored,
10731
+ scope.deps
10732
+ );
10733
+ }
10734
+ function appendDeletion(scope, documentId) {
10735
+ return Promise.resolve(
10736
+ appendDeleteReplicationEntry(scope.indexName, scope.partitionId, scope.assignment, documentId, scope.deps)
10737
+ );
10738
+ }
10739
+
10740
+ // src/distribution/cluster-node/write-routing/forward-window.ts
10741
+ var WINDOW_ENDED = /* @__PURE__ */ Symbol("forward window ended");
10742
+ async function beforeWindowEnds(work, deadline) {
10743
+ work.catch(() => void 0);
10744
+ let timer;
10745
+ const ended = new Promise((resolve) => {
10746
+ timer = setTimeout(() => resolve(WINDOW_ENDED), Math.max(0, deadline - Date.now()));
10747
+ });
10748
+ try {
10749
+ return await Promise.race([work, ended]);
10750
+ } finally {
10751
+ clearTimeout(timer);
10752
+ }
10753
+ }
10754
+ function windowEndedOutcome(indexName, docId) {
10755
+ return new NarsilError(
10756
+ ErrorCodes.REPLICATION_OUTCOME_UNKNOWN,
10757
+ `This node stopped waiting for the primary of index '${indexName}' to answer the write to document '${docId}' once ${PRIMARY_CHANGE_RETRY_WINDOW_MS}ms had passed, so the write may or may not have taken effect. Read the document before you retry the write.`,
10758
+ { indexName, docId, reason: "forward_window_ended" }
10759
+ );
10760
+ }
10761
+ function interruptedOutcome(indexName, docId, error) {
10762
+ return new NarsilError(
10763
+ ErrorCodes.REPLICATION_OUTCOME_UNKNOWN,
10764
+ `The connection to the primary of index '${indexName}' failed after this node forwarded the write to document '${docId}', so the write may or may not have taken effect. Read the document before you retry the write.`,
10765
+ { indexName, docId, reason: "forward_interrupted", originalError: describeError(error) }
10766
+ );
10767
+ }
10768
+
10769
+ // src/distribution/cluster-node/write-routing/sent-fields.ts
10770
+ function isRecord3(value) {
10771
+ return typeof value === "object" && value !== null && !Array.isArray(value) && !ArrayBuffer.isView(value);
10772
+ }
10773
+ function numbersOf(value) {
10774
+ if (value instanceof Float32Array || value instanceof Float64Array) return value;
10775
+ if (Array.isArray(value) && value.every((element) => typeof element === "number")) return value;
10776
+ return null;
10777
+ }
10778
+ function sameNumber(sent, stored) {
10779
+ return sent === stored || Number.isNaN(sent) && Number.isNaN(stored);
10780
+ }
10781
+ function sameVector(sent, stored) {
10782
+ if (sent.length !== stored.length) return false;
10783
+ for (let position = 0; position < sent.length; position++) {
10784
+ const sentValue = sent[position];
10785
+ const storedValue = stored[position];
10786
+ if (!sameNumber(sentValue, storedValue) && !sameNumber(Math.fround(sentValue), Math.fround(storedValue))) {
10787
+ return false;
10788
+ }
10789
+ }
10790
+ return true;
10791
+ }
10792
+ function fieldPath(parent, key) {
10793
+ return parent.length === 0 ? key : `${parent}.${key}`;
10794
+ }
10795
+ function matchesSent(sent, stored, path, derivedFields) {
10796
+ const sentNumbers = numbersOf(sent);
10797
+ const storedNumbers = numbersOf(stored);
10798
+ if (sentNumbers !== null && storedNumbers !== null) return sameVector(sentNumbers, storedNumbers);
10799
+ if (Array.isArray(sent)) {
10800
+ if (!Array.isArray(stored) || sent.length !== stored.length) return false;
10801
+ return sent.every((element, position) => matchesSent(element, stored[position], path, /* @__PURE__ */ new Set()));
10802
+ }
10803
+ if (isRecord3(sent)) {
10804
+ if (!isRecord3(stored)) return false;
10805
+ for (const [key, value] of Object.entries(sent)) {
10806
+ if (value === void 0) continue;
10807
+ if (!matchesSent(value, stored[key], fieldPath(path, key), derivedFields)) return false;
10808
+ }
10809
+ for (const [key, value] of Object.entries(stored)) {
10810
+ if (value === void 0 || sent[key] !== void 0) continue;
10811
+ if (!derivedFields.has(fieldPath(path, key))) return false;
10812
+ }
10813
+ return true;
10814
+ }
10815
+ if (typeof sent === "number" && typeof stored === "number") return sameNumber(sent, stored);
10816
+ return sent === stored;
10817
+ }
10818
+ function storedCopyMatchesSent(sent, stored, derivedFields) {
10819
+ return matchesSent(sent, stored, "", derivedFields);
10820
+ }
10821
+
10822
+ // src/distribution/cluster-node/write-routing/primary-change.ts
10823
+ function refusedByPrimaryChange(error, retriesEndedTerm = false) {
10824
+ if (error instanceof NarsilError && error.code === ErrorCodes.PARTITION_NOT_PRIMARY) return true;
10825
+ return abandonedWithTerm(error) && (retriesEndedTerm || abandonedWriteOf(error) !== void 0);
10826
+ }
10827
+ function nextCondition(error, current) {
10828
+ const abandoned = abandonedWriteOf(error);
10829
+ return abandoned === void 0 ? current : conditionOf(abandoned);
10830
+ }
10831
+ function refusedForAnotherIndex(error) {
10832
+ return refusedByPrimaryChange(error) && error instanceof NarsilError && typeof error.details.heldIndexUuid === "string";
10833
+ }
10834
+ function retriedAfterDelay(error) {
10835
+ return refusedForAnotherIndex(error) || forwardUnsent(error) || forwardInterrupted(error);
10836
+ }
10837
+ function retriable(error, settlesAsEqual) {
10838
+ return refusedByPrimaryChange(error, settlesAsEqual) || forwardUnsent(error) || settlesAsEqual && forwardInterrupted(error);
10839
+ }
10840
+ function hasCode(error, code) {
10841
+ return error instanceof NarsilError && error.code === code;
10842
+ }
10843
+ function primaryFor(table, docId) {
10844
+ if (table === null || table.assignments.size === 0) return null;
10845
+ const partitionId = resolvePartitionId(docId, table.assignments.size);
10846
+ const assignment = table.assignments.get(partitionId);
10847
+ return {
10848
+ partitionCount: table.assignments.size,
10849
+ partitionId,
10850
+ primary: assignment?.primary ?? null,
10851
+ primaryTerm: assignment?.primaryTerm ?? 0
10273
10852
  };
10274
10853
  }
10275
10854
  function movedOn(table, refusing) {
@@ -10307,19 +10886,22 @@ async function awaitNewPrimary(deps, indexName, refusing, deadline) {
10307
10886
  }
10308
10887
  return false;
10309
10888
  }
10310
- async function retryOnPrimaryChange(deps, indexName, docId, attempt, settleRetried, settledValue, retriesEndedTerm = false) {
10889
+ async function retryOnPrimaryChange(deps, indexName, docId, attempt, settleRetried, settledValue, settlesAsEqual = false) {
10311
10890
  const deadline = Date.now() + PRIMARY_CHANGE_RETRY_WINDOW_MS;
10312
10891
  let retryDelayMs = FORWARD_RETRY_INITIAL_DELAY_MS;
10313
10892
  let retried = false;
10893
+ let interrupted = false;
10894
+ let condition;
10895
+ const finalError = (error) => interrupted ? interruptedOutcome(indexName, docId, error) : error;
10314
10896
  for (; ; ) {
10315
10897
  const refusing = primaryFor(await deps.coordinator.getAllocation(indexName), docId);
10898
+ let outcome;
10316
10899
  try {
10317
- return await attempt();
10900
+ outcome = await beforeWindowEnds(attempt(condition), deadline);
10318
10901
  } catch (error) {
10319
10902
  if (retried && (await settleRetried([{ docId, error }])).has(docId)) return settledValue();
10320
- if (!(refusedByPrimaryChange(error, retriesEndedTerm) || forwardUndelivered(error)) || refusing === null) {
10321
- throw error;
10322
- }
10903
+ interrupted ||= forwardInterrupted(error);
10904
+ if (!retriable(error, settlesAsEqual) || refusing === null) throw finalError(error);
10323
10905
  let retryable;
10324
10906
  if (retriedAfterDelay(error)) {
10325
10907
  retryable = await pauseAndRereadTable(deps, indexName, retryDelayMs, deadline);
@@ -10327,20 +10909,36 @@ async function retryOnPrimaryChange(deps, indexName, docId, attempt, settleRetri
10327
10909
  } else {
10328
10910
  retryable = await awaitNewPrimary(deps, indexName, [refusing], deadline);
10329
10911
  }
10330
- if (!retryable) throw error;
10912
+ if (!retryable) throw finalError(error);
10913
+ condition = nextCondition(error, condition);
10331
10914
  retried = true;
10915
+ continue;
10332
10916
  }
10917
+ if (outcome === WINDOW_ENDED) throw windowEndedOutcome(indexName, docId);
10918
+ return outcome;
10333
10919
  }
10334
10920
  }
10335
- async function retryBatchOnPrimaryChange(deps, indexName, items, docIdOf, attempt, settleRetried, deadline, retriesEndedTerm) {
10921
+ async function retryBatchOnPrimaryChange(deps, indexName, items, docIdOf, attempt, settleRetried, deadline, settlesAsEqual) {
10336
10922
  const succeeded2 = [];
10337
10923
  const failed2 = [];
10924
+ const conditions = /* @__PURE__ */ new Map();
10925
+ const interrupted = /* @__PURE__ */ new Set();
10926
+ const failWith = (failure) => {
10927
+ failed2.push(
10928
+ interrupted.has(failure.docId) ? { docId: failure.docId, error: interruptedOutcome(indexName, failure.docId, failure.error) } : failure
10929
+ );
10930
+ };
10338
10931
  let pending = [...items];
10339
10932
  let retryDelayMs = FORWARD_RETRY_INITIAL_DELAY_MS;
10340
10933
  let retried = false;
10341
10934
  while (pending.length > 0) {
10342
10935
  const table = await deps.coordinator.getAllocation(indexName);
10343
- const result = await attempt(pending);
10936
+ const result = await beforeWindowEnds(attempt(pending, conditions), deadline);
10937
+ if (result === WINDOW_ENDED) {
10938
+ for (const item of pending)
10939
+ failed2.push({ docId: docIdOf(item), error: windowEndedOutcome(indexName, docIdOf(item)) });
10940
+ break;
10941
+ }
10344
10942
  succeeded2.push(...result.succeeded);
10345
10943
  const settled = retried ? await settleRetried(result.failed) : /* @__PURE__ */ new Set();
10346
10944
  const pendingById = new Map(pending.map((item) => [docIdOf(item), item]));
@@ -10352,8 +10950,9 @@ async function retryBatchOnPrimaryChange(deps, indexName, items, docIdOf, attemp
10352
10950
  }
10353
10951
  const item = pendingById.get(failure.docId);
10354
10952
  const refusing = primaryFor(table, failure.docId);
10355
- if (item === void 0 || refusing === null || !(refusedByPrimaryChange(failure.error, retriesEndedTerm) || forwardUndelivered(failure.error))) {
10356
- failed2.push(failure);
10953
+ if (forwardInterrupted(failure.error)) interrupted.add(failure.docId);
10954
+ if (item === void 0 || refusing === null || !retriable(failure.error, settlesAsEqual)) {
10955
+ failWith(failure);
10357
10956
  continue;
10358
10957
  }
10359
10958
  refused.push({ item, failure, refusing });
@@ -10372,9 +10971,13 @@ async function retryBatchOnPrimaryChange(deps, indexName, items, docIdOf, attemp
10372
10971
  );
10373
10972
  }
10374
10973
  if (!retryable) {
10375
- failed2.push(...refused.map((entry) => entry.failure));
10974
+ for (const entry of refused) failWith(entry.failure);
10376
10975
  break;
10377
10976
  }
10977
+ for (const { failure } of refused) {
10978
+ const condition = nextCondition(failure.error, conditions.get(failure.docId));
10979
+ if (condition !== void 0) conditions.set(failure.docId, condition);
10980
+ }
10378
10981
  pending = refused.map((entry) => entry.item);
10379
10982
  retried = true;
10380
10983
  }
@@ -10407,12 +11010,12 @@ async function settleEachBounded(inputs, limit, run) {
10407
11010
  await Promise.all(Array.from({ length: Math.min(limit, inputs.length) }, work));
10408
11011
  return outcomes;
10409
11012
  }
10410
- async function retryEachPartition(deps, indexName, table, items, docIdOf, attempt, settleRetried, retriesEndedTerm = false) {
11013
+ async function retryEachPartition(deps, indexName, table, items, docIdOf, attempt, settleRetried, settlesAsEqual = false) {
10411
11014
  const deadline = Date.now() + PRIMARY_CHANGE_RETRY_WINDOW_MS;
10412
11015
  const outcomes = await settleEachBounded(
10413
11016
  groupByPartition(table, items, docIdOf),
10414
11017
  PARTITION_WRITE_CONCURRENCY,
10415
- (group) => retryBatchOnPrimaryChange(deps, indexName, group, docIdOf, attempt, settleRetried, deadline, retriesEndedTerm)
11018
+ (group) => retryBatchOnPrimaryChange(deps, indexName, group, docIdOf, attempt, settleRetried, deadline, settlesAsEqual)
10416
11019
  );
10417
11020
  const merged = { succeeded: [], failed: [] };
10418
11021
  for (const outcome of outcomes) {
@@ -10423,7 +11026,6 @@ async function retryEachPartition(deps, indexName, table, items, docIdOf, attemp
10423
11026
  return merged;
10424
11027
  }
10425
11028
  var settleNothing = async () => /* @__PURE__ */ new Set();
10426
- var settleAbsentRemovals = async (failures) => new Set(failures.filter((failure) => hasCode(failure.error, ErrorCodes.DOC_NOT_FOUND)).map((failure) => failure.docId));
10427
11029
  async function derivedFieldsOf(deps, indexName) {
10428
11030
  const schema = await deps.coordinator.getSchema(indexName);
10429
11031
  if (schema === null) return /* @__PURE__ */ new Set();
@@ -10511,78 +11113,20 @@ async function replicateAppendedWrites(scope, appended, failed2, compensate, dep
10511
11113
  for (let index = abandoned.length - 1; index >= 0; index--) {
10512
11114
  const item = abandoned[index];
10513
11115
  try {
10514
- await compensate(item, abortError);
10515
- compensated.push(item);
11116
+ const compensation = await compensate(item, abortError);
11117
+ compensated.push({ item, compensation: compensation ?? null });
10516
11118
  } catch (err) {
10517
11119
  failed2.push({ docId: item.docId, error: asWriteError(err, ErrorCodes.DOC_VALIDATION_FAILED, item.docId) });
10518
11120
  }
10519
11121
  }
10520
11122
  const answer = await settleRollback(scope, first.entry.seqNo, abortError, failedAt);
10521
- for (const item of compensated) {
10522
- failed2.push({ docId: item.docId, error: asWriteError(answer, ErrorCodes.DOC_VALIDATION_FAILED, item.docId) });
11123
+ for (const { item, compensation } of compensated) {
11124
+ const marked = markAbandoned(answer, item.entry, compensation, item.previousRecord);
11125
+ failed2.push({ docId: item.docId, error: asWriteError(marked, ErrorCodes.DOC_VALIDATION_FAILED, item.docId) });
10523
11126
  }
10524
11127
  return succeeded2;
10525
11128
  }
10526
11129
 
10527
- // src/distribution/cluster-node/write-routing/numbered-write.ts
10528
- async function numberedWrite(scope, write, appendOutsideTheLog, deferCommit = false) {
10529
- const writes = scope.deps.numbering?.open(
10530
- scope.indexName,
10531
- scope.partitionId,
10532
- () => scope.deps.getReplicationLog(scope.indexName, scope.partitionId),
10533
- scope.assignment.primaryTerm,
10534
- deferCommit && scope.deps.commitPartitionLog !== void 0
10535
- );
10536
- let value;
10537
- try {
10538
- value = await write();
10539
- } finally {
10540
- writes?.close();
10541
- }
10542
- const recorded = writes?.entries.at(-1);
10543
- if (recorded !== void 0) return { value, entry: recorded };
10544
- const admitted = (documentId) => writes?.admittedDocument(documentId);
10545
- return { value, entry: await appendOutsideTheLog(value, admitted) };
10546
- }
10547
- async function storedCopyOf(scope, documentId) {
10548
- const stored = await scope.deps.engine.get(scope.indexName, documentId);
10549
- if (stored === void 0) {
10550
- throw new NarsilError(
10551
- ErrorCodes.REPLICATION_ENTRY_INVALID,
10552
- `Document '${documentId}' could not be read back for replication`,
10553
- { indexName: scope.indexName, documentId, partitionId: scope.partitionId }
10554
- );
10555
- }
10556
- return stored;
10557
- }
10558
- async function appendStoredCopy(scope, documentId, admitted) {
10559
- const admittedDocument = admitted(documentId);
10560
- if (admittedDocument instanceof Uint8Array) {
10561
- return scope.deps.getReplicationLog(scope.indexName, scope.partitionId).append({
10562
- primaryTerm: scope.assignment.primaryTerm,
10563
- operation: "INDEX",
10564
- partitionId: scope.partitionId,
10565
- indexName: scope.indexName,
10566
- documentId,
10567
- document: admittedDocument
10568
- });
10569
- }
10570
- const stored = await storedCopyOf(scope, documentId);
10571
- return appendIndexReplicationEntry(
10572
- scope.indexName,
10573
- scope.partitionId,
10574
- scope.assignment,
10575
- documentId,
10576
- stored,
10577
- scope.deps
10578
- );
10579
- }
10580
- function appendDeletion(scope, documentId) {
10581
- return Promise.resolve(
10582
- appendDeleteReplicationEntry(scope.indexName, scope.partitionId, scope.assignment, documentId, scope.deps)
10583
- );
10584
- }
10585
-
10586
11130
  // src/distribution/cluster-node/write-routing/failures.ts
10587
11131
  function throwWriteFailure(error) {
10588
11132
  if (error instanceof Error) {
@@ -10606,15 +11150,17 @@ function createRollbackFailure(operation, scope, documentId, originalError, roll
10606
11150
  }
10607
11151
  async function compensateInsert(scope, documentId, originalError) {
10608
11152
  try {
10609
- await numberedWrite(
11153
+ const removed = await numberedWrite(
10610
11154
  scope,
10611
11155
  () => scope.deps.engine.remove(scope.indexName, documentId),
10612
11156
  () => appendDeletion(scope, documentId)
10613
11157
  );
11158
+ return removed.entry;
10614
11159
  } catch (rollbackError) {
10615
11160
  if (!(rollbackError instanceof NarsilError && rollbackError.code === ErrorCodes.DOC_NOT_FOUND)) {
10616
11161
  throw createRollbackFailure("insert", scope, documentId, originalError, rollbackError);
10617
11162
  }
11163
+ return void 0;
10618
11164
  }
10619
11165
  }
10620
11166
  async function compensateUpdate(scope, documentId, previousDocument, originalError) {
@@ -10628,11 +11174,12 @@ async function compensateUpdate(scope, documentId, previousDocument, originalErr
10628
11174
  );
10629
11175
  }
10630
11176
  try {
10631
- await numberedWrite(
11177
+ const restored = await numberedWrite(
10632
11178
  scope,
10633
11179
  () => scope.deps.engine.update(scope.indexName, documentId, previousDocument),
10634
11180
  (_, admitted) => appendStoredCopy(scope, documentId, admitted)
10635
11181
  );
11182
+ return restored.entry;
10636
11183
  } catch (rollbackError) {
10637
11184
  throw createRollbackFailure("update", scope, documentId, originalError, rollbackError);
10638
11185
  }
@@ -10648,11 +11195,12 @@ async function compensateRemove(scope, documentId, previousDocument, originalErr
10648
11195
  );
10649
11196
  }
10650
11197
  try {
10651
- await numberedWrite(
11198
+ const restored = await numberedWrite(
10652
11199
  scope,
10653
11200
  () => scope.deps.engine.insert(scope.indexName, previousDocument, documentId),
10654
11201
  (_, admitted) => appendStoredCopy(scope, documentId, admitted)
10655
11202
  );
11203
+ return restored.entry;
10656
11204
  } catch (rollbackError) {
10657
11205
  throw createRollbackFailure("remove", scope, documentId, originalError, rollbackError);
10658
11206
  }
@@ -10747,6 +11295,141 @@ async function assertReshardOpen(state, coordinator, indexName, partitionId, pri
10747
11295
  }
10748
11296
  if (state.blocks.has(key)) throw rebalancingBackpressure(indexName, partitionId);
10749
11297
  }
11298
+ function isRecord4(value) {
11299
+ return value !== null && typeof value === "object" && !Array.isArray(value);
11300
+ }
11301
+ function requireCount(record, field, payloadName) {
11302
+ const value = record[field];
11303
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
11304
+ throw new Error(`Invalid ${payloadName}: "${field}" must be a non-negative integer`);
11305
+ }
11306
+ return value;
11307
+ }
11308
+ function requireIndexName(record, payloadName) {
11309
+ if (typeof record.indexName !== "string" || record.indexName.length === 0) {
11310
+ throw new Error(`Invalid ${payloadName}: "indexName" must be a non-empty string`);
11311
+ }
11312
+ return record.indexName;
11313
+ }
11314
+ function createTermStartMessage(payload, sourceId) {
11315
+ return {
11316
+ type: ReplicationMessageTypes.TERM_START,
11317
+ sourceId,
11318
+ requestId: generateId(),
11319
+ payload: encode(payload)
11320
+ };
11321
+ }
11322
+ function createTermStartAckMessage(payload, sourceId, requestId) {
11323
+ return {
11324
+ type: ReplicationMessageTypes.TERM_START_ACK,
11325
+ sourceId,
11326
+ requestId,
11327
+ payload: encode(payload)
11328
+ };
11329
+ }
11330
+ function validateTermStartPayload(payload) {
11331
+ const decoded = decode(payload);
11332
+ if (!isRecord4(decoded)) {
11333
+ throw new Error("Invalid TermStartPayload: expected an object");
11334
+ }
11335
+ const validated = {
11336
+ indexName: requireIndexName(decoded, "TermStartPayload"),
11337
+ partitionId: requireCount(decoded, "partitionId", "TermStartPayload"),
11338
+ primaryTerm: requireCount(decoded, "primaryTerm", "TermStartPayload"),
11339
+ lastSeqNo: requireCount(decoded, "lastSeqNo", "TermStartPayload"),
11340
+ lastPrimaryTerm: requireCount(decoded, "lastPrimaryTerm", "TermStartPayload")
11341
+ };
11342
+ const indexUuid = optionalIndexUuid(decoded.indexUuid, "TermStartPayload");
11343
+ if (indexUuid !== void 0) validated.indexUuid = indexUuid;
11344
+ return validated;
11345
+ }
11346
+ function termStartAckOf(response, request) {
11347
+ if (response.type !== ReplicationMessageTypes.TERM_START_ACK) return null;
11348
+ try {
11349
+ const decoded = decode(response.payload);
11350
+ if (!isRecord4(decoded)) return null;
11351
+ const answer = {
11352
+ indexName: requireIndexName(decoded, "TermStartAckPayload"),
11353
+ partitionId: requireCount(decoded, "partitionId", "TermStartAckPayload"),
11354
+ lastSeqNo: requireCount(decoded, "lastSeqNo", "TermStartAckPayload"),
11355
+ lastPrimaryTerm: requireCount(decoded, "lastPrimaryTerm", "TermStartAckPayload")
11356
+ };
11357
+ return answer.indexName === request.indexName && answer.partitionId === request.partitionId ? answer : null;
11358
+ } catch {
11359
+ return null;
11360
+ }
11361
+ }
11362
+
11363
+ // src/distribution/cluster-node/write-routing/term-start.ts
11364
+ async function startTermInQueue(scope) {
11365
+ const { deps, indexName, partitionId, assignment } = scope;
11366
+ const key = partitionKey2(indexName, partitionId);
11367
+ if ((deps.catchUp.startedTerms.get(key) ?? 0) >= assignment.primaryTerm) return false;
11368
+ deps.catchUp.startedTerms.set(key, assignment.primaryTerm);
11369
+ const copies = getInSyncReplicaTargets(assignment, deps.nodeId);
11370
+ if (copies.length === 0) return false;
11371
+ const work = startTermOnCopies(scope, copies).catch(() => void 0);
11372
+ await withinDeadline(work, replicationTimeoutOf(deps), () => new Error("The term start outlasted its hold")).catch(
11373
+ () => void 0
11374
+ );
11375
+ return true;
11376
+ }
11377
+ async function startTermOnCopies(scope, copies) {
11378
+ const { deps, indexName, partitionId, assignment } = scope;
11379
+ const log = deps.getReplicationLog(indexName, partitionId);
11380
+ const lastSeqNo = log.localLogEnd;
11381
+ const request = {
11382
+ indexName,
11383
+ partitionId,
11384
+ primaryTerm: assignment.primaryTerm,
11385
+ lastSeqNo,
11386
+ lastPrimaryTerm: lastSeqNo === 0 ? 0 : log.termAt(lastSeqNo) ?? 0
11387
+ };
11388
+ if (scope.indexUuid !== void 0) request.indexUuid = scope.indexUuid;
11389
+ const inStep = await Promise.all(copies.map((copy) => settleCopy(scope, log, request, copy).catch(() => false)));
11390
+ const removed = copies.filter((_, at) => !inStep[at]);
11391
+ await removeFailedReplicasFromInsync({ indexName, partitionId, primaryTerm: assignment.primaryTerm }, removed, deps);
11392
+ }
11393
+ async function settleCopy(scope, log, request, copy) {
11394
+ const { deps } = scope;
11395
+ const message = createTermStartMessage(request, deps.nodeId);
11396
+ const response = await sendThroughTargets(
11397
+ await resolveNodeTargets(copy, deps),
11398
+ (target) => deps.transport.send(target, message),
11399
+ {
11400
+ message: `Node '${copy}' did not answer the start of term ${request.primaryTerm}`,
11401
+ details: { indexName: request.indexName, partitionId: request.partitionId, replicaNodeId: copy }
11402
+ }
11403
+ );
11404
+ const answer = termStartAckOf(response, request);
11405
+ if (answer === null) return false;
11406
+ if (answer.lastSeqNo === request.lastSeqNo && answer.lastPrimaryTerm === request.lastPrimaryTerm) return true;
11407
+ if (answer.lastSeqNo >= request.lastSeqNo) return false;
11408
+ const termHere = answer.lastSeqNo === 0 ? 0 : log.termAt(answer.lastSeqNo);
11409
+ if (termHere !== answer.lastPrimaryTerm) return false;
11410
+ return sendMissingEntries(scope, log, copy, answer.lastSeqNo);
11411
+ }
11412
+ async function sendMissingEntries(scope, log, copy, heldSeqNo) {
11413
+ const { deps } = scope;
11414
+ const missing = log.getEntriesFrom(heldSeqNo + 1);
11415
+ if (missing[0]?.seqNo !== heldSeqNo + 1) return false;
11416
+ for (const chunk of chunkReplicationEntries(missing.map((entry) => ({ entry })))) {
11417
+ const entries = chunk.map((item) => item.entry);
11418
+ const first = entries[0];
11419
+ if (first === void 0) continue;
11420
+ const result = await replicateBatchToReplicas(
11421
+ entries,
11422
+ [copy],
11423
+ deps.transport,
11424
+ deps.nodeId,
11425
+ deps.resolveNodeTargets,
11426
+ scope.indexUuid,
11427
+ previousTermOf(log, first.seqNo)
11428
+ );
11429
+ if (!result.acknowledged.includes(copy)) return false;
11430
+ }
11431
+ return true;
11432
+ }
10750
11433
 
10751
11434
  // src/distribution/cluster-node/write-routing/leading-queue.ts
10752
11435
  var REFUSALS_FROM_LOCAL_COPY = /* @__PURE__ */ new Set([ErrorCodes.DOC_NOT_FOUND, ErrorCodes.DOC_ALREADY_EXISTS]);
@@ -10845,6 +11528,7 @@ async function inLeadingPartitionQueue(scope, task, failed2) {
10845
11528
  scope.partitionId,
10846
11529
  async (hold) => {
10847
11530
  current = { ...await currentScope(scope), holdQueue: hold };
11531
+ if (await startTermInQueue(current)) current = { ...await currentScope(scope), holdQueue: hold };
10848
11532
  logEndBefore = logEndOf(current);
10849
11533
  return task(current);
10850
11534
  }
@@ -10869,6 +11553,10 @@ async function inLeadingPartitionQueue(scope, task, failed2) {
10869
11553
  if (failed2 !== void 0) await confirmBatchRefusals(current, failed2);
10870
11554
  return result;
10871
11555
  }
11556
+ function startLeadingTerm(deps, indexName, partitionId, assignment) {
11557
+ if ((deps.catchUp.startedTerms.get(partitionKey2(indexName, partitionId)) ?? 0) >= assignment.primaryTerm) return;
11558
+ inLeadingPartitionQueue({ indexName, partitionId, assignment, deps }, async () => void 0).catch(() => void 0);
11559
+ }
10872
11560
 
10873
11561
  // src/distribution/cluster-node/memory/document-bound.ts
10874
11562
  var SINGLE_CLASS_REPEATED = /^\[(?:[^\\\]]|\\.)*\]\+$/;
@@ -11139,6 +11827,83 @@ async function applyInProjectedGroups(deps, indexName, batch) {
11139
11827
  }
11140
11828
  }
11141
11829
 
11830
+ // src/distribution/cluster-node/write-routing/write-condition.ts
11831
+ var CONDITION_FIELDS = [
11832
+ "ifSeqNo",
11833
+ "ifPrimaryTerm",
11834
+ "abandonedSeqNo",
11835
+ "abandonedPrimaryTerm",
11836
+ "compensationSeqNo"
11837
+ ];
11838
+ function isSequenceNumber2(value) {
11839
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
11840
+ }
11841
+ function conditionOfFields(fields, operation, documentId) {
11842
+ const present = CONDITION_FIELDS.filter((field) => fields[field] !== void 0 && fields[field] !== null);
11843
+ if (present.length === 0) return void 0;
11844
+ if (present.length !== CONDITION_FIELDS.length || operation === "insert") {
11845
+ throw new NarsilError(
11846
+ ErrorCodes.REPLICATION_ENTRY_INVALID,
11847
+ operation === "insert" ? `A forwarded insert of document '${documentId}' cannot carry a write condition` : `A forwarded ${operation} of document '${documentId}' must carry all five condition fields or none`,
11848
+ { documentId, operation, present }
11849
+ );
11850
+ }
11851
+ const values = CONDITION_FIELDS.map((field) => fields[field]);
11852
+ if (!values.every(isSequenceNumber2)) {
11853
+ throw new NarsilError(
11854
+ ErrorCodes.REPLICATION_ENTRY_INVALID,
11855
+ `The condition fields of a forwarded ${operation} of document '${documentId}' must be non-negative integers`,
11856
+ { documentId, operation }
11857
+ );
11858
+ }
11859
+ const [ifSeqNo, ifPrimaryTerm, abandonedSeqNo, abandonedPrimaryTerm, compensationSeqNo] = values;
11860
+ return { ifSeqNo, ifPrimaryTerm, abandonedSeqNo, abandonedPrimaryTerm, compensationSeqNo };
11861
+ }
11862
+ function outcomeUnknownAfterRetry(scope, documentId, because) {
11863
+ return new NarsilError(
11864
+ ErrorCodes.REPLICATION_OUTCOME_UNKNOWN,
11865
+ `A write to document '${documentId}' in partition ${scope.partitionId} of index '${scope.indexName}' was sent again after the earlier primary's term ended, and ${because}, so the earlier attempt may or may not have taken effect. Read the document before you retry the write.`,
11866
+ {
11867
+ indexName: scope.indexName,
11868
+ partitionId: scope.partitionId,
11869
+ primaryTerm: scope.assignment.primaryTerm,
11870
+ reason: "term_ended"
11871
+ }
11872
+ );
11873
+ }
11874
+ function decideCondition(scope, documentId, condition) {
11875
+ const records = scope.deps.writeRecords;
11876
+ const { indexName, partitionId } = scope;
11877
+ const failed2 = records?.heldEntry(indexName, partitionId, condition.abandonedSeqNo, condition.abandonedPrimaryTerm);
11878
+ if (records === void 0 || failed2 === void 0 || failed2 === "uncovered") {
11879
+ throw outcomeUnknownAfterRetry(scope, documentId, "this copy no longer records the term of the earlier attempt");
11880
+ }
11881
+ if (failed2 === "held") {
11882
+ const compensation = records.heldEntry(
11883
+ indexName,
11884
+ partitionId,
11885
+ condition.compensationSeqNo,
11886
+ condition.abandonedPrimaryTerm
11887
+ );
11888
+ if (compensation === "uncovered") {
11889
+ throw outcomeUnknownAfterRetry(scope, documentId, "this copy no longer records the term of the earlier attempt");
11890
+ }
11891
+ if (compensation === "absent") return "acknowledge";
11892
+ }
11893
+ const record = records.recordOf(indexName, partitionId, documentId);
11894
+ if (record === null) {
11895
+ throw outcomeUnknownAfterRetry(scope, documentId, "this copy holds no write record for the document");
11896
+ }
11897
+ const expectedSeqNo = failed2 === "held" ? condition.compensationSeqNo : condition.ifSeqNo;
11898
+ const expectedTerm = failed2 === "held" ? condition.abandonedPrimaryTerm : condition.ifPrimaryTerm;
11899
+ if (record.seqNo === expectedSeqNo && record.primaryTerm === expectedTerm) return "apply";
11900
+ throw new NarsilError(
11901
+ ErrorCodes.REPLICATION_WRITE_CONFLICT,
11902
+ `Another write touched document '${documentId}' in index '${indexName}' after the earlier primary's attempt, so this primary refused the write that was sent again. The write did not take effect.`,
11903
+ { indexName, partitionId, documentId }
11904
+ );
11905
+ }
11906
+
11142
11907
  // src/distribution/cluster-node/write-routing/primary-writes.ts
11143
11908
  async function applyPrimaryInsert(indexName, document, docId, partitionId, assignment, deps, options) {
11144
11909
  assertSufficientActiveReplicas(indexName, partitionId, assignment, deps);
@@ -11164,11 +11929,13 @@ async function applyPrimaryInsert(indexName, document, docId, partitionId, assig
11164
11929
  }
11165
11930
  });
11166
11931
  }
11167
- async function applyPrimaryRemove(indexName, docId, partitionId, assignment, deps) {
11932
+ async function applyPrimaryRemove(indexName, docId, partitionId, assignment, deps, condition) {
11168
11933
  assertSufficientActiveReplicas(indexName, partitionId, assignment, deps);
11169
11934
  const queued = { indexName, partitionId, assignment, deps };
11170
11935
  await inLeadingPartitionQueue(queued, async (scope) => {
11936
+ if (condition !== void 0 && decideCondition(scope, docId, condition) === "acknowledge") return;
11171
11937
  const previousDocument = await deps.engine.get(indexName, docId);
11938
+ const previousRecord = previousRecordOf(scope, docId);
11172
11939
  const removed = await numberedWrite(
11173
11940
  scope,
11174
11941
  () => deps.engine.remove(indexName, docId),
@@ -11178,18 +11945,21 @@ async function applyPrimaryRemove(indexName, docId, partitionId, assignment, dep
11178
11945
  await replicateEntry(removed.entry, scope.assignment, deps, scope.indexUuid);
11179
11946
  } catch (error) {
11180
11947
  const failedAt = Date.now();
11181
- await compensateRemove(scope, docId, previousDocument, error);
11182
- throwWriteFailure(await settleRollback(scope, removed.entry.seqNo, error, failedAt));
11948
+ const compensation = await compensateRemove(scope, docId, previousDocument, error);
11949
+ const answer = await settleRollback(scope, removed.entry.seqNo, error, failedAt);
11950
+ throwWriteFailure(markAbandoned(answer, removed.entry, compensation, previousRecord));
11183
11951
  }
11184
11952
  });
11185
11953
  }
11186
- async function applyPrimaryUpdate(indexName, docId, document, partitionId, assignment, deps) {
11954
+ async function applyPrimaryUpdate(indexName, docId, document, partitionId, assignment, deps, condition) {
11187
11955
  assertSufficientActiveReplicas(indexName, partitionId, assignment, deps);
11188
11956
  const queued = { indexName, partitionId, assignment, deps };
11189
11957
  await inLeadingPartitionQueue(queued, async (scope) => {
11958
+ if (condition !== void 0 && decideCondition(scope, docId, condition) === "acknowledge") return;
11190
11959
  const projected = projectOne(deps, indexName, document);
11191
11960
  try {
11192
11961
  const previousDocument = await deps.engine.get(indexName, docId);
11962
+ const previousRecord = previousRecordOf(scope, docId);
11193
11963
  const updated = await numberedWrite(
11194
11964
  scope,
11195
11965
  () => deps.engine.update(indexName, docId, document),
@@ -11199,8 +11969,9 @@ async function applyPrimaryUpdate(indexName, docId, document, partitionId, assig
11199
11969
  await replicateEntry(updated.entry, scope.assignment, deps, scope.indexUuid);
11200
11970
  } catch (error) {
11201
11971
  const failedAt = Date.now();
11202
- await compensateUpdate(scope, docId, previousDocument, error);
11203
- throwWriteFailure(await settleRollback(scope, updated.entry.seqNo, error, failedAt));
11972
+ const compensation = await compensateUpdate(scope, docId, previousDocument, error);
11973
+ const answer = await settleRollback(scope, updated.entry.seqNo, error, failedAt);
11974
+ throwWriteFailure(markAbandoned(answer, updated.entry, compensation, previousRecord));
11204
11975
  }
11205
11976
  } finally {
11206
11977
  projected.release();
@@ -11257,6 +12028,7 @@ async function applyPrimaryUpdateBatch(indexName, updates, partitionId, assignme
11257
12028
  queued,
11258
12029
  async (scope) => {
11259
12030
  const appended = [];
12031
+ const acknowledged = [];
11260
12032
  const groupCommit = createGroupCommit(scope, appended, failed2);
11261
12033
  await applyInProjectedGroups(deps, indexName, {
11262
12034
  items: updates,
@@ -11267,14 +12039,19 @@ async function applyPrimaryUpdateBatch(indexName, updates, partitionId, assignme
11267
12039
  apply: async (update) => {
11268
12040
  if (groupCommit.refuse(update.docId)) return;
11269
12041
  try {
12042
+ if (update.condition !== void 0 && decideCondition(scope, update.docId, update.condition) === "acknowledge") {
12043
+ acknowledged.push(update.docId);
12044
+ return;
12045
+ }
11270
12046
  const previousDocument = await deps.engine.get(indexName, update.docId);
12047
+ const previousRecord = previousRecordOf(scope, update.docId);
11271
12048
  const updated = await numberedWrite(
11272
12049
  scope,
11273
12050
  () => deps.engine.update(indexName, update.docId, update.document),
11274
12051
  (_, admitted) => appendStoredCopy(scope, update.docId, admitted),
11275
12052
  true
11276
12053
  );
11277
- appended.push({ docId: update.docId, entry: updated.entry, previousDocument });
12054
+ appended.push({ docId: update.docId, entry: updated.entry, previousDocument, previousRecord });
11278
12055
  } catch (err) {
11279
12056
  failed2.push({
11280
12057
  docId: update.docId,
@@ -11283,19 +12060,20 @@ async function applyPrimaryUpdateBatch(indexName, updates, partitionId, assignme
11283
12060
  }
11284
12061
  }
11285
12062
  });
11286
- return replicateAppendedWrites(
12063
+ const replicated = await replicateAppendedWrites(
11287
12064
  scope,
11288
12065
  appended,
11289
12066
  failed2,
11290
12067
  (write, error) => compensateUpdate(scope, write.docId, write.previousDocument, error),
11291
12068
  deps
11292
12069
  );
12070
+ return [...acknowledged, ...replicated];
11293
12071
  },
11294
12072
  failed2
11295
12073
  );
11296
12074
  return { succeeded: succeeded2, failed: failed2 };
11297
12075
  }
11298
- async function applyPrimaryRemoveBatch(indexName, docIds, partitionId, assignment, deps) {
12076
+ async function applyPrimaryRemoveBatch(indexName, docIds, partitionId, assignment, deps, conditions) {
11299
12077
  assertSufficientActiveReplicas(indexName, partitionId, assignment, deps);
11300
12078
  const queued = { indexName, partitionId, assignment, deps };
11301
12079
  const failed2 = [];
@@ -11303,29 +12081,37 @@ async function applyPrimaryRemoveBatch(indexName, docIds, partitionId, assignmen
11303
12081
  queued,
11304
12082
  async (scope) => {
11305
12083
  const appended = [];
12084
+ const acknowledged = [];
11306
12085
  const groupCommit = createGroupCommit(scope, appended, failed2);
11307
12086
  for (const docId of docIds) {
11308
12087
  try {
12088
+ const condition = conditions?.get(docId);
12089
+ if (condition !== void 0 && decideCondition(scope, docId, condition) === "acknowledge") {
12090
+ acknowledged.push(docId);
12091
+ continue;
12092
+ }
11309
12093
  const previousDocument = await deps.engine.get(indexName, docId);
12094
+ const previousRecord = previousRecordOf(scope, docId);
11310
12095
  const removed = await numberedWrite(
11311
12096
  scope,
11312
12097
  () => deps.engine.remove(indexName, docId),
11313
12098
  () => appendDeletion(scope, docId),
11314
12099
  true
11315
12100
  );
11316
- appended.push({ docId, entry: removed.entry, previousDocument });
12101
+ appended.push({ docId, entry: removed.entry, previousDocument, previousRecord });
11317
12102
  } catch (err) {
11318
12103
  failed2.push({ docId, error: asWriteError(err, ErrorCodes.QUERY_ROUTING_FAILED, docId) });
11319
12104
  }
11320
12105
  }
11321
12106
  await groupCommit.afterGroup();
11322
- return replicateAppendedWrites(
12107
+ const replicated = await replicateAppendedWrites(
11323
12108
  scope,
11324
12109
  appended,
11325
12110
  failed2,
11326
12111
  (write, error) => compensateRemove(scope, write.docId, write.previousDocument, error),
11327
12112
  deps
11328
12113
  );
12114
+ return [...acknowledged, ...replicated];
11329
12115
  },
11330
12116
  failed2
11331
12117
  );
@@ -11343,6 +12129,7 @@ function applyForwardedWrite(payload, deps) {
11343
12129
  );
11344
12130
  }
11345
12131
  async function applyAdmittedForwardedWrite(payload, deps) {
12132
+ const condition = conditionOfFields(payload, payload.operation, payload.documentId);
11346
12133
  const resolution = await resolvePrimaryAssignment(payload.indexName, payload.documentId, deps, true);
11347
12134
  if (resolution === null) {
11348
12135
  throw new NarsilError(
@@ -11369,7 +12156,14 @@ async function applyAdmittedForwardedWrite(payload, deps) {
11369
12156
  );
11370
12157
  }
11371
12158
  if (payload.operation === "remove") {
11372
- await applyPrimaryRemove(payload.indexName, payload.documentId, resolution.partitionId, resolution.assignment, deps);
12159
+ await applyPrimaryRemove(
12160
+ payload.indexName,
12161
+ payload.documentId,
12162
+ resolution.partitionId,
12163
+ resolution.assignment,
12164
+ deps,
12165
+ condition
12166
+ );
11373
12167
  return payload.documentId;
11374
12168
  }
11375
12169
  const replacement = await resolveForwardedUpdateDocument(
@@ -11385,7 +12179,8 @@ async function applyAdmittedForwardedWrite(payload, deps) {
11385
12179
  replacement,
11386
12180
  resolution.partitionId,
11387
12181
  resolution.assignment,
11388
- deps
12182
+ deps,
12183
+ condition
11389
12184
  );
11390
12185
  return payload.documentId;
11391
12186
  }
@@ -11415,7 +12210,8 @@ function toWireOperation(item) {
11415
12210
  documentId: item.documentId,
11416
12211
  operation: item.operation,
11417
12212
  document: item.document === null ? null : encodeDocument(item.document),
11418
- updateFields: null
12213
+ updateFields: null,
12214
+ ...conditionFields(item.condition)
11419
12215
  };
11420
12216
  }
11421
12217
  function chunkWireOperations(operations) {
@@ -11439,10 +12235,18 @@ async function forwardSingleItem(indexName, item, primaryNodeId, deps, indexUuid
11439
12235
  documentId: item.documentId
11440
12236
  });
11441
12237
  }
11442
- await forwardUpdateToRemote(indexName, item.document, item.documentId, primaryNodeId, deps, indexUuid);
12238
+ await forwardUpdateToRemote(
12239
+ indexName,
12240
+ item.document,
12241
+ item.documentId,
12242
+ primaryNodeId,
12243
+ deps,
12244
+ indexUuid,
12245
+ item.condition
12246
+ );
11443
12247
  return item.documentId;
11444
12248
  }
11445
- await forwardRemoveToRemote(indexName, item.documentId, primaryNodeId, deps, indexUuid);
12249
+ await forwardRemoveToRemote(indexName, item.documentId, primaryNodeId, deps, indexUuid, item.condition);
11446
12250
  return item.documentId;
11447
12251
  }
11448
12252
  async function forwardBatchToRemote(indexName, items, primaryNodeId, deps, indexUuid) {
@@ -11498,17 +12302,19 @@ async function forwardBatchToRemote(indexName, items, primaryNodeId, deps, index
11498
12302
  succeeded2.push(operationResult.documentId);
11499
12303
  continue;
11500
12304
  }
12305
+ const refusal = new NarsilError(
12306
+ operationResult.errorCode ?? ErrorCodes.QUERY_ROUTING_FAILED,
12307
+ operationResult.errorMessage ?? `Remote primary rejected forwarded operation '${operationResult.documentId}'`,
12308
+ { ...operationResult.errorDetails ?? {}, indexName, primaryNodeId }
12309
+ );
12310
+ const abandonedWrite = abandonedWriteFrom(operationResult.abandonedWrite);
11501
12311
  failed2.push({
11502
12312
  docId: operationResult.documentId,
11503
- error: new NarsilError(
11504
- operationResult.errorCode ?? ErrorCodes.QUERY_ROUTING_FAILED,
11505
- operationResult.errorMessage ?? `Remote primary rejected forwarded operation '${operationResult.documentId}'`,
11506
- { ...operationResult.errorDetails ?? {}, indexName, primaryNodeId }
11507
- )
12313
+ error: abandonedWrite === void 0 ? refusal : withAbandonedWrite(refusal, abandonedWrite)
11508
12314
  });
11509
12315
  }
11510
12316
  } catch (err) {
11511
- if (forwardUndelivered(err)) undelivered = err;
12317
+ if (err instanceof NarsilError && (forwardUnsent(err) || forwardInterrupted(err))) undelivered = unsentAfter(err);
11512
12318
  for (const operation of chunk) {
11513
12319
  failed2.push({ docId: operation.documentId, error: toForwardError(err, operation.documentId) });
11514
12320
  }
@@ -11575,7 +12381,7 @@ function routeInsertBatch(indexName, documents, deps, options) {
11575
12381
  async function routeAdmittedInsertBatch(indexName, documents, deps, options) {
11576
12382
  const table = await routableAllocation(deps.coordinator, indexName);
11577
12383
  if (table === null) {
11578
- return deps.engine.insertBatch(indexName, documents, options);
12384
+ return writeLocalOnly(deps, indexName, () => deps.engine.insertBatch(indexName, documents, options));
11579
12385
  }
11580
12386
  requireWritableIndex(table, deps.memoryView);
11581
12387
  const withIds = documents.map((doc) => ({
@@ -11640,7 +12446,7 @@ async function routeInsertItems(table, indexName, withIds, deps, options) {
11640
12446
  async function routeRemoveBatch(indexName, docIds, deps) {
11641
12447
  const table = await routableAllocation(deps.coordinator, indexName);
11642
12448
  if (table === null) {
11643
- return deps.engine.removeBatch(indexName, docIds);
12449
+ return writeLocalOnly(deps, indexName, () => deps.engine.removeBatch(indexName, docIds));
11644
12450
  }
11645
12451
  return retryEachPartition(
11646
12452
  deps,
@@ -11648,17 +12454,24 @@ async function routeRemoveBatch(indexName, docIds, deps) {
11648
12454
  table,
11649
12455
  docIds,
11650
12456
  (docId) => docId,
11651
- async (pending) => routeRemoveItems(await requireRoutableTable(deps, indexName), indexName, pending, deps),
11652
- settleAbsentRemovals
12457
+ async (pending, conditions) => routeRemoveItems(await requireRoutableTable(deps, indexName), indexName, pending, deps, conditions),
12458
+ settleNothing
11653
12459
  );
11654
12460
  }
11655
- async function routeRemoveItems(table, indexName, docIds, deps) {
12461
+ async function routeRemoveItems(table, indexName, docIds, deps, conditions) {
11656
12462
  const routed = routeBatchItems(table, indexName, docIds, (docId) => docId, deps.nodeId);
11657
12463
  const succeeded2 = [];
11658
12464
  const failed2 = routed.failed;
11659
12465
  for (const [partitionId, group] of routed.localGroups) {
11660
12466
  try {
11661
- const result = await applyPrimaryRemoveBatch(indexName, group.items, partitionId, group.assignment, deps);
12467
+ const result = await applyPrimaryRemoveBatch(
12468
+ indexName,
12469
+ group.items,
12470
+ partitionId,
12471
+ group.assignment,
12472
+ deps,
12473
+ conditions
12474
+ );
11662
12475
  succeeded2.push(...result.succeeded);
11663
12476
  failed2.push(...result.failed);
11664
12477
  } catch (err) {
@@ -11668,7 +12481,14 @@ async function routeRemoveItems(table, indexName, docIds, deps) {
11668
12481
  for (const [primaryNodeId, items] of routed.remoteGroups) {
11669
12482
  const result = await forwardBatchToRemote(
11670
12483
  indexName,
11671
- items.map((docId) => ({ documentId: docId, operation: "remove", document: null })),
12484
+ items.map(
12485
+ (docId) => ({
12486
+ documentId: docId,
12487
+ operation: "remove",
12488
+ document: null,
12489
+ condition: conditions.get(docId)
12490
+ })
12491
+ ),
11672
12492
  primaryNodeId,
11673
12493
  deps,
11674
12494
  table.indexUuid
@@ -11689,7 +12509,7 @@ function routeUpdateBatch(indexName, updates, deps) {
11689
12509
  async function routeAdmittedUpdateBatch(indexName, updates, deps) {
11690
12510
  const table = await routableAllocation(deps.coordinator, indexName);
11691
12511
  if (table === null) {
11692
- return deps.engine.updateBatch(indexName, updates);
12512
+ return writeLocalOnly(deps, indexName, () => deps.engine.updateBatch(indexName, updates));
11693
12513
  }
11694
12514
  requireWritableIndex(table, deps.memoryView);
11695
12515
  return retryEachPartition(
@@ -11698,17 +12518,23 @@ async function routeAdmittedUpdateBatch(indexName, updates, deps) {
11698
12518
  table,
11699
12519
  updates,
11700
12520
  (update) => update.docId,
11701
- async (pending) => routeUpdateItems(await requireRoutableTable(deps, indexName), indexName, pending, deps),
12521
+ async (pending, conditions) => routeUpdateItems(await requireRoutableTable(deps, indexName), indexName, pending, deps, conditions),
11702
12522
  settleNothing
11703
12523
  );
11704
12524
  }
11705
- async function routeUpdateItems(table, indexName, updates, deps) {
12525
+ async function routeUpdateItems(table, indexName, updates, deps, conditions) {
11706
12526
  const routed = routeBatchItems(table, indexName, updates, (update) => update.docId, deps.nodeId);
11707
12527
  const succeeded2 = [];
11708
12528
  const failed2 = routed.failed;
11709
12529
  for (const [partitionId, group] of routed.localGroups) {
11710
12530
  try {
11711
- const result = await applyPrimaryUpdateBatch(indexName, group.items, partitionId, group.assignment, deps);
12531
+ const result = await applyPrimaryUpdateBatch(
12532
+ indexName,
12533
+ group.items.map((item) => ({ ...item, condition: conditions.get(item.docId) })),
12534
+ partitionId,
12535
+ group.assignment,
12536
+ deps
12537
+ );
11712
12538
  succeeded2.push(...result.succeeded);
11713
12539
  failed2.push(...result.failed);
11714
12540
  } catch (err) {
@@ -11725,7 +12551,12 @@ async function routeUpdateItems(table, indexName, updates, deps) {
11725
12551
  const result = await forwardBatchToRemote(
11726
12552
  indexName,
11727
12553
  items.map(
11728
- (update) => ({ documentId: update.docId, operation: "update", document: update.document })
12554
+ (update) => ({
12555
+ documentId: update.docId,
12556
+ operation: "update",
12557
+ document: update.document,
12558
+ condition: conditions.get(update.docId)
12559
+ })
11729
12560
  ),
11730
12561
  primaryNodeId,
11731
12562
  deps,
@@ -11855,17 +12686,20 @@ async function applyRun(indexName, partitionId, run, assignment, deps, results)
11855
12686
  }
11856
12687
  async function applyRunToPrimary(indexName, partitionId, run, assignment, deps, results) {
11857
12688
  if (run.operation === "remove") {
11858
- return applyPrimaryRemoveBatch(
11859
- indexName,
11860
- run.operations.map((operation) => operation.wire.documentId),
11861
- partitionId,
11862
- assignment,
11863
- deps
11864
- );
12689
+ const docIds = [];
12690
+ const conditions = /* @__PURE__ */ new Map();
12691
+ for (const operation of run.operations) {
12692
+ const condition = conditionOrFailure(operation.wire, results, operation.index);
12693
+ if (condition === null) continue;
12694
+ docIds.push(operation.wire.documentId);
12695
+ if (condition !== void 0) conditions.set(operation.wire.documentId, condition);
12696
+ }
12697
+ return applyPrimaryRemoveBatch(indexName, docIds, partitionId, assignment, deps, conditions);
11865
12698
  }
11866
12699
  if (run.operation === "insert") {
11867
12700
  const items = [];
11868
12701
  for (const operation of run.operations) {
12702
+ if (conditionOrFailure(operation.wire, results, operation.index) === null) continue;
11869
12703
  const decodedDocument = decodeOperationDocument(operation.wire, results, operation.index);
11870
12704
  if (decodedDocument !== null) {
11871
12705
  items.push({ doc: decodedDocument, docId: operation.wire.documentId });
@@ -11875,6 +12709,8 @@ async function applyRunToPrimary(indexName, partitionId, run, assignment, deps,
11875
12709
  }
11876
12710
  const updates = [];
11877
12711
  for (const operation of run.operations) {
12712
+ const condition = conditionOrFailure(operation.wire, results, operation.index);
12713
+ if (condition === null) continue;
11878
12714
  try {
11879
12715
  const replacement = await resolveForwardedUpdateDocument(
11880
12716
  indexName,
@@ -11883,7 +12719,9 @@ async function applyRunToPrimary(indexName, partitionId, run, assignment, deps,
11883
12719
  operation.wire.updateFields,
11884
12720
  deps
11885
12721
  );
11886
- updates.push({ docId: operation.wire.documentId, document: replacement });
12722
+ updates.push(
12723
+ condition === void 0 ? { docId: operation.wire.documentId, document: replacement } : { docId: operation.wire.documentId, document: replacement, condition }
12724
+ );
11887
12725
  } catch (err) {
11888
12726
  const error = err instanceof NarsilError ? err : new NarsilError(ErrorCodes.CONFIG_INVALID, String(err));
11889
12727
  results[operation.index] = failureResult(operation.wire.documentId, error);
@@ -11891,6 +12729,15 @@ async function applyRunToPrimary(indexName, partitionId, run, assignment, deps,
11891
12729
  }
11892
12730
  return applyPrimaryUpdateBatch(indexName, updates, partitionId, assignment, deps);
11893
12731
  }
12732
+ function conditionOrFailure(wire, results, index) {
12733
+ try {
12734
+ return conditionOfFields(wire, wire.operation, wire.documentId);
12735
+ } catch (err) {
12736
+ const error = err instanceof NarsilError ? err : new NarsilError(ErrorCodes.REPLICATION_ENTRY_INVALID, String(err));
12737
+ results[index] = failureResult(wire.documentId, error);
12738
+ return null;
12739
+ }
12740
+ }
11894
12741
  function decodeOperationDocument(wire, results, index) {
11895
12742
  if (wire.document === null) {
11896
12743
  results[index] = failureResult(
@@ -11914,13 +12761,16 @@ function decodeOperationDocument(wire, results, index) {
11914
12761
  }
11915
12762
  }
11916
12763
  function failureResult(documentId, error) {
11917
- return {
12764
+ const result = {
11918
12765
  documentId,
11919
12766
  success: false,
11920
12767
  errorCode: error.code,
11921
12768
  errorMessage: error.message,
11922
12769
  errorDetails: transportableDetails(error) ?? null
11923
12770
  };
12771
+ const abandonedWrite = abandonedWriteOf(error);
12772
+ if (abandonedWrite !== void 0) result.abandonedWrite = abandonedWrite;
12773
+ return result;
11924
12774
  }
11925
12775
 
11926
12776
  // src/distribution/cluster-node/write-routing/index.ts
@@ -12093,7 +12943,7 @@ async function routeAdmittedInsert(indexName, document, docId, deps, options) {
12093
12943
  async function insertOnce(indexName, document, resolvedDocId, deps, options) {
12094
12944
  const resolution = await resolvePrimaryAssignment(indexName, resolvedDocId, deps, false);
12095
12945
  if (resolution === null) {
12096
- return deps.engine.insert(indexName, document, resolvedDocId, options);
12946
+ return writeLocalOnly(deps, indexName, () => deps.engine.insert(indexName, document, resolvedDocId, options));
12097
12947
  }
12098
12948
  requireWritableIndex(resolution.table, deps.memoryView);
12099
12949
  const primaryNodeId = resolution.assignment.primary;
@@ -12115,21 +12965,21 @@ function routeRemove(indexName, docId, deps) {
12115
12965
  deps,
12116
12966
  indexName,
12117
12967
  docId,
12118
- () => removeOnce(indexName, docId, deps),
12119
- settleAbsentRemovals,
12968
+ (condition) => removeOnce(indexName, docId, deps, condition),
12969
+ settleNothing,
12120
12970
  () => void 0
12121
12971
  );
12122
12972
  }
12123
- async function removeOnce(indexName, docId, deps) {
12973
+ async function removeOnce(indexName, docId, deps, condition) {
12124
12974
  const resolution = await resolvePrimaryAssignment(indexName, docId, deps, false);
12125
12975
  if (resolution === null) {
12126
- return deps.engine.remove(indexName, docId);
12976
+ return writeLocalOnly(deps, indexName, () => deps.engine.remove(indexName, docId));
12127
12977
  }
12128
12978
  const primaryNodeId = resolution.assignment.primary;
12129
12979
  if (primaryNodeId === deps.nodeId) {
12130
- return applyPrimaryRemove(indexName, docId, resolution.partitionId, resolution.assignment, deps);
12980
+ return applyPrimaryRemove(indexName, docId, resolution.partitionId, resolution.assignment, deps, condition);
12131
12981
  }
12132
- return forwardRemoveToRemote(indexName, docId, primaryNodeId, deps, resolution.table.indexUuid);
12982
+ return forwardRemoveToRemote(indexName, docId, primaryNodeId, deps, resolution.table.indexUuid, condition);
12133
12983
  }
12134
12984
  function routeUpdate(indexName, docId, document, deps) {
12135
12985
  return withAdmittedWrite(
@@ -12140,23 +12990,31 @@ function routeUpdate(indexName, docId, document, deps) {
12140
12990
  deps,
12141
12991
  indexName,
12142
12992
  docId,
12143
- () => routeAdmittedUpdate(indexName, docId, document, deps),
12993
+ (condition) => routeAdmittedUpdate(indexName, docId, document, deps, condition),
12144
12994
  settleNothing,
12145
12995
  () => void 0
12146
12996
  )
12147
12997
  );
12148
12998
  }
12149
- async function routeAdmittedUpdate(indexName, docId, document, deps) {
12999
+ async function routeAdmittedUpdate(indexName, docId, document, deps, condition) {
12150
13000
  const resolution = await resolvePrimaryAssignment(indexName, docId, deps, false);
12151
13001
  if (resolution === null) {
12152
- return deps.engine.update(indexName, docId, document);
13002
+ return writeLocalOnly(deps, indexName, () => deps.engine.update(indexName, docId, document));
12153
13003
  }
12154
13004
  requireWritableIndex(resolution.table, deps.memoryView);
12155
13005
  const primaryNodeId = resolution.assignment.primary;
12156
13006
  if (primaryNodeId === deps.nodeId) {
12157
- return applyPrimaryUpdate(indexName, docId, document, resolution.partitionId, resolution.assignment, deps);
13007
+ return applyPrimaryUpdate(
13008
+ indexName,
13009
+ docId,
13010
+ document,
13011
+ resolution.partitionId,
13012
+ resolution.assignment,
13013
+ deps,
13014
+ condition
13015
+ );
12158
13016
  }
12159
- return forwardUpdateToRemote(indexName, document, docId, primaryNodeId, deps, resolution.table.indexUuid);
13017
+ return forwardUpdateToRemote(indexName, document, docId, primaryNodeId, deps, resolution.table.indexUuid, condition);
12160
13018
  }
12161
13019
 
12162
13020
  // src/distribution/cluster-node/node-messaging.ts
@@ -12178,10 +13036,6 @@ async function sendToNode2(config, targetNodeId, message, failure) {
12178
13036
  details: { targetNodeId, ...failure?.details }
12179
13037
  });
12180
13038
  }
12181
- function fetchFieldsFor(projection) {
12182
- if (projection.kind !== "fields" || projection.include === null) return null;
12183
- return projection.include.map((path) => path.join("."));
12184
- }
12185
13039
  function routeDocumentIds(docIds, allocation) {
12186
13040
  const partitionCount = allocation.assignments.size;
12187
13041
  const routing = {
@@ -12261,30 +13115,20 @@ async function readFromCopies(config, nodeId, engine, indexName, documentRouting
12261
13115
  });
12262
13116
  return documents;
12263
13117
  }
12264
- async function fetchDistributedDocuments(config, nodeId, engine, indexName, result, allocation, projection) {
12265
- const documents = /* @__PURE__ */ new Map();
12266
- if (projection.kind === "none") return documents;
12267
- const documentRouting = routeDocumentIds(
12268
- result.scored.map((entry) => entry.docId),
12269
- allocation
12270
- );
12271
- const found = await readFromCopies(config, nodeId, engine, indexName, documentRouting, fetchFieldsFor(projection));
12272
- for (const [docId, document] of found) {
12273
- documents.set(docId, applyProjection(document, projection));
12274
- }
12275
- return documents;
12276
- }
12277
- async function readDistributedDocuments(config, nodeId, engine, indexName, docIds, allocation) {
13118
+ async function readDocumentsFromCopies(config, nodeId, engine, indexName, docIds, allocation, fields, requireEveryPartition) {
12278
13119
  const documentRouting = routeDocumentIds(docIds, allocation);
12279
13120
  const unreachablePartitions = documentRouting.routing.unavailablePartitions;
12280
- if (unreachablePartitions.length > 0) {
13121
+ if (requireEveryPartition && unreachablePartitions.length > 0) {
12281
13122
  throw new NarsilError(
12282
13123
  ErrorCodes.QUERY_NO_ACTIVE_REPLICA,
12283
13124
  `No active replica serves one or more partitions of index '${indexName}'`,
12284
13125
  { indexName, partitionIds: [...unreachablePartitions].sort((a, b) => a - b) }
12285
13126
  );
12286
13127
  }
12287
- return readFromCopies(config, nodeId, engine, indexName, documentRouting, null);
13128
+ return readFromCopies(config, nodeId, engine, indexName, documentRouting, fields);
13129
+ }
13130
+ async function readDistributedDocuments(config, nodeId, engine, indexName, docIds, allocation) {
13131
+ return readDocumentsFromCopies(config, nodeId, engine, indexName, docIds, allocation, null, true);
12288
13132
  }
12289
13133
 
12290
13134
  // src/distribution/cluster-node/synonym-sets.ts
@@ -13215,7 +14059,8 @@ async function handleIndexSettings(message, respond, deps) {
13215
14059
  var DEFAULT_QUERY_CONFIG = {
13216
14060
  allowPartialResults: true,
13217
14061
  partitionTimeout: 5e3,
13218
- defaultFacetSize: 10
14062
+ defaultFacetSize: 10,
14063
+ searchContexts: false
13219
14064
  };
13220
14065
 
13221
14066
  // src/distribution/cluster-node/query-conversion.ts
@@ -13447,6 +14292,93 @@ function convertWireFacetsToLocal(result, wireFacets, requested) {
13447
14292
  return converted;
13448
14293
  }
13449
14294
 
14295
+ // src/distribution/cluster-node/search-contexts/departures.ts
14296
+ function releaseContextsOfDepartedNodes(coordinator, registry) {
14297
+ return coordinator.watchNodes((event) => {
14298
+ if (event.type === "node_left") registry.releaseCoordinator(event.nodeId);
14299
+ });
14300
+ }
14301
+
14302
+ // src/distribution/cluster-node/search-contexts/documents.ts
14303
+ function documentOfSnapshot(snapshot) {
14304
+ const document = structuredClone(snapshot.fields);
14305
+ for (const [fieldPath2, vector] of snapshot.vectors) setNestedValue(document, fieldPath2, new Float32Array(vector));
14306
+ return document;
14307
+ }
14308
+ function heldBytesOf(snapshots) {
14309
+ let bytes = 0;
14310
+ for (const snapshot of snapshots.values()) {
14311
+ bytes += SEARCH_CONTEXT_ENTRY_HEAP_BYTES;
14312
+ for (const [, vector] of snapshot.vectors) bytes += vector.byteLength;
14313
+ }
14314
+ return bytes;
14315
+ }
14316
+
14317
+ // src/distribution/cluster-node/search-contexts/registry.ts
14318
+ function keepAliveOf(requested) {
14319
+ return Math.min(requested, SEARCH_CONTEXT_KEEP_ALIVE_LIMIT_MS);
14320
+ }
14321
+ function createSearchContextRegistry(now3 = () => performance.now()) {
14322
+ const contexts = /* @__PURE__ */ new Map();
14323
+ let reaper = null;
14324
+ function reap() {
14325
+ const at = now3();
14326
+ for (const [contextId, context] of contexts) {
14327
+ if (context.expiresAt <= at) contexts.delete(contextId);
14328
+ }
14329
+ if (contexts.size === 0) stopReaper();
14330
+ }
14331
+ function startReaper() {
14332
+ if (reaper !== null) return;
14333
+ reaper = setInterval(reap, SEARCH_CONTEXT_REAPER_INTERVAL_MS);
14334
+ if (typeof reaper === "object" && "unref" in reaper) reaper.unref();
14335
+ }
14336
+ function stopReaper() {
14337
+ if (reaper === null) return;
14338
+ clearInterval(reaper);
14339
+ reaper = null;
14340
+ }
14341
+ return {
14342
+ open(coordinatorNodeId, keepAliveMs, documents) {
14343
+ const contextId = generateId();
14344
+ contexts.set(contextId, { coordinatorNodeId, keepAliveMs, expiresAt: now3() + keepAliveMs, documents });
14345
+ startReaper();
14346
+ return contextId;
14347
+ },
14348
+ read(contextId, docIds) {
14349
+ const context = contexts.get(contextId);
14350
+ if (context === void 0) return null;
14351
+ const at = now3();
14352
+ if (context.expiresAt <= at) {
14353
+ contexts.delete(contextId);
14354
+ return null;
14355
+ }
14356
+ context.expiresAt = at + context.keepAliveMs;
14357
+ const found = /* @__PURE__ */ new Map();
14358
+ for (const docId of docIds) {
14359
+ const snapshot = context.documents.get(docId);
14360
+ if (snapshot !== void 0) found.set(docId, snapshot);
14361
+ }
14362
+ return found;
14363
+ },
14364
+ release(contextId) {
14365
+ contexts.delete(contextId);
14366
+ if (contexts.size === 0) stopReaper();
14367
+ },
14368
+ releaseCoordinator(coordinatorNodeId) {
14369
+ for (const [contextId, context] of contexts) {
14370
+ if (context.coordinatorNodeId === coordinatorNodeId) contexts.delete(contextId);
14371
+ }
14372
+ if (contexts.size === 0) stopReaper();
14373
+ },
14374
+ openCount: () => contexts.size,
14375
+ stop() {
14376
+ contexts.clear();
14377
+ stopReaper();
14378
+ }
14379
+ };
14380
+ }
14381
+
13450
14382
  // src/distribution/cluster-node/message-handler/queries.ts
13451
14383
  async function handleSearch(message, respond, deps) {
13452
14384
  const decoded = decode(message.payload);
@@ -13456,7 +14388,9 @@ async function handleSearch(message, respond, deps) {
13456
14388
  queryParams.group = { ...queryParams.group, limit: oversampledShardSize(queryParams.group.limit) };
13457
14389
  }
13458
14390
  const rescoreWindow = payload.params.vector?.rescoreWindow ?? void 0;
13459
- if (rescoreWindow !== void 0 && queryParams.sort === void 0 && queryParams.group === void 0) {
14391
+ const keepAliveMs = payload.keepAliveMs ?? null;
14392
+ const snapshots = keepAliveMs === null || deps.searchContexts === void 0 ? void 0 : /* @__PURE__ */ new Map();
14393
+ if (snapshots !== void 0 || rescoreWindow !== void 0 && queryParams.sort === void 0 && queryParams.group === void 0) {
13460
14394
  queryParams.document = false;
13461
14395
  }
13462
14396
  const queryResult = await deps.engine.queryPartitions(
@@ -13464,15 +14398,17 @@ async function handleSearch(message, respond, deps) {
13464
14398
  queryParams,
13465
14399
  payload.partitionIds,
13466
14400
  payload.globalStats ?? void 0,
13467
- rescoreWindow
14401
+ rescoreWindow,
14402
+ snapshots
13468
14403
  );
14404
+ const rankedDocument = (hit) => snapshots?.get(hit.id)?.fields ?? hit.document;
13469
14405
  const normalizedSort = queryParams.sort !== void 0 ? normalizeSort(queryParams.sort) : null;
13470
14406
  const sortFields = normalizedSort === null ? null : normalizedSort.map((entry) => entry.field);
13471
14407
  const sortModes = normalizedSort === null ? [] : sortModesOf(normalizedSort);
13472
14408
  const scored = queryResult.hits.map((hit) => ({
13473
14409
  docId: hit.id,
13474
14410
  score: hit.score ?? null,
13475
- sortValues: sortFields !== null ? readSortValues(hit.document, sortFields, sortModes).map(toWireSortValue) : null
14411
+ sortValues: sortFields !== null ? readSortValues(rankedDocument(hit), sortFields, sortModes).map(toWireSortValue) : null
13476
14412
  }));
13477
14413
  const results = payload.partitionIds.map((partitionId) => ({
13478
14414
  partitionId,
@@ -13483,8 +14419,18 @@ async function handleSearch(message, respond, deps) {
13483
14419
  results,
13484
14420
  facets: convertLocalFacetsToWire2(queryResult.facets),
13485
14421
  facetErrorBounds: convertLocalFacetBoundsToWire(queryResult.facets),
13486
- groups: await convertLocalGroupsToWire(queryResult.groups, sortFields, sortModes, deps, payload.indexName)
14422
+ groups: await convertLocalGroupsToWire(
14423
+ queryResult.groups,
14424
+ sortFields,
14425
+ sortModes,
14426
+ deps,
14427
+ payload.indexName,
14428
+ snapshots
14429
+ )
13487
14430
  };
14431
+ if (snapshots !== void 0 && keepAliveMs !== null) {
14432
+ resultPayload.contextId = openSearchContext(deps, message.sourceId, keepAliveMs, snapshots);
14433
+ }
13488
14434
  await respond({
13489
14435
  type: QueryMessageTypes.SEARCH_RESULT,
13490
14436
  sourceId: deps.nodeId,
@@ -13492,17 +14438,17 @@ async function handleSearch(message, respond, deps) {
13492
14438
  payload: encodeDocumentReply(resultPayload)
13493
14439
  });
13494
14440
  }
13495
- async function convertLocalGroupsToWire(groups, sortFields, sortModes, deps, indexName) {
14441
+ async function convertLocalGroupsToWire(groups, sortFields, sortModes, deps, indexName, snapshots) {
13496
14442
  if (groups === void 0) {
13497
14443
  return null;
13498
14444
  }
13499
- const unreadIds = sortFields === null ? [] : groups.flatMap((group) => group.hits.filter((hit) => hit.document === void 0).map((hit) => hit.id));
14445
+ const unreadIds = sortFields === null || snapshots !== void 0 ? [] : groups.flatMap((group) => group.hits.filter((hit) => hit.document === void 0).map((hit) => hit.id));
13500
14446
  const readDocuments = unreadIds.length > 0 ? await deps.engine.getMultiple(indexName, unreadIds) : /* @__PURE__ */ new Map();
13501
14447
  const wireGroups = [];
13502
14448
  for (const group of groups) {
13503
14449
  const scored = group.hits.map((hit) => {
13504
14450
  if (sortFields === null) return { docId: hit.id, score: hit.score ?? null, sortValues: null };
13505
- const document = hit.document ?? readDocuments.get(hit.id);
14451
+ const document = snapshots?.get(hit.id)?.fields ?? hit.document ?? readDocuments.get(hit.id);
13506
14452
  const sortValues = readSortValues(document, sortFields, sortModes).map(toWireSortValue);
13507
14453
  return { docId: hit.id, score: hit.score ?? null, sortValues };
13508
14454
  });
@@ -13514,9 +14460,21 @@ async function handleFetch(message, respond, deps) {
13514
14460
  const decoded = decode(message.payload);
13515
14461
  const payload = validateFetchPayload(decoded);
13516
14462
  const projection = resolveProjection(payload.fields === null ? void 0 : { include: payload.fields });
14463
+ const contextId = payload.contextId ?? null;
14464
+ const documents = contextId === null ? await readCurrentDocuments(deps, payload.indexName, payload.documentIds, projection) : readContextDocuments(deps, contextId, payload.documentIds, payload.releaseContext !== false, projection);
14465
+ refuseEncodeWithoutRoom(documents.map((fetched) => fetched.document));
14466
+ const resultPayload = { documents };
14467
+ await respond({
14468
+ type: QueryMessageTypes.FETCH_RESULT,
14469
+ sourceId: deps.nodeId,
14470
+ requestId: message.requestId,
14471
+ payload: encodeDocumentReply(resultPayload)
14472
+ });
14473
+ }
14474
+ async function readCurrentDocuments(deps, indexName, documentIds, projection) {
13517
14475
  const documents = [];
13518
- for (const docRef of payload.documentIds) {
13519
- const doc = await deps.engine.get(payload.indexName, docRef.docId);
14476
+ for (const docRef of documentIds) {
14477
+ const doc = await deps.engine.get(indexName, docRef.docId);
13520
14478
  if (doc !== void 0) {
13521
14479
  documents.push({
13522
14480
  docId: docRef.docId,
@@ -13525,14 +14483,37 @@ async function handleFetch(message, respond, deps) {
13525
14483
  });
13526
14484
  }
13527
14485
  }
13528
- refuseEncodeWithoutRoom(documents.map((fetched) => fetched.document));
13529
- const resultPayload = { documents };
13530
- await respond({
13531
- type: QueryMessageTypes.FETCH_RESULT,
13532
- sourceId: deps.nodeId,
13533
- requestId: message.requestId,
13534
- payload: encodeDocumentReply(resultPayload)
13535
- });
14486
+ return documents;
14487
+ }
14488
+ function readContextDocuments(deps, contextId, documentIds, releaseContext, projection) {
14489
+ const docIds = documentIds.map((docRef) => docRef.docId);
14490
+ const found = deps.searchContexts?.read(contextId, docIds) ?? null;
14491
+ if (releaseContext) deps.searchContexts?.release(contextId);
14492
+ if (found === null) {
14493
+ if (docIds.length === 0) return [];
14494
+ throw new NarsilError(
14495
+ ErrorCodes.SEARCH_CONTEXT_MISSING,
14496
+ `Node '${deps.nodeId}' holds no search context '${contextId}', because it expired, the node restarted, or the coordinating node released it`,
14497
+ { contextId, nodeId: deps.nodeId }
14498
+ );
14499
+ }
14500
+ const documents = [];
14501
+ for (const docId of docIds) {
14502
+ const snapshot = found.get(docId);
14503
+ if (snapshot === void 0) continue;
14504
+ documents.push({
14505
+ docId,
14506
+ document: applyProjection(documentOfSnapshot(snapshot), projection),
14507
+ highlights: null
14508
+ });
14509
+ }
14510
+ return documents;
14511
+ }
14512
+ function openSearchContext(deps, coordinatorNodeId, keepAliveMs, snapshots) {
14513
+ if (deps.searchContexts === void 0 || snapshots.size === 0) return null;
14514
+ const refusal = answerRefusalOf(heldBytesOf(snapshots));
14515
+ if (refusal !== null) throw refusal;
14516
+ return deps.searchContexts.open(coordinatorNodeId, keepAliveOf(keepAliveMs), snapshots);
13536
14517
  }
13537
14518
  async function handleStats(message, respond, deps) {
13538
14519
  const decoded = decode(message.payload);
@@ -14217,8 +15198,6 @@ async function resolveValidatedAssignment(entry, sourceNodeId, deps) {
14217
15198
  if (live instanceof NarsilError) throw live;
14218
15199
  return live;
14219
15200
  }
14220
-
14221
- // src/distribution/cluster-node/message-handler/writes.ts
14222
15201
  async function handleForward(message, respond, deps) {
14223
15202
  const decoded = decode(message.payload);
14224
15203
  const payload = validateForwardPayload(decoded);
@@ -14256,14 +15235,14 @@ async function handleReplicationEntry(message, respond, deps) {
14256
15235
  const payload = validateEntryPayload(decode(message.payload));
14257
15236
  const { entry } = payload;
14258
15237
  refuseAnotherIndex(entry.indexName, payload.indexUuid, deps);
14259
- await applyEntriesFromPrimary([entry], message.sourceId, deps);
15238
+ await applyEntriesFromPrimary([entry], message.sourceId, deps, payload.previousPrimaryTerm);
14260
15239
  await respond(createAckMessage(entry.seqNo, entry.partitionId, entry.indexName, deps.nodeId, message.requestId));
14261
15240
  }
14262
15241
  async function handleReplicationEntryBatch(message, respond, deps) {
14263
15242
  const payload = validateEntryBatchPayload(decode(message.payload));
14264
15243
  const lastEntry = payload.entries[payload.entries.length - 1];
14265
15244
  refuseAnotherIndex(lastEntry.indexName, payload.indexUuid, deps);
14266
- await applyEntriesFromPrimary(payload.entries, message.sourceId, deps);
15245
+ await applyEntriesFromPrimary(payload.entries, message.sourceId, deps, payload.previousPrimaryTerm);
14267
15246
  await respond(
14268
15247
  createAckMessage(lastEntry.seqNo, lastEntry.partitionId, lastEntry.indexName, deps.nodeId, message.requestId)
14269
15248
  );
@@ -14283,7 +15262,7 @@ function refuseAnotherIndex(indexName, indexUuid, deps) {
14283
15262
  { indexName, localNodeId: deps.nodeId }
14284
15263
  );
14285
15264
  }
14286
- async function applyEntriesFromPrimary(entries, sourceNodeId, deps) {
15265
+ async function applyEntriesFromPrimary(entries, sourceNodeId, deps, previousPrimaryTerm) {
14287
15266
  const first = entries[0];
14288
15267
  if (first === void 0) return;
14289
15268
  const assignment = await resolveValidatedAssignment(first, sourceNodeId, deps);
@@ -14297,15 +15276,18 @@ async function applyEntriesFromPrimary(entries, sourceNodeId, deps) {
14297
15276
  await enqueuePartitionWrite(deps.writeDeps.partitionWriteQueues, first.indexName, first.partitionId, async () => {
14298
15277
  const current = await resolveValidatedAssignment(first, sourceNodeId, deps);
14299
15278
  noteAcceptedPrimaryTerm(deps.writeDeps.getReplicationLog(first.indexName, first.partitionId), current.primaryTerm);
14300
- await applyEntriesInLogOrder(first, entries, deps);
15279
+ await applyEntriesInLogOrder(first, entries, deps, previousPrimaryTerm);
14301
15280
  });
14302
15281
  }
14303
- async function applyEntriesInLogOrder(first, entries, deps) {
15282
+ async function applyEntriesInLogOrder(first, entries, deps, previousPrimaryTerm) {
14304
15283
  let log = deps.writeDeps.getReplicationLog(first.indexName, first.partitionId);
14305
15284
  let tailSeqNo = log.localLogEnd;
14306
15285
  let tailPrimaryTerm = log.localLogEndPrimaryTerm;
15286
+ let expectedPreviousTerm = previousPrimaryTerm;
14307
15287
  const run = [];
14308
15288
  for (const entry of entries) {
15289
+ const expectedTermBefore = expectedPreviousTerm;
15290
+ expectedPreviousTerm = entry.primaryTerm;
14309
15291
  const existing = log.getEntry(entry.seqNo);
14310
15292
  if (existing !== void 0) {
14311
15293
  if (existing.checksum !== entry.checksum) {
@@ -14318,6 +15300,21 @@ async function applyEntriesInLogOrder(first, entries, deps) {
14318
15300
  }
14319
15301
  continue;
14320
15302
  }
15303
+ const heldTermBefore = run.at(-1)?.primaryTerm ?? (tailSeqNo === 0 ? 0 : log.termAt(tailSeqNo));
15304
+ if (entry.seqNo === tailSeqNo + 1 && expectedTermBefore !== void 0 && heldTermBefore !== void 0 && heldTermBefore !== expectedTermBefore) {
15305
+ await applyRun2(run, log, deps);
15306
+ throw new NarsilError(
15307
+ ErrorCodes.REPLICATION_ENTRY_INVALID,
15308
+ `Replication entry ${entry.seqNo} follows an entry of term ${expectedTermBefore}, but this copy holds sequence number ${tailSeqNo} under term ${heldTermBefore}`,
15309
+ {
15310
+ indexName: entry.indexName,
15311
+ partitionId: entry.partitionId,
15312
+ seqNo: entry.seqNo,
15313
+ previousPrimaryTerm: expectedTermBefore,
15314
+ heldPrimaryTerm: heldTermBefore
15315
+ }
15316
+ );
15317
+ }
14321
15318
  const expectedSeqNo = tailSeqNo + 1;
14322
15319
  if (entry.seqNo !== expectedSeqNo) {
14323
15320
  const canSeedFromCompletedBootstrap = run.length === 0 && log.entryCount === 0 && entry.seqNo > expectedSeqNo && deps.isBootstrapSynced?.(entry.indexName, entry.partitionId) === true;
@@ -14378,6 +15375,28 @@ function validateForwardPayload(decoded) {
14378
15375
  return decoded;
14379
15376
  }
14380
15377
 
15378
+ // src/distribution/cluster-node/message-handler/term-start.ts
15379
+ async function handleTermStart(message, respond, deps) {
15380
+ const request = validateTermStartPayload(message.payload);
15381
+ const { indexName, partitionId, primaryTerm } = request;
15382
+ refuseAnotherIndex(indexName, request.indexUuid, deps);
15383
+ await resolveValidatedAssignment({ indexName, partitionId, primaryTerm }, message.sourceId, deps);
15384
+ noteSupersedingPrimary(
15385
+ deps.writeDeps.getReplicationLog(indexName, partitionId),
15386
+ indexName,
15387
+ partitionId,
15388
+ message.sourceId,
15389
+ primaryTerm
15390
+ );
15391
+ const held = await enqueuePartitionWrite(deps.writeDeps.partitionWriteQueues, indexName, partitionId, async () => {
15392
+ const log = deps.writeDeps.getReplicationLog(indexName, partitionId);
15393
+ noteAcceptedPrimaryTerm(log, primaryTerm);
15394
+ const lastSeqNo = log.localLogEnd;
15395
+ return { lastSeqNo, lastPrimaryTerm: lastSeqNo === 0 ? 0 : log.termAt(lastSeqNo) ?? 0 };
15396
+ });
15397
+ await respond(createTermStartAckMessage({ indexName, partitionId, ...held }, deps.nodeId, message.requestId));
15398
+ }
15399
+
14381
15400
  // src/distribution/cluster-node/message-handler/index.ts
14382
15401
  function createDataNodeHandler(deps) {
14383
15402
  return async (message, respond) => {
@@ -14398,6 +15417,9 @@ function createDataNodeHandler(deps) {
14398
15417
  case ReplicationMessageTypes.ENTRY_BATCH:
14399
15418
  await handleReplicationEntryBatch(message, respond, deps);
14400
15419
  return;
15420
+ case ReplicationMessageTypes.TERM_START:
15421
+ await handleTermStart(message, respond, deps);
15422
+ return;
14401
15423
  case QueryMessageTypes.SEARCH:
14402
15424
  await handleSearch(message, respond, deps);
14403
15425
  return;
@@ -14444,7 +15466,7 @@ function createDataNodeHandler(deps) {
14444
15466
  return;
14445
15467
  }
14446
15468
  } catch (err) {
14447
- const errorPayload = encodeErrorReplyFrom(err, "INTERNAL_ERROR");
15469
+ const errorPayload = encodeErrorReplyFrom(err, "INTERNAL_ERROR", abandonedWriteOf(err));
14448
15470
  await respond({
14449
15471
  type: `${message.type}.error`,
14450
15472
  sourceId: deps.nodeId,
@@ -14522,10 +15544,85 @@ async function resolveNodeId(explicitNodeId, directory) {
14522
15544
  });
14523
15545
  }
14524
15546
  }
15547
+
15548
+ // src/distribution/query/node-send.ts
15549
+ async function resolveTargets(nodeId, deps) {
15550
+ if (deps.resolveNodeTargets === void 0) {
15551
+ return [nodeId];
15552
+ }
15553
+ const targets = await deps.resolveNodeTargets(nodeId);
15554
+ return targets.length > 0 ? targets : [nodeId];
15555
+ }
15556
+ async function sendToFirstReachableTarget2(transport, targets, message) {
15557
+ let lastError;
15558
+ for (const target of targets) {
15559
+ try {
15560
+ return await transport.send(target, message);
15561
+ } catch (error) {
15562
+ lastError = error;
15563
+ }
15564
+ }
15565
+ throw lastError instanceof Error ? lastError : new Error(String(lastError));
15566
+ }
15567
+
15568
+ // src/distribution/query/search-contexts.ts
15569
+ function searchContextKeepAlive(routing, partitionTimeout, transport) {
15570
+ let copies = 1;
15571
+ for (const partitionCopies of routing.copiesByPartition.values()) copies = Math.max(copies, partitionCopies.length);
15572
+ const requestTimeout = transport.timeouts?.requestTimeout ?? DEFAULT_TRANSPORT_CONFIG.requestTimeout;
15573
+ const longestGap = partitionTimeout * copies + 2 * requestTimeout;
15574
+ return Math.min(SEARCH_CONTEXT_KEEP_ALIVE_LIMIT_MS, Math.max(SEARCH_CONTEXT_KEEP_ALIVE_MS, longestGap));
15575
+ }
15576
+ function collectSearchContexts(...outcomeSets) {
15577
+ const sources = /* @__PURE__ */ new Map();
15578
+ const opened = [];
15579
+ for (const outcomes of outcomeSets) {
15580
+ for (const outcome of outcomes) {
15581
+ const contextId = outcome.status === "success" ? outcome.results?.contextId ?? null : null;
15582
+ if (contextId === null || outcome.results === null) continue;
15583
+ const context = { nodeId: outcome.nodeId, contextId };
15584
+ opened.push(context);
15585
+ for (const partition of outcome.results.results) {
15586
+ for (const entry of partition.scored) if (!sources.has(entry.docId)) sources.set(entry.docId, context);
15587
+ }
15588
+ for (const group of outcome.results.groups ?? []) {
15589
+ for (const entry of group.scored) if (!sources.has(entry.docId)) sources.set(entry.docId, context);
15590
+ }
15591
+ }
15592
+ }
15593
+ return { sources, opened };
15594
+ }
15595
+ function releaseSearchContexts(indexName, contexts, deps) {
15596
+ for (const context of contexts) {
15597
+ const message = createFetchMessage(
15598
+ {
15599
+ indexName,
15600
+ documentIds: [],
15601
+ fields: null,
15602
+ highlight: null,
15603
+ contextId: context.contextId,
15604
+ releaseContext: true
15605
+ },
15606
+ deps.sourceNodeId
15607
+ );
15608
+ resolveTargets(context.nodeId, deps).then((targets) => sendToFirstReachableTarget2(deps.transport, targets, message)).catch(() => void 0);
15609
+ }
15610
+ }
15611
+
15612
+ // src/distribution/query/fan-out.ts
14525
15613
  function queryRejectionOf(response, decoded) {
14526
15614
  const body = decoded;
14527
15615
  if (!response.type.endsWith(".error") && body?.error !== true) return null;
14528
- return nodeRejectionOf(body?.code, body?.message);
15616
+ if (body?.code !== ServerErrorCodes.TOO_MANY_REQUESTS) return nodeRejectionOf(body?.code, body?.message);
15617
+ const details = typeof body.details === "object" && body.details !== null ? { ...body.details } : {};
15618
+ return new NarsilError(
15619
+ ServerErrorCodes.TOO_MANY_REQUESTS,
15620
+ typeof body.message === "string" ? body.message : "A node had no room in memory to answer its part of the search",
15621
+ details
15622
+ );
15623
+ }
15624
+ function isMemoryRefusal(error) {
15625
+ return error instanceof NarsilError && error.code === ServerErrorCodes.TOO_MANY_REQUESTS;
14529
15626
  }
14530
15627
  function statsExpansionOf(params) {
14531
15628
  return {
@@ -14594,10 +15691,17 @@ async function collectDistributedStats(indexName, params, routing, deps, config)
14594
15691
  const leftOut = await countLeftOutTokens(indexName, answered, deps, config);
14595
15692
  return mergePartitionStats([...answered.map(([, { result }]) => result), ...leftOut]);
14596
15693
  }
14597
- async function fanOutSearch(indexName, params, globalStats, facetShardSize, routing, deps, config) {
15694
+ async function fanOutSearch(indexName, params, globalStats, facetShardSize, routing, deps, config, keepAliveMs = null) {
14598
15695
  const outcomes = await sendWithFailover(routing, async (nodeId, partitionIds) => {
14599
15696
  const message = createSearchMessage(
14600
- { indexName, partitionIds, params, globalStats, facetShardSize },
15697
+ {
15698
+ indexName,
15699
+ partitionIds,
15700
+ params,
15701
+ globalStats,
15702
+ facetShardSize,
15703
+ ...keepAliveMs === null ? {} : { keepAliveMs }
15704
+ },
14601
15705
  deps.sourceNodeId
14602
15706
  );
14603
15707
  const targets = await resolveTargets(nodeId, deps);
@@ -14606,11 +15710,21 @@ async function fanOutSearch(indexName, params, globalStats, facetShardSize, rout
14606
15710
  targets,
14607
15711
  message,
14608
15712
  config.partitionTimeout,
14609
- validateSearchResultPayload
15713
+ validateSearchResultPayload,
15714
+ (late) => {
15715
+ if (typeof late.contextId === "string") {
15716
+ releaseSearchContexts(indexName, [{ nodeId, contextId: late.contextId }], deps);
15717
+ }
15718
+ }
14610
15719
  );
14611
15720
  });
14612
- const rejection = outcomes.find((outcome) => isNodeRejection(outcome.error));
14613
- if (rejection !== void 0) throw rejection.error;
15721
+ const memoryRefusal = outcomes.find((outcome) => isMemoryRefusal(outcome.error));
15722
+ const everyPartitionFailed = outcomes.every((outcome) => outcome.status !== "success");
15723
+ const rejection = outcomes.find((outcome) => isNodeRejection(outcome.error)) ?? (!config.allowPartialResults || everyPartitionFailed ? memoryRefusal : void 0);
15724
+ if (rejection !== void 0) {
15725
+ releaseSearchContexts(indexName, openedContextsOf(outcomes), deps);
15726
+ throw rejection.error;
15727
+ }
14614
15728
  return outcomes.map((outcome) => ({
14615
15729
  nodeId: outcome.nodeId,
14616
15730
  partitionIds: outcome.partitionIds,
@@ -14618,6 +15732,14 @@ async function fanOutSearch(indexName, params, globalStats, facetShardSize, rout
14618
15732
  results: outcome.result
14619
15733
  }));
14620
15734
  }
15735
+ function openedContextsOf(outcomes) {
15736
+ const opened = [];
15737
+ for (const outcome of outcomes) {
15738
+ const contextId = outcome.result?.contextId;
15739
+ if (typeof contextId === "string") opened.push({ nodeId: outcome.nodeId, contextId });
15740
+ }
15741
+ return opened;
15742
+ }
14621
15743
  var OUTCOME_SEVERITY = { success: 0, timeout: 1, failed: 2 };
14622
15744
  function buildCoverage(totalPartitions, unavailablePartitions, ...outcomeSets) {
14623
15745
  const worstByPartition = /* @__PURE__ */ new Map();
@@ -14653,7 +15775,7 @@ function buildCoverage(totalPartitions, unavailablePartitions, ...outcomeSets) {
14653
15775
  failedPartitions
14654
15776
  };
14655
15777
  }
14656
- async function sendWithTimeout(transport, targets, message, timeout, validate) {
15778
+ async function sendWithTimeout(transport, targets, message, timeout, validate, onLate) {
14657
15779
  let timerId;
14658
15780
  const abortPromise = new Promise((resolve) => {
14659
15781
  timerId = setTimeout(() => resolve(null), timeout);
@@ -14668,31 +15790,15 @@ async function sendWithTimeout(transport, targets, message, timeout, validate) {
14668
15790
  return validate(decoded);
14669
15791
  });
14670
15792
  try {
14671
- return await Promise.race([sendPromise, abortPromise]);
15793
+ const answered = await Promise.race([sendPromise, abortPromise]);
15794
+ if (answered === null && onLate !== void 0) sendPromise.then(onLate, () => void 0);
15795
+ return answered;
14672
15796
  } finally {
14673
15797
  if (timerId !== void 0) {
14674
15798
  clearTimeout(timerId);
14675
15799
  }
14676
15800
  }
14677
15801
  }
14678
- async function resolveTargets(nodeId, deps) {
14679
- if (deps.resolveNodeTargets === void 0) {
14680
- return [nodeId];
14681
- }
14682
- const targets = await deps.resolveNodeTargets(nodeId);
14683
- return targets.length > 0 ? targets : [nodeId];
14684
- }
14685
- async function sendToFirstReachableTarget2(transport, targets, message) {
14686
- let lastError;
14687
- for (const target of targets) {
14688
- try {
14689
- return await transport.send(target, message);
14690
- } catch (error) {
14691
- lastError = error;
14692
- }
14693
- }
14694
- throw lastError instanceof Error ? lastError : new Error(String(lastError));
14695
- }
14696
15802
 
14697
15803
  // src/distribution/query/merge.ts
14698
15804
  function compareByScoreThenId(a, b) {
@@ -15070,14 +16176,26 @@ async function executeHybridQuery(indexName, params, page, facetShardSize, facet
15070
16176
  if (params.scoring === "dfs") {
15071
16177
  textGlobalStats = await collectDistributedStats(indexName, textParams, routing, deps, config);
15072
16178
  }
15073
- const [textOutcomes, vectorOutcomes] = await Promise.all([
15074
- fanOutSearch(indexName, textParams, textGlobalStats, facetShardSize, routing, deps, config),
15075
- fanOutSearch(indexName, vectorParams, null, null, routing, deps, config)
16179
+ const keepAliveMs = config.searchContexts ? searchContextKeepAlive(routing, config.partitionTimeout, deps.transport) : null;
16180
+ const legs = await Promise.allSettled([
16181
+ fanOutSearch(indexName, textParams, textGlobalStats, facetShardSize, routing, deps, config, keepAliveMs),
16182
+ fanOutSearch(indexName, vectorParams, null, null, routing, deps, config, keepAliveMs)
15076
16183
  ]);
16184
+ const answered = legs.flatMap((leg) => leg.status === "fulfilled" ? [leg.value] : []);
16185
+ const contexts = collectSearchContexts(...answered);
16186
+ const failedLeg = legs.find((leg) => leg.status === "rejected");
16187
+ if (failedLeg !== void 0) {
16188
+ releaseSearchContexts(indexName, contexts.opened, deps);
16189
+ throw failedLeg.reason;
16190
+ }
16191
+ const [textLeg, vectorLeg] = legs;
16192
+ const textOutcomes = textLeg.status === "fulfilled" ? textLeg.value : [];
16193
+ const vectorOutcomes = vectorLeg.status === "fulfilled" ? vectorLeg.value : [];
15077
16194
  const coverage = buildCoverage(totalPartitions, routing.unavailablePartitions, textOutcomes, vectorOutcomes);
15078
16195
  if (!config.allowPartialResults) {
15079
16196
  const failedCount = coverage.timedOutPartitions + coverage.failedPartitions;
15080
16197
  if (failedCount > 0) {
16198
+ releaseSearchContexts(indexName, contexts.opened, deps);
15081
16199
  throw new NarsilError(ErrorCodes.QUERY_PARTIAL_FAILURE, "One or more partitions failed during hybrid query", {
15082
16200
  coverage
15083
16201
  });
@@ -15139,7 +16257,8 @@ async function executeHybridQuery(indexName, params, page, facetShardSize, facet
15139
16257
  cursor: paged.cursor,
15140
16258
  coverage,
15141
16259
  pinnedFromOutside: placement.placedFromOutside,
15142
- mergeHeldEveryMatch: allMatchesPresent
16260
+ mergeHeldEveryMatch: allMatchesPresent,
16261
+ contexts
15143
16262
  };
15144
16263
  }
15145
16264
 
@@ -15243,7 +16362,18 @@ async function executeSingleFanOut(indexName, params, binding, limit, offset, fa
15243
16362
  }
15244
16363
  const depth = limit + offset;
15245
16364
  const searchParams = { ...params, limit: depth, offset: 0 };
15246
- const outcomes = await fanOutSearch(indexName, searchParams, globalStats, facetShardSize, routing, deps, config);
16365
+ const keepAliveMs = config.searchContexts ? searchContextKeepAlive(routing, config.partitionTimeout, deps.transport) : null;
16366
+ const outcomes = await fanOutSearch(
16367
+ indexName,
16368
+ searchParams,
16369
+ globalStats,
16370
+ facetShardSize,
16371
+ routing,
16372
+ deps,
16373
+ config,
16374
+ keepAliveMs
16375
+ );
16376
+ const contexts = collectSearchContexts(outcomes);
15247
16377
  const sortFields = params.sort !== null && params.sort.length > 0 ? params.sort : null;
15248
16378
  if (sortFields !== null) {
15249
16379
  failOutcomesMissingSortValues(outcomes);
@@ -15252,6 +16382,7 @@ async function executeSingleFanOut(indexName, params, binding, limit, offset, fa
15252
16382
  if (!config.allowPartialResults) {
15253
16383
  const failedCount = coverage.timedOutPartitions + coverage.failedPartitions;
15254
16384
  if (failedCount > 0) {
16385
+ releaseSearchContexts(indexName, contexts.opened, deps);
15255
16386
  throw new NarsilError(ErrorCodes.QUERY_PARTIAL_FAILURE, "One or more partitions failed during query", {
15256
16387
  coverage
15257
16388
  });
@@ -15319,7 +16450,8 @@ async function executeSingleFanOut(indexName, params, binding, limit, offset, fa
15319
16450
  cursor,
15320
16451
  coverage,
15321
16452
  pinnedFromOutside: placement.placedFromOutside,
15322
- mergeHeldEveryMatch: allMatchesPresent
16453
+ mergeHeldEveryMatch: allMatchesPresent,
16454
+ contexts
15323
16455
  };
15324
16456
  }
15325
16457
  function failOutcomesMissingSortValues(outcomes) {
@@ -15334,7 +16466,7 @@ function failOutcomesMissingSortValues(outcomes) {
15334
16466
  }
15335
16467
 
15336
16468
  // src/distribution/cluster-node/reads/groups.ts
15337
- async function assembleDistributedGroups(deps, indexName, params, distributed, allocation, projection) {
16469
+ async function assembleDistributedGroups(params, distributed, projection, readDocuments) {
15338
16470
  const wireGroups = distributed.groups;
15339
16471
  const group = params.group;
15340
16472
  if (wireGroups === null || group === void 0) {
@@ -15347,19 +16479,19 @@ async function assembleDistributedGroups(deps, indexName, params, distributed, a
15347
16479
  const results = [];
15348
16480
  let batch = [];
15349
16481
  let batchDocIds = /* @__PURE__ */ new Set();
15350
- async function assembleBatch() {
15351
- const documents = needsDocuments && batchDocIds.size > 0 ? await readDistributedDocuments(deps.config, deps.nodeId, deps.engine, indexName, [...batchDocIds], allocation) : /* @__PURE__ */ new Map();
16482
+ async function assembleBatch(last) {
16483
+ const documents = needsDocuments && batchDocIds.size > 0 ? await readDocuments([...batchDocIds], last) : /* @__PURE__ */ new Map();
15352
16484
  for (const entry of batch) results.push(assembleGroup(entry, documents, keptPerGroup, projection, reduce));
15353
16485
  batch = [];
15354
16486
  batchDocIds = /* @__PURE__ */ new Set();
15355
16487
  }
15356
16488
  for (const entry of wireGroups) {
15357
16489
  const docIds = docIdsRead(entry);
15358
- if (batch.length > 0 && batchDocIds.size + docIds.length > MAX_FETCH_DOCUMENT_IDS) await assembleBatch();
16490
+ if (batch.length > 0 && batchDocIds.size + docIds.length > MAX_FETCH_DOCUMENT_IDS) await assembleBatch(false);
15359
16491
  batch.push(entry);
15360
16492
  for (const docId of docIds) batchDocIds.add(docId);
15361
16493
  }
15362
- if (batch.length > 0) await assembleBatch();
16494
+ if (batch.length > 0) await assembleBatch(true);
15363
16495
  return results;
15364
16496
  }
15365
16497
  function assembleGroup(entry, documents, keptPerGroup, projection, reduce) {
@@ -15430,6 +16562,147 @@ function countStoredPinsFromOutside(distributed, presence) {
15430
16562
  }
15431
16563
  return { stored: count2, everyPinVerified };
15432
16564
  }
16565
+ var NODE_LOSS_CODES = /* @__PURE__ */ new Set([
16566
+ TransportErrorCodes.CONNECT_FAILED,
16567
+ TransportErrorCodes.PEER_UNAVAILABLE,
16568
+ TransportErrorCodes.TIMEOUT
16569
+ ]);
16570
+ function contextLost(context, indexName, cause) {
16571
+ return new NarsilError(
16572
+ ErrorCodes.SEARCH_CONTEXT_MISSING,
16573
+ `Node '${context.nodeId}' no longer holds the documents it ranked for a search of index '${indexName}'`,
16574
+ { indexName, nodeId: context.nodeId, contextId: context.contextId, ...cause ? { cause: cause.code } : {} }
16575
+ );
16576
+ }
16577
+ function lostContextOf(error, context, indexName) {
16578
+ if (!(error instanceof NarsilError)) return null;
16579
+ if (error.code === ErrorCodes.SEARCH_CONTEXT_MISSING) return contextLost(context, indexName, error);
16580
+ if (NODE_LOSS_CODES.has(error.details.transportCode)) return contextLost(context, indexName, error);
16581
+ return null;
16582
+ }
16583
+ function createRankedDocumentReader(deps, indexName, allocation) {
16584
+ const sources = /* @__PURE__ */ new Map();
16585
+ const open = /* @__PURE__ */ new Map();
16586
+ const partitionCount = allocation.assignments.size;
16587
+ function releaseQuietly(contexts) {
16588
+ const remote = [];
16589
+ for (const context of contexts) {
16590
+ open.delete(context.contextId);
16591
+ if (context.nodeId === deps.nodeId && deps.searchContexts !== void 0) {
16592
+ deps.searchContexts.release(context.contextId);
16593
+ } else {
16594
+ remote.push(context);
16595
+ }
16596
+ }
16597
+ releaseSearchContexts(indexName, remote, {
16598
+ transport: deps.config.transport,
16599
+ sourceNodeId: deps.nodeId,
16600
+ resolveNodeTargets: deps.resolveNodeTargets
16601
+ });
16602
+ }
16603
+ function readOwnContext(context, docIds, last) {
16604
+ const registry = deps.searchContexts;
16605
+ const found = registry?.read(context.contextId, docIds) ?? null;
16606
+ if (last) {
16607
+ registry?.release(context.contextId);
16608
+ open.delete(context.contextId);
16609
+ }
16610
+ if (found === null) throw contextLost(context, indexName);
16611
+ const documents = /* @__PURE__ */ new Map();
16612
+ for (const [docId, snapshot] of found) documents.set(docId, documentOfSnapshot(snapshot));
16613
+ return documents;
16614
+ }
16615
+ async function readRemoteContext(context, docIds, last, fields) {
16616
+ const documents = /* @__PURE__ */ new Map();
16617
+ for (let start = 0; start < docIds.length; start += FETCH_GROUP_DOCUMENT_IDS) {
16618
+ const chunk = docIds.slice(start, start + FETCH_GROUP_DOCUMENT_IDS);
16619
+ const releaseContext = last && start + FETCH_GROUP_DOCUMENT_IDS >= docIds.length;
16620
+ const message = createFetchMessage(
16621
+ {
16622
+ indexName,
16623
+ documentIds: chunk.map((docId) => ({ docId, partitionId: resolvePartitionId(docId, partitionCount) })),
16624
+ fields,
16625
+ highlight: null,
16626
+ contextId: context.contextId,
16627
+ releaseContext
16628
+ },
16629
+ deps.nodeId
16630
+ );
16631
+ let response;
16632
+ try {
16633
+ response = await sendToNode2(deps.config, context.nodeId, message, {
16634
+ message: `Node '${context.nodeId}' did not answer a document fetch for index '${indexName}'`,
16635
+ details: { indexName }
16636
+ });
16637
+ } catch (error) {
16638
+ throw lostContextOf(error, context, indexName) ?? error;
16639
+ }
16640
+ if (releaseContext) open.delete(context.contextId);
16641
+ refuseDecodeWithoutRoom(response.payload);
16642
+ const decoded = decode(response.payload);
16643
+ if (response.type.endsWith(".error") || decoded.error === true) {
16644
+ const rejection = readRejectionOf(decoded, context.nodeId, indexName);
16645
+ throw lostContextOf(rejection, context, indexName) ?? rejection;
16646
+ }
16647
+ for (const fetched of validateFetchResultPayload(decoded).documents) {
16648
+ documents.set(fetched.docId, restoreVectorValues(fetched.document));
16649
+ }
16650
+ }
16651
+ return documents;
16652
+ }
16653
+ return {
16654
+ adopt(contexts) {
16655
+ if (contexts === void 0) return;
16656
+ for (const context of contexts.opened) open.set(context.contextId, context);
16657
+ for (const [docId, context] of contexts.sources) if (!sources.has(docId)) sources.set(docId, context);
16658
+ },
16659
+ async read(docIds, options) {
16660
+ const byContext = /* @__PURE__ */ new Map();
16661
+ const unsourced = [];
16662
+ for (const docId of new Set(docIds)) {
16663
+ const context = sources.get(docId);
16664
+ if (context === void 0 || !open.has(context.contextId)) {
16665
+ unsourced.push(docId);
16666
+ continue;
16667
+ }
16668
+ const group = byContext.get(context.contextId);
16669
+ if (group === void 0) byContext.set(context.contextId, { context, docIds: [docId] });
16670
+ else group.docIds.push(docId);
16671
+ }
16672
+ if (options.last) {
16673
+ releaseQuietly([...open.values()].filter((context) => !byContext.has(context.contextId)));
16674
+ }
16675
+ const reads = [];
16676
+ for (const { context, docIds: contextIds } of byContext.values()) {
16677
+ reads.push(
16678
+ context.nodeId === deps.nodeId && deps.searchContexts !== void 0 ? Promise.resolve().then(() => readOwnContext(context, contextIds, options.last)) : readRemoteContext(context, contextIds, options.last, options.fields)
16679
+ );
16680
+ }
16681
+ if (unsourced.length > 0) {
16682
+ reads.push(
16683
+ readDocumentsFromCopies(
16684
+ deps.config,
16685
+ deps.nodeId,
16686
+ deps.engine,
16687
+ indexName,
16688
+ unsourced,
16689
+ allocation,
16690
+ options.fields,
16691
+ options.requireEveryPartition
16692
+ )
16693
+ );
16694
+ }
16695
+ const documents = /* @__PURE__ */ new Map();
16696
+ for (const found of await Promise.all(reads)) {
16697
+ for (const [docId, document] of found) documents.set(docId, document);
16698
+ }
16699
+ return documents;
16700
+ },
16701
+ release() {
16702
+ releaseQuietly([...open.values()]);
16703
+ }
16704
+ };
16705
+ }
15433
16706
  async function gatherFromEveryPartition(allocation, indexName, read) {
15434
16707
  const routing = selectReplicasForQuery(allocation, randomSelector);
15435
16708
  if (routing.unavailablePartitions.length > 0) {
@@ -15800,35 +17073,70 @@ async function readClusterDocuments(deps, indexName, docIds) {
15800
17073
  }
15801
17074
  return readDistributedDocuments(deps.config, deps.nodeId, deps.engine, indexName, docIds, allocation);
15802
17075
  }
15803
- function routingConfig(query) {
15804
- if (query === void 0) {
15805
- return void 0;
15806
- }
15807
- const config = {};
15808
- if (query.allowPartialResults !== void 0) {
17076
+ function routingConfig(query, searchContexts) {
17077
+ const config = { searchContexts };
17078
+ if (query?.allowPartialResults !== void 0) {
15809
17079
  config.allowPartialResults = query.allowPartialResults;
15810
17080
  }
15811
- if (query.partitionTimeout !== void 0) {
17081
+ if (query?.partitionTimeout !== void 0) {
15812
17082
  config.partitionTimeout = query.partitionTimeout;
15813
17083
  }
15814
17084
  return config;
15815
17085
  }
17086
+ function fetchFieldsFor(projection) {
17087
+ if (projection.kind !== "fields" || projection.include === null) return null;
17088
+ return projection.include.map((path) => path.join("."));
17089
+ }
17090
+ async function rerunOnLostContext(run) {
17091
+ try {
17092
+ return await run();
17093
+ } catch (error) {
17094
+ if (!(error instanceof NarsilError) || error.code !== ErrorCodes.SEARCH_CONTEXT_MISSING) throw error;
17095
+ return run();
17096
+ }
17097
+ }
17098
+ async function withRankedReader(deps, indexName, allocation, run) {
17099
+ const reader = createRankedDocumentReader(deps, indexName, allocation);
17100
+ try {
17101
+ return await run(reader);
17102
+ } finally {
17103
+ reader.release();
17104
+ }
17105
+ }
15816
17106
  async function queryCluster(deps, indexName, params) {
15817
17107
  const allocation = await routableAllocation(deps.config.coordinator, indexName);
15818
17108
  if (allocation === null) {
15819
17109
  return deps.engine.query(indexName, params);
15820
17110
  }
15821
- if (params.rerank !== void 0) {
15822
- const { rerankClusterQuery } = await import('./rerank-WVFFZV76.mjs');
15823
- return rerankClusterQuery(
17111
+ return rerunOnLostContext(async () => {
17112
+ if (params.rerank === void 0) {
17113
+ return withRankedReader(
17114
+ deps,
17115
+ indexName,
17116
+ allocation,
17117
+ (reader) => searchCluster(deps, indexName, params, allocation, reader, false)
17118
+ );
17119
+ }
17120
+ const { rerankClusterQuery } = await import('./rerank-3POMPQNX.mjs');
17121
+ return withRankedReader(
15824
17122
  deps,
15825
17123
  indexName,
15826
- params,
15827
- (firstStage) => queryCluster(deps, indexName, firstStage),
15828
- (docIds) => readDistributedDocuments(deps.config, deps.nodeId, deps.engine, indexName, docIds, allocation),
15829
- clusterRerankHost
17124
+ allocation,
17125
+ (reader) => rerankClusterQuery(
17126
+ deps,
17127
+ indexName,
17128
+ params,
17129
+ (firstStage) => searchCluster(deps, indexName, firstStage, allocation, reader, true),
17130
+ (docIds, last) => reader.read(docIds, { last, fields: null, requireEveryPartition: true }),
17131
+ clusterRerankHost
17132
+ )
15830
17133
  );
15831
- }
17134
+ });
17135
+ }
17136
+ async function searchCluster(deps, indexName, params, allocation, reader, readsLater) {
17137
+ const projection = resolveProjection(params.document);
17138
+ const groupsRead = params.group !== void 0 && (params.group.reduce !== void 0 || projection.kind !== "none");
17139
+ const readsDocuments = readsLater || projection.kind !== "none" || groupsRead;
15832
17140
  const wireParams = localParamsToWire(params);
15833
17141
  const queryDeps = {
15834
17142
  transport: deps.config.transport,
@@ -15836,17 +17144,21 @@ async function queryCluster(deps, indexName, params) {
15836
17144
  getAllocation: (idx) => deps.config.coordinator.getAllocation(idx),
15837
17145
  resolveNodeTargets: deps.resolveNodeTargets
15838
17146
  };
15839
- const distributed = await distributedQuery(indexName, wireParams, queryDeps, routingConfig(deps.config.query));
15840
- const projection = resolveProjection(params.document);
15841
- const documents = await fetchDistributedDocuments(
15842
- deps.config,
15843
- deps.nodeId,
15844
- deps.engine,
17147
+ const distributed = await distributedQuery(
15845
17148
  indexName,
15846
- distributed,
15847
- allocation,
15848
- projection
17149
+ wireParams,
17150
+ queryDeps,
17151
+ routingConfig(deps.config.query, readsDocuments)
15849
17152
  );
17153
+ reader.adopt(distributed.contexts);
17154
+ const documents = /* @__PURE__ */ new Map();
17155
+ if (projection.kind !== "none") {
17156
+ const found = await reader.read(
17157
+ distributed.scored.map((entry) => entry.docId),
17158
+ { last: !groupsRead && !readsLater, fields: fetchFieldsFor(projection), requireEveryPartition: false }
17159
+ );
17160
+ for (const [docId, document] of found) documents.set(docId, applyProjection(document, projection));
17161
+ }
15850
17162
  const pinPresence = await readPinPresence(deps, indexName, params, allocation);
15851
17163
  const scored = dropUnstoredPinnedEntries(distributed, pinPresence);
15852
17164
  const pins = countStoredPinsFromOutside(distributed, pinPresence);
@@ -15863,7 +17175,12 @@ async function queryCluster(deps, indexName, params) {
15863
17175
  documents,
15864
17176
  params.facets
15865
17177
  );
15866
- const groups = await assembleDistributedGroups(deps, indexName, params, distributed, allocation, projection);
17178
+ const groups = await assembleDistributedGroups(
17179
+ params,
17180
+ distributed,
17181
+ projection,
17182
+ (docIds, last) => reader.read(docIds, { last: last && !readsLater, fields: null, requireEveryPartition: true })
17183
+ );
15867
17184
  if (groups !== void 0) {
15868
17185
  result.groups = groups;
15869
17186
  }
@@ -15874,7 +17191,7 @@ async function queryCluster(deps, indexName, params) {
15874
17191
  async function clearCluster(readDeps, writeDeps, indexName) {
15875
17192
  const allocation = await routableAllocation(readDeps.config.coordinator, indexName);
15876
17193
  if (allocation === null) {
15877
- return readDeps.engine.clear(indexName);
17194
+ return writeLocalOnly(writeDeps, indexName, () => readDeps.engine.clear(indexName));
15878
17195
  }
15879
17196
  for (; ; ) {
15880
17197
  const page = await listCluster(readDeps, indexName, { limit: CLEAR_PAGE_SIZE, document: false });
@@ -16466,7 +17783,7 @@ function createNodeMemoryReporting(options) {
16466
17783
  }
16467
17784
 
16468
17785
  // src/distribution/cluster-node/write-routing/numbering.ts
16469
- function createPrimaryNumbering() {
17786
+ function createPrimaryNumbering(onNumbered) {
16470
17787
  const scopes = /* @__PURE__ */ new Map();
16471
17788
  return {
16472
17789
  admit(indexName, partitionId, documentId, document) {
@@ -16488,6 +17805,7 @@ function createPrimaryNumbering() {
16488
17805
  document: record.document
16489
17806
  });
16490
17807
  scope.entries.push(entry);
17808
+ onNumbered?.(entry);
16491
17809
  return entry;
16492
17810
  },
16493
17811
  defersCommit(record) {
@@ -16551,12 +17869,16 @@ function createNodeWiring(input) {
16551
17869
  config: config.memory,
16552
17870
  onFullChange: input.renewRegistration
16553
17871
  });
16554
- const numbering = createPrimaryNumbering();
17872
+ engine.writeRecords.readRetainedLogWith(
17873
+ (indexName, partitionId) => replicationLogs.get(replicationLogKey(indexName, partitionId))?.oldestSeqNo
17874
+ );
17875
+ const numbering = createPrimaryNumbering(engine.writeRecords.applied);
16555
17876
  engine.numberEntriesWith(numbering.numberEntry, numbering.defersCommit);
16556
17877
  engine.admitWritesWith(numbering.admit);
16557
17878
  const writeDeps = {
16558
17879
  nodeId,
16559
17880
  numbering,
17881
+ writeRecords: engine.writeRecords,
16560
17882
  commitPartitionLog: engine.commitPartitionLog,
16561
17883
  coordinator: allocationView.routing,
16562
17884
  refreshAllocation: allocationView.refresh,
@@ -17184,6 +18506,8 @@ async function createClusterNode(callerConfig) {
17184
18506
  let controller = null;
17185
18507
  let unregisterHandler = null;
17186
18508
  let unregisterNotReadyHandler = null;
18509
+ let unwatchDepartures = null;
18510
+ const searchContexts = hasDataRole ? createSearchContextRegistry() : void 0;
17187
18511
  let serving = false;
17188
18512
  let isShutdown = false;
17189
18513
  const copyFailures = createCopyFailures({
@@ -17225,6 +18549,7 @@ async function createClusterNode(callerConfig) {
17225
18549
  allocationDebounceMs: DEFAULT_NODE_LIFECYCLE_CONFIG.allocationDebounceMs,
17226
18550
  nodeHeartbeatIntervalMs: DEFAULT_NODE_LIFECYCLE_CONFIG.nodeHeartbeatIntervalMs,
17227
18551
  onBootstrapPartition: async (indexName, partitionId, primaryNodeId) => await copyFailures.awaitReset(indexName, partitionId) && bootstrapPartition2(indexName, partitionId, primaryNodeId),
18552
+ onLeadingTerm: (indexName, partitionId, assignment) => startLeadingTerm(writeDeps, indexName, partitionId, assignment),
17228
18553
  onHoldPartition: (indexName, partitionId) => {
17229
18554
  trackHeldPartitionWrite(engine.recordHeldPartition(indexName, partitionId));
17230
18555
  },
@@ -17261,7 +18586,7 @@ async function createClusterNode(callerConfig) {
17261
18586
  });
17262
18587
  const trackOp = operationTracker.track;
17263
18588
  const transitionIndex = operationTracker.transition;
17264
- const readDeps = { config: routingConfig2, nodeId, engine, resolveNodeTargets: resolveNodeTargets3 };
18589
+ const readDeps = { config: routingConfig2, nodeId, engine, resolveNodeTargets: resolveNodeTargets3, searchContexts };
17265
18590
  const servingName = (indexName) => servingNameOf(allocationView.routing, indexName);
17266
18591
  const cluster = createClusterNamespace({
17267
18592
  nodeId,
@@ -17340,12 +18665,16 @@ async function createClusterNode(callerConfig) {
17340
18665
  allocations: allocationView,
17341
18666
  writeDeps,
17342
18667
  snapshotSyncState: snapshotSyncHandlerState,
18668
+ searchContexts,
17343
18669
  snapshotSpoolDirectory: snapshotSpool.path,
17344
18670
  isBootstrapSynced: (indexName, partitionId) => hasCompletedBootstrapSync(bootstrapSyncState, indexName, partitionId),
17345
18671
  onError: forwardOnError
17346
18672
  });
17347
18673
  const listener = controllerTransport !== null ? controllerTransport.createHandler(dataHandler) : dataHandler;
17348
18674
  unregisterHandler = await config.transport.listen(listener);
18675
+ if (searchContexts !== void 0) {
18676
+ unwatchDepartures = await releaseContextsOfDepartedNodes(config.coordinator, searchContexts);
18677
+ }
17349
18678
  startCatchUpPump(writeDeps.catchUp, writeDeps);
17350
18679
  }
17351
18680
  serving = true;
@@ -17375,6 +18704,9 @@ async function createClusterNode(callerConfig) {
17375
18704
  unregisterNotReadyHandler();
17376
18705
  unregisterNotReadyHandler = null;
17377
18706
  }
18707
+ unwatchDepartures?.();
18708
+ unwatchDepartures = null;
18709
+ searchContexts?.stop();
17378
18710
  await lifecycle.shutdown();
17379
18711
  memoryReporting.stop();
17380
18712
  if (controller !== null) {