lix 0.17.1

Embeddable version control for apps and AI agents.
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
use std::collections::BTreeSet;
use std::sync::Arc;

use serde_json::{Value as JsonValue, json};

use crate::binary_cas::BinaryCasContext;
use crate::branch::{
    BranchContext, BranchHeadControl, BranchHeadControlContext, stage_branch_head_control,
};
use crate::catalog::CatalogContext;
use crate::changelog::{
    ChangeId, ChangeRecord, ChangelogAppend, ChangelogContext, ChangelogWriter, CommitId,
};
use crate::common::LixTimestamp;
use crate::hot_state::{
    CurrentStateDeltaRef, HotStateContext, HotStateFilter, HotStateProjection, HotStateRowRequest,
    HotStateScanRequest, TrackedHeadContext, WorkingDiffIndexCoverage,
};
use crate::row_pk::RowPk;
use crate::session::SessionBranch;
use crate::storage_adapter::Storage;
use crate::storage_adapter::{
    SharedStorageAdapterRead, StorageAdapter, StorageReadOptions, StorageWriteSet,
    StorageWriteSetStats,
};
use crate::tracked_state::TrackedStateContext;
use crate::transaction_types::{RawWriteBatch, TransactionJson, TransactionWriteRow};
use crate::{GLOBAL_BRANCH_ID, NullableKeyFilter};

const SCHEMA_FIXTURE_COMMIT_ID: &str = "01920000-0000-7000-8000-00000000b001";
const TIMESTAMP: &str = "2026-05-19T00:00:00.000Z";
const BENCH_BRANCH_ID: &str = "01920000-0000-7000-8000-0000000000a1";

#[derive(Clone, Debug)]
pub struct BenchTransactionRow {
    pub schema_key: String,
    pub file_id: Option<String>,
    pub row_pk: String,
    pub value: Arc<JsonValue>,
    pub updated_value: Arc<JsonValue>,
}

#[expect(missing_debug_implementations)]
pub struct BenchTransactionFixture<StorageImpl: Storage> {
    storage: StorageAdapter<StorageImpl>,
    hot_state: Arc<HotStateContext>,
    tracked_state: Arc<TrackedStateContext>,
    binary_cas: Arc<BinaryCasContext>,
    branch_ctx: Arc<BranchContext>,
    catalog_context: Arc<CatalogContext>,
    rows: Vec<BenchTransactionRow>,
    delete_one_offset: usize,
}

#[derive(Clone, Copy, Debug)]
pub struct BenchWriteAccounting {
    pub logical_rows: usize,
    pub staged_puts: u64,
    pub staged_deletes: u64,
    pub touched_spaces: u64,
    pub storage_calls: u64,
    pub put_batches: u64,
    pub delete_batches: u64,
    pub written_bytes: u64,
}

#[derive(Clone, Copy, Debug)]
pub struct BenchLayoutAccounting {
    pub space_id: u32,
    pub space: &'static str,
    pub rows: u64,
    pub key_bytes: u64,
    pub value_bytes: u64,
}

impl<StorageImpl> BenchTransactionFixture<StorageImpl>
where
    StorageImpl: Storage + Clone + Send + Sync + 'static,
{
    /// Like [`Self::new`], but enables the engine's deterministic functions
    /// before any transaction runs, so ids and timestamps are
    /// sequence-derived and two fixtures produce byte-identical storage.
    pub async fn new_deterministic(
        storage: StorageAdapter<StorageImpl>,
        rows: Vec<BenchTransactionRow>,
    ) -> Self {
        let fixture = Self::new(storage.clone(), rows).await;
        seed_deterministic_mode(storage).await;
        fixture
    }

    pub async fn new(storage: StorageAdapter<StorageImpl>, rows: Vec<BenchTransactionRow>) -> Self {
        let tracked_state = Arc::new(TrackedStateContext::new());
        let hot_state = Arc::new(HotStateContext::new(
            tracked_state.as_ref().clone(),
            crate::commit_graph::CommitGraphContext::new(),
        ));
        let branch_ctx = Arc::new(BranchContext::new());
        seed_visible_schema_rows(storage.clone(), tracked_state.as_ref()).await;
        Self {
            storage,
            hot_state,
            tracked_state,
            binary_cas: Arc::new(BinaryCasContext::new()),
            branch_ctx,
            catalog_context: Arc::new(CatalogContext::new()),
            rows,
            delete_one_offset: 0,
        }
    }

    pub async fn seed(&mut self) -> usize {
        self.insert_all().await
    }

    pub async fn insert_all(&mut self) -> usize {
        self.insert_all_accounting().await.logical_rows
    }

    pub async fn insert_all_accounting(&mut self) -> BenchWriteAccounting {
        let mut rows = RawWriteBatch::with_capacity(self.rows.len());
        for row in &self.rows {
            rows.push(transaction_row(row, &row.value));
        }
        self.commit_rows(rows).await
    }

    pub async fn update_all(&mut self) -> usize {
        self.update_all_accounting().await.logical_rows
    }

    pub async fn update_all_accounting(&mut self) -> BenchWriteAccounting {
        let mut rows = RawWriteBatch::with_capacity(self.rows.len());
        for row in &self.rows {
            rows.push(transaction_row(row, &row.updated_value));
        }
        self.commit_rows(rows).await
    }

    pub async fn update_one_by_pk(&mut self) -> usize {
        self.update_one_by_pk_accounting().await.logical_rows
    }

    pub async fn update_one_by_pk_accounting(&mut self) -> BenchWriteAccounting {
        let row = &self.rows[self.rows.len() / 2];
        let mut rows = RawWriteBatch::with_capacity(1);
        rows.push(transaction_row(row, &row.updated_value));
        self.commit_rows(rows).await
    }

    pub async fn delete_all(&mut self) -> usize {
        self.delete_all_accounting().await.logical_rows
    }

    pub async fn delete_all_accounting(&mut self) -> BenchWriteAccounting {
        let mut rows = RawWriteBatch::with_capacity(self.rows.len());
        for row in &self.rows {
            rows.push(transaction_delete_row(row));
        }
        self.commit_rows(rows).await
    }

    pub async fn delete_one_by_pk(&mut self) -> usize {
        self.delete_one_by_pk_accounting().await.logical_rows
    }

    pub async fn delete_one_by_pk_accounting(&mut self) -> BenchWriteAccounting {
        let row_index = (self.rows.len() / 2 + self.delete_one_offset) % self.rows.len();
        self.delete_one_offset += 1;
        let row = &self.rows[row_index];
        let mut rows = RawWriteBatch::with_capacity(1);
        rows.push(transaction_delete_row(row));
        self.commit_rows(rows).await
    }

    pub async fn read_all(&self) -> usize {
        let count = self.scan_count().await;
        assert_eq!(count, self.rows.len());
        count
    }

    async fn scan_count(&self) -> usize {
        let read = SharedStorageAdapterRead::new(
            self.storage
                .begin_read(StorageReadOptions::default())
                .await
                .expect("begin transaction bench read"),
        );
        let rows = self
            .hot_state
            .reader(read)
            .scan_batch(&HotStateScanRequest {
                filter: HotStateFilter {
                    schema_keys: vec!["json_pointer".to_string()],
                    branch_ids: vec![BENCH_BRANCH_ID.to_string()],
                    file_ids: vec![NullableKeyFilter::Null],
                    include_tombstones: false,
                    ..HotStateFilter::default()
                },
                projection: HotStateProjection::default(),
                limit: None,
            })
            .await
            .expect("scan transaction bench rows");
        rows.len()
    }

    pub async fn read_all_by_pk(&self) -> usize {
        let mut count = 0;
        for row in &self.rows {
            count += self.read_one(row).await;
        }
        count
    }

    pub async fn read_many_by_pk(&self, count: usize) -> usize {
        let count = count.min(self.rows.len());
        let mut found = 0;
        for row in &self.rows[..count] {
            found += self.read_one(row).await;
        }
        found
    }

    pub async fn read_one_by_pk(&self) -> usize {
        self.read_one(&self.rows[self.rows.len() / 2]).await
    }

    /// Returns every visible row's identity and snapshot content, sorted by
    /// identity. Unlike the timed read helpers this materializes contents,
    /// so equivalence tests can compare logical state across storage implementations.
    pub async fn read_all_contents(&self) -> Vec<(String, String)> {
        let read = SharedStorageAdapterRead::new(
            self.storage
                .begin_read(StorageReadOptions::default())
                .await
                .expect("begin transaction bench read"),
        );
        let rows = self
            .hot_state
            .reader(read)
            .scan_batch(&HotStateScanRequest {
                filter: HotStateFilter {
                    schema_keys: vec!["json_pointer".to_string()],
                    branch_ids: vec![BENCH_BRANCH_ID.to_string()],
                    file_ids: vec![NullableKeyFilter::Null],
                    include_tombstones: false,
                    ..HotStateFilter::default()
                },
                projection: HotStateProjection::default(),
                limit: None,
            })
            .await
            .expect("scan transaction bench rows");
        let mut contents = rows
            .iter()
            .map(|row| {
                let row_pk = row
                    .row_pk()
                    .as_json_array_text()
                    .expect("bench row pk should render");
                (
                    row_pk,
                    row.snapshot_content()
                        .map(ToString::to_string)
                        .unwrap_or_default(),
                )
            })
            .collect::<Vec<_>>();
        contents.sort();
        contents
    }

    async fn read_one(&self, row: &BenchTransactionRow) -> usize {
        let read = SharedStorageAdapterRead::new(
            self.storage
                .begin_read(StorageReadOptions::default())
                .await
                .expect("begin transaction bench read"),
        );
        let row = self
            .hot_state
            .reader(read)
            .load_row(&HotStateRowRequest {
                schema_key: "json_pointer".to_string(),
                branch_id: BENCH_BRANCH_ID.to_string(),
                row_pk: RowPk::single(row.row_pk.clone()),
                file_id: NullableKeyFilter::Null,
            })
            .await
            .expect("load transaction bench row");
        assert!(row.is_some());
        1
    }

    #[expect(clippy::needless_pass_by_ref_mut)]
    async fn commit_rows(&mut self, rows: RawWriteBatch) -> BenchWriteAccounting {
        let logical_rows = rows.len();
        let opened = super::open_transaction(
            &SessionBranch::new(BENCH_BRANCH_ID.to_string()),
            crate::ANONYMOUS_ACCOUNT_ID.to_string(),
            self.storage.clone(),
            Arc::clone(&self.hot_state),
            Arc::clone(&self.tracked_state),
            Arc::clone(&self.binary_cas),
            crate::plugin::runtime::PluginRuntimeHost::new(Arc::new(
                crate::plugin::runtime::UnsupportedWasmRuntime,
            )),
            Arc::clone(&self.branch_ctx),
            Arc::clone(&self.catalog_context),
            Arc::new(crate::sql2::SqlPlanningCache::default()),
            crate::sql2::SessionFileViews::default(),
        )
        .await
        .expect("open transaction bench transaction");
        let mut transaction = opened.transaction;
        transaction
            .stage_rows(rows)
            .await
            .expect("stage transaction bench rows");
        let outcome = transaction
            .commit(&opened.runtime_functions)
            .await
            .expect("commit transaction bench rows");
        write_accounting(logical_rows, outcome.storage_stats)
    }

    /// Per-row inventory of one space, for byte-exact layout comparison.
    pub async fn space_inventory(&self, space_name: &str) -> Vec<(Vec<u8>, Vec<u8>)> {
        let read = self
            .storage
            .begin_read(StorageReadOptions::default())
            .await
            .expect("begin transaction inventory read");
        crate::storage_bench::space_inventory(&read, space_name).await
    }

    pub async fn layout_accounting(&self) -> Vec<BenchLayoutAccounting> {
        let read = self
            .storage
            .begin_read(StorageReadOptions::default())
            .await
            .expect("begin transaction layout accounting read");
        crate::storage_bench::layout_accounting(&read)
            .await
            .into_iter()
            .map(|space| BenchLayoutAccounting {
                space_id: space.space_id,
                space: space.space,
                rows: space.rows,
                key_bytes: space.key_bytes,
                value_bytes: space.value_bytes,
            })
            .collect()
    }
}

async fn seed_deterministic_mode<StorageImpl>(storage: StorageAdapter<StorageImpl>)
where
    StorageImpl: Storage + Clone + Send + Sync + 'static,
    for<'storage> StorageImpl::Read<'storage>: Send,
{
    let snapshot_content = serde_json::to_string(&json!({
        "key": crate::functions::DETERMINISTIC_MODE_KEY,
        "value": { "enabled": true },
    }))
    .expect("deterministic mode snapshot should serialize");
    let timestamp = LixTimestamp::expect_parse("created_at", "1970-01-01T00:00:00.000Z");
    let row_pk = RowPk::single(crate::functions::DETERMINISTIC_MODE_KEY);
    let read = SharedStorageAdapterRead::new(
        storage
            .begin_read(StorageReadOptions::default())
            .await
            .expect("deterministic mode read should open"),
    );
    let mut writes = storage.new_write_set();
    let control = BranchHeadControlContext::new()
        .reader(&read)
        .load(GLOBAL_BRANCH_ID)
        .await
        .expect("global branch control should load")
        .expect("global branch control should exist");
    let snapshot_value: serde_json::Value =
        serde_json::from_str(&snapshot_content).expect("deterministic mode snapshot should parse");
    let decoded_snapshot =
        crate::row_payload::TypedRow::from_builtin_json("lix_key_value", &row_pk, &snapshot_value)
            .expect("deterministic mode snapshot should type");
    let snapshot = decoded_snapshot
        .durable_payload_ref()
        .expect("deterministic mode snapshot should encode");
    let mut working_diff_coverage = WorkingDiffIndexCoverage::default();
    TrackedHeadContext::new()
        .writer(&read, &mut writes)
        .stage_current_state_with_working_diff(
            GLOBAL_BRANCH_ID,
            Some(control.tracked_generation),
            control.head_commit_id,
            &[CurrentStateDeltaRef {
                schema_key: "lix_key_value",
                file_id: None,
                row_pk: &row_pk,
                change_id: Some(ChangeId::for_test_label("bench-deterministic-mode")),
                commit_id: None,
                untracked: true,
                deleted: false,
                created_at: timestamp,
                updated_at: timestamp,
                snapshot: Some(snapshot),
                metadata: None,
                columnar_base_coordinate: None,
            }],
            &BTreeSet::new(),
            None,
            None,
            &mut working_diff_coverage,
        )
        .await
        .expect("deterministic mode current row should stage");
    crate::storage_bench::commit_write_set_for_bench(&storage, writes)
        .await
        .expect("deterministic mode row should commit");
}

fn write_accounting(logical_rows: usize, stats: StorageWriteSetStats) -> BenchWriteAccounting {
    BenchWriteAccounting {
        logical_rows,
        staged_puts: stats.staged_puts,
        staged_deletes: stats.staged_deletes,
        touched_spaces: stats.touched_spaces,
        storage_calls: stats.storage_calls,
        put_batches: stats.put_batches,
        delete_batches: stats.delete_batches,
        written_bytes: stats.written_bytes,
    }
}

fn transaction_row(row: &BenchTransactionRow, value: &Arc<JsonValue>) -> TransactionWriteRow {
    TransactionWriteRow {
        row_pk: Some(RowPk::single(row.row_pk.clone())),
        schema_key: row.schema_key.as_str().into(),
        file_id: row.file_id.as_deref().map(Into::into),
        snapshot: Some(TransactionJson::from_shared_value_unchecked(Arc::clone(
            value,
        ))),
        metadata: None,
        origin: None,
        created_at: None,
        updated_at: None,
        global: false,
        change_id: None,
        commit_id: None,
        untracked: false,
        branch_id: BENCH_BRANCH_ID.into(),
    }
}

fn transaction_delete_row(row: &BenchTransactionRow) -> TransactionWriteRow {
    let mut out = transaction_row(row, &row.value);
    out.snapshot = None;
    out
}

async fn seed_visible_schema_rows<StorageImpl>(
    storage: StorageAdapter<StorageImpl>,
    tracked_state: &TrackedStateContext,
) where
    StorageImpl: Storage + Clone,
{
    let mut writes = StorageWriteSet::new();
    let mut schemas = crate::schema::seed_schema_definitions()
        .into_iter()
        .cloned()
        .collect::<Vec<_>>();
    schemas.push(json_pointer_schema());
    let rows = schemas
        .iter()
        .map(|schema| {
            let key = crate::schema::schema_key_from_definition(schema)
                .expect("seed schema key should derive");
            let snapshot = json!({
                "schema_key": key.schema_key.clone(),
                "value": schema,
            });
            let row_pk = crate::schema::registered_schema_row_pk(&key.schema_key)
                .expect("registered schema identity should derive");
            let typed = crate::row_payload::TypedRow::from_builtin_json(
                "lix_registered_schema",
                &row_pk,
                &snapshot,
            )
            .expect("registered schema fixture should type");
            crate::tracked_state::MaterializedTrackedStateRow {
                row_pk,
                schema_key: "lix_registered_schema".to_string(),
                file_id: None,
                snapshot_content: None,
                decoded_snapshot: Some(Arc::new(typed)),
                metadata: None,
                deleted: false,
                created_at: TIMESTAMP.to_string(),
                updated_at: TIMESTAMP.to_string(),
                change_id: ChangeId::for_test_label(&format!("schema-fixture-{}", key.schema_key)),
                commit_id: CommitId::for_test_label(SCHEMA_FIXTURE_COMMIT_ID),
            }
        })
        .collect::<Vec<_>>();
    let mut read = SharedStorageAdapterRead::new(
        storage
            .begin_read(StorageReadOptions::default())
            .await
            .expect("schema fixture read should open"),
    );
    crate::test_support::stage_tracked_root_from_materialized(
        &mut read,
        &mut writes,
        tracked_state,
        SCHEMA_FIXTURE_COMMIT_ID,
        None,
        &rows,
    )
    .await
    .expect("schema fixture rows should stage");
    // Production initialization records this revision with the schema root.
    // Keep the low-level benchmark fixture on the normal transaction-open
    // cache path rather than measuring a legacy no-revision fallback.
    crate::catalog::stage_catalog_revision(&mut writes);
    crate::storage_bench::commit_write_set_for_bench(&storage, writes)
        .await
        .expect("schema fixture tracked rows should commit");

    drop(read);
    let mut read = SharedStorageAdapterRead::new(
        storage
            .begin_read(StorageReadOptions::default())
            .await
            .expect("schema fixture current read should open"),
    );
    let timestamp = LixTimestamp::expect_parse("timestamp", TIMESTAMP);
    let commit_id = CommitId::for_test_label(SCHEMA_FIXTURE_COMMIT_ID);
    let branch_refs = [GLOBAL_BRANCH_ID, BENCH_BRANCH_ID].map(|branch_id| {
        let row_pk =
            RowPk::uuid_from_canonical(branch_id).expect("benchmark branch ID is canonical");
        let snapshot = json!({"id": branch_id, "commit_id": commit_id}).to_string();
        let change_id = ChangeId::for_test_label(&format!("bench-branch-ref-{branch_id}"));
        (branch_id, row_pk, snapshot, change_id)
    });
    let mut writes = StorageWriteSet::new();
    ChangelogContext::new()
        .writer(&mut read, &mut writes)
        .stage_append(ChangelogAppend {
            changes: branch_refs
                .iter()
                .map(|(_, row_pk, snapshot, change_id)| ChangeRecord {
                    format_version: 2,
                    change_id: *change_id,
                    account_id: crate::ANONYMOUS_ACCOUNT_ID.to_string(),
                    row_pk: row_pk.clone(),
                    schema_key: crate::branch::BRANCH_REF_SCHEMA_KEY.to_string(),
                    file_id: None,
                    snapshot: Some(
                        crate::row_payload::TypedRow::from_test_json_unchecked(
                            row_pk,
                            &serde_json::from_str(snapshot)
                                .expect("branch-ref benchmark row is JSON"),
                        )
                        .expect("branch-ref benchmark row should type")
                        .durable_payload()
                        .expect("branch-ref benchmark row should encode")
                        .to_vec(),
                    ),
                    metadata: None,
                    created_at: timestamp,
                    origin_key: None,
                })
                .collect(),
            ..ChangelogAppend::default()
        })
        .await
        .expect("schema fixture branch-ref changes should stage");
    // Match repository initialization: the immutable schema root and each
    // visible branch control get a complete grouped current-state generation
    // before the timed fixture begins. Otherwise the first timed tracked
    // write would bootstrap from history instead of exercising the normal
    // path.
    let tracked_head = TrackedHeadContext::new();
    let absence_guards = BTreeSet::new();
    for (branch_id, _, _, _) in &branch_refs {
        #[cfg(test)]
        {
            let mut coverage = WorkingDiffIndexCoverage::default();
            tracked_head
                .writer(&read, &mut writes)
                .stage_current_state_with_working_diff(
                    branch_id,
                    None,
                    commit_id,
                    &[],
                    &absence_guards,
                    Some(rows.clone()),
                    None,
                    &mut coverage,
                )
                .await
                .expect("schema fixture tracked head should stage");
        }
        #[cfg(not(test))]
        tracked_head
            .writer(&read, &mut writes)
            .stage_commit(
                branch_id,
                None,
                commit_id,
                &[],
                &absence_guards,
                Some(rows.clone()),
            )
            .await
            .expect("schema fixture tracked head should stage");
    }
    // Branch controls are the authoritative publication fence. The
    // branch-ref ledger changes above remain part of the public fixture, but
    // branch refs are synthesized from controls rather than duplicated in
    // current-state rows.
    for (branch_id, _, _, change_id) in &branch_refs {
        let mut control = BranchHeadControl {
            head_commit_id: commit_id,
            tracked_generation: commit_id,
            current_state_revision: 0,
            working_diff_checkpoint_commit_id: None,
            created_at: timestamp,
            updated_at: timestamp,
            ref_change_id: *change_id,
            schema_presence_bloom: [0; 4],
        };
        control.note_schemas(rows.iter().map(|row| row.schema_key.as_str()));
        stage_branch_head_control(&mut writes, branch_id, control)
            .expect("schema fixture branch control should stage");
    }
    // A branch ref can change the registered-schema catalog reachable from a
    // branch, so it rotates the same cache revision in production commits.
    crate::catalog::stage_catalog_revision(&mut writes);
    crate::storage_bench::commit_write_set_for_bench(&storage, writes)
        .await
        .expect("schema fixture grouped current state should commit");
}

fn json_pointer_schema() -> JsonValue {
    json!({
        "$schema": "https://lix.dev/schema-v1.json",
        "key": "json_pointer",
        "columns": [
            { "name": "path", "type": "text", "nullable": false },
            { "name": "value", "type": "jsonb", "nullable": false }
        ],
        "primary_key": ["path"]
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage_adapter::Memory;

    const BULK_ROWS: usize = 1_000;

    fn bulk_rows(count: usize) -> Vec<BenchTransactionRow> {
        (0..count)
            .map(|index| BenchTransactionRow {
                schema_key: "json_pointer".to_owned(),
                file_id: None,
                row_pk: format!("/bulk/{index:04}"),
                value: Arc::new(json!({
                    "path": format!("/bulk/{index:04}"),
                    "value": format!("before-{index:04}"),
                })),
                updated_value: Arc::new(json!({
                    "path": format!("/bulk/{index:04}"),
                    "value": format!("after-{index:04}"),
                })),
            })
            .collect()
    }

    #[tokio::test]
    async fn bulk_current_state_uses_compact_certificates_without_changing_point_writes() {
        let mut fixture =
            BenchTransactionFixture::new(StorageAdapter::new(Memory::new()), bulk_rows(BULK_ROWS))
                .await;

        let inserted = fixture.insert_all_accounting().await;
        assert_eq!(inserted.logical_rows, BULK_ROWS);
        // The v74 row-PK index adds a bounded set of authenticated tree chunks
        // while remaining independent of one-put-per-row publication.
        assert!(inserted.staged_puts < 50, "{inserted:?}");
        assert_eq!(fixture.read_all().await, BULK_ROWS);

        let point_update = fixture.update_one_by_pk_accounting().await;
        assert_eq!(point_update.logical_rows, 1);
        // Point publication retains the authenticated state/branch controls,
        // binary-CAS and tracked-mutation fences, checkpoint retirement and
        // row-PK index updates. Typed rows need no JSON-store publication put.
        assert_eq!(point_update.staged_puts, 13, "{point_update:?}");
        assert_eq!(point_update.touched_spaces, 9, "{point_update:?}");
        assert_eq!(point_update.put_batches, 9, "{point_update:?}");

        // A sparse overlay deliberately invalidates the complete-generation
        // digest, so use a fresh fixture to exercise exact bulk replacement
        // and deletion certificates.
        let mut bulk_fixture =
            BenchTransactionFixture::new(StorageAdapter::new(Memory::new()), bulk_rows(BULK_ROWS))
                .await;
        bulk_fixture.insert_all().await;
        let updated = bulk_fixture.update_all_accounting().await;
        assert_eq!(updated.logical_rows, BULK_ROWS);
        assert!(updated.staged_puts < 50, "{updated:?}");
        assert_eq!(bulk_fixture.read_all().await, BULK_ROWS);

        let deleted = bulk_fixture.delete_all_accounting().await;
        assert_eq!(deleted.logical_rows, BULK_ROWS);
        assert!(deleted.staged_puts < 40, "{deleted:?}");
        assert_eq!(bulk_fixture.scan_count().await, 0);
    }
}