lix 0.18.0

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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//! SQL integration starting from only a partial descriptor and native demands.
//! No traced HOT state, raw arbitrary-key transfer or full replica bootstrap.

use std::collections::BTreeSet;
use std::time::Instant;

use crate::changelog::ChangelogReader as _;
use crate::engine::{Engine, EngineOptions};
use crate::session::SessionContext;
use crate::storage_adapter::{StorageAdapter, StorageWriteOptions};
use crate::tracked_state::{NativeMetadataRef, NativeObjectRef};
use crate::{ExecuteResult, Lix, LixError, Memory, Value, open_lix};

use super::native_metadata::{NativeMetadataRequest, stage_native_metadata};
use super::partial_bootstrap::stage_partial_bootstrap;
use super::partial_hydration::hydrate_native_object;
use super::partial_state::PartialReplicaState;

#[derive(Default, Debug)]
pub(super) struct Fetches {
    object_requests: usize,
    metadata_requests: usize,
    payload_bytes: usize,
}

async fn admitted_controls<S: crate::storage_adapter::Storage + Clone + Send + Sync + 'static>(
    storage: &StorageAdapter<S>,
    state: &PartialReplicaState,
) -> Result<Vec<crate::branch::BranchHeadControl>, LixError> {
    let read = storage.begin_read(Default::default()).await?;
    let mut controls = Vec::new();
    for branch in [
        &state.descriptor().selected_branch,
        &state.descriptor().global_branch,
    ] {
        controls.push(
            crate::branch::BranchHeadControlContext::new()
                .reader(&read)
                .load(&branch.branch_id)
                .await?
                .expect("admitted partial branch control remains present"),
        );
    }
    Ok(controls)
}

async fn hydrate_metadata<S: crate::storage_adapter::Storage + Clone + Send + Sync + 'static>(
    storage: &StorageAdapter<S>,
    state: &PartialReplicaState,
    authority: &Lix<Memory>,
    address: NativeMetadataRef,
    fetches: &mut Fetches,
) -> Result<(), LixError> {
    let request = NativeMetadataRequest {
        epoch_id: state.epoch_id().to_owned(),
        objects: vec![address],
    };
    let response = authority.read_sync_native_metadata(&request).await?;
    let wire = serde_json::to_vec(&response).unwrap();
    let response = serde_json::from_slice(&wire).unwrap();
    let read = storage.begin_read(Default::default()).await?;
    let mut writes = storage.new_write_set();
    let preconditions =
        stage_native_metadata(&read, &mut writes, state, &request, &response).await?;
    drop(read);
    storage
        .commit_partial_replica_write_set(
            super::partial_replica_write_capability(),
            writes,
            StorageWriteOptions {
                preconditions,
                await_durable: true,
                ..Default::default()
            },
        )
        .await?;
    fetches.metadata_requests += 1;
    fetches.payload_bytes += response
        .objects
        .iter()
        .map(|object| object.bytes.len())
        .sum::<usize>();
    Ok(())
}

/// Retain only the immutable jump spine rooted in the declared baseline.
/// The Myers recurrence keeps every future jump in this set plus locally
/// authored descendants; preparation never follows ordinary parent ancestry.
async fn prepare_baseline_jump_spines(
    storage: &StorageAdapter<Memory>,
    state: &PartialReplicaState,
    authority: &Lix<Memory>,
    fetches: &mut Fetches,
) -> Result<(), LixError> {
    let mut prepared = BTreeSet::new();
    for branch in [
        &state.descriptor().selected_branch,
        &state.descriptor().global_branch,
    ] {
        let mut cursor = crate::changelog::CommitId::parse(&branch.head.commit_id)
            .map_err(|error| LixError::unknown(error.to_string()))?;
        let mut previous: Option<crate::changelog::CommitRecord> = None;
        loop {
            let read = storage.begin_read(Default::default()).await?;
            let ids = [cursor];
            let record = crate::changelog::ChangelogContext::new()
                .reader(&read)
                .load_commits(crate::changelog::CommitLoadRequest { commit_ids: &ids })
                .await?
                .into_iter()
                .next()
                .unwrap()
                .1;
            drop(read);
            let record = match record {
                Some(record) => record,
                None => {
                    hydrate_metadata(
                        storage,
                        state,
                        authority,
                        NativeMetadataRef::CommitGraphRecord(cursor.to_string()),
                        fetches,
                    )
                    .await?;
                    let read = storage.begin_read(Default::default()).await?;
                    crate::changelog::ChangelogContext::new()
                        .reader(&read)
                        .load_commits(crate::changelog::CommitLoadRequest { commit_ids: &ids })
                        .await?
                        .into_iter()
                        .next()
                        .unwrap()
                        .1
                        .expect("native metadata installation supplies requested graph record")
                }
            };
            if let Some(previous) = previous.as_ref() {
                assert_eq!(
                    previous.generation.checked_sub(record.generation),
                    Some(previous.first_parent_jump_span)
                );
            }
            if !prepared.insert(cursor) {
                break;
            }
            if record.first_parent_jump_commit_id == cursor {
                assert_eq!(record.first_parent_jump_span, 0);
                break;
            }
            cursor = record.first_parent_jump_commit_id;
            previous = Some(record);
        }
    }
    eprintln!(
        "partial SQL prepared fixed Myers spine headers={}",
        prepared.len()
    );
    Ok(())
}

pub(super) async fn execute_hydrating<
    S: crate::storage_adapter::Storage + Clone + Send + Sync + 'static,
>(
    session: &SessionContext<S>,
    storage: &StorageAdapter<S>,
    state: &PartialReplicaState,
    authority: &Lix<Memory>,
    sql: &str,
    params: &[Value],
    fetches: &mut Fetches,
) -> Result<ExecuteResult, LixError> {
    let mut seen = BTreeSet::new();
    for _ in 0..512 {
        let before = admitted_controls(storage, state).await?;
        let error = match session.execute(sql, params).await {
            Ok(result) => return Ok(result),
            Err(error) => error,
        };
        assert_eq!(
            admitted_controls(storage, state).await?,
            before,
            "a failed statement must not publish state before demand retry: {error}"
        );
        if let Some(address) = NativeObjectRef::from_missing_error(&error)? {
            if !seen.insert(format!("object:{address:?}")) {
                return Err(LixError::new(
                    "LIX_PARTIAL_SQL_NO_PROGRESS",
                    format!("object hydration did not resolve {address:?}: {error}"),
                ));
            }
            eprintln!("partial SQL hydrate object {address:?}");
            let report = hydrate_native_object(
                storage,
                state,
                address,
                32 * 1024 * 1024,
                |request| async move {
                    let response = authority.read_sync_native_object_range(&request).await?;
                    // Exercise the actual bounded response's base64 wire format.
                    let wire = serde_json::to_vec(&response).unwrap();
                    Ok(serde_json::from_slice(&wire).unwrap())
                },
            )
            .await?;
            fetches.object_requests += report.requests;
            fetches.payload_bytes += report.payload_bytes;
            continue;
        }
        if let Some(address) = NativeMetadataRef::from_missing_error(&error)? {
            if !seen.insert(format!("metadata:{address:?}")) {
                return Err(LixError::new(
                    "LIX_PARTIAL_SQL_NO_PROGRESS",
                    format!("metadata hydration did not resolve {address:?}: {error}"),
                ));
            }
            eprintln!("partial SQL hydrate metadata {address:?}");
            hydrate_metadata(storage, state, authority, address, fetches).await?;
            continue;
        }
        eprintln!(
            "partial SQL UNHANDLED code={} message={} details={:?}",
            error.code, error.message, error.details
        );
        return Err(error);
    }
    Err(LixError::new(
        "LIX_PARTIAL_SQL_DEMAND_LIMIT",
        "SQL exceeded 512 explicit native dependency demands",
    ))
}

fn value(result: ExecuteResult) -> String {
    assert_eq!(
        result.rows().len(),
        1,
        "cold SQL must not report an unknown native row as absent"
    );
    match result.rows()[0].get::<Value>("value").unwrap() {
        Value::Jsonb(value) => value.as_json_string().unwrap(),
        Value::Text(value) => value,
        value => panic!("unexpected SQL value: {value:?}"),
    }
}

#[tokio::test]
#[ignore = "manual SQL-driven partial native hydration integration gate"]
async fn descriptor_only_sql_hydrates_then_reads_and_writes_offline() {
    for width in [16usize, 1600] {
        let authority = open_lix().await.unwrap();
        let values = (0..width)
            .map(|index| format!("('partial-demand-{index:06}', 'before')"))
            .collect::<Vec<_>>()
            .join(",");
        authority
            .execute(
                &format!("INSERT INTO lix_key_value (key, value) VALUES {values}"),
                &[],
            )
            .await
            .unwrap();
        let descriptor = authority.partial_replica_descriptor(None).await.unwrap();
        let descriptor = serde_json::from_slice(&serde_json::to_vec(&descriptor).unwrap()).unwrap();
        let state = PartialReplicaState::new(
            format!("https://example.test/lix/{}", authority.lix_id()),
            crate::ANONYMOUS_ACCOUNT_ID.to_owned(),
            "00000000-0000-7000-8000-000000000399".to_owned(),
            descriptor,
        )
        .unwrap();
        let memory = Memory::new();
        let storage = StorageAdapter::new(memory.clone());
        let read = storage.begin_read(Default::default()).await.unwrap();
        let mut writes = storage.new_write_set();
        let preconditions = stage_partial_bootstrap(&read, &mut writes, &state).unwrap();
        crate::init::stage_partial_repository_protocol(&mut writes);
        drop(read);
        storage
            .commit_write_set(
                writes,
                StorageWriteOptions {
                    preconditions,
                    await_durable: true,
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        let opened = Instant::now();
        let (engine, session) =
            Engine::new_partial_replica(storage.clone(), EngineOptions::new(), &state)
                .await
                .unwrap();
        engine.sync_mode().admit_partial_replica(
            std::sync::Arc::new(state.clone()),
            super::partial_replica_write_capability(),
        );
        storage.admit_partial_replica_writer(super::partial_replica_write_capability());
        let open_us = opened.elapsed().as_micros();
        let sql = "SELECT value FROM lix_key_value WHERE key = $1";
        let params = [Value::Text("partial-demand-000000".to_owned())];
        let mut fetches = Fetches::default();
        let cold = Instant::now();
        let result = execute_hydrating(
            &session,
            &storage,
            &state,
            &authority,
            sql,
            &params,
            &mut fetches,
        )
        .await
        .unwrap_or_else(|error| panic!("descriptor-only cold SQL blocked width={width}: {error}"));
        assert_eq!(value(result), "before");
        eprintln!(
            "partial SQL cold width={width} open_us={open_us} cold_us={} fetches={fetches:?}",
            cold.elapsed().as_micros()
        );
        // Direct session calls below have no demand callback or network path.
        for _ in 0..3 {
            assert_eq!(
                value(session.execute(sql, &params).await.unwrap()),
                "before"
            );
        }
        // A first read does not promise to prepare every validation/publication
        // dependency of an UPDATE. Prepare this workload with one real cold
        // mutation, then retain the fixed remote jump spine for future appends.
        let mut write_fetches = Fetches::default();
        let preparation = Instant::now();
        execute_hydrating(
            &session,
            &storage,
            &state,
            &authority,
            "UPDATE lix_key_value SET value = $2 WHERE key = $1",
            &[params[0].clone(), Value::Text("prepared".to_owned())],
            &mut write_fetches,
        )
        .await
        .unwrap_or_else(|error| panic!("cold write preparation blocked width={width}: {error}"));
        eprintln!(
            "partial SQL cold write preparation width={width} elapsed_us={} fetches={write_fetches:?}",
            preparation.elapsed().as_micros()
        );
        let mut spine_fetches = Fetches::default();
        let spine_started = Instant::now();
        prepare_baseline_jump_spines(&storage, &state, &authority, &mut spine_fetches)
            .await
            .unwrap();
        eprintln!(
            "partial SQL spine preparation width={width} elapsed_us={} fetches={spine_fetches:?}",
            spine_started.elapsed().as_micros()
        );
        const WARM_ITERATIONS: usize = 30;
        let mut partial_update_us = Vec::new();
        let mut partial_read_us = Vec::new();
        let mut partial_update_root = crate::hot_state::root_exact_profile::Profile::default();
        let mut partial_read_root = crate::hot_state::root_exact_profile::Profile::default();
        for index in 0..WARM_ITERATIONS {
            let expected = format!("after-{}", index % 3);
            crate::hot_state::root_exact_profile::begin();
            let start = Instant::now();
            session.execute("UPDATE lix_key_value SET value = $2 WHERE key = $1", &[params[0].clone(), Value::Text(expected.clone())]).await
                .unwrap_or_else(|error| panic!("resident offline SQL update blocked width={width} update={index}: {error}, details={:?}", error.details));
            partial_update_us.push(start.elapsed().as_micros());
            partial_update_root.add(crate::hot_state::root_exact_profile::take());
            crate::hot_state::root_exact_profile::begin();
            let start = Instant::now();
            assert_eq!(
                value(session.execute(sql, &params).await.unwrap()),
                expected
            );
            partial_read_us.push(start.elapsed().as_micros());
            partial_read_root.add(crate::hot_state::root_exact_profile::take());
        }
        // Same binary, fixture and SQL. Root I/O attribution is test-only and
        // includes adapter-returned local bytes, not wire bytes or OS CPU time.
        authority.execute(sql, &params).await.unwrap();
        let mut full_update_us = Vec::new();
        let mut full_read_us = Vec::new();
        let mut full_update_root = crate::hot_state::root_exact_profile::Profile::default();
        let mut full_read_root = crate::hot_state::root_exact_profile::Profile::default();
        for index in 0..WARM_ITERATIONS {
            let expected = format!("authority-{}", index % 3);
            crate::hot_state::root_exact_profile::begin();
            let start = Instant::now();
            authority
                .execute(
                    "UPDATE lix_key_value SET value = $2 WHERE key = $1",
                    &[params[0].clone(), Value::Text(expected.clone())],
                )
                .await
                .unwrap();
            full_update_us.push(start.elapsed().as_micros());
            full_update_root.add(crate::hot_state::root_exact_profile::take());
            crate::hot_state::root_exact_profile::begin();
            let start = Instant::now();
            assert_eq!(
                value(authority.execute(sql, &params).await.unwrap()),
                expected
            );
            full_read_us.push(start.elapsed().as_micros());
            full_read_root.add(crate::hot_state::root_exact_profile::take());
        }
        let native_read_batches = partial_read_root
            .spaces
            .iter()
            .filter(|(name, _)| name.starts_with("tracked_state."))
            .map(|(_, space)| space.batches)
            .sum::<u64>();
        assert!(
            native_read_batches < WARM_ITERATIONS as u64,
            "warm exact serving must reuse immutable root candidates; profile={partial_read_root:?}"
        );
        eprintln!(
            "PARTIAL_WARM_SQL_PROFILE {}",
            serde_json::json!({
                "width": width, "iterations": WARM_ITERATIONS,
                "partial_update_us": partial_update_us, "partial_read_us": partial_read_us,
                "full_update_us": full_update_us, "full_read_us": full_read_us,
                "partial_update_root": partial_update_root, "partial_read_root": partial_read_root,
                "full_update_root": full_update_root, "full_read_root": full_read_root,
            })
        );
        if std::env::var_os("LIX_PARTIAL_PHASE_PROFILE").is_some() {
            let (_, partial_phases) = phase_profile::capture(async {
                for index in 0..30 {
                    session
                        .execute(
                            "UPDATE lix_key_value SET value = $2 WHERE key = $1",
                            &[
                                params[0].clone(),
                                Value::Text(format!("after-{}", index % 3)),
                            ],
                        )
                        .await
                        .unwrap();
                }
            })
            .await;
            let (_, full_phases) = phase_profile::capture(async {
                for index in 0..30 {
                    authority
                        .execute(
                            "UPDATE lix_key_value SET value = $2 WHERE key = $1",
                            &[
                                params[0].clone(),
                                Value::Text(format!("authority-{}", index % 3)),
                            ],
                        )
                        .await
                        .unwrap();
                }
            })
            .await;
            eprintln!(
                "PARTIAL_WARM_WRITE_PHASES {}",
                serde_json::json!({"width":width,"iterations":30,"partial":partial_phases,"full":full_phases})
            );
        }
        session.close().await.unwrap();
        drop(session);
        drop(engine);
        drop(storage);
        let reopened_storage = StorageAdapter::new(memory);
        let (reopened_engine, reopened) =
            Engine::new_partial_replica(reopened_storage.clone(), EngineOptions::new(), &state)
                .await
                .unwrap();
        reopened_engine.sync_mode().admit_partial_replica(
            std::sync::Arc::new(state.clone()),
            super::partial_replica_write_capability(),
        );
        reopened_storage.admit_partial_replica_writer(super::partial_replica_write_capability());
        assert_eq!(
            value(reopened.execute(sql, &params).await.unwrap()),
            "after-2"
        );
        // Rotate only the remote binding, retaining the epoch, pending tuples,
        // heads and native bytes. An old session must not acquire the new identity from
        // storage implicitly or publish a mutation under its stale binding.
        let read = reopened_storage
            .begin_read(Default::default())
            .await
            .unwrap();
        let before_control = crate::branch::BranchHeadControlContext::new()
            .reader(&read)
            .load(&state.descriptor().selected_branch.branch_id)
            .await
            .unwrap()
            .unwrap();
        let (_, prior_receipt) = super::partial_state::load_partial_replica_state(&read)
            .await
            .unwrap()
            .unwrap();
        let replacement = PartialReplicaState::new(
            format!("https://mirror.example.test/lix/{}", state.repository_id()),
            state.active_account_id().to_owned(),
            state.epoch_id().to_owned(),
            state.descriptor().clone(),
        )
        .unwrap();
        let mut writes = reopened_storage.new_write_set();
        let guard = super::partial_state::stage_partial_replica_state(
            &mut writes,
            &replacement,
            Some(prior_receipt),
        )
        .unwrap();
        drop(read);
        reopened_storage
            .commit_partial_replica_write_set(
                super::partial_replica_write_capability(),
                writes,
                StorageWriteOptions {
                    preconditions: vec![guard],
                    await_durable: true,
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        let error = reopened
            .execute(
                "UPDATE lix_key_value SET value = $2 WHERE key = $1",
                &[params[0].clone(), Value::Text("stale-write".to_owned())],
            )
            .await
            .unwrap_err();
        assert_eq!(error.code, "LIX_PARTIAL_REPLICA_ADMISSION_MISMATCH");
        let read = reopened_storage
            .begin_read(Default::default())
            .await
            .unwrap();
        let after_control = crate::branch::BranchHeadControlContext::new()
            .reader(&read)
            .load(&state.descriptor().selected_branch.branch_id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(
            after_control, before_control,
            "stale remote rejection must not publish a local commit"
        );
        drop(read);
        assert_eq!(
            reopened.execute(sql, &params).await.unwrap_err().code,
            "LIX_PARTIAL_REPLICA_ADMISSION_MISMATCH"
        );
        let (replacement_engine, replacement_session) = Engine::new_partial_replica(
            reopened_storage.clone(),
            EngineOptions::new(),
            &replacement,
        )
        .await
        .unwrap();
        replacement_engine.sync_mode().admit_partial_replica(
            std::sync::Arc::new(replacement),
            super::partial_replica_write_capability(),
        );
        assert_eq!(
            value(replacement_session.execute(sql, &params).await.unwrap()),
            "after-2"
        );
        drop(replacement_session);
        drop(replacement_engine);
        reopened.close().await.unwrap();
    }
}

#[path = "partial_file_sql_tests.rs"]
mod file_content;

#[path = "partial_upload_sql_tests.rs"]
mod upload_recovery;

mod checkpoint;

#[path = "partial_sql_phase_profile.rs"]
mod phase_profile;

#[path = "partial_worker_sql_tests.rs"]
mod worker;

#[tokio::test]
async fn partial_catalog_revision_invalidates_negative_and_amended_schema_cache() {
    let authority = open_lix().await.unwrap();
    let descriptor = authority.partial_replica_descriptor(None).await.unwrap();
    let descriptor = serde_json::from_slice(&serde_json::to_vec(&descriptor).unwrap()).unwrap();
    let state = PartialReplicaState::new(
        format!("https://example.test/lix/{}", authority.lix_id()),
        crate::ANONYMOUS_ACCOUNT_ID.to_owned(),
        "00000000-0000-7000-8000-000000000399".to_owned(),
        descriptor,
    )
    .unwrap();
    let memory = Memory::new();
    let storage = StorageAdapter::new(memory.clone());
    let read = storage.begin_read(Default::default()).await.unwrap();
    let mut writes = storage.new_write_set();
    let preconditions = stage_partial_bootstrap(&read, &mut writes, &state).unwrap();
    crate::init::stage_partial_repository_protocol(&mut writes);
    drop(read);
    storage
        .commit_write_set(
            writes,
            StorageWriteOptions {
                preconditions,
                await_durable: true,
                ..Default::default()
            },
        )
        .await
        .unwrap();
    let (engine, session) =
        Engine::new_partial_replica(storage.clone(), EngineOptions::new(), &state)
            .await
            .unwrap();
    engine.sync_mode().admit_partial_replica(
        std::sync::Arc::new(state.clone()),
        super::partial_replica_write_capability(),
    );
    storage.admit_partial_replica_writer(super::partial_replica_write_capability());

    let revision = async || {
        let read = storage.begin_read(Default::default()).await.unwrap();
        crate::catalog::load_catalog_revision(&read)
            .await
            .unwrap()
            .unwrap()
    };
    let initial = revision().await;
    let mut fetches = Fetches::default();
    // A failed cold compilation cannot cache its incomplete schema scan.
    assert!(
        session
            .execute("SELECT id FROM partial_catalog_probe", &[])
            .await
            .is_err()
    );
    execute_hydrating(
        &session,
        &storage,
        &state,
        &authority,
        "SELECT key FROM lix_key_value WHERE key = 'missing'",
        &[],
        &mut fetches,
    )
    .await
    .unwrap();
    // Resident native graph/delta subsets must never masquerade as complete
    // public inventories, including COUNT and point-negative queries.
    for sql in [
        "SELECT COUNT(*) FROM lix_commit",
        "SELECT COUNT(*) FROM lix_change",
        "SELECT * FROM lix_commit WHERE id = '00000000-0000-7000-8000-000000099999'",
    ] {
        let error = session
            .execute(sql, &[])
            .await
            .expect_err("partial inventory must fail closed");
        assert_eq!(
            error.code, "LIX_PARTIAL_REPLICA_SCOPE_UNSUPPORTED",
            "{sql}: {error}"
        );
    }
    session
        .execute("SELECT key FROM lix_key_value WHERE key = 'missing'", &[])
        .await
        .unwrap();
    // Warm the transaction-opening catalog, including absence of this surface.
    execute_hydrating(
        &session,
        &storage,
        &state,
        &authority,
        "UPDATE lix_key_value SET value = 'none' WHERE key = 'missing'",
        &[],
        &mut fetches,
    )
    .await
    .unwrap();
    assert_eq!(revision().await, initial);
    assert!(
        session
            .execute("SELECT id FROM partial_catalog_probe", &[])
            .await
            .is_err()
    );
    let schema = serde_json::json!({
        "$schema":"https://lix.dev/schema-v1.json", "key":"partial_catalog_probe",
        "columns":[{"name":"id","type":"text","nullable":false}], "primary_key":["id"]
    });
    execute_hydrating(&session, &storage, &state, &authority,
        "INSERT INTO lix_registered_schema (schema_key, value, lixcol_global, lixcol_untracked) VALUES ($1 ->> 'key', $1, false, true)",
        &[Value::Jsonb(schema.clone().into())], &mut fetches).await.unwrap();
    let registered = revision().await;
    assert_ne!(registered, initial);
    session
        .execute(
            "INSERT INTO partial_catalog_probe (id, lixcol_untracked) VALUES ('local', true)",
            &[],
        )
        .await
        .unwrap();
    session
        .execute("SELECT id FROM partial_catalog_probe", &[])
        .await
        .unwrap();
    assert_eq!(revision().await, registered);
    assert!(
        session
            .execute("SELECT title FROM partial_catalog_probe", &[])
            .await
            .is_err()
    );
    let mut amended = schema;
    amended["columns"]
        .as_array_mut()
        .unwrap()
        .push(serde_json::json!({"name":"title","type":"text","nullable":true}));
    session.execute("UPDATE lix_registered_schema SET value = $1 WHERE schema_key = 'partial_catalog_probe' AND lixcol_untracked = true",
        &[Value::Jsonb(amended.into())]).await.unwrap();
    assert_ne!(revision().await, registered);
    session.execute("UPDATE partial_catalog_probe SET title = 'fresh' WHERE id = 'local' AND lixcol_untracked = true", &[]).await.unwrap();
    session
        .execute(
            "SELECT title FROM partial_catalog_probe WHERE id = 'local'",
            &[],
        )
        .await
        .unwrap();
    // A same-epoch baseline receipt change must not authorize an already
    // admitted engine implicitly. Simulate another publisher's receipt CAS.
    let read = storage.begin_read(Default::default()).await.unwrap();
    let (_, raw) = super::partial_state::load_partial_replica_state(&read)
        .await
        .unwrap()
        .unwrap();
    let mut descriptor = state.descriptor().clone();
    descriptor.selected_branch.ref_change_id = "00000000-0000-7000-8000-000000000599".into();
    let replacement = PartialReplicaState::new(
        state.remote_id().into(),
        state.active_account_id().into(),
        state.epoch_id().into(),
        descriptor,
    )
    .unwrap();
    let mut writes = storage.new_write_set();
    let guard =
        super::partial_state::stage_partial_replica_state(&mut writes, &replacement, Some(raw))
            .unwrap();
    drop(read);
    storage
        .commit_partial_replica_write_set(
            super::partial_replica_write_capability(),
            writes,
            StorageWriteOptions {
                preconditions: vec![guard],
                ..Default::default()
            },
        )
        .await
        .unwrap();
    let before = admitted_controls(&storage, &replacement).await.unwrap();
    let error = session.execute("UPDATE partial_catalog_probe SET title = 'stale' WHERE id = 'local' AND lixcol_untracked = true", &[]).await.unwrap_err();
    assert_eq!(error.code, "LIX_PARTIAL_REPLICA_ADMISSION_MISMATCH");
    assert_eq!(
        admitted_controls(&storage, &replacement).await.unwrap(),
        before
    );
    session.close().await.unwrap();
}

mod publication;

#[cfg(feature = "server-protocol")]
mod runtime_http;

#[cfg(feature = "server-protocol")]
mod global_runtime_http;

mod file_diff_content;