@delali/narsil 0.3.1-next.109 → 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 (93) hide show
  1. package/dist/adapters/filesystem.mjs +1 -1
  2. package/dist/adapters/indexeddb.mjs +1 -1
  3. package/dist/browser/{chunk-RYWURYTW.mjs → chunk-2CBQ2PCC.mjs} +1 -1
  4. package/dist/browser/{chunk-3RXEGUGO.mjs → chunk-5BQPBG3G.mjs} +41 -4
  5. package/dist/browser/{chunk-XHRHOH2P.mjs → chunk-6YUUEMQ4.mjs} +139 -28
  6. package/dist/browser/{chunk-SCBBEQWD.mjs → chunk-HQOYKRMB.mjs} +13 -13
  7. package/dist/browser/{chunk-EQ7BQ6VZ.mjs → chunk-LJS6PREA.mjs} +1 -0
  8. package/dist/browser/{chunk-Z2K5AGZL.mjs → chunk-OVY4RPDS.mjs} +3 -3
  9. package/dist/browser/{chunk-S7KNCVPU.mjs → chunk-P7C2S5JX.mjs} +2 -2
  10. package/dist/browser/{durability-wiring-JJGE7MED.mjs → durability-wiring-CYAJBYCO.mjs} +48 -20
  11. package/dist/browser/{module-PCKQADMQ.mjs → module-DYKR4INA.mjs} +5 -5
  12. package/dist/browser/{restore-queue-2STSWU2E.mjs → restore-queue-RTZGJT4D.mjs} +1 -1
  13. package/dist/browser/{runtime-ARKW5NZX.mjs → runtime-FL5K4JOH.mjs} +1 -1
  14. package/dist/browser/{snapshot-3AZDQKNA.mjs → snapshot-BU6I25CW.mjs} +27 -14
  15. package/dist/browser/{write-XXLMPT3Y.mjs → write-RUPYHPBZ.mjs} +5 -5
  16. package/dist/{capabilities-D4MUmjfe.d.ts → capabilities-ByaX8Arf.d.ts} +2 -2
  17. package/dist/{chunk-FN75BCXC.mjs → chunk-2RAQKITH.mjs} +1 -1
  18. package/dist/{chunk-SAZB7ZHT.mjs → chunk-542TD4BL.mjs} +1 -1
  19. package/dist/{chunk-YP23IAV5.mjs → chunk-63HFWQEA.mjs} +4 -3
  20. package/dist/{chunk-FV5CP3E2.mjs → chunk-6ODIY6ZB.mjs} +1 -1
  21. package/dist/{chunk-RQ7LOBLD.mjs → chunk-6PN3U6XW.mjs} +1 -1
  22. package/dist/{chunk-CYXLPFBG.mjs → chunk-6S7YRUPQ.mjs} +123 -35
  23. package/dist/{chunk-C57FPCTL.mjs → chunk-7JOONQ24.mjs} +6 -6
  24. package/dist/{chunk-IBJWO5PF.mjs → chunk-7ZZQNOIY.mjs} +1 -1
  25. package/dist/{chunk-SYHS56F7.mjs → chunk-CUVTRLA4.mjs} +26 -13
  26. package/dist/{chunk-AWZVJCCW.mjs → chunk-DG62H7OD.mjs} +2 -2
  27. package/dist/{chunk-LWQTFKRS.mjs → chunk-FUUP45IS.mjs} +1 -1
  28. package/dist/{chunk-C55OFKU6.mjs → chunk-HNAP2KTG.mjs} +2 -2
  29. package/dist/{chunk-5LP7OU6B.mjs → chunk-IR4KQ4WB.mjs} +2 -2
  30. package/dist/{chunk-ICZW4J3I.mjs → chunk-IW6UR3AF.mjs} +21 -21
  31. package/dist/{chunk-FABI2JA7.mjs → chunk-J6YVB2XM.mjs} +1 -1
  32. package/dist/{chunk-H5465KUZ.mjs → chunk-JBPYFYIU.mjs} +4 -3
  33. package/dist/{chunk-62GK7UUS.mjs → chunk-KCQG2PJT.mjs} +3 -3
  34. package/dist/{chunk-U6FT7LKG.mjs → chunk-LOBYK2FG.mjs} +3 -3
  35. package/dist/{chunk-XY6Z7T5W.mjs → chunk-N57XXGPG.mjs} +2 -2
  36. package/dist/{chunk-S2CKO6PP.mjs → chunk-OBWUWZRA.mjs} +1 -0
  37. package/dist/{chunk-CKRP7GCS.mjs → chunk-OCB7TMUQ.mjs} +1 -1
  38. package/dist/{chunk-NYWUBTMH.mjs → chunk-OFYKBKKV.mjs} +1 -1
  39. package/dist/{chunk-VIXXQEX6.mjs → chunk-PKE2PCTS.mjs} +43 -6
  40. package/dist/{chunk-ZBZRBM2Z.mjs → chunk-PT7VRWS7.mjs} +3 -3
  41. package/dist/{chunk-DUWYU4D5.mjs → chunk-PWIWSUCX.mjs} +2 -2
  42. package/dist/{chunk-5W7ADHK7.mjs → chunk-QI6JW4PM.mjs} +1 -1
  43. package/dist/{chunk-SD2EYVU4.mjs → chunk-QIFOXLWF.mjs} +2 -2
  44. package/dist/{chunk-N75MAOM7.mjs → chunk-R2MRGYU5.mjs} +2 -2
  45. package/dist/{chunk-4GD43FSW.mjs → chunk-R2MRMH6M.mjs} +10 -10
  46. package/dist/{chunk-RSSLOFRP.mjs → chunk-SXVDBHYJ.mjs} +39 -8
  47. package/dist/{chunk-HL2GAS5N.mjs → chunk-UGKO3RG2.mjs} +7 -7
  48. package/dist/{chunk-5DPTHLMX.mjs → chunk-V7G627TA.mjs} +1 -1
  49. package/dist/{chunk-MVWWAHTR.mjs → chunk-VK45J47R.mjs} +1 -1
  50. package/dist/{chunk-RPIE4QCL.mjs → chunk-YH5OIL7B.mjs} +2 -0
  51. package/dist/{chunk-JKOBHB4A.mjs → chunk-YIDSHBQS.mjs} +2 -2
  52. package/dist/{chunk-PA5WZWLC.mjs → chunk-YLE75NQ5.mjs} +2 -2
  53. package/dist/{chunk-A2HWRBQE.mjs → chunk-YSR5L3XJ.mjs} +2 -2
  54. package/dist/{client-Ba9yP-Rr.d.ts → client-BdnzDFtO.d.ts} +2 -2
  55. package/dist/client.d.ts +6 -6
  56. package/dist/client.mjs +1 -0
  57. package/dist/{config-C-xXz8bC.d.ts → config-CSgvApt0.d.ts} +1 -1
  58. package/dist/{config-Dc_8-byz.d.ts → config-ujZESkJz.d.ts} +1 -1
  59. package/dist/{direct-executor-QH2M6AK2.mjs → direct-executor-QFAUKTCQ.mjs} +11 -11
  60. package/dist/distribution/coordinator/etcd.mjs +94 -5
  61. package/dist/distribution/transport/grpc.mjs +24 -8
  62. package/dist/distribution/transport/in-memory.mjs +4 -4
  63. package/dist/distribution/transport/tcp.mjs +6 -6
  64. package/dist/distribution.d.ts +5 -5
  65. package/dist/distribution.mjs +1232 -377
  66. package/dist/{durability-wiring-4HWX7OVP.mjs → durability-wiring-NUHTYXHA.mjs} +65 -38
  67. package/dist/embeddings/openai.mjs +2 -2
  68. package/dist/{engine-CMZ5XSLy.d.ts → engine-C_HSy3iz.d.ts} +1 -1
  69. package/dist/{events-dnvgprIl.d.ts → events-CJsXF1F9.d.ts} +1 -0
  70. package/dist/index.browser.mjs +3 -3
  71. package/dist/index.d.ts +5 -5
  72. package/dist/index.mjs +16 -16
  73. package/dist/{module-IYILOTU3.mjs → module-WP4VVGV2.mjs} +9 -9
  74. package/dist/partial-words.mjs +5 -5
  75. package/dist/pattern.mjs +3 -3
  76. package/dist/persistence/durability/checkpoint-worker.d.ts +12 -0
  77. package/dist/persistence/durability/checkpoint-worker.mjs +25 -23
  78. package/dist/react.d.ts +6 -6
  79. package/dist/react.mjs +1 -0
  80. package/dist/{rerank-EHJVXEDP.mjs → rerank-3POMPQNX.mjs} +4 -4
  81. package/dist/rerankers/http.mjs +2 -2
  82. package/dist/{restore-queue-BONSQWCI.mjs → restore-queue-QSLGXZJB.mjs} +1 -1
  83. package/dist/{runtime-LEX2F7OW.mjs → runtime-C2435MPK.mjs} +5 -5
  84. package/dist/server.d.ts +8 -8
  85. package/dist/server.mjs +12 -12
  86. package/dist/{snapshot-DWDLN5YW.mjs → snapshot-IV3S2TMI.mjs} +25 -25
  87. package/dist/synonyms.mjs +3 -3
  88. package/dist/{tasks-B4B2cMK5.d.ts → tasks-CI3Fvkxw.d.ts} +1 -1
  89. package/dist/{thread-I32PUURR.mjs → thread-THLCKMEB.mjs} +11 -11
  90. package/dist/vector/search-worker.mjs +4 -4
  91. package/dist/workers/entry.mjs +5 -5
  92. package/dist/{write-AOFSHACR.mjs → write-VGRTOS7N.mjs} +18 -18
  93. package/package.json +7 -7
@@ -1,30 +1,30 @@
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-U6FT7LKG.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_POLL_INTERVAL_MS, PARTITION_WRITE_CONCURRENCY, FORWARD_RETRY_INITIAL_DELAY_MS, PRIMARY_CHANGE_RETRY_WINDOW_MS, FORWARD_RETRY_DELAY_CAP_MS } from './chunk-5DPTHLMX.mjs';
3
- export { DEFAULT_CAPACITY, DEFAULT_PARTITION_COUNT, DEFAULT_REPLICATION_FACTOR } from './chunk-5DPTHLMX.mjs';
4
- import { fixedHybridDepthOf, requireWithinHybridDepth, pageByPosition } from './chunk-FN75BCXC.mjs';
5
- import { closePartitionWriter, replaySyncedWal } from './chunk-H5465KUZ.mjs';
6
- import { buildArchiveFromMemory, archiveStream, memoryCheckpointStore, readSnapshotArchive } from './chunk-SYHS56F7.mjs';
7
- import './chunk-ZBZRBM2Z.mjs';
8
- import { indexMetadataOf, loadPartitionSegments, readSegmentManifest, readVectorFile } from './chunk-RSSLOFRP.mjs';
9
- import { decodeVectorFilePayload, bytesToVectors, isDead } from './chunk-CKRP7GCS.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-JKOBHB4A.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-CYXLPFBG.mjs';
17
- import { applyIndexEntry, applyDeleteEntry, validateReplicationEntry } from './chunk-VIXXQEX6.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-ICZW4J3I.mjs';
21
- import { runtimeHost, requireSurfaceFormsForPartialFields, now, listBindingOf, clampLimit, hitsKeptPerGroup, queryBindingOf, resolveHybridFusion, dedupePinnedEntries, foldGroupReducer } from './chunk-HL2GAS5N.mjs';
22
- import { bindRerankWindowSource, rerankWindowCapOf, providedDocId, engineCoreOf } from './chunk-XY6Z7T5W.mjs';
23
- import './chunk-RQ7LOBLD.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-4GD43FSW.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-C57FPCTL.mjs';
27
- import { generateId, getLanguage } from './chunk-LWQTFKRS.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
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';
@@ -32,29 +32,29 @@ 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-MVWWAHTR.mjs';
36
- import { isPatternTolerance } from './chunk-C55OFKU6.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-62GK7UUS.mjs';
39
- import { validateSchema, requireCreatableSchema, encodeDocumentReply, encodeDocument, schemaFieldsOf, MIN_POLYGON_POINTS, isLatitude, MAX_LATITUDE_DEGREES, isLongitude, MAX_LONGITUDE_DEGREES } from './chunk-AWZVJCCW.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-FV5CP3E2.mjs';
45
- import { resolveIndexAnalysis } from './chunk-5LP7OU6B.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-NYWUBTMH.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
51
  import { collectAndReturnPages } from './chunk-6OXNHLNZ.mjs';
52
52
  import './chunk-CQUMNIIG.mjs';
53
- import './chunk-5W7ADHK7.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, ServerErrorCodes } from './chunk-S2CKO6PP.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);
@@ -7949,18 +8057,251 @@ function createAdmittingRecorder(core, admission, durable) {
7949
8057
  };
7950
8058
  }
7951
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
+
7952
8261
  // src/distribution/cluster-node/local-engine.ts
7953
8262
  async function createClusterLocalEngine(config, hooks) {
7954
8263
  requireKnownConfig(config);
7955
8264
  const runnableConfig = await resolveRunnableConfig(config);
7956
- 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);
7957
8278
  await startEngineCore(core, runnableConfig);
7958
8279
  core.orchestrator.shareMainThread();
7959
8280
  const engine = createNarsilFromCore(core, runnableConfig);
7960
8281
  const heldPartitions = createHeldPartitionRecord(core);
8282
+ const dropIndex = engine.dropIndex.bind(engine);
8283
+ const restore = engine.restore.bind(engine);
7961
8284
  return Object.assign(engine, {
7962
- createIndex: (name, indexConfig) => createEngineIndex(core, runnableConfig, name, indexConfig, { adapterBinding: "deferred" }),
7963
- 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
+ },
7964
8305
  acquireIndexForReplication: (indexName) => core.indexState.acquire(indexName, false),
7965
8306
  indexUuidOf: (indexName) => core.indexRegistry.get(indexName)?.indexUuid,
7966
8307
  stampIndexUuid: (indexName, indexUuid) => stampIndexUuid(core, indexName, indexUuid),
@@ -7977,15 +8318,19 @@ async function createClusterLocalEngine(config, hooks) {
7977
8318
  writeAnalysisOf: (indexName) => writeAnalysisOf(core, indexName),
7978
8319
  restartLocalLog: (indexName, partitionId, position) => restartLocalLog(core, indexName, partitionId, position),
7979
8320
  isolateCopyFailures: (onFailed) => core.durability?.manager.isolatePartitionFailures?.(onFailed),
7980
- emptyPartitionCopy: (indexName, partitionId) => emptyPartitionCopy(core, indexName, partitionId),
7981
- 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),
7982
8323
  heldPartitionsOf: (indexName) => heldPartitions.held(indexName),
7983
8324
  recordHeldPartition: (indexName, partitionId) => heldPartitions.record(indexName, partitionId),
7984
8325
  forgetHeldPartition: (indexName, partitionId) => heldPartitions.forget(indexName, partitionId),
7985
- applyReplicationEntry: (entry) => applyLocalReplicationEntry(core, entry),
7986
- applyReplicationEntries: (entries) => applyLocalReplicationEntries(core, entries),
7987
- archiveReplicationPartition: (indexName, partitionId, lastSeqNo) => archiveReplicationPartition(core, indexName, partitionId, lastSeqNo),
7988
- restoreReplicationPartition: (indexName, partitionId, source, indexConfig, partitionCount) => restoreReplicationPartition(core, engine, indexName, partitionId, source, indexConfig, partitionCount),
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
+ }),
7989
8334
  queryPartitions: (indexName, params, partitionIds, globalStats, rescoreWindow, snapshots) => runEngineQuery(core, indexName, params, {
7990
8335
  partitionIds,
7991
8336
  globalStats,
@@ -9893,21 +10238,23 @@ function transportableDetails(error) {
9893
10238
  return void 0;
9894
10239
  }
9895
10240
  }
9896
- 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 };
9897
10243
  if (details !== void 0) {
9898
10244
  try {
9899
- return encode({ error: true, code, message, details });
10245
+ return encode({ ...reply2, details });
9900
10246
  } catch {
9901
- return encode({ error: true, code, message });
10247
+ return encode(reply2);
9902
10248
  }
9903
10249
  }
9904
- return encode({ error: true, code, message });
10250
+ return encode(reply2);
9905
10251
  }
9906
- function encodeErrorReplyFrom(error, fallbackCode) {
10252
+ function encodeErrorReplyFrom(error, fallbackCode, abandonedWrite) {
9907
10253
  return encodeErrorReply(
9908
10254
  error instanceof NarsilError ? error.code : fallbackCode,
9909
10255
  error instanceof Error ? error.message : String(error),
9910
- error instanceof NarsilError ? error.details : void 0
10256
+ error instanceof NarsilError ? error.details : void 0,
10257
+ abandonedWrite
9911
10258
  );
9912
10259
  }
9913
10260
  function isPlainRecord(value) {
@@ -10072,71 +10419,6 @@ function requireWritableIndex(table, view) {
10072
10419
  throw block;
10073
10420
  }
10074
10421
  }
10075
- function assertForwardResponse(response, indexName, primaryNodeId) {
10076
- const decoded = decode(response.payload);
10077
- if (response.type.endsWith(".error") || response.type === "error" || decoded.error === true) {
10078
- throw new NarsilError(
10079
- typeof decoded.code === "string" ? decoded.code : ErrorCodes.QUERY_ROUTING_FAILED,
10080
- typeof decoded.message === "string" ? decoded.message : `Remote primary rejected a forwarded write for index '${indexName}'`,
10081
- { ...remoteErrorDetails(decoded.details), indexName, primaryNodeId }
10082
- );
10083
- }
10084
- return decoded;
10085
- }
10086
- async function forwardInsertToRemote(indexName, document, docId, primaryNodeId, deps, indexUuid) {
10087
- const payload = {
10088
- indexName,
10089
- documentId: docId,
10090
- operation: "insert",
10091
- document: encodeDocument(document),
10092
- updateFields: null
10093
- };
10094
- if (indexUuid !== void 0) payload.indexUuid = indexUuid;
10095
- const message = createForwardMessage(payload, deps.nodeId);
10096
- const response = await sendToNode(primaryNodeId, message, deps);
10097
- const decoded = assertForwardResponse(response, indexName, primaryNodeId);
10098
- if (typeof decoded.documentId !== "string") {
10099
- throw new NarsilError(
10100
- ErrorCodes.QUERY_ROUTING_FAILED,
10101
- `Remote primary returned invalid forward response for index '${indexName}'`,
10102
- { indexName, primaryNodeId }
10103
- );
10104
- }
10105
- return decoded.documentId;
10106
- }
10107
- async function forwardUpdateToRemote(indexName, document, docId, primaryNodeId, deps, indexUuid) {
10108
- const payload = {
10109
- indexName,
10110
- documentId: docId,
10111
- operation: "update",
10112
- document: encodeDocument(document),
10113
- updateFields: null
10114
- };
10115
- if (indexUuid !== void 0) payload.indexUuid = indexUuid;
10116
- const message = createForwardMessage(payload, deps.nodeId);
10117
- const response = await sendToNode(primaryNodeId, message, deps);
10118
- assertForwardResponse(response, indexName, primaryNodeId);
10119
- }
10120
- async function forwardRemoveToRemote(indexName, docId, primaryNodeId, deps, indexUuid) {
10121
- const payload = {
10122
- indexName,
10123
- documentId: docId,
10124
- operation: "remove",
10125
- document: null,
10126
- updateFields: null
10127
- };
10128
- if (indexUuid !== void 0) payload.indexUuid = indexUuid;
10129
- const message = createForwardMessage(payload, deps.nodeId);
10130
- const response = await sendToNode(primaryNodeId, message, deps);
10131
- assertForwardResponse(response, indexName, primaryNodeId);
10132
- }
10133
- async function sendToNode(nodeId, message, deps) {
10134
- const targets = await resolveNodeTargets(nodeId, deps);
10135
- return sendThroughTargets(targets, (target) => deps.transport.send(target, message), {
10136
- message: `Node '${nodeId}' did not answer a forwarded write`,
10137
- details: { targetNodeId: nodeId }
10138
- });
10139
- }
10140
10422
 
10141
10423
  // src/distribution/cluster-node/write-routing/rollback.ts
10142
10424
  function outcomeUnknown(scope, originalError, reason, because) {
@@ -10244,97 +10526,344 @@ async function settleRollback(scope, fromSeqNo, originalError, failedAt) {
10244
10526
  return outcomeUnknown(scope, originalError, "rollback_open", because);
10245
10527
  }
10246
10528
 
10247
- // src/distribution/cluster-node/write-routing/sent-fields.ts
10248
- function isRecord3(value) {
10249
- return typeof value === "object" && value !== null && !Array.isArray(value) && !ArrayBuffer.isView(value);
10250
- }
10251
- function numbersOf(value) {
10252
- if (value instanceof Float32Array || value instanceof Float64Array) return value;
10253
- if (Array.isArray(value) && value.every((element) => typeof element === "number")) return value;
10254
- return null;
10255
- }
10256
- function sameNumber(sent, stored) {
10257
- return sent === stored || Number.isNaN(sent) && Number.isNaN(stored);
10258
- }
10259
- function sameVector(sent, stored) {
10260
- if (sent.length !== stored.length) return false;
10261
- for (let position = 0; position < sent.length; position++) {
10262
- const sentValue = sent[position];
10263
- const storedValue = stored[position];
10264
- if (!sameNumber(sentValue, storedValue) && !sameNumber(Math.fround(sentValue), Math.fround(storedValue))) {
10265
- return false;
10266
- }
10267
- }
10268
- return true;
10269
- }
10270
- function fieldPath(parent, key) {
10271
- return parent.length === 0 ? key : `${parent}.${key}`;
10272
- }
10273
- function matchesSent(sent, stored, path, derivedFields) {
10274
- const sentNumbers = numbersOf(sent);
10275
- const storedNumbers = numbersOf(stored);
10276
- if (sentNumbers !== null && storedNumbers !== null) return sameVector(sentNumbers, storedNumbers);
10277
- if (Array.isArray(sent)) {
10278
- if (!Array.isArray(stored) || sent.length !== stored.length) return false;
10279
- return sent.every((element, position) => matchesSent(element, stored[position], path, /* @__PURE__ */ new Set()));
10280
- }
10281
- if (isRecord3(sent)) {
10282
- if (!isRecord3(stored)) return false;
10283
- for (const [key, value] of Object.entries(sent)) {
10284
- if (value === void 0) continue;
10285
- if (!matchesSent(value, stored[key], fieldPath(path, key), derivedFields)) return false;
10286
- }
10287
- for (const [key, value] of Object.entries(stored)) {
10288
- if (value === void 0 || sent[key] !== void 0) continue;
10289
- if (!derivedFields.has(fieldPath(path, key))) return false;
10290
- }
10291
- 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;
10292
10548
  }
10293
- if (typeof sent === "number" && typeof stored === "number") return sameNumber(sent, stored);
10294
- return sent === stored;
10295
- }
10296
- function storedCopyMatchesSent(sent, stored, derivedFields) {
10297
- return matchesSent(sent, stored, "", derivedFields);
10298
- }
10299
-
10300
- // src/distribution/cluster-node/write-routing/primary-change.ts
10301
- function refusedByPrimaryChange(error, retriesEndedTerm = false) {
10302
- if (error instanceof NarsilError && error.code === ErrorCodes.PARTITION_NOT_PRIMARY) return true;
10303
- return retriesEndedTerm && abandonedWithTerm(error);
10304
- }
10305
- function refusedForAnotherIndex(error) {
10306
- return refusedByPrimaryChange(error) && error instanceof NarsilError && typeof error.details.heldIndexUuid === "string";
10549
+ return { seqNo, primaryTerm, compensationSeqNo, previousSeqNo, previousPrimaryTerm };
10307
10550
  }
10308
- function retriedAfterDelay(error) {
10309
- return refusedForAnotherIndex(error) || forwardUndelivered(error);
10551
+ function previousRecordOf(scope, documentId) {
10552
+ return scope.deps.writeRecords?.recordOf(scope.indexName, scope.partitionId, documentId) ?? null;
10310
10553
  }
10311
- function hasCode(error, code) {
10312
- 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
+ });
10313
10564
  }
10314
- function primaryFor(table, docId) {
10315
- if (table === null || table.assignments.size === 0) return null;
10316
- const partitionId = resolvePartitionId(docId, table.assignments.size);
10317
- const assignment = table.assignments.get(partitionId);
10565
+ function conditionOf(write) {
10318
10566
  return {
10319
- partitionCount: table.assignments.size,
10320
- partitionId,
10321
- primary: assignment?.primary ?? null,
10322
- primaryTerm: assignment?.primaryTerm ?? 0
10567
+ ifSeqNo: write.previousSeqNo,
10568
+ ifPrimaryTerm: write.previousPrimaryTerm,
10569
+ abandonedSeqNo: write.seqNo,
10570
+ abandonedPrimaryTerm: write.primaryTerm,
10571
+ compensationSeqNo: write.compensationSeqNo
10323
10572
  };
10324
10573
  }
10325
- function movedOn(table, refusing) {
10326
- if (table.assignments.size !== refusing.partitionCount) return true;
10327
- const assignment = table.assignments.get(refusing.partitionId);
10328
- if (assignment === void 0 || assignment.primary === null) return false;
10329
- return assignment.primary !== refusing.primary || assignment.primaryTerm > refusing.primaryTerm;
10330
- }
10331
- async function liveTable(deps, indexName) {
10332
- return deps.refreshAllocation !== void 0 ? deps.refreshAllocation(indexName) : deps.coordinator.getAllocation(indexName);
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
+ };
10333
10583
  }
10334
- async function pauseAndRereadTable(deps, indexName, delayMs, deadline) {
10335
- if (Date.now() >= deadline) return false;
10336
- await pause(Math.min(delayMs, deadline - Date.now()));
10337
- try {
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
10852
+ };
10853
+ }
10854
+ function movedOn(table, refusing) {
10855
+ if (table.assignments.size !== refusing.partitionCount) return true;
10856
+ const assignment = table.assignments.get(refusing.partitionId);
10857
+ if (assignment === void 0 || assignment.primary === null) return false;
10858
+ return assignment.primary !== refusing.primary || assignment.primaryTerm > refusing.primaryTerm;
10859
+ }
10860
+ async function liveTable(deps, indexName) {
10861
+ return deps.refreshAllocation !== void 0 ? deps.refreshAllocation(indexName) : deps.coordinator.getAllocation(indexName);
10862
+ }
10863
+ async function pauseAndRereadTable(deps, indexName, delayMs, deadline) {
10864
+ if (Date.now() >= deadline) return false;
10865
+ await pause(Math.min(delayMs, deadline - Date.now()));
10866
+ try {
10338
10867
  if (await liveTable(deps, indexName) === null) return false;
10339
10868
  } catch (_) {
10340
10869
  }
@@ -10357,19 +10886,22 @@ async function awaitNewPrimary(deps, indexName, refusing, deadline) {
10357
10886
  }
10358
10887
  return false;
10359
10888
  }
10360
- async function retryOnPrimaryChange(deps, indexName, docId, attempt, settleRetried, settledValue, retriesEndedTerm = false) {
10889
+ async function retryOnPrimaryChange(deps, indexName, docId, attempt, settleRetried, settledValue, settlesAsEqual = false) {
10361
10890
  const deadline = Date.now() + PRIMARY_CHANGE_RETRY_WINDOW_MS;
10362
10891
  let retryDelayMs = FORWARD_RETRY_INITIAL_DELAY_MS;
10363
10892
  let retried = false;
10893
+ let interrupted = false;
10894
+ let condition;
10895
+ const finalError = (error) => interrupted ? interruptedOutcome(indexName, docId, error) : error;
10364
10896
  for (; ; ) {
10365
10897
  const refusing = primaryFor(await deps.coordinator.getAllocation(indexName), docId);
10898
+ let outcome;
10366
10899
  try {
10367
- return await attempt();
10900
+ outcome = await beforeWindowEnds(attempt(condition), deadline);
10368
10901
  } catch (error) {
10369
10902
  if (retried && (await settleRetried([{ docId, error }])).has(docId)) return settledValue();
10370
- if (!(refusedByPrimaryChange(error, retriesEndedTerm) || forwardUndelivered(error)) || refusing === null) {
10371
- throw error;
10372
- }
10903
+ interrupted ||= forwardInterrupted(error);
10904
+ if (!retriable(error, settlesAsEqual) || refusing === null) throw finalError(error);
10373
10905
  let retryable;
10374
10906
  if (retriedAfterDelay(error)) {
10375
10907
  retryable = await pauseAndRereadTable(deps, indexName, retryDelayMs, deadline);
@@ -10377,20 +10909,36 @@ async function retryOnPrimaryChange(deps, indexName, docId, attempt, settleRetri
10377
10909
  } else {
10378
10910
  retryable = await awaitNewPrimary(deps, indexName, [refusing], deadline);
10379
10911
  }
10380
- if (!retryable) throw error;
10912
+ if (!retryable) throw finalError(error);
10913
+ condition = nextCondition(error, condition);
10381
10914
  retried = true;
10915
+ continue;
10382
10916
  }
10917
+ if (outcome === WINDOW_ENDED) throw windowEndedOutcome(indexName, docId);
10918
+ return outcome;
10383
10919
  }
10384
10920
  }
10385
- async function retryBatchOnPrimaryChange(deps, indexName, items, docIdOf, attempt, settleRetried, deadline, retriesEndedTerm) {
10921
+ async function retryBatchOnPrimaryChange(deps, indexName, items, docIdOf, attempt, settleRetried, deadline, settlesAsEqual) {
10386
10922
  const succeeded2 = [];
10387
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
+ };
10388
10931
  let pending = [...items];
10389
10932
  let retryDelayMs = FORWARD_RETRY_INITIAL_DELAY_MS;
10390
10933
  let retried = false;
10391
10934
  while (pending.length > 0) {
10392
10935
  const table = await deps.coordinator.getAllocation(indexName);
10393
- 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
+ }
10394
10942
  succeeded2.push(...result.succeeded);
10395
10943
  const settled = retried ? await settleRetried(result.failed) : /* @__PURE__ */ new Set();
10396
10944
  const pendingById = new Map(pending.map((item) => [docIdOf(item), item]));
@@ -10402,8 +10950,9 @@ async function retryBatchOnPrimaryChange(deps, indexName, items, docIdOf, attemp
10402
10950
  }
10403
10951
  const item = pendingById.get(failure.docId);
10404
10952
  const refusing = primaryFor(table, failure.docId);
10405
- if (item === void 0 || refusing === null || !(refusedByPrimaryChange(failure.error, retriesEndedTerm) || forwardUndelivered(failure.error))) {
10406
- 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);
10407
10956
  continue;
10408
10957
  }
10409
10958
  refused.push({ item, failure, refusing });
@@ -10422,9 +10971,13 @@ async function retryBatchOnPrimaryChange(deps, indexName, items, docIdOf, attemp
10422
10971
  );
10423
10972
  }
10424
10973
  if (!retryable) {
10425
- failed2.push(...refused.map((entry) => entry.failure));
10974
+ for (const entry of refused) failWith(entry.failure);
10426
10975
  break;
10427
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
+ }
10428
10981
  pending = refused.map((entry) => entry.item);
10429
10982
  retried = true;
10430
10983
  }
@@ -10457,12 +11010,12 @@ async function settleEachBounded(inputs, limit, run) {
10457
11010
  await Promise.all(Array.from({ length: Math.min(limit, inputs.length) }, work));
10458
11011
  return outcomes;
10459
11012
  }
10460
- 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) {
10461
11014
  const deadline = Date.now() + PRIMARY_CHANGE_RETRY_WINDOW_MS;
10462
11015
  const outcomes = await settleEachBounded(
10463
11016
  groupByPartition(table, items, docIdOf),
10464
11017
  PARTITION_WRITE_CONCURRENCY,
10465
- (group) => retryBatchOnPrimaryChange(deps, indexName, group, docIdOf, attempt, settleRetried, deadline, retriesEndedTerm)
11018
+ (group) => retryBatchOnPrimaryChange(deps, indexName, group, docIdOf, attempt, settleRetried, deadline, settlesAsEqual)
10466
11019
  );
10467
11020
  const merged = { succeeded: [], failed: [] };
10468
11021
  for (const outcome of outcomes) {
@@ -10473,7 +11026,6 @@ async function retryEachPartition(deps, indexName, table, items, docIdOf, attemp
10473
11026
  return merged;
10474
11027
  }
10475
11028
  var settleNothing = async () => /* @__PURE__ */ new Set();
10476
- var settleAbsentRemovals = async (failures) => new Set(failures.filter((failure) => hasCode(failure.error, ErrorCodes.DOC_NOT_FOUND)).map((failure) => failure.docId));
10477
11029
  async function derivedFieldsOf(deps, indexName) {
10478
11030
  const schema = await deps.coordinator.getSchema(indexName);
10479
11031
  if (schema === null) return /* @__PURE__ */ new Set();
@@ -10561,78 +11113,20 @@ async function replicateAppendedWrites(scope, appended, failed2, compensate, dep
10561
11113
  for (let index = abandoned.length - 1; index >= 0; index--) {
10562
11114
  const item = abandoned[index];
10563
11115
  try {
10564
- await compensate(item, abortError);
10565
- compensated.push(item);
11116
+ const compensation = await compensate(item, abortError);
11117
+ compensated.push({ item, compensation: compensation ?? null });
10566
11118
  } catch (err) {
10567
11119
  failed2.push({ docId: item.docId, error: asWriteError(err, ErrorCodes.DOC_VALIDATION_FAILED, item.docId) });
10568
11120
  }
10569
11121
  }
10570
11122
  const answer = await settleRollback(scope, first.entry.seqNo, abortError, failedAt);
10571
- for (const item of compensated) {
10572
- 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) });
10573
11126
  }
10574
11127
  return succeeded2;
10575
11128
  }
10576
11129
 
10577
- // src/distribution/cluster-node/write-routing/numbered-write.ts
10578
- async function numberedWrite(scope, write, appendOutsideTheLog, deferCommit = false) {
10579
- const writes = scope.deps.numbering?.open(
10580
- scope.indexName,
10581
- scope.partitionId,
10582
- () => scope.deps.getReplicationLog(scope.indexName, scope.partitionId),
10583
- scope.assignment.primaryTerm,
10584
- deferCommit && scope.deps.commitPartitionLog !== void 0
10585
- );
10586
- let value;
10587
- try {
10588
- value = await write();
10589
- } finally {
10590
- writes?.close();
10591
- }
10592
- const recorded = writes?.entries.at(-1);
10593
- if (recorded !== void 0) return { value, entry: recorded };
10594
- const admitted = (documentId) => writes?.admittedDocument(documentId);
10595
- return { value, entry: await appendOutsideTheLog(value, admitted) };
10596
- }
10597
- async function storedCopyOf(scope, documentId) {
10598
- const stored = await scope.deps.engine.get(scope.indexName, documentId);
10599
- if (stored === void 0) {
10600
- throw new NarsilError(
10601
- ErrorCodes.REPLICATION_ENTRY_INVALID,
10602
- `Document '${documentId}' could not be read back for replication`,
10603
- { indexName: scope.indexName, documentId, partitionId: scope.partitionId }
10604
- );
10605
- }
10606
- return stored;
10607
- }
10608
- async function appendStoredCopy(scope, documentId, admitted) {
10609
- const admittedDocument = admitted(documentId);
10610
- if (admittedDocument instanceof Uint8Array) {
10611
- return scope.deps.getReplicationLog(scope.indexName, scope.partitionId).append({
10612
- primaryTerm: scope.assignment.primaryTerm,
10613
- operation: "INDEX",
10614
- partitionId: scope.partitionId,
10615
- indexName: scope.indexName,
10616
- documentId,
10617
- document: admittedDocument
10618
- });
10619
- }
10620
- const stored = await storedCopyOf(scope, documentId);
10621
- return appendIndexReplicationEntry(
10622
- scope.indexName,
10623
- scope.partitionId,
10624
- scope.assignment,
10625
- documentId,
10626
- stored,
10627
- scope.deps
10628
- );
10629
- }
10630
- function appendDeletion(scope, documentId) {
10631
- return Promise.resolve(
10632
- appendDeleteReplicationEntry(scope.indexName, scope.partitionId, scope.assignment, documentId, scope.deps)
10633
- );
10634
- }
10635
-
10636
11130
  // src/distribution/cluster-node/write-routing/failures.ts
10637
11131
  function throwWriteFailure(error) {
10638
11132
  if (error instanceof Error) {
@@ -10656,15 +11150,17 @@ function createRollbackFailure(operation, scope, documentId, originalError, roll
10656
11150
  }
10657
11151
  async function compensateInsert(scope, documentId, originalError) {
10658
11152
  try {
10659
- await numberedWrite(
11153
+ const removed = await numberedWrite(
10660
11154
  scope,
10661
11155
  () => scope.deps.engine.remove(scope.indexName, documentId),
10662
11156
  () => appendDeletion(scope, documentId)
10663
11157
  );
11158
+ return removed.entry;
10664
11159
  } catch (rollbackError) {
10665
11160
  if (!(rollbackError instanceof NarsilError && rollbackError.code === ErrorCodes.DOC_NOT_FOUND)) {
10666
11161
  throw createRollbackFailure("insert", scope, documentId, originalError, rollbackError);
10667
11162
  }
11163
+ return void 0;
10668
11164
  }
10669
11165
  }
10670
11166
  async function compensateUpdate(scope, documentId, previousDocument, originalError) {
@@ -10678,11 +11174,12 @@ async function compensateUpdate(scope, documentId, previousDocument, originalErr
10678
11174
  );
10679
11175
  }
10680
11176
  try {
10681
- await numberedWrite(
11177
+ const restored = await numberedWrite(
10682
11178
  scope,
10683
11179
  () => scope.deps.engine.update(scope.indexName, documentId, previousDocument),
10684
11180
  (_, admitted) => appendStoredCopy(scope, documentId, admitted)
10685
11181
  );
11182
+ return restored.entry;
10686
11183
  } catch (rollbackError) {
10687
11184
  throw createRollbackFailure("update", scope, documentId, originalError, rollbackError);
10688
11185
  }
@@ -10698,11 +11195,12 @@ async function compensateRemove(scope, documentId, previousDocument, originalErr
10698
11195
  );
10699
11196
  }
10700
11197
  try {
10701
- await numberedWrite(
11198
+ const restored = await numberedWrite(
10702
11199
  scope,
10703
11200
  () => scope.deps.engine.insert(scope.indexName, previousDocument, documentId),
10704
11201
  (_, admitted) => appendStoredCopy(scope, documentId, admitted)
10705
11202
  );
11203
+ return restored.entry;
10706
11204
  } catch (rollbackError) {
10707
11205
  throw createRollbackFailure("remove", scope, documentId, originalError, rollbackError);
10708
11206
  }
@@ -10797,6 +11295,141 @@ async function assertReshardOpen(state, coordinator, indexName, partitionId, pri
10797
11295
  }
10798
11296
  if (state.blocks.has(key)) throw rebalancingBackpressure(indexName, partitionId);
10799
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
+ }
10800
11433
 
10801
11434
  // src/distribution/cluster-node/write-routing/leading-queue.ts
10802
11435
  var REFUSALS_FROM_LOCAL_COPY = /* @__PURE__ */ new Set([ErrorCodes.DOC_NOT_FOUND, ErrorCodes.DOC_ALREADY_EXISTS]);
@@ -10895,6 +11528,7 @@ async function inLeadingPartitionQueue(scope, task, failed2) {
10895
11528
  scope.partitionId,
10896
11529
  async (hold) => {
10897
11530
  current = { ...await currentScope(scope), holdQueue: hold };
11531
+ if (await startTermInQueue(current)) current = { ...await currentScope(scope), holdQueue: hold };
10898
11532
  logEndBefore = logEndOf(current);
10899
11533
  return task(current);
10900
11534
  }
@@ -10919,6 +11553,10 @@ async function inLeadingPartitionQueue(scope, task, failed2) {
10919
11553
  if (failed2 !== void 0) await confirmBatchRefusals(current, failed2);
10920
11554
  return result;
10921
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
+ }
10922
11560
 
10923
11561
  // src/distribution/cluster-node/memory/document-bound.ts
10924
11562
  var SINGLE_CLASS_REPEATED = /^\[(?:[^\\\]]|\\.)*\]\+$/;
@@ -11189,6 +11827,83 @@ async function applyInProjectedGroups(deps, indexName, batch) {
11189
11827
  }
11190
11828
  }
11191
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
+
11192
11907
  // src/distribution/cluster-node/write-routing/primary-writes.ts
11193
11908
  async function applyPrimaryInsert(indexName, document, docId, partitionId, assignment, deps, options) {
11194
11909
  assertSufficientActiveReplicas(indexName, partitionId, assignment, deps);
@@ -11214,11 +11929,13 @@ async function applyPrimaryInsert(indexName, document, docId, partitionId, assig
11214
11929
  }
11215
11930
  });
11216
11931
  }
11217
- async function applyPrimaryRemove(indexName, docId, partitionId, assignment, deps) {
11932
+ async function applyPrimaryRemove(indexName, docId, partitionId, assignment, deps, condition) {
11218
11933
  assertSufficientActiveReplicas(indexName, partitionId, assignment, deps);
11219
11934
  const queued = { indexName, partitionId, assignment, deps };
11220
11935
  await inLeadingPartitionQueue(queued, async (scope) => {
11936
+ if (condition !== void 0 && decideCondition(scope, docId, condition) === "acknowledge") return;
11221
11937
  const previousDocument = await deps.engine.get(indexName, docId);
11938
+ const previousRecord = previousRecordOf(scope, docId);
11222
11939
  const removed = await numberedWrite(
11223
11940
  scope,
11224
11941
  () => deps.engine.remove(indexName, docId),
@@ -11228,18 +11945,21 @@ async function applyPrimaryRemove(indexName, docId, partitionId, assignment, dep
11228
11945
  await replicateEntry(removed.entry, scope.assignment, deps, scope.indexUuid);
11229
11946
  } catch (error) {
11230
11947
  const failedAt = Date.now();
11231
- await compensateRemove(scope, docId, previousDocument, error);
11232
- 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));
11233
11951
  }
11234
11952
  });
11235
11953
  }
11236
- async function applyPrimaryUpdate(indexName, docId, document, partitionId, assignment, deps) {
11954
+ async function applyPrimaryUpdate(indexName, docId, document, partitionId, assignment, deps, condition) {
11237
11955
  assertSufficientActiveReplicas(indexName, partitionId, assignment, deps);
11238
11956
  const queued = { indexName, partitionId, assignment, deps };
11239
11957
  await inLeadingPartitionQueue(queued, async (scope) => {
11958
+ if (condition !== void 0 && decideCondition(scope, docId, condition) === "acknowledge") return;
11240
11959
  const projected = projectOne(deps, indexName, document);
11241
11960
  try {
11242
11961
  const previousDocument = await deps.engine.get(indexName, docId);
11962
+ const previousRecord = previousRecordOf(scope, docId);
11243
11963
  const updated = await numberedWrite(
11244
11964
  scope,
11245
11965
  () => deps.engine.update(indexName, docId, document),
@@ -11249,8 +11969,9 @@ async function applyPrimaryUpdate(indexName, docId, document, partitionId, assig
11249
11969
  await replicateEntry(updated.entry, scope.assignment, deps, scope.indexUuid);
11250
11970
  } catch (error) {
11251
11971
  const failedAt = Date.now();
11252
- await compensateUpdate(scope, docId, previousDocument, error);
11253
- 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));
11254
11975
  }
11255
11976
  } finally {
11256
11977
  projected.release();
@@ -11307,6 +12028,7 @@ async function applyPrimaryUpdateBatch(indexName, updates, partitionId, assignme
11307
12028
  queued,
11308
12029
  async (scope) => {
11309
12030
  const appended = [];
12031
+ const acknowledged = [];
11310
12032
  const groupCommit = createGroupCommit(scope, appended, failed2);
11311
12033
  await applyInProjectedGroups(deps, indexName, {
11312
12034
  items: updates,
@@ -11317,14 +12039,19 @@ async function applyPrimaryUpdateBatch(indexName, updates, partitionId, assignme
11317
12039
  apply: async (update) => {
11318
12040
  if (groupCommit.refuse(update.docId)) return;
11319
12041
  try {
12042
+ if (update.condition !== void 0 && decideCondition(scope, update.docId, update.condition) === "acknowledge") {
12043
+ acknowledged.push(update.docId);
12044
+ return;
12045
+ }
11320
12046
  const previousDocument = await deps.engine.get(indexName, update.docId);
12047
+ const previousRecord = previousRecordOf(scope, update.docId);
11321
12048
  const updated = await numberedWrite(
11322
12049
  scope,
11323
12050
  () => deps.engine.update(indexName, update.docId, update.document),
11324
12051
  (_, admitted) => appendStoredCopy(scope, update.docId, admitted),
11325
12052
  true
11326
12053
  );
11327
- appended.push({ docId: update.docId, entry: updated.entry, previousDocument });
12054
+ appended.push({ docId: update.docId, entry: updated.entry, previousDocument, previousRecord });
11328
12055
  } catch (err) {
11329
12056
  failed2.push({
11330
12057
  docId: update.docId,
@@ -11333,19 +12060,20 @@ async function applyPrimaryUpdateBatch(indexName, updates, partitionId, assignme
11333
12060
  }
11334
12061
  }
11335
12062
  });
11336
- return replicateAppendedWrites(
12063
+ const replicated = await replicateAppendedWrites(
11337
12064
  scope,
11338
12065
  appended,
11339
12066
  failed2,
11340
12067
  (write, error) => compensateUpdate(scope, write.docId, write.previousDocument, error),
11341
12068
  deps
11342
12069
  );
12070
+ return [...acknowledged, ...replicated];
11343
12071
  },
11344
12072
  failed2
11345
12073
  );
11346
12074
  return { succeeded: succeeded2, failed: failed2 };
11347
12075
  }
11348
- async function applyPrimaryRemoveBatch(indexName, docIds, partitionId, assignment, deps) {
12076
+ async function applyPrimaryRemoveBatch(indexName, docIds, partitionId, assignment, deps, conditions) {
11349
12077
  assertSufficientActiveReplicas(indexName, partitionId, assignment, deps);
11350
12078
  const queued = { indexName, partitionId, assignment, deps };
11351
12079
  const failed2 = [];
@@ -11353,29 +12081,37 @@ async function applyPrimaryRemoveBatch(indexName, docIds, partitionId, assignmen
11353
12081
  queued,
11354
12082
  async (scope) => {
11355
12083
  const appended = [];
12084
+ const acknowledged = [];
11356
12085
  const groupCommit = createGroupCommit(scope, appended, failed2);
11357
12086
  for (const docId of docIds) {
11358
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
+ }
11359
12093
  const previousDocument = await deps.engine.get(indexName, docId);
12094
+ const previousRecord = previousRecordOf(scope, docId);
11360
12095
  const removed = await numberedWrite(
11361
12096
  scope,
11362
12097
  () => deps.engine.remove(indexName, docId),
11363
12098
  () => appendDeletion(scope, docId),
11364
12099
  true
11365
12100
  );
11366
- appended.push({ docId, entry: removed.entry, previousDocument });
12101
+ appended.push({ docId, entry: removed.entry, previousDocument, previousRecord });
11367
12102
  } catch (err) {
11368
12103
  failed2.push({ docId, error: asWriteError(err, ErrorCodes.QUERY_ROUTING_FAILED, docId) });
11369
12104
  }
11370
12105
  }
11371
12106
  await groupCommit.afterGroup();
11372
- return replicateAppendedWrites(
12107
+ const replicated = await replicateAppendedWrites(
11373
12108
  scope,
11374
12109
  appended,
11375
12110
  failed2,
11376
12111
  (write, error) => compensateRemove(scope, write.docId, write.previousDocument, error),
11377
12112
  deps
11378
12113
  );
12114
+ return [...acknowledged, ...replicated];
11379
12115
  },
11380
12116
  failed2
11381
12117
  );
@@ -11393,6 +12129,7 @@ function applyForwardedWrite(payload, deps) {
11393
12129
  );
11394
12130
  }
11395
12131
  async function applyAdmittedForwardedWrite(payload, deps) {
12132
+ const condition = conditionOfFields(payload, payload.operation, payload.documentId);
11396
12133
  const resolution = await resolvePrimaryAssignment(payload.indexName, payload.documentId, deps, true);
11397
12134
  if (resolution === null) {
11398
12135
  throw new NarsilError(
@@ -11419,7 +12156,14 @@ async function applyAdmittedForwardedWrite(payload, deps) {
11419
12156
  );
11420
12157
  }
11421
12158
  if (payload.operation === "remove") {
11422
- 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
+ );
11423
12167
  return payload.documentId;
11424
12168
  }
11425
12169
  const replacement = await resolveForwardedUpdateDocument(
@@ -11435,7 +12179,8 @@ async function applyAdmittedForwardedWrite(payload, deps) {
11435
12179
  replacement,
11436
12180
  resolution.partitionId,
11437
12181
  resolution.assignment,
11438
- deps
12182
+ deps,
12183
+ condition
11439
12184
  );
11440
12185
  return payload.documentId;
11441
12186
  }
@@ -11465,7 +12210,8 @@ function toWireOperation(item) {
11465
12210
  documentId: item.documentId,
11466
12211
  operation: item.operation,
11467
12212
  document: item.document === null ? null : encodeDocument(item.document),
11468
- updateFields: null
12213
+ updateFields: null,
12214
+ ...conditionFields(item.condition)
11469
12215
  };
11470
12216
  }
11471
12217
  function chunkWireOperations(operations) {
@@ -11489,10 +12235,18 @@ async function forwardSingleItem(indexName, item, primaryNodeId, deps, indexUuid
11489
12235
  documentId: item.documentId
11490
12236
  });
11491
12237
  }
11492
- 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
+ );
11493
12247
  return item.documentId;
11494
12248
  }
11495
- await forwardRemoveToRemote(indexName, item.documentId, primaryNodeId, deps, indexUuid);
12249
+ await forwardRemoveToRemote(indexName, item.documentId, primaryNodeId, deps, indexUuid, item.condition);
11496
12250
  return item.documentId;
11497
12251
  }
11498
12252
  async function forwardBatchToRemote(indexName, items, primaryNodeId, deps, indexUuid) {
@@ -11548,17 +12302,19 @@ async function forwardBatchToRemote(indexName, items, primaryNodeId, deps, index
11548
12302
  succeeded2.push(operationResult.documentId);
11549
12303
  continue;
11550
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);
11551
12311
  failed2.push({
11552
12312
  docId: operationResult.documentId,
11553
- error: new NarsilError(
11554
- operationResult.errorCode ?? ErrorCodes.QUERY_ROUTING_FAILED,
11555
- operationResult.errorMessage ?? `Remote primary rejected forwarded operation '${operationResult.documentId}'`,
11556
- { ...operationResult.errorDetails ?? {}, indexName, primaryNodeId }
11557
- )
12313
+ error: abandonedWrite === void 0 ? refusal : withAbandonedWrite(refusal, abandonedWrite)
11558
12314
  });
11559
12315
  }
11560
12316
  } catch (err) {
11561
- if (forwardUndelivered(err)) undelivered = err;
12317
+ if (err instanceof NarsilError && (forwardUnsent(err) || forwardInterrupted(err))) undelivered = unsentAfter(err);
11562
12318
  for (const operation of chunk) {
11563
12319
  failed2.push({ docId: operation.documentId, error: toForwardError(err, operation.documentId) });
11564
12320
  }
@@ -11625,7 +12381,7 @@ function routeInsertBatch(indexName, documents, deps, options) {
11625
12381
  async function routeAdmittedInsertBatch(indexName, documents, deps, options) {
11626
12382
  const table = await routableAllocation(deps.coordinator, indexName);
11627
12383
  if (table === null) {
11628
- return deps.engine.insertBatch(indexName, documents, options);
12384
+ return writeLocalOnly(deps, indexName, () => deps.engine.insertBatch(indexName, documents, options));
11629
12385
  }
11630
12386
  requireWritableIndex(table, deps.memoryView);
11631
12387
  const withIds = documents.map((doc) => ({
@@ -11690,7 +12446,7 @@ async function routeInsertItems(table, indexName, withIds, deps, options) {
11690
12446
  async function routeRemoveBatch(indexName, docIds, deps) {
11691
12447
  const table = await routableAllocation(deps.coordinator, indexName);
11692
12448
  if (table === null) {
11693
- return deps.engine.removeBatch(indexName, docIds);
12449
+ return writeLocalOnly(deps, indexName, () => deps.engine.removeBatch(indexName, docIds));
11694
12450
  }
11695
12451
  return retryEachPartition(
11696
12452
  deps,
@@ -11698,17 +12454,24 @@ async function routeRemoveBatch(indexName, docIds, deps) {
11698
12454
  table,
11699
12455
  docIds,
11700
12456
  (docId) => docId,
11701
- async (pending) => routeRemoveItems(await requireRoutableTable(deps, indexName), indexName, pending, deps),
11702
- settleAbsentRemovals
12457
+ async (pending, conditions) => routeRemoveItems(await requireRoutableTable(deps, indexName), indexName, pending, deps, conditions),
12458
+ settleNothing
11703
12459
  );
11704
12460
  }
11705
- async function routeRemoveItems(table, indexName, docIds, deps) {
12461
+ async function routeRemoveItems(table, indexName, docIds, deps, conditions) {
11706
12462
  const routed = routeBatchItems(table, indexName, docIds, (docId) => docId, deps.nodeId);
11707
12463
  const succeeded2 = [];
11708
12464
  const failed2 = routed.failed;
11709
12465
  for (const [partitionId, group] of routed.localGroups) {
11710
12466
  try {
11711
- 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
+ );
11712
12475
  succeeded2.push(...result.succeeded);
11713
12476
  failed2.push(...result.failed);
11714
12477
  } catch (err) {
@@ -11718,7 +12481,14 @@ async function routeRemoveItems(table, indexName, docIds, deps) {
11718
12481
  for (const [primaryNodeId, items] of routed.remoteGroups) {
11719
12482
  const result = await forwardBatchToRemote(
11720
12483
  indexName,
11721
- 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
+ ),
11722
12492
  primaryNodeId,
11723
12493
  deps,
11724
12494
  table.indexUuid
@@ -11739,7 +12509,7 @@ function routeUpdateBatch(indexName, updates, deps) {
11739
12509
  async function routeAdmittedUpdateBatch(indexName, updates, deps) {
11740
12510
  const table = await routableAllocation(deps.coordinator, indexName);
11741
12511
  if (table === null) {
11742
- return deps.engine.updateBatch(indexName, updates);
12512
+ return writeLocalOnly(deps, indexName, () => deps.engine.updateBatch(indexName, updates));
11743
12513
  }
11744
12514
  requireWritableIndex(table, deps.memoryView);
11745
12515
  return retryEachPartition(
@@ -11748,17 +12518,23 @@ async function routeAdmittedUpdateBatch(indexName, updates, deps) {
11748
12518
  table,
11749
12519
  updates,
11750
12520
  (update) => update.docId,
11751
- async (pending) => routeUpdateItems(await requireRoutableTable(deps, indexName), indexName, pending, deps),
12521
+ async (pending, conditions) => routeUpdateItems(await requireRoutableTable(deps, indexName), indexName, pending, deps, conditions),
11752
12522
  settleNothing
11753
12523
  );
11754
12524
  }
11755
- async function routeUpdateItems(table, indexName, updates, deps) {
12525
+ async function routeUpdateItems(table, indexName, updates, deps, conditions) {
11756
12526
  const routed = routeBatchItems(table, indexName, updates, (update) => update.docId, deps.nodeId);
11757
12527
  const succeeded2 = [];
11758
12528
  const failed2 = routed.failed;
11759
12529
  for (const [partitionId, group] of routed.localGroups) {
11760
12530
  try {
11761
- 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
+ );
11762
12538
  succeeded2.push(...result.succeeded);
11763
12539
  failed2.push(...result.failed);
11764
12540
  } catch (err) {
@@ -11775,7 +12551,12 @@ async function routeUpdateItems(table, indexName, updates, deps) {
11775
12551
  const result = await forwardBatchToRemote(
11776
12552
  indexName,
11777
12553
  items.map(
11778
- (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
+ })
11779
12560
  ),
11780
12561
  primaryNodeId,
11781
12562
  deps,
@@ -11905,17 +12686,20 @@ async function applyRun(indexName, partitionId, run, assignment, deps, results)
11905
12686
  }
11906
12687
  async function applyRunToPrimary(indexName, partitionId, run, assignment, deps, results) {
11907
12688
  if (run.operation === "remove") {
11908
- return applyPrimaryRemoveBatch(
11909
- indexName,
11910
- run.operations.map((operation) => operation.wire.documentId),
11911
- partitionId,
11912
- assignment,
11913
- deps
11914
- );
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);
11915
12698
  }
11916
12699
  if (run.operation === "insert") {
11917
12700
  const items = [];
11918
12701
  for (const operation of run.operations) {
12702
+ if (conditionOrFailure(operation.wire, results, operation.index) === null) continue;
11919
12703
  const decodedDocument = decodeOperationDocument(operation.wire, results, operation.index);
11920
12704
  if (decodedDocument !== null) {
11921
12705
  items.push({ doc: decodedDocument, docId: operation.wire.documentId });
@@ -11925,6 +12709,8 @@ async function applyRunToPrimary(indexName, partitionId, run, assignment, deps,
11925
12709
  }
11926
12710
  const updates = [];
11927
12711
  for (const operation of run.operations) {
12712
+ const condition = conditionOrFailure(operation.wire, results, operation.index);
12713
+ if (condition === null) continue;
11928
12714
  try {
11929
12715
  const replacement = await resolveForwardedUpdateDocument(
11930
12716
  indexName,
@@ -11933,7 +12719,9 @@ async function applyRunToPrimary(indexName, partitionId, run, assignment, deps,
11933
12719
  operation.wire.updateFields,
11934
12720
  deps
11935
12721
  );
11936
- 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
+ );
11937
12725
  } catch (err) {
11938
12726
  const error = err instanceof NarsilError ? err : new NarsilError(ErrorCodes.CONFIG_INVALID, String(err));
11939
12727
  results[operation.index] = failureResult(operation.wire.documentId, error);
@@ -11941,6 +12729,15 @@ async function applyRunToPrimary(indexName, partitionId, run, assignment, deps,
11941
12729
  }
11942
12730
  return applyPrimaryUpdateBatch(indexName, updates, partitionId, assignment, deps);
11943
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
+ }
11944
12741
  function decodeOperationDocument(wire, results, index) {
11945
12742
  if (wire.document === null) {
11946
12743
  results[index] = failureResult(
@@ -11964,13 +12761,16 @@ function decodeOperationDocument(wire, results, index) {
11964
12761
  }
11965
12762
  }
11966
12763
  function failureResult(documentId, error) {
11967
- return {
12764
+ const result = {
11968
12765
  documentId,
11969
12766
  success: false,
11970
12767
  errorCode: error.code,
11971
12768
  errorMessage: error.message,
11972
12769
  errorDetails: transportableDetails(error) ?? null
11973
12770
  };
12771
+ const abandonedWrite = abandonedWriteOf(error);
12772
+ if (abandonedWrite !== void 0) result.abandonedWrite = abandonedWrite;
12773
+ return result;
11974
12774
  }
11975
12775
 
11976
12776
  // src/distribution/cluster-node/write-routing/index.ts
@@ -12143,7 +12943,7 @@ async function routeAdmittedInsert(indexName, document, docId, deps, options) {
12143
12943
  async function insertOnce(indexName, document, resolvedDocId, deps, options) {
12144
12944
  const resolution = await resolvePrimaryAssignment(indexName, resolvedDocId, deps, false);
12145
12945
  if (resolution === null) {
12146
- return deps.engine.insert(indexName, document, resolvedDocId, options);
12946
+ return writeLocalOnly(deps, indexName, () => deps.engine.insert(indexName, document, resolvedDocId, options));
12147
12947
  }
12148
12948
  requireWritableIndex(resolution.table, deps.memoryView);
12149
12949
  const primaryNodeId = resolution.assignment.primary;
@@ -12165,21 +12965,21 @@ function routeRemove(indexName, docId, deps) {
12165
12965
  deps,
12166
12966
  indexName,
12167
12967
  docId,
12168
- () => removeOnce(indexName, docId, deps),
12169
- settleAbsentRemovals,
12968
+ (condition) => removeOnce(indexName, docId, deps, condition),
12969
+ settleNothing,
12170
12970
  () => void 0
12171
12971
  );
12172
12972
  }
12173
- async function removeOnce(indexName, docId, deps) {
12973
+ async function removeOnce(indexName, docId, deps, condition) {
12174
12974
  const resolution = await resolvePrimaryAssignment(indexName, docId, deps, false);
12175
12975
  if (resolution === null) {
12176
- return deps.engine.remove(indexName, docId);
12976
+ return writeLocalOnly(deps, indexName, () => deps.engine.remove(indexName, docId));
12177
12977
  }
12178
12978
  const primaryNodeId = resolution.assignment.primary;
12179
12979
  if (primaryNodeId === deps.nodeId) {
12180
- return applyPrimaryRemove(indexName, docId, resolution.partitionId, resolution.assignment, deps);
12980
+ return applyPrimaryRemove(indexName, docId, resolution.partitionId, resolution.assignment, deps, condition);
12181
12981
  }
12182
- return forwardRemoveToRemote(indexName, docId, primaryNodeId, deps, resolution.table.indexUuid);
12982
+ return forwardRemoveToRemote(indexName, docId, primaryNodeId, deps, resolution.table.indexUuid, condition);
12183
12983
  }
12184
12984
  function routeUpdate(indexName, docId, document, deps) {
12185
12985
  return withAdmittedWrite(
@@ -12190,23 +12990,31 @@ function routeUpdate(indexName, docId, document, deps) {
12190
12990
  deps,
12191
12991
  indexName,
12192
12992
  docId,
12193
- () => routeAdmittedUpdate(indexName, docId, document, deps),
12993
+ (condition) => routeAdmittedUpdate(indexName, docId, document, deps, condition),
12194
12994
  settleNothing,
12195
12995
  () => void 0
12196
12996
  )
12197
12997
  );
12198
12998
  }
12199
- async function routeAdmittedUpdate(indexName, docId, document, deps) {
12999
+ async function routeAdmittedUpdate(indexName, docId, document, deps, condition) {
12200
13000
  const resolution = await resolvePrimaryAssignment(indexName, docId, deps, false);
12201
13001
  if (resolution === null) {
12202
- return deps.engine.update(indexName, docId, document);
13002
+ return writeLocalOnly(deps, indexName, () => deps.engine.update(indexName, docId, document));
12203
13003
  }
12204
13004
  requireWritableIndex(resolution.table, deps.memoryView);
12205
13005
  const primaryNodeId = resolution.assignment.primary;
12206
13006
  if (primaryNodeId === deps.nodeId) {
12207
- 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
+ );
12208
13016
  }
12209
- return forwardUpdateToRemote(indexName, document, docId, primaryNodeId, deps, resolution.table.indexUuid);
13017
+ return forwardUpdateToRemote(indexName, document, docId, primaryNodeId, deps, resolution.table.indexUuid, condition);
12210
13018
  }
12211
13019
 
12212
13020
  // src/distribution/cluster-node/node-messaging.ts
@@ -14390,8 +15198,6 @@ async function resolveValidatedAssignment(entry, sourceNodeId, deps) {
14390
15198
  if (live instanceof NarsilError) throw live;
14391
15199
  return live;
14392
15200
  }
14393
-
14394
- // src/distribution/cluster-node/message-handler/writes.ts
14395
15201
  async function handleForward(message, respond, deps) {
14396
15202
  const decoded = decode(message.payload);
14397
15203
  const payload = validateForwardPayload(decoded);
@@ -14429,14 +15235,14 @@ async function handleReplicationEntry(message, respond, deps) {
14429
15235
  const payload = validateEntryPayload(decode(message.payload));
14430
15236
  const { entry } = payload;
14431
15237
  refuseAnotherIndex(entry.indexName, payload.indexUuid, deps);
14432
- await applyEntriesFromPrimary([entry], message.sourceId, deps);
15238
+ await applyEntriesFromPrimary([entry], message.sourceId, deps, payload.previousPrimaryTerm);
14433
15239
  await respond(createAckMessage(entry.seqNo, entry.partitionId, entry.indexName, deps.nodeId, message.requestId));
14434
15240
  }
14435
15241
  async function handleReplicationEntryBatch(message, respond, deps) {
14436
15242
  const payload = validateEntryBatchPayload(decode(message.payload));
14437
15243
  const lastEntry = payload.entries[payload.entries.length - 1];
14438
15244
  refuseAnotherIndex(lastEntry.indexName, payload.indexUuid, deps);
14439
- await applyEntriesFromPrimary(payload.entries, message.sourceId, deps);
15245
+ await applyEntriesFromPrimary(payload.entries, message.sourceId, deps, payload.previousPrimaryTerm);
14440
15246
  await respond(
14441
15247
  createAckMessage(lastEntry.seqNo, lastEntry.partitionId, lastEntry.indexName, deps.nodeId, message.requestId)
14442
15248
  );
@@ -14456,7 +15262,7 @@ function refuseAnotherIndex(indexName, indexUuid, deps) {
14456
15262
  { indexName, localNodeId: deps.nodeId }
14457
15263
  );
14458
15264
  }
14459
- async function applyEntriesFromPrimary(entries, sourceNodeId, deps) {
15265
+ async function applyEntriesFromPrimary(entries, sourceNodeId, deps, previousPrimaryTerm) {
14460
15266
  const first = entries[0];
14461
15267
  if (first === void 0) return;
14462
15268
  const assignment = await resolveValidatedAssignment(first, sourceNodeId, deps);
@@ -14470,15 +15276,18 @@ async function applyEntriesFromPrimary(entries, sourceNodeId, deps) {
14470
15276
  await enqueuePartitionWrite(deps.writeDeps.partitionWriteQueues, first.indexName, first.partitionId, async () => {
14471
15277
  const current = await resolveValidatedAssignment(first, sourceNodeId, deps);
14472
15278
  noteAcceptedPrimaryTerm(deps.writeDeps.getReplicationLog(first.indexName, first.partitionId), current.primaryTerm);
14473
- await applyEntriesInLogOrder(first, entries, deps);
15279
+ await applyEntriesInLogOrder(first, entries, deps, previousPrimaryTerm);
14474
15280
  });
14475
15281
  }
14476
- async function applyEntriesInLogOrder(first, entries, deps) {
15282
+ async function applyEntriesInLogOrder(first, entries, deps, previousPrimaryTerm) {
14477
15283
  let log = deps.writeDeps.getReplicationLog(first.indexName, first.partitionId);
14478
15284
  let tailSeqNo = log.localLogEnd;
14479
15285
  let tailPrimaryTerm = log.localLogEndPrimaryTerm;
15286
+ let expectedPreviousTerm = previousPrimaryTerm;
14480
15287
  const run = [];
14481
15288
  for (const entry of entries) {
15289
+ const expectedTermBefore = expectedPreviousTerm;
15290
+ expectedPreviousTerm = entry.primaryTerm;
14482
15291
  const existing = log.getEntry(entry.seqNo);
14483
15292
  if (existing !== void 0) {
14484
15293
  if (existing.checksum !== entry.checksum) {
@@ -14491,6 +15300,21 @@ async function applyEntriesInLogOrder(first, entries, deps) {
14491
15300
  }
14492
15301
  continue;
14493
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
+ }
14494
15318
  const expectedSeqNo = tailSeqNo + 1;
14495
15319
  if (entry.seqNo !== expectedSeqNo) {
14496
15320
  const canSeedFromCompletedBootstrap = run.length === 0 && log.entryCount === 0 && entry.seqNo > expectedSeqNo && deps.isBootstrapSynced?.(entry.indexName, entry.partitionId) === true;
@@ -14551,6 +15375,28 @@ function validateForwardPayload(decoded) {
14551
15375
  return decoded;
14552
15376
  }
14553
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
+
14554
15400
  // src/distribution/cluster-node/message-handler/index.ts
14555
15401
  function createDataNodeHandler(deps) {
14556
15402
  return async (message, respond) => {
@@ -14571,6 +15417,9 @@ function createDataNodeHandler(deps) {
14571
15417
  case ReplicationMessageTypes.ENTRY_BATCH:
14572
15418
  await handleReplicationEntryBatch(message, respond, deps);
14573
15419
  return;
15420
+ case ReplicationMessageTypes.TERM_START:
15421
+ await handleTermStart(message, respond, deps);
15422
+ return;
14574
15423
  case QueryMessageTypes.SEARCH:
14575
15424
  await handleSearch(message, respond, deps);
14576
15425
  return;
@@ -14617,7 +15466,7 @@ function createDataNodeHandler(deps) {
14617
15466
  return;
14618
15467
  }
14619
15468
  } catch (err) {
14620
- const errorPayload = encodeErrorReplyFrom(err, "INTERNAL_ERROR");
15469
+ const errorPayload = encodeErrorReplyFrom(err, "INTERNAL_ERROR", abandonedWriteOf(err));
14621
15470
  await respond({
14622
15471
  type: `${message.type}.error`,
14623
15472
  sourceId: deps.nodeId,
@@ -16268,7 +17117,7 @@ async function queryCluster(deps, indexName, params) {
16268
17117
  (reader) => searchCluster(deps, indexName, params, allocation, reader, false)
16269
17118
  );
16270
17119
  }
16271
- const { rerankClusterQuery } = await import('./rerank-EHJVXEDP.mjs');
17120
+ const { rerankClusterQuery } = await import('./rerank-3POMPQNX.mjs');
16272
17121
  return withRankedReader(
16273
17122
  deps,
16274
17123
  indexName,
@@ -16342,7 +17191,7 @@ async function searchCluster(deps, indexName, params, allocation, reader, readsL
16342
17191
  async function clearCluster(readDeps, writeDeps, indexName) {
16343
17192
  const allocation = await routableAllocation(readDeps.config.coordinator, indexName);
16344
17193
  if (allocation === null) {
16345
- return readDeps.engine.clear(indexName);
17194
+ return writeLocalOnly(writeDeps, indexName, () => readDeps.engine.clear(indexName));
16346
17195
  }
16347
17196
  for (; ; ) {
16348
17197
  const page = await listCluster(readDeps, indexName, { limit: CLEAR_PAGE_SIZE, document: false });
@@ -16934,7 +17783,7 @@ function createNodeMemoryReporting(options) {
16934
17783
  }
16935
17784
 
16936
17785
  // src/distribution/cluster-node/write-routing/numbering.ts
16937
- function createPrimaryNumbering() {
17786
+ function createPrimaryNumbering(onNumbered) {
16938
17787
  const scopes = /* @__PURE__ */ new Map();
16939
17788
  return {
16940
17789
  admit(indexName, partitionId, documentId, document) {
@@ -16956,6 +17805,7 @@ function createPrimaryNumbering() {
16956
17805
  document: record.document
16957
17806
  });
16958
17807
  scope.entries.push(entry);
17808
+ onNumbered?.(entry);
16959
17809
  return entry;
16960
17810
  },
16961
17811
  defersCommit(record) {
@@ -17019,12 +17869,16 @@ function createNodeWiring(input) {
17019
17869
  config: config.memory,
17020
17870
  onFullChange: input.renewRegistration
17021
17871
  });
17022
- const numbering = createPrimaryNumbering();
17872
+ engine.writeRecords.readRetainedLogWith(
17873
+ (indexName, partitionId) => replicationLogs.get(replicationLogKey(indexName, partitionId))?.oldestSeqNo
17874
+ );
17875
+ const numbering = createPrimaryNumbering(engine.writeRecords.applied);
17023
17876
  engine.numberEntriesWith(numbering.numberEntry, numbering.defersCommit);
17024
17877
  engine.admitWritesWith(numbering.admit);
17025
17878
  const writeDeps = {
17026
17879
  nodeId,
17027
17880
  numbering,
17881
+ writeRecords: engine.writeRecords,
17028
17882
  commitPartitionLog: engine.commitPartitionLog,
17029
17883
  coordinator: allocationView.routing,
17030
17884
  refreshAllocation: allocationView.refresh,
@@ -17695,6 +18549,7 @@ async function createClusterNode(callerConfig) {
17695
18549
  allocationDebounceMs: DEFAULT_NODE_LIFECYCLE_CONFIG.allocationDebounceMs,
17696
18550
  nodeHeartbeatIntervalMs: DEFAULT_NODE_LIFECYCLE_CONFIG.nodeHeartbeatIntervalMs,
17697
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),
17698
18553
  onHoldPartition: (indexName, partitionId) => {
17699
18554
  trackHeldPartitionWrite(engine.recordHeldPartition(indexName, partitionId));
17700
18555
  },