@optimystic/db-p2p 0.21.0 → 0.24.0

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 (219) hide show
  1. package/dist/src/cluster/client.d.ts +10 -0
  2. package/dist/src/cluster/client.d.ts.map +1 -1
  3. package/dist/src/cluster/client.js +30 -1
  4. package/dist/src/cluster/client.js.map +1 -1
  5. package/dist/src/cluster/cluster-policy.d.ts +13 -2
  6. package/dist/src/cluster/cluster-policy.d.ts.map +1 -1
  7. package/dist/src/cluster/cluster-policy.js +51 -4
  8. package/dist/src/cluster/cluster-policy.js.map +1 -1
  9. package/dist/src/cluster/cluster-repo.d.ts +42 -17
  10. package/dist/src/cluster/cluster-repo.d.ts.map +1 -1
  11. package/dist/src/cluster/cluster-repo.js +229 -122
  12. package/dist/src/cluster/cluster-repo.js.map +1 -1
  13. package/dist/src/cluster/cluster-size-coupling.d.ts +28 -0
  14. package/dist/src/cluster/cluster-size-coupling.d.ts.map +1 -0
  15. package/dist/src/cluster/cluster-size-coupling.js +35 -0
  16. package/dist/src/cluster/cluster-size-coupling.js.map +1 -0
  17. package/dist/src/cluster/quorum-restore.d.ts +6 -0
  18. package/dist/src/cluster/quorum-restore.d.ts.map +1 -1
  19. package/dist/src/cluster/quorum-restore.js +1 -1
  20. package/dist/src/cluster/quorum-restore.js.map +1 -1
  21. package/dist/src/cluster/reconcile-block.d.ts.map +1 -1
  22. package/dist/src/cluster/reconcile-block.js +15 -3
  23. package/dist/src/cluster/reconcile-block.js.map +1 -1
  24. package/dist/src/cluster/service.d.ts +32 -1
  25. package/dist/src/cluster/service.d.ts.map +1 -1
  26. package/dist/src/cluster/service.js +43 -2
  27. package/dist/src/cluster/service.js.map +1 -1
  28. package/dist/src/cohort-topic/stream-util.d.ts +22 -6
  29. package/dist/src/cohort-topic/stream-util.d.ts.map +1 -1
  30. package/dist/src/cohort-topic/stream-util.js +56 -10
  31. package/dist/src/cohort-topic/stream-util.js.map +1 -1
  32. package/dist/src/dispute/dispute-service.d.ts.map +1 -1
  33. package/dist/src/dispute/dispute-service.js +9 -3
  34. package/dist/src/dispute/dispute-service.js.map +1 -1
  35. package/dist/src/index.d.ts +5 -0
  36. package/dist/src/index.d.ts.map +1 -1
  37. package/dist/src/index.js +5 -0
  38. package/dist/src/index.js.map +1 -1
  39. package/dist/src/libp2p-key-network.d.ts +134 -7
  40. package/dist/src/libp2p-key-network.d.ts.map +1 -1
  41. package/dist/src/libp2p-key-network.js +174 -37
  42. package/dist/src/libp2p-key-network.js.map +1 -1
  43. package/dist/src/libp2p-node-base.d.ts +3 -2
  44. package/dist/src/libp2p-node-base.d.ts.map +1 -1
  45. package/dist/src/libp2p-node-base.js +859 -778
  46. package/dist/src/libp2p-node-base.js.map +1 -1
  47. package/dist/src/libp2p-node-rn.d.ts +2 -2
  48. package/dist/src/libp2p-node-rn.d.ts.map +1 -1
  49. package/dist/src/libp2p-node-rn.js.map +1 -1
  50. package/dist/src/libp2p-node.d.ts +2 -2
  51. package/dist/src/libp2p-node.d.ts.map +1 -1
  52. package/dist/src/libp2p-node.js.map +1 -1
  53. package/dist/src/logger.d.ts +17 -1
  54. package/dist/src/logger.d.ts.map +1 -1
  55. package/dist/src/logger.js +19 -2
  56. package/dist/src/logger.js.map +1 -1
  57. package/dist/src/network/network-manager-service.d.ts +2 -0
  58. package/dist/src/network/network-manager-service.d.ts.map +1 -1
  59. package/dist/src/network/network-manager-service.js +4 -0
  60. package/dist/src/network/network-manager-service.js.map +1 -1
  61. package/dist/src/optimystic-node.d.ts +35 -0
  62. package/dist/src/optimystic-node.d.ts.map +1 -0
  63. package/dist/src/optimystic-node.js +2 -0
  64. package/dist/src/optimystic-node.js.map +1 -0
  65. package/dist/src/owned-block-seed.d.ts +6 -3
  66. package/dist/src/owned-block-seed.d.ts.map +1 -1
  67. package/dist/src/owned-block-seed.js +16 -3
  68. package/dist/src/owned-block-seed.js.map +1 -1
  69. package/dist/src/peer-address-book.d.ts +72 -0
  70. package/dist/src/peer-address-book.d.ts.map +1 -0
  71. package/dist/src/peer-address-book.js +123 -0
  72. package/dist/src/peer-address-book.js.map +1 -0
  73. package/dist/src/repo/client.d.ts.map +1 -1
  74. package/dist/src/repo/client.js +11 -2
  75. package/dist/src/repo/client.js.map +1 -1
  76. package/dist/src/repo/cluster-coordinator.d.ts +30 -0
  77. package/dist/src/repo/cluster-coordinator.d.ts.map +1 -1
  78. package/dist/src/repo/cluster-coordinator.js +95 -3
  79. package/dist/src/repo/cluster-coordinator.js.map +1 -1
  80. package/dist/src/repo/coordinator-repo.d.ts +78 -14
  81. package/dist/src/repo/coordinator-repo.d.ts.map +1 -1
  82. package/dist/src/repo/coordinator-repo.js +266 -81
  83. package/dist/src/repo/coordinator-repo.js.map +1 -1
  84. package/dist/src/rn.d.ts +5 -0
  85. package/dist/src/rn.d.ts.map +1 -1
  86. package/dist/src/rn.js +5 -0
  87. package/dist/src/rn.js.map +1 -1
  88. package/dist/src/storage/block-storage.d.ts.map +1 -1
  89. package/dist/src/storage/block-storage.js +57 -5
  90. package/dist/src/storage/block-storage.js.map +1 -1
  91. package/dist/src/storage/cached-raw-storage.d.ts +83 -0
  92. package/dist/src/storage/cached-raw-storage.d.ts.map +1 -0
  93. package/dist/src/storage/cached-raw-storage.js +152 -0
  94. package/dist/src/storage/cached-raw-storage.js.map +1 -0
  95. package/dist/src/storage/cached-store-driver.d.ts +186 -0
  96. package/dist/src/storage/cached-store-driver.d.ts.map +1 -0
  97. package/dist/src/storage/cached-store-driver.js +775 -0
  98. package/dist/src/storage/cached-store-driver.js.map +1 -0
  99. package/dist/src/storage/i-block-storage.d.ts +20 -1
  100. package/dist/src/storage/i-block-storage.d.ts.map +1 -1
  101. package/dist/src/storage/i-raw-storage.d.ts +12 -5
  102. package/dist/src/storage/i-raw-storage.d.ts.map +1 -1
  103. package/dist/src/storage/shared-cache-pool.d.ts +234 -0
  104. package/dist/src/storage/shared-cache-pool.d.ts.map +1 -0
  105. package/dist/src/storage/shared-cache-pool.js +354 -0
  106. package/dist/src/storage/shared-cache-pool.js.map +1 -0
  107. package/dist/src/storage/storage-repo.d.ts +56 -3
  108. package/dist/src/storage/storage-repo.d.ts.map +1 -1
  109. package/dist/src/storage/storage-repo.js +124 -18
  110. package/dist/src/storage/storage-repo.js.map +1 -1
  111. package/dist/src/testing/raw-storage-conformance.d.ts +2 -1
  112. package/dist/src/testing/raw-storage-conformance.d.ts.map +1 -1
  113. package/dist/src/testing/raw-storage-conformance.js +52 -2
  114. package/dist/src/testing/raw-storage-conformance.js.map +1 -1
  115. package/package.json +3 -3
  116. package/readme.md +668 -653
  117. package/src/cluster/block-transfer.ts +424 -424
  118. package/src/cluster/client.ts +119 -88
  119. package/src/cluster/cluster-error.ts +64 -64
  120. package/src/cluster/cluster-policy.ts +203 -152
  121. package/src/cluster/cluster-repo.ts +245 -125
  122. package/src/cluster/cluster-size-coupling.ts +45 -0
  123. package/src/cluster/commit-cert.ts +139 -139
  124. package/src/cluster/i-transaction-state-store.ts +43 -43
  125. package/src/cluster/memory-transaction-state-store.ts +56 -56
  126. package/src/cluster/peer-key-binding.ts +37 -37
  127. package/src/cluster/persistent-transaction-state-store.ts +92 -92
  128. package/src/cluster/quorum-restore.ts +223 -223
  129. package/src/cluster/reconcile-block.ts +203 -191
  130. package/src/cluster/service.ts +293 -241
  131. package/src/cluster/supermajority-coupling.ts +37 -37
  132. package/src/cohort-topic/bootstrap-evidence-builder.ts +122 -122
  133. package/src/cohort-topic/bootstrap-evidence-verifiers.ts +132 -132
  134. package/src/cohort-topic/bootstrap-parent-reference.ts +159 -159
  135. package/src/cohort-topic/change-bridge.ts +109 -109
  136. package/src/cohort-topic/cohort-gossip-driver.ts +231 -231
  137. package/src/cohort-topic/cohort-gossip-transport.ts +84 -84
  138. package/src/cohort-topic/fret-trust-anchor.ts +153 -153
  139. package/src/cohort-topic/host.ts +2901 -2901
  140. package/src/cohort-topic/index.ts +13 -13
  141. package/src/cohort-topic/membership-publish-sink.ts +20 -20
  142. package/src/cohort-topic/membership-source.ts +68 -68
  143. package/src/cohort-topic/peer-codec.ts +31 -31
  144. package/src/cohort-topic/peer-sig.ts +86 -86
  145. package/src/cohort-topic/protocols.ts +71 -71
  146. package/src/cohort-topic/reactivity-membership-gate.ts +77 -77
  147. package/src/cohort-topic/size-estimator.ts +16 -16
  148. package/src/cohort-topic/stream-util.ts +135 -87
  149. package/src/cohort-topic/threshold-crypto.ts +239 -239
  150. package/src/cohort-topic/topic-router.ts +77 -77
  151. package/src/dispute/arbitrator-selection.ts +138 -138
  152. package/src/dispute/cascade.ts +524 -524
  153. package/src/dispute/dispute-service.ts +11 -5
  154. package/src/dispute/invalidation.ts +625 -625
  155. package/src/inbound-authorization.ts +190 -190
  156. package/src/index.ts +52 -47
  157. package/src/libp2p-key-network.ts +1120 -958
  158. package/src/libp2p-node-base.ts +1675 -1591
  159. package/src/libp2p-node-rn.ts +30 -30
  160. package/src/libp2p-node.ts +36 -36
  161. package/src/logger.ts +19 -2
  162. package/src/matchmaking/aggregate-counts.ts +104 -104
  163. package/src/matchmaking/index.ts +20 -20
  164. package/src/matchmaking/module.ts +363 -363
  165. package/src/matchmaking/protocols.ts +51 -51
  166. package/src/matchmaking/provider-manager.ts +95 -95
  167. package/src/matchmaking/query-handler.ts +88 -88
  168. package/src/matchmaking/query-transport.ts +492 -492
  169. package/src/matchmaking/seeker-manager.ts +64 -64
  170. package/src/matchmaking/seeker-walk-client.ts +293 -293
  171. package/src/matchmaking/traffic-validation.ts +195 -195
  172. package/src/network/network-manager-service.ts +5 -0
  173. package/src/optimystic-node.ts +36 -0
  174. package/src/owned-block-seed.ts +53 -40
  175. package/src/peer-address-book.ts +149 -0
  176. package/src/protocol-limits.ts +33 -33
  177. package/src/reactivity/forwarder-host.ts +438 -438
  178. package/src/reactivity/index.ts +19 -19
  179. package/src/reactivity/notify-transport.ts +144 -144
  180. package/src/reactivity/origination-manager.ts +192 -192
  181. package/src/reactivity/protocols.ts +61 -61
  182. package/src/reactivity/push-state-gossip.ts +291 -291
  183. package/src/reactivity/recover-transport.ts +408 -408
  184. package/src/reactivity/rotation-rereg-scheduler.ts +256 -256
  185. package/src/reactivity/subscriber-registry.ts +96 -96
  186. package/src/reactivity/subscription-manager.ts +450 -450
  187. package/src/reactivity/topic-bytes.ts +37 -37
  188. package/src/repo/client.ts +12 -2
  189. package/src/repo/cluster-coordinator.ts +99 -3
  190. package/src/repo/coordinator-repo.ts +305 -82
  191. package/src/repo/types.ts +7 -7
  192. package/src/rn.ts +39 -34
  193. package/src/rpc-deadline.ts +45 -45
  194. package/src/storage/arachnode-partition.ts +74 -74
  195. package/src/storage/block-storage.ts +59 -6
  196. package/src/storage/cached-raw-storage.ts +180 -0
  197. package/src/storage/cached-store-driver.ts +859 -0
  198. package/src/storage/i-block-storage.ts +20 -1
  199. package/src/storage/i-kv-store.ts +8 -8
  200. package/src/storage/i-raw-storage.ts +12 -5
  201. package/src/storage/kv-raw-storage.ts +135 -135
  202. package/src/storage/memory-kv-store.ts +28 -28
  203. package/src/storage/memory-storage.ts +25 -25
  204. package/src/storage/memory-store-driver.ts +157 -157
  205. package/src/storage/raw-store-codec.ts +42 -42
  206. package/src/storage/raw-store-driver.ts +80 -80
  207. package/src/storage/ring-selector.ts +317 -317
  208. package/src/storage/ring-shift-coordinator.ts +271 -271
  209. package/src/storage/shared-cache-pool.ts +452 -0
  210. package/src/storage/storage-repo.ts +1014 -903
  211. package/src/testing/cohort-topic-mesh-harness.ts +663 -663
  212. package/src/testing/index.ts +8 -8
  213. package/src/testing/matchmaking-mesh-harness.ts +475 -475
  214. package/src/testing/raw-storage-conformance.ts +453 -397
  215. package/src/testing/reactivity-mesh-harness.ts +922 -922
  216. package/dist/src/storage/restoration-coordinator-v2.d.ts +0 -67
  217. package/dist/src/storage/restoration-coordinator-v2.d.ts.map +0 -1
  218. package/dist/src/storage/restoration-coordinator-v2.js +0 -172
  219. package/dist/src/storage/restoration-coordinator-v2.js.map +0 -1
@@ -0,0 +1,354 @@
1
+ /**
2
+ * A store's registration with the pool. `id` is process-unique and never reused (monotonic
3
+ * counter), so entries and ghost keys of a departed store can never alias a later one.
4
+ * `bytes`/`entries` are live occupancy, maintained by the pool — read-only for callers.
5
+ */
6
+ export class CacheStoreHandle {
7
+ id;
8
+ label;
9
+ bytes = 0;
10
+ entries = 0;
11
+ constructor(id, label) {
12
+ this.id = id;
13
+ this.label = label;
14
+ }
15
+ }
16
+ /**
17
+ * Separator for pool keys: NUL, spelled as an escape so this file stays plain text — a literal
18
+ * NUL byte in the source makes the whole file binary to git (no diff, no blame, no review).
19
+ * NUL is the one character no id can carry: block ids are user-supplied collection URI paths
20
+ * (which may well contain spaces) and action ids are base64url with an optional `tx:`/`stamp:`
21
+ * prefix, so `(storeId, class, blockId, actionId?)` always splits back unambiguously.
22
+ */
23
+ const SEP = '\u0000';
24
+ /**
25
+ * A shared, bounded, clean-value cache pool: one per process (see {@link defaultCachePool}),
26
+ * shared by every `CachedStoreDriver` so that N stores compete inside ONE memory budget
27
+ * instead of keeping N unbounded caches. The pool owns residency and eviction only; all
28
+ * cache *semantics* (coherence, negatives, completeness) stay in the owning store.
29
+ *
30
+ * **Always evict, never refuse, never grow unbounded.** Entries are never pinned and never
31
+ * dirty (the cache is write-through; see `CachedStoreDriver`), so eviction is always safe
32
+ * and there is no exhaustion condition to raise. At the bound the pool evicts; the one entry
33
+ * class that can grow while resident (a store's revision map) is simply evicted like any
34
+ * other when its growth pushes the pool over budget.
35
+ *
36
+ * **2Q admission** (default): a first-touch entry enters the A1in probation FIFO; re-hits
37
+ * inside A1in do NOT promote (FIFO order stands, which is also what absorbs the several
38
+ * correlated touches one logical operation makes); eviction from A1in demotes the key to
39
+ * the A1out ghost set; a later admission of a ghosted key — reuse across operations — goes
40
+ * straight to the protected Am LRU. Am evictions do NOT ghost. One-pass bulk scans
41
+ * therefore live and die inside A1in without displacing any store's hot Am set.
42
+ *
43
+ * **Large values bypass**: an entry whose charge exceeds 1/16 of the byte budget is not
44
+ * admitted at all (the owner reads/writes through), so one oversized value cannot flush
45
+ * the pool.
46
+ *
47
+ * **Budget sanity**: `maxBytes`/`maxEntries` must be positive finite numbers — anything else
48
+ * throws. Absurdly small values are accepted as-is: they cannot break coherence (the owning
49
+ * cache is correct at any residency, including zero), they just degrade toward read-through.
50
+ *
51
+ * Not safe for use from multiple threads; like the caches it serves, it relies on JS
52
+ * single-threaded execution — every mutation completes synchronously.
53
+ */
54
+ export class SharedCachePool {
55
+ maxBytesValue;
56
+ maxEntriesValue;
57
+ admission;
58
+ bypassLimit = 0;
59
+ a1inTargetBytes = 0;
60
+ a1inTargetEntries = 0;
61
+ ghostCap = 0;
62
+ a1in = new Map();
63
+ am = new Map();
64
+ ghost = new Set();
65
+ a1inBytes = 0;
66
+ bytesTotal = 0;
67
+ entriesTotal = 0;
68
+ storeCounter = 0;
69
+ stores = new Map();
70
+ hits = 0;
71
+ admissions = 0;
72
+ ghostHits = 0;
73
+ evictions = 0;
74
+ bypasses = 0;
75
+ constructor(options) {
76
+ this.admission = options?.admission ?? '2q';
77
+ this.maxBytesValue = validateBound('maxBytes', options?.maxBytes ?? platformDefaultBytes());
78
+ this.maxEntriesValue = validateBound('maxEntries', options?.maxEntries ?? Math.max(16, Math.floor(this.maxBytesValue / 512)));
79
+ this.recomputeDerived();
80
+ }
81
+ get maxBytes() { return this.maxBytesValue; }
82
+ get maxEntries() { return this.maxEntriesValue; }
83
+ get bytes() { return this.bytesTotal; }
84
+ get entries() { return this.entriesTotal; }
85
+ /**
86
+ * Re-budget a live pool (e.g. host config change, or a host correcting the platform
87
+ * default). Shrinking evicts immediately down to the new bound.
88
+ */
89
+ setBudget(budget) {
90
+ if (budget.maxBytes !== undefined) {
91
+ this.maxBytesValue = validateBound('maxBytes', budget.maxBytes);
92
+ }
93
+ if (budget.maxEntries !== undefined) {
94
+ this.maxEntriesValue = validateBound('maxEntries', budget.maxEntries);
95
+ }
96
+ this.recomputeDerived();
97
+ this.evictToFit(0, 0);
98
+ this.trimGhost();
99
+ }
100
+ /** Register a store. The returned handle's `id` is unique for the pool's lifetime — never reused. */
101
+ registerStore(label) {
102
+ const handle = new CacheStoreHandle(`s${++this.storeCounter}`, label);
103
+ this.stores.set(handle.id, handle);
104
+ return handle;
105
+ }
106
+ /**
107
+ * Release a departing store: evict any of its entries still resident (defensively — the
108
+ * owner normally clears first), purge its ghost keys, and remove it from the registry.
109
+ * Idempotent. This forced removal notifies the owner like any other eviction — `drop` here
110
+ * would de-account entries the owner still references, leaving it serving values the pool
111
+ * no longer counts. It does NOT ghost (the ghosts are being purged anyway) and does NOT
112
+ * count as a budget eviction: it is a lifecycle release, not memory pressure.
113
+ */
114
+ unregisterStore(handle) {
115
+ for (const queue of [this.a1in, this.am]) {
116
+ for (const entry of [...queue.values()]) {
117
+ if (entry.store !== handle)
118
+ continue;
119
+ this.unlink(entry);
120
+ entry.owner.onPoolEvict(entry);
121
+ }
122
+ }
123
+ this.purgeGhosts(handle);
124
+ this.stores.delete(handle.id);
125
+ }
126
+ /** Drop every ghost key belonging to `handle` (used by store clear/close — pre-clear recency must not survive the clear). */
127
+ purgeGhosts(handle) {
128
+ const prefix = handle.id + SEP;
129
+ for (const key of [...this.ghost]) {
130
+ if (key.startsWith(prefix))
131
+ this.ghost.delete(key);
132
+ }
133
+ }
134
+ /** Build the pool key for an entry. Store-id first: keying by block id alone would alias name-derived header block ids across stores. */
135
+ keyFor(store, cls, blockId, actionId) {
136
+ return actionId === undefined
137
+ ? store.id + SEP + cls + SEP + blockId
138
+ : store.id + SEP + cls + SEP + blockId + SEP + actionId;
139
+ }
140
+ /**
141
+ * Would an entry of this total charge be admitted? `false` = large-value bypass (counted);
142
+ * the owner must then read/write through without caching. Also the gate for value
143
+ * *updates*: an update growing past the limit should drop the entry instead.
144
+ */
145
+ admits(charge) {
146
+ if (charge > this.bypassLimit) {
147
+ this.bypasses++;
148
+ return false;
149
+ }
150
+ return true;
151
+ }
152
+ /**
153
+ * Admit a non-resident entry the owner has ALREADY installed in its own state. Evicts
154
+ * first (so the incoming entry can never be its own victim), then links: to Am when the
155
+ * key is in the ghost set (2Q reuse signal), to A1in probation otherwise.
156
+ */
157
+ admit(entry) {
158
+ if (entry.where !== 'none') {
159
+ throw new Error(`pool admit of already-resident entry ${entry.key}`);
160
+ }
161
+ this.evictToFit(entry.charge, 1);
162
+ this.admissions++;
163
+ if (this.admission === 'lru') {
164
+ this.link(entry, 'am');
165
+ return;
166
+ }
167
+ if (this.ghost.delete(entry.key)) {
168
+ this.ghostHits++;
169
+ this.link(entry, 'am');
170
+ }
171
+ else {
172
+ this.link(entry, 'a1in');
173
+ }
174
+ }
175
+ /** Record a read hit: Am refreshes to MRU; A1in stays put (FIFO — no intra-probation promotion). */
176
+ touch(entry) {
177
+ this.hits++;
178
+ this.refresh(entry);
179
+ }
180
+ /**
181
+ * Re-account a resident entry after the owner mutated its value (write hit, or container
182
+ * growth like a revision-map fill). Queue position refreshes like a touch (without
183
+ * counting a hit); growth over budget evicts — possibly the updated entry itself, which
184
+ * is why owners call this LAST and never touch their cache state after.
185
+ *
186
+ * NOTE: growth here is NOT re-checked against {@link admits}' large-value bypass, so a
187
+ * container entry (a block's revision map or pending-id set) can grow past 1/16 of the
188
+ * budget and squat where a single oversized value would have been refused. Bounded and
189
+ * self-correcting today: an over-share entry keeps `a1inBytes` above its target, so
190
+ * probation evicts it preferentially, and anything past `maxBytes` is evicted outright.
191
+ * If a block ever accumulates enough revisions for one map to hold a real fraction of the
192
+ * budget (order 20k revs at the 32 MB Node default), gate growth on `admits(charge)` in
193
+ * the owner's revision/pending-list paths and drop the entry when it fails, exactly as
194
+ * `CachedStoreDriver.cachePoint` already does for point values.
195
+ */
196
+ updated(entry, newCharge) {
197
+ if (entry.where === 'none')
198
+ return;
199
+ const delta = newCharge - entry.charge;
200
+ entry.charge = newCharge;
201
+ this.bytesTotal += delta;
202
+ entry.store.bytes += delta;
203
+ if (entry.where === 'a1in')
204
+ this.a1inBytes += delta;
205
+ this.refresh(entry);
206
+ if (delta > 0)
207
+ this.evictToFit(0, 0);
208
+ }
209
+ /**
210
+ * Remove an entry WITHOUT the eviction callback — for owners invalidating or clearing
211
+ * their own entries (the owner is already removing its reference). Never ghosts: an
212
+ * invalidation says nothing about reuse. No-op if not resident.
213
+ */
214
+ drop(entry) {
215
+ if (entry.where === 'none')
216
+ return;
217
+ this.unlink(entry);
218
+ }
219
+ stats() {
220
+ const amBytes = this.bytesTotal - this.a1inBytes;
221
+ return {
222
+ maxBytes: this.maxBytesValue,
223
+ maxEntries: this.maxEntriesValue,
224
+ bytes: this.bytesTotal,
225
+ entries: this.entriesTotal,
226
+ a1in: { bytes: this.a1inBytes, entries: this.a1in.size },
227
+ am: { bytes: amBytes, entries: this.am.size },
228
+ ghostKeys: this.ghost.size,
229
+ hits: this.hits,
230
+ admissions: this.admissions,
231
+ ghostHits: this.ghostHits,
232
+ evictions: this.evictions,
233
+ bypasses: this.bypasses,
234
+ stores: [...this.stores.values()].map(s => ({ id: s.id, label: s.label, bytes: s.bytes, entries: s.entries })),
235
+ };
236
+ }
237
+ // ---- internals ----
238
+ recomputeDerived() {
239
+ this.bypassLimit = Math.floor(this.maxBytesValue / 16);
240
+ this.a1inTargetBytes = Math.floor(this.maxBytesValue / 4);
241
+ this.a1inTargetEntries = Math.floor(this.maxEntriesValue / 4);
242
+ this.ghostCap = Math.floor(this.maxEntriesValue / 2);
243
+ }
244
+ link(entry, where) {
245
+ entry.where = where;
246
+ (where === 'a1in' ? this.a1in : this.am).set(entry.key, entry);
247
+ if (where === 'a1in')
248
+ this.a1inBytes += entry.charge;
249
+ this.bytesTotal += entry.charge;
250
+ this.entriesTotal++;
251
+ entry.store.bytes += entry.charge;
252
+ entry.store.entries++;
253
+ }
254
+ unlink(entry) {
255
+ (entry.where === 'a1in' ? this.a1in : this.am).delete(entry.key);
256
+ if (entry.where === 'a1in')
257
+ this.a1inBytes -= entry.charge;
258
+ this.bytesTotal -= entry.charge;
259
+ this.entriesTotal--;
260
+ entry.store.bytes -= entry.charge;
261
+ entry.store.entries--;
262
+ entry.where = 'none';
263
+ }
264
+ /** Map insertion order gives A1in its FIFO and Am its LRU: refresh = delete + re-insert. */
265
+ refresh(entry) {
266
+ if (entry.where !== 'am')
267
+ return;
268
+ this.am.delete(entry.key);
269
+ this.am.set(entry.key, entry);
270
+ }
271
+ evictToFit(incomingCharge, incomingEntries) {
272
+ while (this.bytesTotal + incomingCharge > this.maxBytesValue
273
+ || this.entriesTotal + incomingEntries > this.maxEntriesValue) {
274
+ const victim = this.pickVictim();
275
+ if (victim === undefined)
276
+ break; // pool empty — nothing left to evict
277
+ this.evict(victim);
278
+ }
279
+ }
280
+ pickVictim() {
281
+ if (this.admission === 'lru') {
282
+ return first(this.am) ?? first(this.a1in);
283
+ }
284
+ // 2Q: take A1in's FIFO head while probation is over its share of EITHER rail (or Am has
285
+ // nothing); otherwise the protected queue's LRU entry. Probation needs a share of the
286
+ // entry rail too, not just the byte rail: entries small enough that the entry count binds
287
+ // first (cached negatives are near-empty) would otherwise leave `a1inBytes` permanently
288
+ // under its byte target, so every admission would evict from Am and a one-pass scan would
289
+ // flush the protected set — 2Q silently degraded to plain LRU.
290
+ const a1inHead = first(this.a1in);
291
+ if (a1inHead !== undefined && (this.a1inBytes > this.a1inTargetBytes
292
+ || this.a1in.size > this.a1inTargetEntries || this.am.size === 0)) {
293
+ return a1inHead;
294
+ }
295
+ return first(this.am) ?? a1inHead;
296
+ }
297
+ evict(entry) {
298
+ const fromA1in = entry.where === 'a1in';
299
+ this.unlink(entry);
300
+ if (fromA1in && this.admission === '2q') {
301
+ this.pushGhost(entry.key);
302
+ }
303
+ this.evictions++;
304
+ entry.owner.onPoolEvict(entry);
305
+ }
306
+ pushGhost(key) {
307
+ this.ghost.delete(key);
308
+ this.ghost.add(key);
309
+ this.trimGhost();
310
+ }
311
+ trimGhost() {
312
+ while (this.ghost.size > this.ghostCap) {
313
+ const oldest = this.ghost.values().next().value;
314
+ if (oldest === undefined)
315
+ break;
316
+ this.ghost.delete(oldest);
317
+ }
318
+ }
319
+ }
320
+ function first(queue) {
321
+ return queue.values().next().value;
322
+ }
323
+ function validateBound(name, value) {
324
+ if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
325
+ throw new TypeError(`SharedCachePool ${name} must be a positive finite number, got ${value}`);
326
+ }
327
+ return Math.floor(value);
328
+ }
329
+ /**
330
+ * Honest per-platform default byte budget: ~8 MB on React Native (the phone running twenty
331
+ * workspaces is the device least able to afford more), 16 MB in browsers, 32 MB on Node
332
+ * (desktop / provider hosts). Hosts with better knowledge should size explicitly — either
333
+ * `defaultCachePool().setBudget(...)` or their own `SharedCachePool` passed to each cache.
334
+ */
335
+ function platformDefaultBytes() {
336
+ const g = globalThis;
337
+ if (g.navigator?.product === 'ReactNative')
338
+ return 8 * 1024 * 1024;
339
+ if (g.window !== undefined)
340
+ return 16 * 1024 * 1024;
341
+ return 32 * 1024 * 1024;
342
+ }
343
+ let defaultPool;
344
+ /**
345
+ * The process-wide shared pool every `CachedStoreDriver` joins unless handed a specific one.
346
+ * Lazily created on first use with the platform-default budget; re-budget it live via
347
+ * `setBudget`. One pool per process is the point: twenty workspaces' caches compete inside
348
+ * one budget instead of each sizing itself as if it were alone.
349
+ */
350
+ export function defaultCachePool() {
351
+ defaultPool ??= new SharedCachePool();
352
+ return defaultPool;
353
+ }
354
+ //# sourceMappingURL=shared-cache-pool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-cache-pool.js","sourceRoot":"","sources":["../../../src/storage/shared-cache-pool.ts"],"names":[],"mappings":"AAgDA;;;;GAIG;AACH,MAAM,OAAO,gBAAgB;IAIlB;IACA;IAJV,KAAK,GAAG,CAAC,CAAC;IACV,OAAO,GAAG,CAAC,CAAC;IACZ,YACU,EAAU,EACV,KAAyB;QADzB,OAAE,GAAF,EAAE,CAAQ;QACV,UAAK,GAAL,KAAK,CAAoB;IAChC,CAAC;CACJ;AAmCD;;;;;;GAMG;AACH,MAAM,GAA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C,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;AACpC,CAAC;AAED,SAAS,aAAa,CAAC,IAAY,EAAE,KAAa;IACjD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;QACxE,MAAM,IAAI,SAAS,CAAC,mBAAmB,IAAI,0CAA0C,KAAK,EAAE,CAAC,CAAC;IAC/F,CAAC;IACD,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,SAAS,oBAAoB;IAC5B,MAAM,CAAC,GAAG,UAAoE,CAAC;IAC/E,IAAI,CAAC,CAAC,SAAS,EAAE,OAAO,KAAK,aAAa;QAAE,OAAO,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;IACnE,IAAI,CAAC,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;IACpD,OAAO,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;AACzB,CAAC;AAED,IAAI,WAAwC,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB;IAC/B,WAAW,KAAK,IAAI,eAAe,EAAE,CAAC;IACtC,OAAO,WAAW,CAAC;AACpB,CAAC"}
@@ -94,7 +94,56 @@ export declare class StorageRepo implements IRepo, IBlockChangeNotifier, IBlockR
94
94
  private emitPromotions;
95
95
  pend(request: PendRequest, _options?: MessageOptions): Promise<PendResult>;
96
96
  cancel(actionRef: ActionBlocks, _options?: MessageOptions): Promise<void>;
97
+ /**
98
+ * Commit a previously-pended action across its blocks, under the per-block commit latches.
99
+ *
100
+ * **Divergence vs genuine fault.** When the batch cannot be completed, the reason decides what
101
+ * happens to the pending records the pend left behind. `ClusterMember.applyConsensusOperation`
102
+ * makes the same split one layer up — it *tolerates* a divergence (and reconciles every
103
+ * `commit.blockIds` entry from a cohort peer) but *propagates* a genuine fault for retry — so this
104
+ * method must agree with it:
105
+ *
106
+ * - **Divergence** — this node is behind the agreed history, either because it holds no
107
+ * materializable base ({@link MissingBaseRevisionError}) or because it never received the pend
108
+ * (the `Pending action … not found` throw). Reconcile is guaranteed to follow and will advance
109
+ * every block in the batch past `request.rev`, so no pending record here can ever be promoted:
110
+ * {@link dropUnpromotablePendings} deletes them (see {@link refuseMissingBase}, which already
111
+ * accepts this tradeoff for the single refusing block).
112
+ * - **Genuine fault** — any other throw out of {@link internalCommit} (a raw-storage error, …).
113
+ * `ClusterMember` propagates it and the commit is retried, and a retry can still replay the
114
+ * pendings, so they are KEPT.
115
+ *
116
+ * The stale/`missedCommits` early return (this node is AHEAD — it already holds a revision at or
117
+ * past `request.rev`, committed under a different action) deliberately keeps pendings too, and its
118
+ * cure is the losing client's `cancel`: `CoordinatorRepo.cancel` runs through consensus, so every
119
+ * member drops the record, not just the coordinator. Replication cannot be the cure here — this
120
+ * node is already ahead, and a later forward write carries a DIFFERENT action id, which is not
121
+ * what `BlockStorage.saveForwardRevision` deletes. A client that dies between the stale result and
122
+ * its `cancel` therefore still strands the record; that is pre-existing and orthogonal to the
123
+ * divergence split above.
124
+ */
97
125
  commit(request: CommitRequest, _options?: MessageOptions): Promise<CommitResult>;
126
+ /**
127
+ * Delete `actionId`'s pending record from every given block, tolerating absence.
128
+ *
129
+ * Called by {@link commit} when it abandons a batch **because this node has diverged from the
130
+ * agreed history** — the caller has already made that determination; this helper does not
131
+ * re-derive it. Once `ClusterMember` reconciles the batch, every one of these blocks sits at or
132
+ * past `request.rev`, so a commit retry partitions them as already-done/stale and never revisits
133
+ * their pendings; left in place they are reported as phantom conflicting actions by {@link pend}
134
+ * for every later write to the block (under `policy: 'f'`, forever).
135
+ *
136
+ * No special-casing is needed for blocks that already landed (record promoted), that were
137
+ * `recovered` (record already gone), or for the refusing block itself
138
+ * ({@link refuseMissingBase} deleted its record): deleting an absent pending record is a no-op on
139
+ * every backend.
140
+ *
141
+ * Per-block failures are logged and swallowed rather than propagated: this cleanup must never
142
+ * replace the failure the caller is about to report — the pre-loop throw's message is pattern-
143
+ * matched by `ClusterMember.isMissingPendingActionError`, and a swapped error would misroute
144
+ * consensus. A leftover record only degrades this node's participation in that one block.
145
+ */
146
+ private dropUnpromotablePendings;
98
147
  /**
99
148
  * Reconciles `metadata.latest` for a single block with the highest contiguous
100
149
  * fully-promoted revision in durable storage. Use after a crash between
@@ -131,9 +180,13 @@ export declare class StorageRepo implements IRepo, IBlockChangeNotifier, IBlockR
131
180
  * BlockStorage reports every one of these as a bare `Error`, so they cannot be told apart here,
132
181
  * and treating them as divergence is the safe default: this node genuinely cannot materialize the
133
182
  * base right now, and the cluster's policy is to heal rather than throw out of consensus. The
134
- * price is that a transient fault ALSO drops the pending (see {@link refuseMissingBase}), so the
135
- * block converges by replication instead of by a replay the retry could have done. Narrowing this
136
- * would require typed faults out of BlockStorage; until then, prefer the tolerant reading.
183
+ * price is that a transient fault ALSO drops pending records this block's (see
184
+ * {@link refuseMissingBase}) AND, because {@link commit} keys its cleanup off the same error type,
185
+ * every not-yet-reached block in the same batch so those blocks converge by replication instead
186
+ * of by a replay the retry could have done. That is a wider blast radius than the per-block
187
+ * refusal alone, and it is why the discriminator must NOT be loosened beyond this error type.
188
+ * Narrowing this would require typed faults out of BlockStorage; until then, prefer the tolerant
189
+ * reading.
137
190
  */
138
191
  private readCommitBase;
139
192
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"storage-repo.d.ts","sourceRoot":"","sources":["../../../src/storage/storage-repo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,eAAe,EAAE,WAAW,EAAE,UAAU,EAAE,YAAY,EACzG,SAAS,EACH,MAAM,EAAE,SAAS,EACjC,kBAAkB,EAClB,YAAY,EAAE,oBAAoB,EAAE,wBAAwB,EAE5D,MAAM,qBAAqB,CA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