hawdb 0.31.0

An embedded Rust graph database for local data-plane workloads
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

//! Initial search-projection import with deferred durability
//! (`nowledge-co/hawdb#1707`).
//!
//! These tests pin the split between `apply_initial_import_projection_delta`
//! (resident only) and `checkpoint_initial_import_projection` (one full
//! rewrite): equivalence with per-page checkpoints, the crash/resume contract,
//! and the write volume of a geometric checkpoint schedule.

use super::*;
use crate::{SearchCheckpointReport, SearchDocument, SearchHit};

const IMPORT_SOURCE_EPOCH: u64 = 77;
const EMBEDDING_DIMENSION: usize = 8;
const QUERIES: [&str; 4] = [
    "alpha river",
    "copper lantern signal",
    "delta",
    "orbit mesa",
];
const VOCABULARY: [&str; 24] = [
    "alpha", "bravo", "copper", "delta", "ember", "fjord", "garnet", "harbor", "iris", "jade",
    "kestrel", "lantern", "meadow", "nectar", "orbit", "prism", "quartz", "river", "signal",
    "tundra", "umber", "vapor", "willow", "mesa",
];

struct ImportFixture {
    root: std::path::PathBuf,
    options: EmbeddedStoreOpenOptions,
}

impl ImportFixture {
    fn new(name: &str) -> Self {
        let root = unique_embedded_store_test_dir(name);
        let options = EmbeddedStoreOpenOptions::with_search_projection(
            root.join("graph"),
            root.join("search"),
            GraphMode::WritableCutover,
        );
        let (mut store, _) = EmbeddedStore::open_with_options(options.clone()).unwrap();
        store
            .graph_mut()
            .query("CREATE (:Memory {id: 'anchor', title: 'Import anchor'})")
            .unwrap();
        drop(store);
        Self { root, options }
    }

    fn open(&self) -> EmbeddedStore {
        EmbeddedStore::open_with_options(self.options.clone())
            .unwrap()
            .0
    }
}

impl Drop for ImportFixture {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.root);
    }
}

fn mix(mut value: u64) -> u64 {
    value ^= value >> 33;
    value = value.wrapping_mul(0xff51_afd7_ed55_8ccd);
    value ^= value >> 33;
    value = value.wrapping_mul(0xc4ce_b9fe_1a85_ec53);
    value ^ (value >> 33)
}

fn import_row(index: usize, revision: u64) -> SearchProjectionRow {
    let seed = mix((index as u64) << 8 | revision);
    let words = 6 + (seed % 10) as usize;
    let body = (0..words)
        .map(|offset| VOCABULARY[(mix(seed.wrapping_add(offset as u64)) % 24) as usize])
        .collect::<Vec<_>>()
        .join(" ");
    let embedding = (0..EMBEDDING_DIMENSION)
        .map(|axis| (mix(seed ^ axis as u64) % 1000) as f32 / 1000.0)
        .collect();
    SearchProjectionRow {
        kind: SearchProjectionKind::Memory,
        external_id: format!("m{index:05}"),
        title: format!("{} {index}", VOCABULARY[index % 24]),
        body,
        embedding: Some(embedding),
        source_id: None,
        metadata: BTreeMap::from([("revision".to_string(), revision.to_string())]),
    }
}

/// Page `page` inserts `rows` new documents, rewrites one document of the
/// previous page, and deletes one document of the page before that, so replay
/// must be order-preserving and idempotent rather than insert-only.
fn import_page(page: usize, rows: usize) -> SearchProjectionDelta {
    let mut upserts = (page * rows..(page + 1) * rows)
        .map(|index| import_row(index, 0))
        .collect::<Vec<_>>();
    let mut deletes = Vec::new();
    if page >= 1 {
        upserts.push(import_row((page - 1) * rows, page as u64));
    }
    if page >= 2 {
        deletes.push(format!("memory:m{:05}", (page - 2) * rows + 1));
    }
    SearchProjectionDelta {
        upserts,
        deletes,
        max_operations: None,
        source_graph_commit_epoch: Some(IMPORT_SOURCE_EPOCH),
    }
}

fn hit_signature(hits: Vec<SearchHit>) -> Vec<(String, u64, u64, u64)> {
    hits.into_iter()
        .map(|hit| {
            (
                hit.id,
                hit.score.to_bits(),
                hit.text_score.to_bits(),
                hit.vector_score.to_bits(),
            )
        })
        .collect()
}

/// Observable projection state: every candidate document plus ranked text and
/// hybrid hits (ids and exact score bits) and the durable import provenance.
#[derive(Debug, PartialEq)]
struct ProjectionState {
    documents: Vec<Option<SearchDocument>>,
    hits: Vec<Vec<(String, u64, u64, u64)>>,
    import_source_graph_commit_epoch: Option<u64>,
}

fn projection_state(store: &EmbeddedStore, max_documents: usize) -> ProjectionState {
    let index = store.search_projection().unwrap().index();
    let documents = (0..max_documents)
        .map(|id| index.document(&format!("memory:m{id:05}")).cloned())
        .collect();
    // Rank with every retriever this feature profile compiles in.
    let probe = import_row(3, 0).embedding.unwrap();
    let mut modes = Vec::new();
    if cfg!(feature = "full-text-search") {
        modes.push((SearchMode::Text, None));
    }
    if cfg!(feature = "vector-search") {
        modes.push((SearchMode::Vector, Some(probe.as_slice())));
    }
    if cfg!(all(feature = "full-text-search", feature = "vector-search")) {
        modes.push((SearchMode::Hybrid, Some(probe.as_slice())));
    }
    let mut hits = Vec::new();
    for query in QUERIES {
        for (mode, embedding) in &modes {
            hits.push(hit_signature(
                index.search(query, *embedding, *mode, 20).unwrap(),
            ));
        }
    }
    ProjectionState {
        documents,
        hits,
        import_source_graph_commit_epoch: index
            .projection_freshness()
            .import_source_graph_commit_epoch,
    }
}

fn checkpoint_bytes(report: &SearchCheckpointReport) -> u64 {
    report
        .snapshot_compressed_bytes
        .saturating_add(report.projection_bytes_written)
}

/// Host-side schedule recommended in the docs: checkpoint when the
/// un-checkpointed input reached the size already checkpointed (doubling), but
/// never let the un-checkpointed gap exceed `cap_bytes`.
fn geometric_checkpoint_due(pending: u64, checkpointed: u64, cap_bytes: Option<u64>) -> bool {
    pending >= checkpointed || cap_bytes.is_some_and(|cap| pending >= cap)
}

#[derive(Debug, Default)]
struct ImportRun {
    checkpoints: Vec<SearchCheckpointReport>,
    /// Pages covered by each checkpoint, i.e. the host's durable cursor.
    checkpoint_cursors: Vec<usize>,
    peak_pending_bytes: u64,
}

impl ImportRun {
    fn total_bytes(&self) -> u64 {
        self.checkpoints.iter().map(checkpoint_bytes).sum()
    }

    fn final_bytes(&self) -> u64 {
        self.checkpoints.last().map_or(0, checkpoint_bytes)
    }
}

/// Imports pages `start..end`, checkpointing after page `p` when `due` says
/// so and always after the last page when `finish` is set.
fn import_pages(
    store: &mut EmbeddedStore,
    pages: std::ops::Range<usize>,
    rows: usize,
    run: &mut ImportRun,
    checkpointed_bytes: &mut u64,
    mut due: impl FnMut(usize, u64, u64) -> bool,
    finish: bool,
) {
    let end = pages.end;
    for page in pages {
        store
            .apply_initial_import_projection_delta(import_page(page, rows), IMPORT_SOURCE_EPOCH)
            .unwrap();
        let pending = store.initial_import_projection_pending().unwrap();
        assert!(pending.uncheckpointed_deltas >= 1);
        run.peak_pending_bytes = run
            .peak_pending_bytes
            .max(pending.uncheckpointed_input_bytes);
        if due(
            page,
            pending.uncheckpointed_input_bytes,
            *checkpointed_bytes,
        ) || (finish && page + 1 == end)
        {
            run.checkpoints
                .push(store.checkpoint_initial_import_projection().unwrap());
            run.checkpoint_cursors.push(page + 1);
            *checkpointed_bytes += pending.uncheckpointed_input_bytes;
            assert_eq!(
                store.initial_import_projection_pending(),
                Some(Default::default())
            );
        }
    }
}

#[test]
fn initial_import_1707_deferred_checkpoint_matches_per_page_checkpoint() {
    const PAGES: usize = 12;
    const ROWS: usize = 25;
    let max_documents = PAGES * ROWS;

    let per_page = ImportFixture::new("initial_import_1707_per_page");
    let mut store = per_page.open();
    let mut per_page_reports = Vec::new();
    for page in 0..PAGES {
        per_page_reports.push(
            store
                .apply_initial_import_projection_delta_and_checkpoint(
                    import_page(page, ROWS),
                    IMPORT_SOURCE_EPOCH,
                )
                .unwrap(),
        );
        assert_eq!(
            store.initial_import_projection_pending(),
            Some(Default::default())
        );
    }
    let expected = projection_state(&store, max_documents);
    drop(store);

    let deferred = ImportFixture::new("initial_import_1707_deferred");
    let mut store = deferred.open();
    let mut deferred_reports = Vec::new();
    for page in 0..PAGES {
        deferred_reports.push(
            store
                .apply_initial_import_projection_delta(import_page(page, ROWS), IMPORT_SOURCE_EPOCH)
                .unwrap(),
        );
    }
    let pending = store.initial_import_projection_pending().unwrap();
    assert_eq!(pending.uncheckpointed_deltas, PAGES as u64);
    assert!(pending.uncheckpointed_input_bytes > 0);
    // The resident state is already searchable before durability.
    assert_eq!(projection_state(&store, max_documents), expected);
    let report = store.checkpoint_initial_import_projection().unwrap();
    assert_eq!(report.document_count, expected_document_count(PAGES, ROWS));
    assert_eq!(per_page_reports, deferred_reports);
    assert_eq!(projection_state(&store, max_documents), expected);
    drop(store);

    // Both durable projections reopen to the same state.
    assert_eq!(projection_state(&deferred.open(), max_documents), expected);
    assert_eq!(projection_state(&per_page.open(), max_documents), expected);
}

fn expected_document_count(pages: usize, rows: usize) -> usize {
    pages * rows - pages.saturating_sub(2)
}

#[test]
fn initial_import_1707_crash_resumes_from_last_checkpoint_without_skips_or_duplicates() {
    const PAGES: usize = 24;
    const ROWS: usize = 10;
    let max_documents = PAGES * ROWS;

    // Uninterrupted reference run with the geometric schedule.
    let reference = ImportFixture::new("initial_import_1707_reference");
    let mut store = reference.open();
    let mut reference_run = ImportRun::default();
    let mut checkpointed = 0;
    import_pages(
        &mut store,
        0..PAGES,
        ROWS,
        &mut reference_run,
        &mut checkpointed,
        |_, pending, checkpointed| geometric_checkpoint_due(pending, checkpointed, None),
        true,
    );
    let final_state = projection_state(&store, max_documents);
    drop(store);
    // Doubling checkpoints at pages 1, 2, 4, 8, 16 (approximately; the input
    // estimate is per byte) plus the end-of-import checkpoint.
    let last_doubling_cursor =
        reference_run.checkpoint_cursors[reference_run.checkpoint_cursors.len() - 2];
    assert!(
        (PAGES / 2..PAGES - 2).contains(&last_doubling_cursor),
        "{:?}",
        reference_run.checkpoint_cursors
    );

    // Crash points: before any checkpoint, inside an early gap, and inside the
    // last and largest gap (after the final doubling checkpoint, before the
    // end-of-import checkpoint), which loses up to about half the import.
    for (crash_after_pages, label) in [
        (1, "before_first"),
        (3, "early_gap"),
        (PAGES - 1, "last_gap"),
    ] {
        let fixture = ImportFixture::new(&format!("initial_import_1707_crash_{label}"));
        let handle = EmbeddedStoreHandle::new(fixture.open()).unwrap();
        // Host-persisted resume state, written only after a checkpoint
        // returned `Ok`: the page cursor and the checkpointed input bytes
        // that seed the doubling schedule.
        let mut durable_cursor = 0usize;
        let mut persisted_checkpointed_bytes = 0u64;
        let mut durable_state = None;
        let mut checkpointed = 0u64;
        let mut pending_at_crash = 0;
        for page in 0..crash_after_pages {
            handle
                .apply_initial_import_projection_delta(import_page(page, ROWS), IMPORT_SOURCE_EPOCH)
                .unwrap();
            let pending = handle.initial_import_projection_pending().unwrap().unwrap();
            pending_at_crash = pending.uncheckpointed_deltas;
            // `before_first` never checkpoints: the crash precedes page 1's
            // durability.
            if label != "before_first"
                && geometric_checkpoint_due(pending.uncheckpointed_input_bytes, checkpointed, None)
            {
                handle.checkpoint_initial_import_projection().unwrap();
                checkpointed += pending.uncheckpointed_input_bytes;
                // The host advances its durable cursor only after the
                // checkpoint acknowledged success.
                durable_cursor = page + 1;
                persisted_checkpointed_bytes = checkpointed;
                let store = handle.read_store().unwrap();
                durable_state = Some(projection_state(&store, max_documents));
            }
        }
        assert!(pending_at_crash >= 1, "{label}: crash must lose pages");
        if label == "last_gap" {
            assert_eq!(durable_cursor, last_doubling_cursor);
            assert_eq!(
                pending_at_crash as usize,
                crash_after_pages - durable_cursor
            );
        }
        // Crash: drop the process state without a checkpoint.
        drop(handle);

        let mut store = fixture.open();
        let recovered = projection_state(&store, max_documents);
        match &durable_state {
            Some(state) => assert_eq!(&recovered, state, "{label}"),
            None => {
                assert_eq!(durable_cursor, 0);
                assert!(recovered.documents.iter().all(Option::is_none));
                assert_eq!(recovered.import_source_graph_commit_epoch, None);
                assert_eq!(
                    store.search_projection().unwrap().index().document_count(),
                    0
                );
            }
        }
        if durable_cursor > 0 {
            assert_eq!(
                recovered.import_source_graph_commit_epoch,
                Some(IMPORT_SOURCE_EPOCH)
            );
        }
        assert_eq!(
            store.initial_import_projection_pending(),
            Some(Default::default())
        );

        // Resume from the durable cursor. Replaying the last durable page as
        // well models a host that crashed after the checkpoint but before it
        // persisted its cursor; replay is idempotent.
        let replay_from = durable_cursor.saturating_sub(1);
        let mut run = ImportRun::default();
        // Seed the doubling schedule from the persisted count, not from 0.
        let mut checkpointed = persisted_checkpointed_bytes;
        import_pages(
            &mut store,
            replay_from..PAGES,
            ROWS,
            &mut run,
            &mut checkpointed,
            |_, pending, checkpointed| geometric_checkpoint_due(pending, checkpointed, None),
            true,
        );
        assert_eq!(
            projection_state(&store, max_documents),
            final_state,
            "{label}"
        );
        assert_eq!(
            store.search_projection().unwrap().index().document_count(),
            expected_document_count(PAGES, ROWS)
        );
        drop(store);
        assert_eq!(
            projection_state(&fixture.open(), max_documents),
            final_state,
            "{label} after reopen"
        );

        // A seeded resume needs at most one more full rewrite than the
        // uninterrupted schedule still had left; restarting the count at 0
        // instead re-checkpoints after 1, 2, 4, ... replayed pages.
        let remaining_reference = reference_run
            .checkpoint_cursors
            .iter()
            .filter(|cursor| **cursor > durable_cursor)
            .count();
        assert_eq!(
            run.checkpoints.len(),
            scheduled_checkpoints(replay_from..PAGES, ROWS, persisted_checkpointed_bytes),
            "{label}"
        );
        assert!(
            run.checkpoints.len() <= remaining_reference + 1,
            "{label}: {} resumed checkpoints, {remaining_reference} remaining in the reference",
            run.checkpoints.len()
        );
        if label == "last_gap" {
            let unseeded = scheduled_checkpoints(replay_from..PAGES, ROWS, 0);
            assert!(
                unseeded >= run.checkpoints.len() + 2,
                "unseeded resume {unseeded} vs seeded {}",
                run.checkpoints.len()
            );
        }
    }
}

/// Checkpoints the doubling schedule takes over `pages` (plus the final one)
/// when seeded with `checkpointed` input bytes. Mirrors `import_pages`.
fn scheduled_checkpoints(
    pages: std::ops::Range<usize>,
    rows: usize,
    mut checkpointed: u64,
) -> usize {
    let end = pages.end;
    let mut pending = 0u64;
    let mut checkpoints = 0;
    for page in pages {
        pending +=
            crate::embedded_store::search_projection_delta_bytes(&import_page(page, rows)) as u64;
        if geometric_checkpoint_due(pending, checkpointed, None) || page + 1 == end {
            checkpoints += 1;
            checkpointed += pending;
            pending = 0;
        }
    }
    checkpoints
}

#[test]
fn initial_import_1711_crash_inside_checkpoint_reopens_previous_or_new() {
    // #1711: the resident checkpoint writes generation-named files, commits by
    // renaming the selector, then re-links the current names. Model a crash
    // on either side of the selector from the files of a completed checkpoint.
    const ROWS: usize = 10;
    const SELECTOR: &str = "search_projection.out_of_core.manifest.hawdb";
    let fixture = ImportFixture::new("initial_import_1711_crash_inside_checkpoint");
    let search = fixture.root.join("search");
    let previous = fixture.root.join("search_previous");
    let mut store = fixture.open();
    store
        .apply_initial_import_projection_delta(import_page(0, ROWS), IMPORT_SOURCE_EPOCH)
        .unwrap();
    store.checkpoint_initial_import_projection().unwrap();
    let first = projection_state(&store, 2 * ROWS);
    drop(store);
    copy_files(&search, &previous, |_| true);
    let mut store = fixture.open();
    store
        .apply_initial_import_projection_delta(import_page(1, ROWS), IMPORT_SOURCE_EPOCH)
        .unwrap();
    store.checkpoint_initial_import_projection().unwrap();
    let second = projection_state(&store, 2 * ROWS);
    drop(store);
    assert_ne!(first, second);
    let new_name = |name: &str| !previous.join(name).exists();

    for (name, after_selector) in [("before_selector", false), ("after_selector", true)] {
        let torn = fixture.root.join(format!("search_{name}"));
        copy_files(&previous, &torn, |_| true);
        // Every generation file of the next checkpoint is present. Before the
        // selector, the snapshot the old protocol replaced first is new too.
        copy_files(&search, &torn, |name| {
            new_name(name)
                || (after_selector && name == SELECTOR)
                || (!after_selector && name == "search_projection.hawdb")
        });
        let mut options = fixture.options.clone();
        options.search_projection_path = Some(torn.clone());
        let (mut store, _) = EmbeddedStore::open_with_options(options).unwrap();
        if after_selector {
            assert_eq!(projection_state(&store, 2 * ROWS), second);
            continue;
        }
        assert_eq!(projection_state(&store, 2 * ROWS), first);
        // The host's cursor still names page 0: replay page 1.
        store
            .apply_initial_import_projection_delta(import_page(1, ROWS), IMPORT_SOURCE_EPOCH)
            .unwrap();
        store.checkpoint_initial_import_projection().unwrap();
        assert_eq!(projection_state(&store, 2 * ROWS), second);
    }
}

fn copy_files(from: &std::path::Path, to: &std::path::Path, keep: impl Fn(&str) -> bool) {
    std::fs::create_dir_all(to).unwrap();
    for entry in std::fs::read_dir(from).unwrap() {
        let entry = entry.unwrap();
        assert!(entry.file_type().unwrap().is_file(), "{entry:?}");
        let name = entry.file_name().into_string().unwrap();
        if keep(&name) {
            std::fs::copy(entry.path(), to.join(&name)).unwrap();
        }
    }
}

fn run_schedule(
    name: &str,
    pages: usize,
    rows: usize,
    due: impl FnMut(usize, u64, u64) -> bool,
) -> ImportRun {
    let fixture = ImportFixture::new(name);
    let mut store = fixture.open();
    let mut run = ImportRun::default();
    let mut checkpointed = 0;
    import_pages(
        &mut store,
        0..pages,
        rows,
        &mut run,
        &mut checkpointed,
        due,
        true,
    );
    run
}

#[test]
fn initial_import_1707_geometric_schedule_bounds_write_amplification() {
    const ROWS: usize = 20;
    let mut geometric_ratios = Vec::new();
    for pages in [8usize, 32] {
        let per_page = run_schedule(
            &format!("initial_import_1707_write_per_page_{pages}"),
            pages,
            ROWS,
            |_, _, _| true,
        );
        let geometric = run_schedule(
            &format!("initial_import_1707_write_geometric_{pages}"),
            pages,
            ROWS,
            |_, pending, checkpointed| geometric_checkpoint_due(pending, checkpointed, None),
        );
        assert_eq!(per_page.checkpoints.len(), pages);
        // The last checkpoint of either schedule writes the same final index.
        let final_bytes = geometric.final_bytes();
        assert!(final_bytes > 0);
        assert_eq!(
            per_page.checkpoints.last().unwrap().document_count,
            geometric.checkpoints.last().unwrap().document_count
        );
        let log2_pages = usize::BITS - pages.leading_zeros();
        assert!(
            geometric.checkpoints.len() <= log2_pages as usize + 2,
            "pages={pages}: {} checkpoints at {:?}",
            geometric.checkpoints.len(),
            geometric.checkpoint_cursors
        );
        // Generations are published once per checkpoint and stay monotonic.
        assert!(geometric
            .checkpoints
            .windows(2)
            .all(|pair| pair[0].projection_generation < pair[1].projection_generation));
        let ratio = geometric.total_bytes() as f64 / final_bytes as f64;
        assert!(
            ratio <= 3.0,
            "pages={pages}: geometric wrote {} bytes for a {final_bytes}-byte index",
            geometric.total_bytes()
        );
        geometric_ratios.push(ratio);
        // Per-page checkpointing is quadratic: about pages/2 full rewrites.
        let per_page_ratio = per_page.total_bytes() as f64 / final_bytes as f64;
        assert!(
            per_page_ratio >= pages as f64 / 4.0,
            "pages={pages}: per-page ratio {per_page_ratio}"
        );
        // Un-checkpointed input peaks near half the import under doubling.
        assert!(geometric.peak_pending_bytes > per_page.peak_pending_bytes);

        // Capping the gap trades back some write volume for bounded loss.
        let cap = geometric.peak_pending_bytes / 4;
        let capped = run_schedule(
            &format!("initial_import_1707_write_capped_{pages}"),
            pages,
            ROWS,
            |_, pending, checkpointed| geometric_checkpoint_due(pending, checkpointed, Some(cap)),
        );
        // The gap can overshoot the cap by at most the page that crossed it.
        assert!(capped.peak_pending_bytes < cap + per_page.peak_pending_bytes);
        assert!(capped.total_bytes() <= per_page.total_bytes());
        assert!(capped.checkpoints.len() >= geometric.checkpoints.len());
    }
    // The geometric amplification does not grow with the page count.
    assert!(geometric_ratios[1] <= 3.0 && geometric_ratios[0] <= 3.0);
}