Skip to main content

liminal/durability/
store.rs

1use haematite::{ApiError, Database, DatabaseConfig, Event, EventStore};
2
3use std::path::Path;
4use std::sync::Arc;
5
6use super::DurabilityError;
7
8use tempfile::TempDir;
9
10/// Entry read from a durable haematite stream.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct StoredEntry {
13    /// Opaque stored payload bytes.
14    pub payload: Vec<u8>,
15    /// Sequence number assigned by the stream.
16    pub sequence: u64,
17    /// Store timestamp associated with the entry.
18    pub timestamp: u64,
19}
20
21/// Direct durability surface matching haematite's append/read/cas/scan API.
22#[async_trait::async_trait]
23pub trait DurableStore: std::fmt::Debug + Send + Sync {
24    /// Appends `payload` to `stream_key` if `expected_seq` matches the stream head.
25    async fn append(
26        &self,
27        stream_key: &str,
28        payload: Vec<u8>,
29        expected_seq: u64,
30    ) -> Result<u64, DurabilityError>;
31
32    /// Reads entries from `stream_key` beginning at `offset`, up to `limit` entries.
33    async fn read_from(
34        &self,
35        stream_key: &str,
36        offset: u64,
37        limit: usize,
38    ) -> Result<Vec<StoredEntry>, DurabilityError>;
39
40    /// Reads exactly the event at `sequence` without traversing its suffix.
41    async fn read_at(
42        &self,
43        stream_key: &str,
44        sequence: u64,
45    ) -> Result<Option<StoredEntry>, DurabilityError> {
46        Ok(self
47            .read_from(stream_key, sequence, 1)
48            .await?
49            .into_iter()
50            .next())
51    }
52
53    /// Atomically replaces a stored numeric value if it equals `old_value`.
54    ///
55    /// An `old_value` of `0` matches a key that is currently *absent* as well as
56    /// one explicitly stored as `0`: a fresh cursor is created on its first
57    /// checkpoint without a prior write. See [`HaematiteStore::cas`] for how this
58    /// "absent == 0" contract is preserved atomically over the real engine.
59    async fn cas(&self, key: &str, old_value: u64, new_value: u64) -> Result<(), DurabilityError>;
60
61    /// Reads a numeric value previously updated through compare-and-swap.
62    async fn read_value(&self, key: &str) -> Result<Option<u64>, DurabilityError>;
63
64    /// Scans entries by store prefix.
65    async fn scan(&self, prefix: &str) -> Result<Vec<StoredEntry>, DurabilityError>;
66
67    /// Flushes buffered writes so completed durable operations are persisted.
68    ///
69    /// # Errors
70    /// Returns [`DurabilityError`] when the underlying store cannot complete the flush.
71    async fn flush(&self) -> Result<(), DurabilityError>;
72}
73
74/// `DurableStore` implementation that delegates directly to haematite's `EventStore`.
75///
76/// The real [`EventStore`] is synchronous (every call blocks on the owning
77/// shard actor's reply), so each `async` method below completes on its first
78/// poll. The synchronous bridge in [`super::bridge`] relies on exactly that.
79#[derive(Clone, Debug)]
80pub struct HaematiteStore {
81    event_store: Arc<EventStore>,
82}
83
84impl HaematiteStore {
85    /// Wraps a haematite `EventStore` handle.
86    #[must_use]
87    pub const fn new(event_store: Arc<EventStore>) -> Self {
88        Self { event_store }
89    }
90
91    /// Reads the half-open key window `[offset, offset + limit)` from one
92    /// stream, or `None` when the window did not fill.
93    ///
94    /// `None` is not "empty": it is "this window cannot answer on its own",
95    /// and the caller must fall through to the unbounded engine read. A window
96    /// short by even one row may be short because the stream ended, because
97    /// history was compacted, or because an entry inside it expired, and only
98    /// the engine's own read distinguishes those.
99    ///
100    /// `limit` must be nonzero; a zero limit has no window to fill and is the
101    /// caller's fall-through case.
102    fn bounded_page(
103        &self,
104        stream_key: &str,
105        offset: u64,
106        limit: usize,
107    ) -> Result<Option<Vec<StoredEntry>>, DurabilityError> {
108        const TIMESTAMP_WIDTH: usize = std::mem::size_of::<u64>();
109
110        // Engine keys are 1-based; the public API is 0-based.
111        let Some(engine_from) = offset.checked_add(1) else {
112            return Ok(None);
113        };
114        let Some(engine_end) = u64::try_from(limit)
115            .ok()
116            .and_then(|limit| engine_from.checked_add(limit))
117        else {
118            return Ok(None);
119        };
120        let key = stream_key.as_bytes();
121        let from = haematite::encode_stream_key(key, engine_from);
122        let to = haematite::encode_stream_key(key, engine_end);
123        let entries = self
124            .event_store
125            .database()
126            .range_routed(key, &from, &to)
127            .map_err(ApiError::from)
128            .map_err(DurabilityError::from)?;
129        if entries.len() != limit {
130            return Ok(None);
131        }
132
133        let mut page = Vec::with_capacity(entries.len());
134        for (encoded_key, value) in entries {
135            let Some((decoded_key, engine_sequence)) = haematite::decode_stream_key(&encoded_key)
136            else {
137                return Err(DurabilityError::StoreError(ApiError::CorruptEvent(
138                    format!("paged read key does not encode an event for stream {stream_key}"),
139                )));
140            };
141            if decoded_key != key {
142                return Err(DurabilityError::StoreError(ApiError::CorruptEvent(
143                    format!("paged read key does not encode stream {stream_key}"),
144                )));
145            }
146            let sequence = engine_sequence.checked_sub(1).ok_or_else(|| {
147                DurabilityError::StoreError(ApiError::CorruptEvent(format!(
148                    "paged read event key has zero seq for stream {stream_key}"
149                )))
150            })?;
151            let Some(timestamp_bytes) = value.get(..TIMESTAMP_WIDTH) else {
152                return Err(DurabilityError::StoreError(ApiError::CorruptEvent(
153                    format!(
154                        "paged read event value is shorter than its timestamp for stream {stream_key}"
155                    ),
156                )));
157            };
158            let timestamp = u64::from_be_bytes(timestamp_bytes.try_into().map_err(|_| {
159                DurabilityError::StoreError(ApiError::CorruptEvent(format!(
160                    "paged read event timestamp has the wrong width for stream {stream_key}"
161                )))
162            })?);
163            let Some(payload) = value.get(TIMESTAMP_WIDTH..) else {
164                return Err(DurabilityError::StoreError(ApiError::CorruptEvent(
165                    format!("paged read event has no payload boundary for stream {stream_key}"),
166                )));
167            };
168            page.push(StoredEntry {
169                payload: payload.to_vec(),
170                sequence,
171                timestamp,
172            });
173        }
174        Ok(Some(page))
175    }
176}
177
178#[async_trait::async_trait]
179impl DurableStore for HaematiteStore {
180    async fn append(
181        &self,
182        stream_key: &str,
183        payload: Vec<u8>,
184        expected_seq: u64,
185    ) -> Result<u64, DurabilityError> {
186        // Contract bridge: liminal's `DurableStore::append` returns the *assigned
187        // event sequence* (0-based position of the just-appended event), which is
188        // exactly `expected_seq` for a single append. The real `EventStore::append`
189        // instead returns the stream's new next-sequence (`expected_seq + 1`), so
190        // subtract one to recover the assigned seq. A `0` next-seq is impossible
191        // after a successful single append, so the `checked_sub` cannot saturate
192        // silently; if it ever did the engine returned a contract-violating value.
193        let next_seq = self
194            .event_store
195            .append(stream_key.as_bytes(), &payload, expected_seq)
196            .map_err(DurabilityError::from)?;
197        next_seq.checked_sub(1).ok_or_else(|| {
198            DurabilityError::StoreError(ApiError::CorruptEvent(format!(
199                "append returned next-seq 0 for stream {stream_key}"
200            )))
201        })
202    }
203
204    async fn read_from(
205        &self,
206        stream_key: &str,
207        offset: u64,
208        limit: usize,
209    ) -> Result<Vec<StoredEntry>, DurabilityError> {
210        // `EventStore::read_from` applies no limit: it materialises every event
211        // with seq >= offset, key and value copied across the shard-actor
212        // boundary, and truncating afterwards throws that work away. Paged
213        // replay therefore costs O(N^2) engine rows to deliver N (#60).
214        //
215        // Ask the engine for the page instead. Event keys are
216        // `stream_key || 0x00 || seq.to_be_bytes()` (haematite 0.8.1
217        // `api/event_store.rs:375`), so byte order is sequence order and a
218        // half-open key window names exactly one page. `range_routed` routes on
219        // the stream key — the same co-location `EventStore` uses for its own
220        // reads — and merges committed tree with WAL buffer, which is the
221        // identical mechanism behind the unbounded read (`db.rs:212`).
222        //
223        // A FULL window is the same answer the unbounded read gave: it holds
224        // `limit` live events, and key order makes those exactly the first
225        // `limit` events at or after `offset`. Anything SHORT falls through to
226        // the unbounded read, so the two answers the window cannot settle by
227        // itself stay the engine's own: the `HistoryCompacted` verdict at
228        // `offset == 0`, and the case where expiry or compaction leaves a hole
229        // inside the window. The fall-through costs a suffix scan only where
230        // the suffix is already shorter than a page — the end-of-stream read
231        // that terminates every walk.
232        if limit > 0 {
233            if let Some(page) = self.bounded_page(stream_key, offset, limit)? {
234                account_engine_read(page.len(), false);
235                return Ok(page);
236            }
237        }
238        let mut events = self
239            .event_store
240            .read_from(stream_key.as_bytes(), offset)
241            .map_err(DurabilityError::from)?;
242        account_engine_read(events.len(), true);
243        events.truncate(limit);
244        Ok(events.into_iter().map(StoredEntry::from).collect())
245    }
246
247    async fn read_at(
248        &self,
249        stream_key: &str,
250        sequence: u64,
251    ) -> Result<Option<StoredEntry>, DurabilityError> {
252        const TIMESTAMP_WIDTH: usize = std::mem::size_of::<u64>();
253
254        let engine_sequence = sequence.checked_add(1).ok_or_else(|| {
255            DurabilityError::StoreError(ApiError::CorruptEvent(format!(
256                "point read sequence overflow for stream {stream_key}"
257            )))
258        })?;
259        let event_key = haematite::encode_stream_key(stream_key.as_bytes(), engine_sequence);
260        let Some(value) = self
261            .event_store
262            .database()
263            .get_routed(stream_key.as_bytes(), &event_key)
264            .map_err(ApiError::from)
265            .map_err(DurabilityError::from)?
266        else {
267            return Ok(None);
268        };
269        let Some(timestamp_bytes) = value.get(..TIMESTAMP_WIDTH) else {
270            return Err(DurabilityError::StoreError(ApiError::CorruptEvent(
271                format!(
272                    "point-read event value is shorter than its timestamp for stream {stream_key}"
273                ),
274            )));
275        };
276        let timestamp = u64::from_be_bytes(timestamp_bytes.try_into().map_err(|_| {
277            DurabilityError::StoreError(ApiError::CorruptEvent(format!(
278                "point-read event timestamp has the wrong width for stream {stream_key}"
279            )))
280        })?);
281        let Some(payload) = value.get(TIMESTAMP_WIDTH..) else {
282            return Err(DurabilityError::StoreError(ApiError::CorruptEvent(
283                format!("point-read event has no payload boundary for stream {stream_key}"),
284            )));
285        };
286        Ok(Some(StoredEntry {
287            payload: payload.to_vec(),
288            sequence,
289            timestamp,
290        }))
291    }
292
293    async fn cas(&self, key: &str, old_value: u64, new_value: u64) -> Result<(), DurabilityError> {
294        // Preserve liminal's "absent == 0" cursor contract faithfully over an
295        // engine that distinguishes `None` (absent) from `Some(0)` (a stored
296        // zero). The invariant that makes the mapping below correct: we NEVER
297        // persist a physical zero, so a logical value of 0 and physical absence
298        // always coincide.
299        //
300        // A `cas` whose target `new_value` is 0 must therefore write nothing — it
301        // only asserts the precondition. This is reachable as `cas(0, 0)` (a
302        // cursor checkpoint at offset 0; offsets are monotonic so they never CAS
303        // down to 0 from a higher value). Were we instead to let it store a
304        // physical zero, the *next* `cas(0, n)` — mapped to expect-absent `None`
305        // — would wrongly fail against the now-present key and permanently stall
306        // the cursor. Asserting via a read is race-free here precisely because no
307        // value is written, so there is no lost-update window.
308        if new_value == 0 {
309            return self
310                .event_store
311                .read_value(key.as_bytes())
312                .map_err(DurabilityError::from)?
313                .map_or(Ok(()), |stored| {
314                    Err(DurabilityError::CursorRegression {
315                        stored,
316                        attempted: old_value,
317                    })
318                });
319        }
320        // With a physical zero never stored, `old_value == 0` is exactly the
321        // expect-absent expectation. Any other `old_value` maps to `Some(_)`.
322        // This is a single CAS routed to the owning shard actor, where read,
323        // compare, and write run with no interleaving point (haematite's
324        // `ShardActor::cas`) — the engine's atomicity is preserved end to end.
325        let expected = if old_value == 0 {
326            None
327        } else {
328            Some(old_value)
329        };
330        self.event_store
331            .cas(key.as_bytes(), expected, new_value)
332            .map_err(DurabilityError::from)
333    }
334
335    async fn read_value(&self, key: &str) -> Result<Option<u64>, DurabilityError> {
336        self.event_store
337            .read_value(key.as_bytes())
338            .map_err(DurabilityError::from)
339    }
340
341    async fn scan(&self, prefix: &str) -> Result<Vec<StoredEntry>, DurabilityError> {
342        // The real `scan` predicate yields stream *metadata* (key + next_seq),
343        // not events. Liminal's contract is to return the events of every stream
344        // whose key matches `prefix`, so collect the matching stream keys, then
345        // read each stream's full event list and flatten the results.
346        let prefix_bytes = prefix.as_bytes().to_vec();
347        let matches = self
348            .event_store
349            .scan(|meta| meta.stream_key.starts_with(&prefix_bytes))
350            .map_err(DurabilityError::from)?;
351        let mut entries = Vec::new();
352        for stream in matches {
353            let events = self
354                .event_store
355                .read(&stream.stream_key)
356                .map_err(DurabilityError::from)?;
357            entries.extend(events.into_iter().map(StoredEntry::from));
358        }
359        Ok(entries)
360    }
361
362    async fn flush(&self) -> Result<(), DurabilityError> {
363        self.event_store.flush().map_err(DurabilityError::from)
364    }
365}
366
367/// Drop shell enforcing "close the store, then remove its directory" as
368/// explicit code rather than field declaration order.
369///
370/// Declaration order alone cannot express the unwind case: if dropping the
371/// store panics (a haematite worker failing to join), Rust would still drop
372/// the remaining fields during the unwind and remove the directory under
373/// possibly-live workers. This `Drop` drops the store inside `catch_unwind`;
374/// on unwind it DISARMS the directory guard — the directory is deliberately
375/// leaked, because visible residue is diagnosable while removal under live
376/// workers is filesystem corruption — logs the leaked path, and re-raises the
377/// panic. On the clean path the directory is removed after the store, HERE,
378/// by an explicit [`TempDir::close`] whose error is logged.
379///
380/// The explicitness is the point. Letting the `TempDir` field drop instead
381/// would remove the directory via `tempfile`'s own `Drop`, which is
382/// `let _ = remove_dir_all(..)` — the `io::Result` is discarded, so a removal
383/// that FAILED would be indistinguishable from one that succeeded and this
384/// doc's "the directory is removed" would be a claim no code could check.
385/// `close()` returns that error; the clean path reports it and leaves the
386/// residue where the log says it is. It never panics (a `Drop` that unwinds
387/// during another unwind aborts the process) and never masks: a failure to
388/// remove is a durability fact, not something to swallow.
389///
390/// Both fields are `Option` only so `drop` can move them out; they are `Some`
391/// for the shell's entire life outside `drop`.
392#[derive(Debug)]
393struct EphemeralGuard<S> {
394    store: Option<S>,
395    dir: Option<TempDir>,
396}
397
398impl<S> Drop for EphemeralGuard<S> {
399    fn drop(&mut self) {
400        let store = self.store.take();
401        // AssertUnwindSafe: the closure owns everything it touches (the moved
402        // store), and the unwind path below observes no state the panicking
403        // drop could have left broken — it only disarms the guard and re-raises.
404        let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || drop(store)));
405        if let Err(panic) = outcome {
406            if let Some(dir) = self.dir.take() {
407                let leaked = dir.keep();
408                tracing::error!(
409                    path = %leaked.display(),
410                    "ephemeral store drop panicked; leaking its directory rather than \
411                     removing it under possibly-live database workers"
412                );
413            }
414            std::panic::resume_unwind(panic);
415        }
416        // Clean path: the store is closed, its workers are joined and the
417        // writer lock is released, so removing the directory now is safe — and
418        // removing it EXPLICITLY is what makes a failure sayable.
419        if let Some(dir) = self.dir.take() {
420            let path = dir.path().to_path_buf();
421            if let Err(error) = dir.close() {
422                tracing::error!(
423                    path = %path.display(),
424                    %error,
425                    "ephemeral store directory removal failed; residue remains at the \
426                     logged path"
427                );
428            }
429        }
430    }
431}
432
433/// Exclusive-ownership ephemeral durable store: the sole owner of both the
434/// haematite database and the temporary directory that backs it.
435///
436/// [`HaematiteStore::new`] takes a *caller-supplied* `Arc<EventStore>`, so a
437/// clone of that inner handle can outlive any guard placed merely beside it —
438/// field declaration order proves nothing across that `Arc` boundary. This
439/// wrapper instead owns the database outright: [`open_ephemeral`] constructs the
440/// inner `Arc` itself, this type never exposes it (no getter) and is deliberately
441/// **not `Clone`**, so the only handle a caller can hold is an
442/// `Arc<dyn DurableStore>` over the whole wrapper. When the last such clone
443/// drops, the [`EphemeralGuard`] drops the store FIRST — the database closes,
444/// its shard actors join and the data-dir writer lock releases on fd close —
445/// and only then removes the directory, logging the error if that removal
446/// fails; if closing the database panics, the directory is deliberately leaked
447/// instead (see [`EphemeralGuard`]).
448#[derive(Debug)]
449pub struct EphemeralHaematiteStore {
450    guard: EphemeralGuard<HaematiteStore>,
451}
452
453impl EphemeralHaematiteStore {
454    /// Takes an already-open ephemeral `Database` and the temporary directory it
455    /// was opened under, becoming their single exclusive owner.
456    ///
457    /// The inner `Arc<EventStore>` is created here and never leaves this type, so
458    /// no caller-supplied clone of it can exist to defeat the drop ordering.
459    /// `ephemeral_dir` must be the directory `database` lives in and must have
460    /// been created before the database was opened (so a failed open removed it
461    /// via the guard's `Drop`, before this constructor was ever reached).
462    fn new(database: Database, ephemeral_dir: TempDir) -> Self {
463        Self {
464            guard: EphemeralGuard {
465                store: Some(HaematiteStore::new(Arc::new(EventStore::new(database)))),
466                dir: Some(ephemeral_dir),
467            },
468        }
469    }
470
471    /// Store handle behind the guard's teardown-only `Option`.
472    ///
473    /// `None` exists only inside [`EphemeralGuard::drop`], which cannot overlap
474    /// a `&self` call, so this error is unreachable by construction — it is a
475    /// typed refusal in place of a panic the workspace forbids, not a state a
476    /// caller can produce.
477    fn store(&self) -> Result<&HaematiteStore, DurabilityError> {
478        self.guard
479            .store
480            .as_ref()
481            .ok_or(DurabilityError::EphemeralStoreDetached)
482    }
483
484    /// Path of the guarding temporary directory, for lifecycle assertions only.
485    #[cfg(test)]
486    pub(crate) fn ephemeral_dir_path(&self) -> Option<&Path> {
487        self.guard.dir.as_ref().map(TempDir::path)
488    }
489}
490
491#[async_trait::async_trait]
492impl DurableStore for EphemeralHaematiteStore {
493    async fn append(
494        &self,
495        stream_key: &str,
496        payload: Vec<u8>,
497        expected_seq: u64,
498    ) -> Result<u64, DurabilityError> {
499        self.store()?
500            .append(stream_key, payload, expected_seq)
501            .await
502    }
503
504    async fn read_from(
505        &self,
506        stream_key: &str,
507        offset: u64,
508        limit: usize,
509    ) -> Result<Vec<StoredEntry>, DurabilityError> {
510        self.store()?.read_from(stream_key, offset, limit).await
511    }
512
513    async fn cas(&self, key: &str, old_value: u64, new_value: u64) -> Result<(), DurabilityError> {
514        self.store()?.cas(key, old_value, new_value).await
515    }
516
517    async fn read_value(&self, key: &str) -> Result<Option<u64>, DurabilityError> {
518        self.store()?.read_value(key).await
519    }
520
521    async fn scan(&self, prefix: &str) -> Result<Vec<StoredEntry>, DurabilityError> {
522        self.store()?.scan(prefix).await
523    }
524
525    async fn flush(&self) -> Result<(), DurabilityError> {
526        self.store()?.flush().await
527    }
528}
529
530/// Opens a self-owning ephemeral haematite store under a fresh temporary
531/// directory below the system temp dir.
532///
533/// The directory is created BEFORE [`Database::create`], so every failure path —
534/// including a haematite open/create error — removes it when the guard drops on
535/// the error return; the returned store owns the guard on success. The database
536/// is created directly in the (empty) temporary directory: haematite's `create`
537/// accepts an existing empty dir and, on failure, removes only a directory *it*
538/// created, never this pre-existing guard dir (haematite 0.4.1
539/// `db/startup.rs`), so the `TempDir` is the sole owner of directory lifetime on
540/// every path.
541///
542/// # Errors
543/// Returns [`DurabilityError::EphemeralStoreOpen`] if haematite cannot create the
544/// database; the temporary directory is already removed when this returns.
545pub fn open_ephemeral(shard_count: usize) -> Result<EphemeralHaematiteStore, DurabilityError> {
546    open_ephemeral_in(ephemeral_tempdir(None)?, shard_count)
547}
548
549/// TEST SEAM: [`open_ephemeral`] with the temporary directory placed under
550/// `root` instead of the system temp dir.
551///
552/// Rooting lets construction gates assert on an isolated directory instead of
553/// scanning the shared temp dir. Same lifecycle contract as
554/// [`open_ephemeral`] — the store owns and removes its directory; `root` must
555/// already exist and must outlive the store.
556///
557/// That last requirement is why this is NOT a production API: the store's
558/// exclusive ownership of its directory (the D3 invariant) says nothing about
559/// the PARENT — a caller rooting the store inside a directory they own via
560/// their own guard can drop that guard while the store is live, deleting the
561/// database out from under its running workers. A general rooted API would
562/// need a root-ownership token so parent cleanup cannot outrun the store;
563/// that is deferred until a real embedder need arrives. Until then the
564/// function is gated to tests (`cfg(test)` in this crate, the default-off
565/// `test-support` feature for downstream test harnesses).
566///
567/// # Errors
568/// Returns [`DurabilityError::EphemeralStoreOpen`] if the directory cannot be
569/// created under `root` or haematite cannot create the database; no residue
570/// remains under `root` when this returns an error.
571#[cfg(any(test, feature = "test-support"))]
572pub fn open_ephemeral_rooted(
573    root: &Path,
574    shard_count: usize,
575) -> Result<EphemeralHaematiteStore, DurabilityError> {
576    open_ephemeral_in(ephemeral_tempdir(Some(root))?, shard_count)
577}
578
579/// Creates the guard directory for an ephemeral store, under `root` when given
580/// and under the system temp dir otherwise.
581fn ephemeral_tempdir(root: Option<&Path>) -> Result<TempDir, DurabilityError> {
582    let mut builder = tempfile::Builder::new();
583    builder.prefix("liminal-durability-");
584    root.map_or_else(|| builder.tempdir(), |root| builder.tempdir_in(root))
585        .map_err(|error| {
586            DurabilityError::EphemeralStoreOpen(format!(
587                "could not create temporary directory: {error}"
588            ))
589        })
590}
591
592/// Opens an ephemeral store inside an already-created guard directory.
593///
594/// Split out so the guard exists before `Database::create` and so lifecycle
595/// tests can inject an open failure into a directory they pre-populated.
596fn open_ephemeral_in(
597    ephemeral_dir: TempDir,
598    shard_count: usize,
599) -> Result<EphemeralHaematiteStore, DurabilityError> {
600    let database = Database::create(DatabaseConfig {
601        data_dir: ephemeral_dir.path().to_path_buf(),
602        shard_count,
603        distributed: None,
604        executor_threads: None,
605    })
606    .map_err(|error| DurabilityError::EphemeralStoreOpen(error.to_string()))?;
607    Ok(EphemeralHaematiteStore::new(database, ephemeral_dir))
608}
609
610/// Engine-read accounting for the paged-read shape (#60).
611///
612/// Counts what the ENGINE handed back, which is the quantity the page limit is
613/// supposed to bound. A `DurableStore` decorator cannot see it: by the time a
614/// wrapper observes the result it has already been cut to `limit`, so the
615/// difference between "read one page" and "read the whole suffix and throw it
616/// away" is invisible from outside this type.
617#[cfg(test)]
618#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
619pub(crate) struct EngineReadAccounting {
620    /// `read_from` calls made while the guard was live.
621    pub(crate) calls: usize,
622    /// Entries the engine returned, summed before any truncation to `limit`.
623    pub(crate) engine_entries: usize,
624    /// Calls that fell through to the unbounded engine read.
625    pub(crate) unbounded_calls: usize,
626    /// Set when a counter would have wrapped; a saturated count is never a pin.
627    pub(crate) counter_overflow_observed: bool,
628}
629
630#[cfg(test)]
631std::thread_local! {
632    static ENGINE_READ_ACCOUNTING: std::cell::RefCell<Option<EngineReadAccounting>> =
633        const { std::cell::RefCell::new(None) };
634}
635
636/// Scopes engine-read accounting to one thread and one measured region.
637///
638/// `!Send` so accounting cannot straddle a thread boundary and report a sum
639/// whose addends came from different call stacks.
640#[cfg(test)]
641pub(crate) struct EngineReadAccountingGuard {
642    _not_send: std::marker::PhantomData<*const ()>,
643}
644
645#[cfg(test)]
646impl EngineReadAccountingGuard {
647    pub(crate) fn start() -> Self {
648        ENGINE_READ_ACCOUNTING.with(|accounting| {
649            *accounting.borrow_mut() = Some(EngineReadAccounting::default());
650        });
651        Self {
652            _not_send: std::marker::PhantomData,
653        }
654    }
655
656    #[allow(clippy::unused_self)]
657    pub(crate) fn snapshot(&self) -> EngineReadAccounting {
658        ENGINE_READ_ACCOUNTING
659            .with(|accounting| accounting.borrow().as_ref().copied().unwrap_or_default())
660    }
661}
662
663#[cfg(test)]
664impl Drop for EngineReadAccountingGuard {
665    fn drop(&mut self) {
666        ENGINE_READ_ACCOUNTING.with(|accounting| {
667            *accounting.borrow_mut() = None;
668        });
669    }
670}
671
672/// Records one engine read. A no-op when no guard is live.
673#[cfg(test)]
674fn account_engine_read(engine_entries: usize, unbounded: bool) {
675    ENGINE_READ_ACCOUNTING.with(|accounting| {
676        if let Some(active) = accounting.borrow_mut().as_mut() {
677            match (
678                active.calls.checked_add(1),
679                active.engine_entries.checked_add(engine_entries),
680            ) {
681                (Some(calls), Some(entries)) => {
682                    active.calls = calls;
683                    active.engine_entries = entries;
684                }
685                _ => active.counter_overflow_observed = true,
686            }
687            if unbounded {
688                match active.unbounded_calls.checked_add(1) {
689                    Some(unbounded_calls) => active.unbounded_calls = unbounded_calls,
690                    None => active.counter_overflow_observed = true,
691                }
692            }
693        }
694    });
695}
696
697#[cfg(not(test))]
698const fn account_engine_read(_engine_entries: usize, _unbounded: bool) {}
699
700impl From<Event> for StoredEntry {
701    fn from(event: Event) -> Self {
702        Self {
703            payload: event.payload,
704            sequence: event.seq,
705            timestamp: event.timestamp,
706        }
707    }
708}
709
710/// Maps a real-engine [`ApiError`] onto liminal's [`DurabilityError`].
711///
712/// The optimistic-concurrency variants route to their dedicated `DurabilityError`
713/// cases (`SequenceConflict`, `CursorRegression`); everything else is a
714/// store-level failure carried verbatim.
715impl From<ApiError> for DurabilityError {
716    fn from(error: ApiError) -> Self {
717        match error {
718            ApiError::SequenceConflict(conflict) => conflict.into(),
719            ApiError::CasMismatch(mismatch) => mismatch.into(),
720            other @ (ApiError::CorruptEvent(_)
721            | ApiError::Storage(_)
722            | ApiError::HistoryCompacted(_)) => Self::StoreError(other),
723        }
724    }
725}
726
727#[cfg(test)]
728#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
729mod ephemeral_lifecycle_tests {
730    //! D3 §9 lifecycle gate. Each test names the gate it pins; all are permanent
731    //! rule-1 assertions that the ephemeral store's directory has an enforced
732    //! owner across every teardown path.
733
734    use std::path::{Path, PathBuf};
735    use std::sync::{Arc, Mutex};
736
737    use super::super::bridge::block_on;
738    use super::{
739        DurableStore, EphemeralGuard, open_ephemeral, open_ephemeral_in, open_ephemeral_rooted,
740    };
741
742    const TEST_SHARD_COUNT: usize = 2;
743
744    /// In-memory `tracing` sink, so a test can assert on what the teardown path
745    /// LOGGED rather than on what it merely did.
746    ///
747    /// Every teardown assertion below runs against this one instrument, and
748    /// [`panic_path_leak_is_logged_with_its_path`] is its positive control: it
749    /// exercises the SAME predicate (`captured` contains the path and `ERROR`)
750    /// against a log line that is emitted today. Without that control an empty
751    /// capture would only measure the harness.
752    #[derive(Clone, Default)]
753    struct CapturedLog(Arc<Mutex<Vec<u8>>>);
754
755    impl CapturedLog {
756        /// Everything written to the sink so far, as text.
757        fn text(&self) -> String {
758            let bytes = self
759                .0
760                .lock()
761                .expect("capture buffer is not poisoned")
762                .clone();
763            String::from_utf8(bytes).expect("tracing's fmt writer emits utf-8")
764        }
765
766        /// Runs `body` with this sink receiving everything the CURRENT THREAD
767        /// logs, via one process-global subscriber and a thread-routed writer.
768        ///
769        /// Why not `tracing::subscriber::with_default`: a scoped subscriber
770        /// registers a dispatcher on entry and deregisters it on exit, and
771        /// tracing maintains global state (the per-callsite interest cache and
772        /// the max-level hint) that is rebuilt on those edges. That produced a
773        /// measured intermittently-EMPTY capture in this module — 3/40
774        /// module-scoped runs raw; serializing the windows on a mutex cured
775        /// the module-scoped loop (0/40) but the full-workspace battery still
776        /// reproduced the empty capture with the mutex in place, so edge
777        /// timing was not the whole mechanism. This design removes the CLASS:
778        /// the global subscriber is installed exactly once and never
779        /// deregistered, so no edge ever exists to re-poison the caches, and
780        /// routing is thread-local so parallel tests cannot cross-capture.
781        fn capturing<R>(&self, body: impl FnOnce() -> R) -> R {
782            static INSTALL: std::sync::Once = std::sync::Once::new();
783            /// Clears the thread's capture slot even when `body` unwinds.
784            struct ResetOnDrop;
785            impl Drop for ResetOnDrop {
786                fn drop(&mut self) {
787                    ACTIVE_CAPTURE.with(|slot| *slot.borrow_mut() = None);
788                }
789            }
790            INSTALL.call_once(|| {
791                let subscriber = tracing_subscriber::fmt()
792                    .with_writer(RoutedWriter)
793                    .with_ansi(false)
794                    .finish();
795                tracing::subscriber::set_global_default(subscriber)
796                    .expect("no other global tracing subscriber is installed in this test binary");
797            });
798            ACTIVE_CAPTURE.with(|slot| *slot.borrow_mut() = Some(self.clone()));
799            let _reset = ResetOnDrop;
800            body()
801        }
802    }
803
804    thread_local! {
805        /// The capture buffer receiving THIS thread's log output, if a
806        /// [`CapturedLog::capturing`] window is active on it.
807        static ACTIVE_CAPTURE: std::cell::RefCell<Option<CapturedLog>> =
808            const { std::cell::RefCell::new(None) };
809    }
810
811    /// The one writer the process-global subscriber owns: appends to the
812    /// emitting thread's active capture buffer, and silently discards output
813    /// from threads with no capture window open.
814    #[derive(Clone, Copy, Default)]
815    struct RoutedWriter;
816
817    impl std::io::Write for RoutedWriter {
818        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
819            ACTIVE_CAPTURE.with(|slot| {
820                if let Some(capture) = slot.borrow().as_ref() {
821                    capture
822                        .0
823                        .lock()
824                        .map_err(|_| std::io::Error::other("capture buffer poisoned"))?
825                        .extend_from_slice(buf);
826                }
827                Ok(buf.len())
828            })
829        }
830
831        fn flush(&mut self) -> std::io::Result<()> {
832            Ok(())
833        }
834    }
835
836    impl<'writer> tracing_subscriber::fmt::MakeWriter<'writer> for RoutedWriter {
837        type Writer = Self;
838
839        fn make_writer(&'writer self) -> Self::Writer {
840            *self
841        }
842    }
843
844    /// Sets `path`'s mode, used to make a parent directory unwritable so that
845    /// removing a directory INSIDE it fails at the final `rmdir`.
846    ///
847    /// That is the observed production failure shape: the contents go, the
848    /// directory itself stays, and the removal error is the only witness.
849    #[cfg(unix)]
850    fn set_mode(path: &Path, mode: u32) {
851        use std::os::unix::fs::PermissionsExt;
852
853        std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode))
854            .expect("test can set permissions on a directory it created");
855    }
856
857    /// Store stand-in whose `Drop` pins the guard's internal ordering: the
858    /// directory must still exist at store-drop time, so this drop FAILS the
859    /// test if the guard ever removes the directory first.
860    struct OrderProbeStore {
861        dir: PathBuf,
862    }
863
864    impl Drop for OrderProbeStore {
865        fn drop(&mut self) {
866            assert!(
867                self.dir.exists(),
868                "the guard must drop the store BEFORE removing the directory"
869            );
870        }
871    }
872
873    /// Store stand-in whose `Drop` panics, modelling a haematite worker failing
874    /// to join while the database closes.
875    struct PanickingProbeStore;
876
877    impl Drop for PanickingProbeStore {
878        fn drop(&mut self) {
879            panic!("injected store-drop panic");
880        }
881    }
882
883    /// Materialises shard directories and fds so the drop path actually has a
884    /// live database to close before the guard removes the directory.
885    fn write_one_event(store: &dyn DurableStore) {
886        block_on(store.append("lifecycle/probe", b"payload".to_vec(), 0))
887            .expect("bridge completes synchronously")
888            .expect("append to a fresh ephemeral stream succeeds");
889        block_on(store.flush())
890            .expect("bridge completes synchronously")
891            .expect("flush of a live ephemeral store succeeds");
892    }
893
894    /// §9 gate — normal drop: the directory is removed once the last (here, only)
895    /// handle drops.
896    #[test]
897    fn ephemeral_dir_removed_after_last_handle_drops() {
898        let store = open_ephemeral(TEST_SHARD_COUNT).expect("ephemeral open succeeds");
899        let dir = store
900            .ephemeral_dir_path()
901            .expect("ephemeral store carries a guard dir")
902            .to_path_buf();
903        assert!(
904            dir.exists(),
905            "the guard directory exists while the store is live"
906        );
907
908        write_one_event(&store);
909        drop(store);
910
911        assert!(
912            !dir.exists(),
913            "the guard directory is removed on normal drop"
914        );
915    }
916
917    /// §9 gate — teardown with store-handle clones alive: the directory survives
918    /// until the LAST `Arc<dyn DurableStore>` clone drops, then is removed. This
919    /// is the `Arc`-shared-into-channel-handles case: clones share one wrapper,
920    /// so none can close the database early.
921    #[test]
922    fn ephemeral_dir_survives_until_last_store_clone_drops() {
923        let store = open_ephemeral(TEST_SHARD_COUNT).expect("ephemeral open succeeds");
924        let dir = store
925            .ephemeral_dir_path()
926            .expect("ephemeral store carries a guard dir")
927            .to_path_buf();
928        write_one_event(&store);
929
930        let erased: Arc<dyn DurableStore> = Arc::new(store);
931        let clone_a = Arc::clone(&erased);
932        let clone_b = Arc::clone(&erased);
933
934        drop(erased);
935        assert!(
936            dir.exists(),
937            "directory survives while store clones remain alive"
938        );
939        drop(clone_a);
940        assert!(
941            dir.exists(),
942            "directory survives while one store clone remains alive"
943        );
944
945        drop(clone_b);
946        assert!(
947            !dir.exists(),
948            "the last store clone dropping removes the directory"
949        );
950    }
951
952    /// §9 gate — startup rollback: an injected haematite open failure (a
953    /// conflicting `config.json` pre-seeded into the guard dir) makes the
954    /// constructor return `Err` AND leaves zero residue — the guard removes the
955    /// directory independently of haematite's own cleanup.
956    #[test]
957    fn ephemeral_open_failure_rolls_back_directory() {
958        let seeded = tempfile::Builder::new()
959            .prefix("liminal-durability-test-")
960            .tempdir()
961            .expect("test can create a temp dir");
962        let dir = seeded.path().to_path_buf();
963        // A pre-existing `config.json` makes haematite refuse the create with
964        // `DataDirAlreadyInitialised`; because the dir pre-existed the create,
965        // haematite never removes it — only the guard does.
966        std::fs::write(dir.join("config.json"), b"not-a-valid-config")
967            .expect("test can seed a conflicting config");
968
969        let result = open_ephemeral_in(seeded, TEST_SHARD_COUNT);
970
971        assert!(result.is_err(), "an injected open failure returns Err");
972        assert!(
973            !dir.exists(),
974            "the guard removes the directory on open failure — zero residue"
975        );
976    }
977
978    /// §9 gate — repeated start/stop: each cycle owns a distinct directory and
979    /// leaves zero residue after it drops.
980    #[test]
981    fn repeated_ephemeral_cycles_each_own_distinct_dir_zero_residue() {
982        let mut seen: Vec<PathBuf> = Vec::new();
983        for _ in 0..5 {
984            let store = open_ephemeral(TEST_SHARD_COUNT).expect("ephemeral open succeeds");
985            let dir = store
986                .ephemeral_dir_path()
987                .expect("ephemeral store carries a guard dir")
988                .to_path_buf();
989            assert!(
990                dir.exists(),
991                "the cycle's directory exists while its store is live"
992            );
993            assert!(!seen.contains(&dir), "each cycle owns a distinct directory");
994            seen.push(dir.clone());
995
996            write_one_event(&store);
997            drop(store);
998            assert!(
999                !dir.exists(),
1000                "the cycle's directory is removed after its store drops"
1001            );
1002        }
1003    }
1004
1005    /// §9 gate (drop-order pin): the guard drops the store strictly before it
1006    /// removes the directory. `OrderProbeStore::drop` asserts the directory
1007    /// still exists, so reversing the order inside [`EphemeralGuard`] fails this
1008    /// test rather than silently passing.
1009    #[test]
1010    fn guard_drops_store_before_removing_directory() {
1011        let dir = tempfile::tempdir().expect("test can create a temp dir");
1012        let path = dir.path().to_path_buf();
1013        let guard = EphemeralGuard {
1014            store: Some(OrderProbeStore { dir: path.clone() }),
1015            dir: Some(dir),
1016        };
1017
1018        drop(guard);
1019
1020        assert!(!path.exists(), "a clean drop still removes the directory");
1021    }
1022
1023    /// §9 gate (unwind pin): a panic while the store drops leaves the directory
1024    /// LEAKED, never removed under possibly-live workers, and the panic still
1025    /// propagates.
1026    #[test]
1027    fn guard_leaks_directory_when_store_drop_panics() {
1028        let dir = tempfile::tempdir().expect("test can create a temp dir");
1029        let path = dir.path().to_path_buf();
1030        let guard = EphemeralGuard {
1031            store: Some(PanickingProbeStore),
1032            dir: Some(dir),
1033        };
1034
1035        let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || drop(guard)));
1036
1037        assert!(unwound.is_err(), "the injected store-drop panic propagates");
1038        assert!(
1039            path.exists(),
1040            "a panicking store drop leaks the directory instead of removing it"
1041        );
1042        std::fs::remove_dir_all(&path).expect("test cleans up the deliberately leaked directory");
1043    }
1044
1045    /// The rooted factory places (and removes) the guard directory under the
1046    /// caller-supplied root, which is what lets construction gates assert on an
1047    /// isolated root instead of scanning the system temp dir.
1048    #[test]
1049    fn rooted_ephemeral_store_lives_and_dies_under_the_given_root() {
1050        let root = tempfile::tempdir().expect("test can create a temp root");
1051        let store =
1052            open_ephemeral_rooted(root.path(), TEST_SHARD_COUNT).expect("rooted open succeeds");
1053        let dir = store
1054            .ephemeral_dir_path()
1055            .expect("ephemeral store carries a guard dir")
1056            .to_path_buf();
1057        assert!(
1058            dir.starts_with(root.path()),
1059            "the guard directory is created under the supplied root"
1060        );
1061
1062        write_one_event(&store);
1063        drop(store);
1064
1065        assert!(!dir.exists(), "the rooted directory is removed on drop");
1066    }
1067
1068    /// Clean-teardown gate (keepalive-honest shape): the guard directory is
1069    /// present for the store's WHOLE life — re-checked between unrelated
1070    /// operations that each succeed — and gone once the store drops cleanly.
1071    ///
1072    /// The "unrelated ops proceed" leg is what makes the final absence mean
1073    /// something: a directory that vanished early would take the appends,
1074    /// reads and CAS down with it, so this cannot pass by removing the
1075    /// directory too soon and cannot pass by never having created it.
1076    #[test]
1077    fn ephemeral_dir_persists_across_unrelated_work_then_goes_on_clean_drop() {
1078        let store = open_ephemeral(TEST_SHARD_COUNT).expect("ephemeral open succeeds");
1079        let dir = store
1080            .ephemeral_dir_path()
1081            .expect("ephemeral store carries a guard dir")
1082            .to_path_buf();
1083        assert!(
1084            dir.exists(),
1085            "the directory exists as soon as the store does"
1086        );
1087
1088        for round in 0..3_u64 {
1089            block_on(store.append("clean-teardown/probe", b"payload".to_vec(), round))
1090                .expect("bridge completes synchronously")
1091                .expect("append to a live ephemeral store succeeds");
1092            assert!(
1093                dir.exists(),
1094                "the directory is still there after append round {round}"
1095            );
1096        }
1097        block_on(store.cas("clean-teardown/counter", 0, 7))
1098            .expect("bridge completes synchronously")
1099            .expect("cas on a live ephemeral store succeeds");
1100        let entries = block_on(store.read_from("clean-teardown/probe", 0, 10))
1101            .expect("bridge completes synchronously")
1102            .expect("read from a live ephemeral store succeeds");
1103        assert_eq!(entries.len(), 3, "every appended entry is readable back");
1104        assert!(
1105            dir.exists(),
1106            "the directory is still there after unrelated cas and read work"
1107        );
1108
1109        block_on(store.flush())
1110            .expect("bridge completes synchronously")
1111            .expect("flush of a live ephemeral store succeeds");
1112        drop(store);
1113
1114        assert!(
1115            !dir.exists(),
1116            "the clean drop removes the directory it kept alive throughout"
1117        );
1118    }
1119
1120    /// Clean-teardown gate: when removal FAILS on the clean path the guard
1121    /// LOGS the failure and its path, and does not panic.
1122    ///
1123    /// Injected the way it fails in production: the parent is made unwritable,
1124    /// so `remove_dir_all` clears the contents and then cannot unlink the
1125    /// directory itself. `tempfile`'s own `Drop` discards that error
1126    /// (`let _ = remove_dir_all(..)`), which is why this pin is red until the
1127    /// clean path calls `close()` and reports what it returns.
1128    #[cfg(unix)]
1129    #[test]
1130    fn clean_drop_removal_failure_is_logged_and_never_panics() {
1131        let parent = tempfile::tempdir().expect("test can create a temp parent");
1132        let dir = tempfile::Builder::new()
1133            .prefix("liminal-durability-")
1134            .tempdir_in(parent.path())
1135            .expect("test can create a guard dir under the parent");
1136        let path = dir.path().to_path_buf();
1137        let guard = EphemeralGuard {
1138            store: Some(()),
1139            dir: Some(dir),
1140        };
1141
1142        set_mode(parent.path(), 0o500);
1143        let captured = CapturedLog::default();
1144        let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1145            captured.capturing(|| drop(guard));
1146        }));
1147        // Restored before the assertions so a failing assertion still leaves a
1148        // parent the outer `TempDir` can clean up.
1149        set_mode(parent.path(), 0o700);
1150
1151        assert!(
1152            outcome.is_ok(),
1153            "a removal failure is reported, never raised as a panic"
1154        );
1155        let logged = captured.text();
1156        assert!(
1157            logged.contains("ERROR"),
1158            "the removal failure is logged at error level; captured: {logged:?}"
1159        );
1160        assert!(
1161            logged.contains(&path.display().to_string()),
1162            "the log names the directory that survived; captured: {logged:?}"
1163        );
1164        assert!(
1165            path.exists(),
1166            "the residue is left where the log says it is, not silently claimed removed"
1167        );
1168    }
1169
1170    /// Clean-teardown gate (negative control for the capture instrument): a
1171    /// removal that SUCCEEDS logs nothing, so the assertion above discriminates
1172    /// failure from success rather than matching any teardown at all.
1173    #[test]
1174    fn clean_drop_that_succeeds_logs_nothing() {
1175        let dir = tempfile::tempdir().expect("test can create a temp dir");
1176        let path = dir.path().to_path_buf();
1177        let guard = EphemeralGuard {
1178            store: Some(()),
1179            dir: Some(dir),
1180        };
1181
1182        let captured = CapturedLog::default();
1183        captured.capturing(|| drop(guard));
1184
1185        assert!(!path.exists(), "the successful clean drop removed the dir");
1186        assert!(
1187            captured.text().is_empty(),
1188            "a successful removal is silent; captured: {:?}",
1189            captured.text()
1190        );
1191    }
1192
1193    /// Positive control for the capture instrument: the panic path's sanctioned
1194    /// leak line IS captured, path and all, by the same predicate the
1195    /// removal-failure gate uses.
1196    ///
1197    /// Without this, an empty capture would be a measurement of the harness
1198    /// rather than of the code under test.
1199    #[test]
1200    fn panic_path_leak_is_logged_with_its_path() {
1201        let dir = tempfile::tempdir().expect("test can create a temp dir");
1202        let path = dir.path().to_path_buf();
1203        let guard = EphemeralGuard {
1204            store: Some(PanickingProbeStore),
1205            dir: Some(dir),
1206        };
1207
1208        let captured = CapturedLog::default();
1209        let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1210            captured.capturing(|| drop(guard));
1211        }));
1212
1213        assert!(unwound.is_err(), "the injected store-drop panic propagates");
1214        let logged = captured.text();
1215        assert!(
1216            logged.contains("ERROR"),
1217            "the sanctioned leak is logged at error level; captured: {logged:?}"
1218        );
1219        assert!(
1220            logged.contains(&path.display().to_string()),
1221            "the leak log names the leaked directory; captured: {logged:?}"
1222        );
1223        std::fs::remove_dir_all(&path).expect("test cleans up the deliberately leaked directory");
1224    }
1225}
1226
1227#[cfg(test)]
1228#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
1229mod paged_read_shape_tests {
1230    //! Board #60. The page limit must be honoured by the ENGINE, not by a
1231    //! truncation applied after the engine has already materialised the suffix.
1232    //!
1233    //! These pins are counts, never durations: the defect is a read shape, and
1234    //! a shape is deterministic where a latency is not.
1235
1236    use super::{DurableStore, EngineReadAccountingGuard, open_ephemeral};
1237    use crate::durability::bridge::block_on;
1238
1239    /// Page size used by both production replay readers (`READ_BATCH_SIZE` and
1240    /// `UNIT2_OUTBOX_RESTORE_BATCH_ROWS` are both 64).
1241    const PAGE: usize = 64;
1242    /// Four pages. Enough that the quadratic and the linear shape differ by
1243    /// more than a factor of two, small enough that seeding stays cheap.
1244    const ROWS: u64 = 256;
1245    const STREAM: &str = "liminal/p0-60/paged-read-shape";
1246
1247    /// Seeds `ROWS` events into one stream.
1248    fn seeded() -> Result<impl DurableStore, Box<dyn std::error::Error>> {
1249        let store = open_ephemeral(1)?;
1250        for sequence in 0..ROWS {
1251            block_on(store.append(STREAM, sequence.to_be_bytes().to_vec(), sequence))??;
1252        }
1253        block_on(store.flush())??;
1254        Ok(store)
1255    }
1256
1257    /// Walks the whole stream one page at a time, exactly as replay does.
1258    fn read_whole_stream(
1259        store: &impl DurableStore,
1260        page: usize,
1261    ) -> Result<usize, Box<dyn std::error::Error>> {
1262        let mut offset = 0_u64;
1263        let mut seen = 0_usize;
1264        loop {
1265            let entries = block_on(store.read_from(STREAM, offset, page))??;
1266            if entries.is_empty() {
1267                return Ok(seen);
1268            }
1269            for entry in &entries {
1270                assert_eq!(entry.sequence, offset, "paged read must stay contiguous");
1271                offset += 1;
1272            }
1273            seen = seen
1274                .checked_add(entries.len())
1275                .ok_or("row counter overflowed")?;
1276        }
1277    }
1278
1279    /// Every read below is bounded by its `limit`, so no read costs more than
1280    /// the rows it returns. One seeded store carries all four shapes.
1281    #[test]
1282    fn a_bounded_read_never_scans_beyond_its_page() -> Result<(), Box<dyn std::error::Error>> {
1283        let store = seeded()?;
1284
1285        // 1. The whole stream, paged. O(N), not O(N^2).
1286        let accounting = EngineReadAccountingGuard::start();
1287        let seen = read_whole_stream(&store, PAGE)?;
1288        let walk = accounting.snapshot();
1289        drop(accounting);
1290        assert_eq!(
1291            u64::try_from(seen)?,
1292            ROWS,
1293            "the walk must deliver every row"
1294        );
1295        assert!(
1296            !walk.counter_overflow_observed,
1297            "a saturated counter is not a measurement"
1298        );
1299        assert!(walk.calls > 0, "the walk must have reached the store");
1300        assert_eq!(
1301            u64::try_from(walk.engine_entries)?,
1302            ROWS,
1303            "a full stream read must scan each row exactly once instead of \
1304             re-scanning every suffix once per page"
1305        );
1306
1307        // 2. One page from the head.
1308        let accounting = EngineReadAccountingGuard::start();
1309        let head = block_on(store.read_from(STREAM, 0, PAGE))??;
1310        let head_read = accounting.snapshot();
1311        drop(accounting);
1312        assert_eq!(head.len(), PAGE, "a full page returns its limit");
1313        assert_eq!(
1314            head_read.engine_entries, PAGE,
1315            "the engine must be asked for one page, not for the whole stream"
1316        );
1317
1318        // 3. One page from the MIDDLE. The rows after the page are the ones a
1319        //    suffix-scanning read would drag along; the rows before it are the
1320        //    ones the offset already excludes, so only a bounded upper edge can
1321        //    make this count come out at PAGE.
1322        let middle_offset = ROWS / 2;
1323        let accounting = EngineReadAccountingGuard::start();
1324        let middle = block_on(store.read_from(STREAM, middle_offset, PAGE))??;
1325        let middle_read = accounting.snapshot();
1326        drop(accounting);
1327        assert_eq!(
1328            middle.len(),
1329            PAGE,
1330            "a full page mid-stream returns its limit"
1331        );
1332        assert_eq!(
1333            middle_read.engine_entries, PAGE,
1334            "a mid-stream page must not scan the rows that follow it"
1335        );
1336
1337        // 4. Past the head: end of stream, and no scan.
1338        let accounting = EngineReadAccountingGuard::start();
1339        let past = block_on(store.read_from(STREAM, ROWS, PAGE))??;
1340        let past_read = accounting.snapshot();
1341        drop(accounting);
1342        assert!(past.is_empty(), "past the head is end of stream");
1343        assert_eq!(
1344            past_read.engine_entries, 0,
1345            "an end-of-stream page must not scan the stream"
1346        );
1347        Ok(())
1348    }
1349
1350    /// The equivalence the pushdown must preserve. This passes before and after
1351    /// the fix by design: it is the control that says the fix changed the read
1352    /// SHAPE and nothing else.
1353    #[test]
1354    fn page_size_never_changes_the_answer() -> Result<(), Box<dyn std::error::Error>> {
1355        let store = seeded()?;
1356        let whole = block_on(store.read_from(STREAM, 0, usize::MAX))??;
1357        assert_eq!(u64::try_from(whole.len())?, ROWS);
1358
1359        for page in [1_usize, 7, 64, 255, 256, 257] {
1360            let mut offset = 0_u64;
1361            let mut collected = Vec::new();
1362            loop {
1363                let entries = block_on(store.read_from(STREAM, offset, page))??;
1364                if entries.is_empty() {
1365                    break;
1366                }
1367                assert!(entries.len() <= page, "a page never exceeds its limit");
1368                offset = offset
1369                    .checked_add(u64::try_from(entries.len())?)
1370                    .ok_or("offset overflowed")?;
1371                collected.extend(entries);
1372            }
1373            assert_eq!(collected, whole, "page size {page} changed the answer");
1374        }
1375
1376        // A zero limit is the one page size that must return nothing, and it
1377        // must not be answered by a bounded window that silently agrees.
1378        assert!(
1379            block_on(store.read_from(STREAM, 0, 0))??.is_empty(),
1380            "a zero limit reads nothing"
1381        );
1382
1383        // Every suffix start agrees with the same suffix of the whole read.
1384        for offset in [0_u64, 1, 63, 64, 65, 128, 255] {
1385            let suffix = block_on(store.read_from(STREAM, offset, usize::MAX))??;
1386            assert_eq!(
1387                suffix,
1388                whole[usize::try_from(offset)?..],
1389                "suffix from {offset} diverged"
1390            );
1391        }
1392        Ok(())
1393    }
1394}