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
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
//! Dependency diagnostic for a partial replica with on-demand sync.
//!
//! This uses the ordinary native engine, without forging a sync receipt or
//! activating a partial replica. Success only establishes that a prepared SQL
//! workload can retain the untouched native base by reference. Logical coverage,
//! missing-input admission, reconciliation and GC remain separate requirements.
#![allow(clippy::manual_async_fn)]

use std::{
    collections::{BTreeMap, BTreeSet},
    future::Future,
    ops::Bound,
    pin::Pin,
    sync::{Arc, Mutex},
};

use crate::changelog::ChangelogReader;
use crate::storage::*;
use crate::storage_adapter::{StorageAdapter, StorageAdapterRead};
use crate::{CreateBranchOptions, Value, open_lix};

#[derive(Default)]
struct Trace {
    phase: &'static str,
    keys: BTreeMap<StorageSpace, BTreeSet<Key>>,
    returned: BTreeMap<(&'static str, StorageSpace), usize>,
    scans: Vec<(&'static str, StorageSpace, KeyRange)>,
    deleted: BTreeSet<(StorageSpace, Key)>,
    deleted_ranges: Vec<(StorageSpace, KeyRange)>,
    write_preconditions: Vec<(&'static str, Precondition)>,
}

#[derive(Clone)]
struct RecordingStorage {
    memory: Memory,
    trace: Arc<Mutex<Trace>>,
}

struct RecordingRead<R> {
    read: R,
    trace: Arc<Mutex<Trace>>,
}

struct RecordingWrite {
    write: MemoryWrite,
    read: MemoryRead,
    trace: Arc<Mutex<Trace>>,
}

impl Storage for RecordingStorage {
    type Read<'a>
        = RecordingRead<MemoryRead>
    where
        Self: 'a;
    type Write<'a>
        = RecordingWrite
    where
        Self: 'a;

    fn acquire_session(
        &self,
    ) -> impl Future<Output = Result<StorageSessionToken, StorageError>> + Send {
        self.memory.acquire_session()
    }

    fn begin_read(
        &self,
        opts: ReadOptions,
    ) -> impl Future<Output = Result<Self::Read<'_>, StorageError>> + Send {
        async move {
            Ok(RecordingRead {
                read: self.memory.begin_read(opts).await?,
                trace: Arc::clone(&self.trace),
            })
        }
    }

    fn begin_write(
        &self,
        opts: WriteOptions,
    ) -> impl Future<Output = Result<Self::Write<'_>, StorageError>> + Send {
        async move {
            // This diagnostic has one writer and no concurrent authority
            // changes, so these two source snapshots are identical.
            let read = self
                .memory
                .begin_read(ReadOptions {
                    session_token: opts.session_token,
                    ..ReadOptions::default()
                })
                .await?;
            {
                let mut trace = self.trace.lock().expect("trace lock");
                let phase = trace.phase;
                for precondition in &opts.preconditions {
                    trace
                        .write_preconditions
                        .push((phase, precondition.clone()));
                    match precondition {
                        Precondition::KeyAbsent { space, key }
                        | Precondition::KeyPresent { space, key }
                        | Precondition::KeyValueHashEquals { space, key, .. }
                        | Precondition::KeyValueEquals { space, key, .. } => {
                            trace.keys.entry(*space).or_default().insert(key.clone());
                        }
                        Precondition::RangeEmpty { space, range } => {
                            trace.scans.push((phase, *space, range.clone()));
                        }
                    }
                }
            }
            Ok(RecordingWrite {
                write: self.memory.begin_write(opts).await?,
                read,
                trace: Arc::clone(&self.trace),
            })
        }
    }

    fn watch_for_changes(
        &self,
    ) -> impl Future<Output = Result<StorageChangeWatch, StorageError>> + Send {
        self.memory.watch_for_changes()
    }
}

impl RecordingWrite {
    fn record_puts(&self, space: StorageSpace, entries: &PutBatch) {
        let mut trace = self.trace.lock().expect("trace lock");
        for entry in &entries.entries {
            // Memory checks prior bytes when putting immutable identities.
            trace
                .keys
                .entry(space)
                .or_default()
                .insert(entry.key.clone());
            trace.deleted.remove(&(space, entry.key.clone()));
        }
    }
}

impl StorageWrite for RecordingWrite {
    fn put_many(
        &mut self,
        space: StorageSpace,
        entries: PutBatch,
    ) -> impl Future<Output = Result<(), StorageError>> + Send {
        self.record_puts(space, &entries);
        self.write.put_many(space, entries)
    }

    fn replace_many(
        &mut self,
        space: StorageSpace,
        entries: PutBatch,
    ) -> impl Future<Output = Result<(), StorageError>> + Send {
        self.record_puts(space, &entries);
        self.write.replace_many(space, entries)
    }

    fn delete_many(
        &mut self,
        space: StorageSpace,
        keys: &[Key],
    ) -> impl Future<Output = Result<(), StorageError>> + Send {
        {
            let mut trace = self.trace.lock().expect("trace lock");
            trace
                .keys
                .entry(space)
                .or_default()
                .extend(keys.iter().cloned());
            trace
                .deleted
                .extend(keys.iter().cloned().map(|key| (space, key)));
        }
        self.write.delete_many(space, keys)
    }

    fn delete_range(
        &mut self,
        space: StorageSpace,
        range: KeyRange,
    ) -> impl Future<Output = Result<(), StorageError>> + Send {
        async move {
            // MemoryWrite enumerates the base keys in this range internally.
            // Record that real dependency rather than overlooking write I/O.
            let entries = self
                .read
                .begin_scan(space, range.clone(), BeginScanOptions::default())
                .await?
                .collect_all()
                .await?;
            {
                let mut trace = self.trace.lock().expect("trace lock");
                let phase = trace.phase;
                trace.scans.push((phase, space, range.clone()));
                trace
                    .keys
                    .entry(space)
                    .or_default()
                    .extend(entries.into_iter().map(|entry| entry.key));
                trace.deleted_ranges.push((space, range.clone()));
            }
            self.write.delete_range(space, range).await
        }
    }

    fn commit(self) -> impl Future<Output = Result<CommitResult, StorageError>> + Send {
        self.write.commit()
    }

    fn rollback(self) -> impl Future<Output = Result<(), StorageError>> + Send {
        self.write.rollback()
    }
}

impl<R: StorageRead> StorageRead for RecordingRead<R> {
    fn get_many(
        &self,
        requests: &[GetManyRequest<'_>],
    ) -> impl Future<Output = Result<GetManyResult, StorageError>> + Send {
        async move {
            let result = self.read.get_many(requests).await?;
            let mut trace = self.trace.lock().expect("trace lock");
            let phase = trace.phase;
            let mut values = result.values.iter();
            for request in requests {
                for key in request.keys {
                    // Include proven absences in the requested closure log;
                    // copying below only installs source-present values.
                    trace
                        .keys
                        .entry(request.space)
                        .or_default()
                        .insert(key.clone());
                    if values.next().expect("point result cardinality").is_some() {
                        *trace.returned.entry((phase, request.space)).or_default() += 1;
                    }
                }
            }
            Ok(result)
        }
    }

    fn begin_scan(
        &self,
        space: StorageSpace,
        range: KeyRange,
        opts: BeginScanOptions,
    ) -> impl Future<Output = Result<ScanCursor<'_>, StorageError>> + Send {
        async move {
            let cursor = self.read.begin_scan(space, range.clone(), opts).await?;
            let phase = {
                let mut trace = self.trace.lock().expect("trace lock");
                let phase = trace.phase;
                trace.scans.push((phase, space, range.clone()));
                phase
            };
            ScanCursor::from_source(
                range,
                opts.order,
                RecordingScan {
                    cursor,
                    space,
                    phase,
                    trace: Arc::clone(&self.trace),
                },
            )
        }
    }
}

struct RecordingScan<'a> {
    cursor: ScanCursor<'a>,
    space: StorageSpace,
    phase: &'static str,
    trace: Arc<Mutex<Trace>>,
}

impl StorageScanSource for RecordingScan<'_> {
    fn next_page(
        &mut self,
        limit_rows: usize,
    ) -> Pin<Box<dyn Future<Output = Result<ScanChunk, StorageError>> + Send + '_>> {
        Box::pin(async move {
            let (entries, more) = self.cursor.next_page(limit_rows).await?.into_parts();
            let mut trace = self.trace.lock().expect("trace lock");
            for entry in &entries {
                trace
                    .keys
                    .entry(self.space)
                    .or_default()
                    .insert(entry.key.clone());
            }
            *trace.returned.entry((self.phase, self.space)).or_default() += entries.len();
            Ok(ScanChunk::new(entries, more))
        })
    }
}

fn all_keys() -> KeyRange {
    KeyRange {
        lower: Bound::Unbounded,
        upper: Bound::Unbounded,
    }
}

fn json_string(value: Value) -> String {
    match value {
        Value::Jsonb(value) => value.as_json_string().expect("JSON string value"),
        Value::Text(value) => value,
        value => panic!("expected string-valued key/value row, got {value:?}"),
    }
}

fn key_in_range(key: &Key, range: &KeyRange) -> bool {
    let lower = match &range.lower {
        Bound::Included(bound) => key >= bound,
        Bound::Excluded(bound) => key > bound,
        Bound::Unbounded => true,
    };
    let upper = match &range.upper {
        Bound::Included(bound) => key <= bound,
        Bound::Excluded(bound) => key < bound,
        Bound::Unbounded => true,
    };
    lower && upper
}

async fn branch_control<S: Storage + Clone + Send + Sync + 'static>(
    lix: &crate::Lix<S>,
    branch_id: &str,
) -> crate::branch::BranchHeadControl {
    let adapter = lix.storage_adapter();
    let read = adapter.begin_read(ReadOptions::default()).await.unwrap();
    crate::branch::BranchHeadControlContext::new()
        .reader(&read)
        .load(branch_id)
        .await
        .unwrap()
        .unwrap()
}

/// Probe checkpoint publication and GC independently after the SQL/upload
/// gate. A blocked lifecycle probe is reported explicitly and cannot silently
/// turn into a full-baseline fetch or invalidate the successful edit result.
async fn probe_partial_checkpoint(
    partial: &Memory,
    baseline: &crate::Lix<Memory>,
    branch_id: &str,
    target: &str,
    root_backed: bool,
    row_count: usize,
) {
    let memory = partial.fork().unwrap();
    let root = open_lix().with_storage(memory.clone()).await.unwrap();
    let selected = root
        .open_another_session()
        .with_branch(branch_id)
        .await
        .unwrap();
    let before = branch_control(&selected, branch_id).await;
    // Checkpoint preparation differs from ordinary edit preparation: its
    // semantic parent is the declared checkpoint cursor. Load only that
    // immutable topology header (replay-debt/root references), not its mutation
    // inventory, tree, rows, contents, or first-parent ancestry.
    if let Some(checkpoint) = before.working_diff_checkpoint_commit_id {
        let source = baseline.storage_adapter();
        let source_read = source.begin_read(ReadOptions::default()).await.unwrap();
        let header =
            crate::tracked_state::load_published_commit_state_topology(&source_read, checkpoint)
                .await
                .unwrap()
                .expect("pinned baseline owns checkpoint parent authority");
        assert_eq!(header.commit_id(), checkpoint);
        let space = crate::tracked_state::TRACKED_STATE_COMMIT_STATE_MANIFEST_SPACE;
        let keys = [Key(bytes::Bytes::copy_from_slice(
            checkpoint.as_uuid().as_bytes(),
        ))];
        let value = source_read
            .get_many(&[GetManyRequest {
                space,
                keys: &keys,
                opts: GetOptions::default(),
            }])
            .await
            .unwrap()
            .values
            .into_iter()
            .next()
            .flatten()
            .unwrap();
        let ProjectedValue::FullValue(bytes) = value else {
            panic!("checkpoint topology omitted bytes")
        };
        let destination = selected.storage_adapter();
        let read = destination
            .begin_read(ReadOptions::default())
            .await
            .unwrap();
        let existing = read
            .get_many(&[GetManyRequest {
                space,
                keys: &keys,
                opts: GetOptions::default(),
            }])
            .await
            .unwrap()
            .values
            .into_iter()
            .next()
            .flatten();
        drop(read);
        if let Some(ProjectedValue::FullValue(existing)) = existing {
            assert_eq!(
                existing, bytes,
                "immutable checkpoint header must agree with pinned baseline"
            );
        } else {
            let copied_bytes = bytes.len();
            let mut writes = destination.new_write_set();
            writes.put_content_addressed_batch(
                space,
                [(
                    keys[0].clone(),
                    crate::storage_adapter::StorageValue { bytes },
                )],
            );
            destination
                .commit_write_set(writes, WriteOptions::default())
                .await
                .unwrap();
            eprintln!(
                "partial checkpoint prepared parent topology root_backed={root_backed} rows={row_count} objects=1 bytes={copied_bytes}"
            );
        }
    }
    let started = std::time::Instant::now();
    let checkpoint = selected
        .execute("SELECT commit_id FROM lix_create_checkpoint()", &[])
        .await;
    let checkpoint = match checkpoint {
        Ok(result) => {
            let commit_id = result.rows()[0].get::<String>("commit_id").unwrap();
            eprintln!(
                "partial checkpoint SUCCESS root_backed={root_backed} rows={row_count} elapsed_us={} commit_id={commit_id}",
                started.elapsed().as_micros()
            );
            commit_id
        }
        Err(error) => {
            eprintln!(
                "partial checkpoint BLOCKED root_backed={root_backed} rows={row_count} code={} message={} details={:?}",
                error.code, error.message, error.details
            );
            assert_eq!(
                branch_control(&selected, branch_id).await,
                before,
                "failed precommit checkpoint must not move the branch"
            );
            selected.close().await.unwrap();
            root.close().await.unwrap();
            return;
        }
    };
    let after_checkpoint = branch_control(&selected, branch_id).await;
    assert_eq!(after_checkpoint.head_commit_id.to_string(), checkpoint);
    let adapter = selected.storage_adapter();
    let read = crate::storage_adapter::SharedStorageAdapterRead::new(
        adapter.begin_read(ReadOptions::default()).await.unwrap(),
    );
    let mut writes = adapter.new_write_set();
    let mut preconditions = Vec::new();
    match crate::gc::stage_repository_gc_with_preconditions(read, &mut writes, &mut preconditions)
        .await
    {
        Ok(plan) => {
            adapter
                .commit_write_set(
                    writes,
                    WriteOptions {
                        preconditions,
                        ..WriteOptions::default()
                    },
                )
                .await
                .unwrap();
            eprintln!(
                "partial GC SUCCESS root_backed={root_backed} rows={row_count} reclaimed_commits={} has_more={}",
                plan.sweep.tracked_commit_roots.len(),
                plan.sweep.has_more
            );
        }
        Err(error) => {
            eprintln!(
                "partial GC BLOCKED root_backed={root_backed} rows={row_count} code={} message={} details={:?}; uncommitted_staged_mutations={}",
                error.code,
                error.message,
                error.details,
                writes.stats().staged_puts + writes.stats().staged_deletes
            );
            // Never commit a partially built sweep after a missing dependency.
            drop(writes);
        }
    }
    assert_eq!(branch_control(&selected, branch_id).await, after_checkpoint);
    selected.close().await.unwrap();
    root.close().await.unwrap();
    let reopened = open_lix().with_storage(memory).await.unwrap();
    let selected = reopened
        .open_another_session()
        .with_branch(branch_id)
        .await
        .unwrap();
    let row = selected
        .execute(
            "SELECT value FROM lix_key_value WHERE key = $1",
            &[Value::Text(target.to_owned())],
        )
        .await
        .unwrap();
    assert_eq!(
        json_string(row.rows()[0].get::<Value>("value").unwrap()),
        "after-2"
    );
    selected.close().await.unwrap();
    reopened.close().await.unwrap();
}

#[tokio::test]
#[ignore = "manual native SQL partial-storage dependency feasibility diagnostic"]
async fn prepared_sql_update_with_only_native_dependency_closure() {
    run_prepared_sql_dependency_case(false).await;
}

#[tokio::test]
#[ignore = "manual root-backed SQL partial-storage dependency feasibility diagnostic"]
async fn prepared_sql_update_from_root_backed_branch_with_native_dependency_closure() {
    run_prepared_sql_dependency_case(true).await;
}

async fn run_prepared_sql_dependency_case(root_backed: bool) {
    for row_count in [128, 4096] {
        eprintln!("native SQL dependency case root_backed={root_backed} rows={row_count}");
        let source_memory = Memory::new();
        let source = open_lix()
            .with_storage(source_memory.clone())
            .await
            .unwrap();
        let values = (0..row_count)
            .map(|index| format!("('partial-sql-{index:06}', 'before')"))
            .collect::<Vec<_>>()
            .join(",");
        source
            .execute(
                &format!("INSERT INTO lix_key_value (key, value) VALUES {values}"),
                &[],
            )
            .await
            .unwrap();
        let branch_id = if root_backed {
            Some(
                source
                    .create_branch(CreateBranchOptions {
                        id: None,
                        name: "partial-root-backed".to_owned(),
                        from_commit_id: None,
                    })
                    .await
                    .unwrap()
                    .id,
            )
        } else {
            None
        };
        source.close().await.unwrap();
        // Logical reads must use the admitted epoch. The recorder sees raw
        // physical spaces, so copying those addresses uses a separate raw
        // adapter; never mix the two coordinate systems.
        let baseline_lix = open_lix()
            .with_storage(source_memory.fork().unwrap())
            .await
            .unwrap();
        let baseline = baseline_lix.storage_adapter();
        let baseline_raw = StorageAdapter::new(source_memory.fork().unwrap());
        if let Some(branch_id) = &branch_id {
            let read = baseline.begin_read(ReadOptions::default()).await.unwrap();
            let control = crate::branch::BranchHeadControlContext::new()
                .reader(&read)
                .load(branch_id)
                .await
                .unwrap()
                .unwrap();
            let keys = [Key(crate::hot_state::hot_generation_scope_prefix(
                branch_id,
                control.tracked_generation,
            )
            .into())];
            let root_pointer = read
                .get_many(&[GetManyRequest {
                    space: crate::hot_state::ROOT_CURRENT_BASE_SPACE,
                    keys: &keys,
                    opts: GetOptions::default(),
                }])
                .await
                .unwrap();
            assert!(
                root_pointer.values[0].is_some(),
                "fixture must actually use the native root-backed current-state route"
            );
        }
        let trace = Arc::new(Mutex::new(Trace {
            phase: "open",
            ..Trace::default()
        }));
        let preparing_root = open_lix()
            .with_storage(RecordingStorage {
                memory: source_memory.fork().unwrap(),
                trace: Arc::clone(&trace),
            })
            .await
            .unwrap();
        trace.lock().unwrap().phase = "selected-session";
        let preparing = match &branch_id {
            Some(branch_id) => preparing_root
                .open_another_session()
                .with_branch(branch_id)
                .await
                .unwrap(),
            None => preparing_root.open_another_session().await.unwrap(),
        };
        let target = format!("partial-sql-{:06}", row_count / 2);
        let parameters = [Value::Text(target.clone())];
        trace.lock().unwrap().phase = "point-read";
        preparing
            .execute(
                "SELECT value FROM lix_key_value WHERE key = $1",
                &parameters,
            )
            .await
            .unwrap();
        trace.lock().unwrap().phase = "write-spine";
        let preparing_branch = preparing.active_branch_id().await.unwrap();
        let mut cursor = branch_control(&preparing, &preparing_branch)
            .await
            .head_commit_id;
        let adapter = preparing.storage_adapter();
        let spine_read = adapter.begin_read(ReadOptions::default()).await.unwrap();
        let mut spine = BTreeSet::new();
        loop {
            assert!(
                spine.insert(cursor),
                "baseline jump spine must be acyclic except its terminal self link"
            );
            let ids = [cursor];
            let record = crate::changelog::ChangelogContext::new()
                .reader(&spine_read)
                .load_commits(crate::changelog::CommitLoadRequest { commit_ids: &ids })
                .await
                .unwrap()
                .into_iter()
                .next()
                .unwrap()
                .1
                .unwrap();
            if record.first_parent_jump_commit_id == cursor {
                break;
            }
            cursor = record.first_parent_jump_commit_id;
        }
        eprintln!(
            "native SQL prepared Myers spine root_backed={root_backed} rows={row_count} headers={}",
            spine.len()
        );
        drop(spine_read);
        trace.lock().unwrap().phase = "update";
        preparing
            .execute(
                "UPDATE lix_key_value SET value = $2 WHERE key = $1",
                &[
                    Value::Text(target.clone()),
                    Value::Text("after-0".to_owned()),
                ],
            )
            .await
            .unwrap();
        trace.lock().unwrap().phase = "close";
        preparing.close().await.unwrap();
        preparing_root.close().await.unwrap();

        let keys = trace.lock().unwrap().keys.clone();
        let local_memory = Memory::new();
        let local_adapter = StorageAdapter::new(local_memory.clone());
        let baseline_read = baseline.begin_read(ReadOptions::default()).await.unwrap();
        let baseline_raw_read = baseline_raw
            .begin_read(ReadOptions::default())
            .await
            .unwrap();
        let mut writes = local_adapter.new_write_set();
        let mut copied = BTreeMap::<StorageSpace, (usize, usize)>::new();
        for (space, keys) in keys {
            let keys = keys.into_iter().collect::<Vec<_>>();
            let values = baseline_raw_read
                .get_many(&[GetManyRequest {
                    space,
                    keys: &keys,
                    opts: GetOptions::default(),
                }])
                .await
                .unwrap();
            for (key, value) in keys.into_iter().zip(values.values) {
                if let Some(ProjectedValue::FullValue(bytes)) = value {
                    let counts = copied.entry(space).or_default();
                    counts.0 += 1;
                    counts.1 += bytes.len();
                    writes.put(space, key, StoredValue { bytes });
                }
            }
        }
        local_adapter
            .commit_write_set(writes, WriteOptions::default())
            .await
            .unwrap();
        let mut total = BTreeMap::new();
        for &space in crate::storage_spaces::ALL_STORAGE_SPACES {
            let entries = baseline_read
                .begin_scan(space, all_keys(), BeginScanOptions::default())
                .await
                .unwrap()
                .collect_all()
                .await
                .unwrap();
            if !entries.is_empty() {
                total.insert(
                    space,
                    (
                        entries.len(),
                        entries
                            .iter()
                            .map(|entry| match &entry.value {
                                ProjectedValue::FullValue(bytes) => bytes.len(),
                                ProjectedValue::KeyOnly => 0,
                            })
                            .sum::<usize>(),
                    ),
                );
            }
        }
        for (space, (total_keys, total_bytes)) in &total {
            let (copied_keys, copied_bytes) = copied
                .iter()
                .filter(|(physical, _)| physical.name == space.name)
                .fold((0, 0), |(keys, bytes), (_, (more_keys, more_bytes))| {
                    (keys + more_keys, bytes + more_bytes)
                });
            eprintln!(
                "native SQL closure rows={row_count} space={} copied_keys={copied_keys}/{total_keys} copied_bytes={copied_bytes}/{total_bytes}",
                space.name
            );
        }
        {
            let trace = trace.lock().unwrap();
            for ((phase, space), count) in &trace.returned {
                eprintln!(
                    "native SQL returned rows={row_count} phase={phase} space={} count={count}",
                    space.name
                );
            }
            for (phase, space, range) in &trace.scans {
                eprintln!(
                    "native SQL range rows={row_count} phase={phase} space={} range={range:?}",
                    space.name
                );
            }
            for (phase, precondition) in &trace.write_preconditions {
                eprintln!(
                    "native SQL write precondition rows={row_count} phase={phase} {precondition:?}"
                );
            }
        }

        // This is the feasibility gate. Any missing native publication input
        // must fail visibly here; never install a fake full-state certificate.
        let local_trace = Arc::new(Mutex::new(Trace {
            phase: "local",
            ..Trace::default()
        }));
        let local_root = open_lix()
            .with_storage(RecordingStorage {
                memory: local_memory.clone(),
                trace: Arc::clone(&local_trace),
            })
            .await
            .unwrap_or_else(|error| {
                panic!("partial native open needs additional dependencies: {error}")
            });
        let local = match &branch_id {
            Some(branch_id) => local_root
                .open_another_session()
                .with_branch(branch_id)
                .await
                .unwrap(),
            None => local_root.open_another_session().await.unwrap(),
        };
        let publication_branch_id = local.active_branch_id().await.unwrap();
        let before_control = branch_control(&local, &publication_branch_id).await;
        let authority_before = crate::branch::BranchHeadControlContext::new()
            .reader(&baseline_read)
            .load(&publication_branch_id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(
            before_control.head_commit_id, authority_before.head_commit_id,
            "selecting the editing session must not author unexported setup commits"
        );
        assert_eq!(
            before_control.working_diff_checkpoint_commit_id,
            authority_before.working_diff_checkpoint_commit_id
        );
        let account_id = local.active_account_id().to_owned();
        let mut commit_ids = Vec::new();
        for step in 0..3 {
            let updated_value = format!("after-{step}");
            local
                .execute(
                    "UPDATE lix_key_value SET value = $2 WHERE key = $1",
                    &[
                        Value::Text(target.clone()),
                        Value::Text(updated_value.clone()),
                    ],
                )
                .await
                .unwrap_or_else(|error| {
                    panic!("prepared partial native SQL update {step} failed: {error}")
                });
            let result = local
                .execute(
                    "SELECT value FROM lix_key_value WHERE key = $1",
                    &parameters,
                )
                .await
                .unwrap();
            assert_eq!(result.rows().len(), 1);
            assert_eq!(
                json_string(result.rows()[0].get::<Value>("value").unwrap()),
                updated_value
            );
            let head = branch_control(&local, &publication_branch_id)
                .await
                .head_commit_id;
            assert_ne!(head, before_control.head_commit_id);
            assert!(
                !commit_ids.contains(&head),
                "every update must author a distinct native commit"
            );
            commit_ids.push(head);
        }
        let after_control = branch_control(&local, &publication_branch_id).await;
        local.close().await.unwrap();
        local_root.close().await.unwrap();

        // Reopen the same still-partial storage before exporting. No baseline
        // bytes or authority reads enter the local path between editing and
        // export: only the three newly authored commit bodies leave it.
        let reopened = open_lix()
            .with_storage(RecordingStorage {
                memory: local_memory.clone(),
                trace: Arc::clone(&local_trace),
            })
            .await
            .unwrap_or_else(|error| {
                panic!("partial native reopen before publication failed: {error}")
            });
        let mut commits = Vec::new();
        let mut expected_parent = before_control.head_commit_id.to_string();
        for commit_id in &commit_ids {
            let commit = crate::sync::export_sync_commit(&reopened, &commit_id.to_string())
                .await
                .unwrap_or_else(|error| panic!("partial native commit export failed: {error}"))
                .expect("locally authored commit must export");
            assert_eq!(commit.parent_commit_ids, vec![expected_parent]);
            assert_eq!(commit.account_id, account_id);
            assert_eq!(
                commit.members.len(),
                1,
                "one-row update must not export the untouched base"
            );
            expected_parent = commit.commit_id.clone();
            commits.push(commit);
        }
        reopened.close().await.unwrap();
        let request = crate::sync::SyncPushRequest {
            commits,
            ref_updates: serde_json::from_value(serde_json::json!([{
                "branchId": publication_branch_id,
                "expectedHeadCommitId": before_control.head_commit_id.to_string(),
                "expectedCheckpointCommitId": before_control.working_diff_checkpoint_commit_id.map(|id| id.to_string()),
                "headCommitId": after_control.head_commit_id.to_string(),
                "checkpointCommitId": after_control.working_diff_checkpoint_commit_id.map(|id| id.to_string()),
            }])).unwrap(),
            inline_blobs: Vec::new(),
        };
        eprintln!(
            "native SQL publication root_backed={root_backed} rows={row_count} commits={} request_bytes={}",
            request.commits.len(),
            serde_json::to_vec(&request).unwrap().len()
        );

        // The independent authority still owns the original complete base.
        // Use normal account-bound import and exact head/checkpoint CAS, never
        // the test-only trusted-authorship importer or a replica receipt.
        let authority = open_lix()
            .with_storage(source_memory.fork().unwrap())
            .await
            .unwrap();
        authority
            .push_sync_repository_for_account(&request, &account_id)
            .await
            .unwrap_or_else(|error| {
                panic!("authority rejected partial native publication: {error}")
            });
        assert_eq!(
            branch_control(&authority, &publication_branch_id)
                .await
                .head_commit_id,
            after_control.head_commit_id
        );
        for commit in &request.commits {
            assert_eq!(
                crate::sync::export_sync_commit(&authority, &commit.commit_id)
                    .await
                    .unwrap()
                    .as_ref(),
                Some(commit),
                "authority must preserve exact native commit identity and membership"
            );
        }
        let authority_selected = authority
            .open_another_session()
            .with_branch(&publication_branch_id)
            .await
            .unwrap();
        let authority_rows = authority_selected
            .execute(
                "SELECT key, value FROM lix_key_value WHERE key LIKE 'partial-sql-%' ORDER BY key",
                &[],
            )
            .await
            .unwrap();
        assert_eq!(authority_rows.rows().len(), row_count);
        for (index, row) in authority_rows.rows().iter().enumerate() {
            let key = format!("partial-sql-{index:06}");
            assert_eq!(row.get::<String>("key").unwrap(), key);
            assert_eq!(
                json_string(row.get::<Value>("value").unwrap()),
                if key == target { "after-2" } else { "before" }
            );
        }
        authority_selected.close().await.unwrap();
        authority.close().await.unwrap();
        probe_partial_checkpoint(
            &local_memory,
            &baseline_lix,
            &publication_branch_id,
            &target,
            root_backed,
            row_count,
        )
        .await;

        // Reconstitute only in the verifier; don't overwrite local mutations.
        let verifier_memory = local_memory.fork().unwrap();
        let verifier_session = open_lix()
            .with_storage(verifier_memory.clone())
            .await
            .unwrap();
        let verifier_adapter = verifier_session.storage_adapter();
        let local_read = verifier_adapter
            .begin_read(ReadOptions::default())
            .await
            .unwrap();
        let mut writes = verifier_adapter.new_write_set();
        for &space in crate::storage_spaces::ALL_STORAGE_SPACES {
            let entries = baseline_read
                .begin_scan(space, all_keys(), BeginScanOptions::default())
                .await
                .unwrap()
                .collect_all()
                .await
                .unwrap();
            for entry in entries {
                // Internal index maintenance can delete keys even for a SQL
                // UPDATE. Restoring those would resurrect deliberately removed
                // state and would invalidate the untouched-row verifier.
                let deleted = {
                    let trace = local_trace.lock().unwrap();
                    trace.deleted.iter().any(|(deleted_space, key)| {
                        deleted_space.name == space.name && key == &entry.key
                    }) || trace.deleted_ranges.iter().any(|(deleted_space, range)| {
                        deleted_space.name == space.name && key_in_range(&entry.key, range)
                    })
                };
                if deleted {
                    continue;
                }
                let keys = [entry.key.clone()];
                let existing = local_read
                    .get_many(&[GetManyRequest {
                        space,
                        keys: &keys,
                        opts: GetOptions::default(),
                    }])
                    .await
                    .unwrap();
                if existing.values[0].is_none() {
                    if let ProjectedValue::FullValue(bytes) = entry.value {
                        writes.put(space, entry.key, StoredValue { bytes });
                    }
                }
            }
        }
        drop(local_read);
        verifier_adapter
            .commit_write_set(writes, WriteOptions::default())
            .await
            .unwrap();
        verifier_session.close().await.unwrap();
        let verifier_root = open_lix().with_storage(verifier_memory).await.unwrap();
        let verifier = verifier_root
            .open_another_session()
            .with_branch(&publication_branch_id)
            .await
            .unwrap();
        let rows = verifier
            .execute(
                "SELECT key, value FROM lix_key_value WHERE key LIKE 'partial-sql-%' ORDER BY key",
                &[],
            )
            .await
            .unwrap();
        assert_eq!(rows.rows().len(), row_count);
        for (index, row) in rows.rows().iter().enumerate() {
            let key = format!("partial-sql-{index:06}");
            assert_eq!(row.get::<String>("key").unwrap(), key);
            assert_eq!(
                json_string(row.get::<Value>("value").unwrap()),
                if key == target { "after-2" } else { "before" }
            );
        }
        verifier.close().await.unwrap();
        verifier_root.close().await.unwrap();
    }
}