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
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
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
//! Exact, storage-independent representation of one immutable Lix commit.
//!
//! The wire shape carries the logical commit authority rather than Lix's
//! physical packed-delta layout. JSON sidecars are materialized so a commit is
//! self-contained, while binary file content remains referenced by the
//! `lix_binary_blob_ref` row and travels through the binary CAS protocol.

use std::collections::BTreeSet;

use bytes::Bytes;
use serde::{Deserialize, Serialize};

use crate::changelog::{
    ChangeRecordProjection, ChangelogContext, ChangelogReader, CommitId, CommitLoadRequest,
    materialize_known_change_payloads_in_order,
};
use crate::common::LixTimestamp;
use crate::row_pk::RowPk;
use crate::storage_adapter::{
    Storage, StorageAdapterRead, StorageGetManyRequest, StorageGetOptions, StorageKey,
    StorageProjectedValue, StorageReadOptions, StorageSpace, StorageSpaceId, StorageValue,
    StorageWriteSet, ValueSemantics, exact_get_many,
};
use crate::tracked_state::{
    load_commit_delta_members_with_payloads, load_commit_state_manifest,
    load_local_commit_delta_members_with_payloads,
};
use crate::{Lix, LixError};

pub(crate) const SYNC_MATERIALIZED_STATE_ALIAS_SPACE: StorageSpace = StorageSpace::declare(
    StorageSpaceId(0x0007_0015),
    "sync.materialized_state_alias.v1",
    ValueSemantics::Immutable,
);

/// Local publication provenance, never a canonical parent or wire state alias.
/// A scoped checkpoint replaces its working interval with selected members;
/// this fixed-size record preserves the captured local head for sync admission.
pub(crate) const SYNC_CHECKPOINT_SOURCE_SPACE: StorageSpace = StorageSpace::declare(
    StorageSpaceId(0x0007_0016),
    "sync.checkpoint_source.v1",
    ValueSemantics::Immutable,
);

pub(crate) fn stage_sync_checkpoint_source(
    writes: &mut StorageWriteSet,
    branch_id: &str,
    checkpoint: CommitId,
    source: CommitId,
) -> Result<(), LixError> {
    let branch = uuid::Uuid::parse_str(branch_id).map_err(|error| {
        LixError::new(
            LixError::CODE_INTERNAL_ERROR,
            format!("invalid checkpoint source branch: {error}"),
        )
    })?;
    let mut bytes = Vec::with_capacity(32);
    bytes.extend_from_slice(branch.as_bytes());
    bytes.extend_from_slice(source.as_uuid().as_bytes());
    writes.put(
        SYNC_CHECKPOINT_SOURCE_SPACE,
        materialized_state_alias_key(checkpoint),
        StorageValue {
            bytes: Bytes::from(bytes),
        },
    );
    Ok(())
}

pub(crate) async fn load_sync_checkpoint_source(
    store: &(impl StorageAdapterRead + ?Sized),
    checkpoint: CommitId,
) -> Result<Option<(String, CommitId)>, LixError> {
    let key = materialized_state_alias_key(checkpoint);
    let values = exact_get_many(
        store,
        &[StorageGetManyRequest {
            space: SYNC_CHECKPOINT_SOURCE_SPACE,
            keys: std::slice::from_ref(&key),
            opts: StorageGetOptions::default(),
        }],
    )
    .await?;
    let Some(value) = values.values.into_iter().next().flatten() else {
        return Ok(None);
    };
    let StorageProjectedValue::FullValue(bytes) = value else {
        return Err(LixError::new(
            LixError::CODE_INTERNAL_ERROR,
            "checkpoint source read omitted its value",
        ));
    };
    if bytes.len() != 32 {
        return Err(LixError::new(
            LixError::CODE_INTERNAL_ERROR,
            "checkpoint source record must contain two UUIDs",
        ));
    }
    let branch = uuid::Uuid::from_slice(&bytes[..16]).expect("validated UUID length");
    let source = uuid::Uuid::from_slice(&bytes[16..]).expect("validated UUID length");
    Ok(Some((
        branch.to_string(),
        CommitId::parse_lix(&source.to_string(), "checkpoint source")?,
    )))
}

pub(crate) fn stage_delete_sync_checkpoint_source(
    writes: &mut StorageWriteSet,
    checkpoint: CommitId,
) {
    writes.delete(
        SYNC_CHECKPOINT_SOURCE_SPACE,
        materialized_state_alias_key(checkpoint),
    );
}

/// A complete immutable commit, independent of the local storage layout.
///
/// Generation and first-parent jump pointers are intentionally omitted: they
/// are derived indexes validated from `parent_commit_ids` when the commit is
/// imported. The remaining header fields are semantic commit authority.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncCommit {
    /// Immutable commit membership; required by the current protocol.
    pub is_checkpoint: bool,
    pub commit_id: String,
    pub parent_commit_ids: Vec<String>,
    pub base_commit_id: Option<String>,
    pub account_id: String,
    pub created_at: String,
    #[serde(default)]
    pub global_scope: bool,
    pub selected_source_commit_id: Option<String>,
    /// Authenticated O(1) representation of a complete-state checkpoint.
    /// The source is a dependency and `state_root_id` binds the alias to the
    /// exact persistent tree the authority captured.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub state_alias: Option<SyncCommitStateAlias>,
    /// Complete compact-state provenance, independent of delta membership.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub complete_incorporation_source_commit_id: Option<String>,
    #[serde(default, skip_serializing_if = "is_false")]
    pub incorporation_unknown: bool,
    pub members: Vec<SyncCommitMember>,
}

fn is_false(value: &bool) -> bool {
    !value
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncCommitStateAlias {
    pub source_commit_id: String,
    pub state_root_id: String,
}

fn materialized_state_alias_key(commit_id: CommitId) -> StorageKey {
    StorageKey(Bytes::copy_from_slice(commit_id.as_uuid().as_bytes()))
}

pub(crate) fn stage_materialized_sync_state_alias(
    writes: &mut StorageWriteSet,
    commit_id: CommitId,
    alias: &SyncCommitStateAlias,
) -> Result<(), LixError> {
    let bytes = serde_json::to_vec(alias).map_err(|error| {
        LixError::new(
            LixError::CODE_INTERNAL_ERROR,
            format!("encode materialized sync state alias: {error}"),
        )
    })?;
    writes.put(
        SYNC_MATERIALIZED_STATE_ALIAS_SPACE,
        materialized_state_alias_key(commit_id),
        StorageValue {
            bytes: Bytes::from(bytes),
        },
    );
    Ok(())
}

pub(crate) fn stage_delete_materialized_sync_state_alias(
    writes: &mut StorageWriteSet,
    commit_id: CommitId,
) {
    writes.delete(
        SYNC_MATERIALIZED_STATE_ALIAS_SPACE,
        materialized_state_alias_key(commit_id),
    );
}

async fn load_materialized_sync_state_alias(
    store: &(impl StorageAdapterRead + ?Sized),
    commit_id: CommitId,
) -> Result<Option<SyncCommitStateAlias>, LixError> {
    let key = materialized_state_alias_key(commit_id);
    let values = exact_get_many(
        store,
        &[StorageGetManyRequest {
            space: SYNC_MATERIALIZED_STATE_ALIAS_SPACE,
            keys: std::slice::from_ref(&key),
            opts: StorageGetOptions::default(),
        }],
    )
    .await?;
    let Some(value) = values.values.into_iter().next().flatten() else {
        return Ok(None);
    };
    let StorageProjectedValue::FullValue(value) = value else {
        return Err(LixError::new(
            LixError::CODE_INTERNAL_ERROR,
            "materialized sync state alias read omitted its value",
        ));
    };
    serde_json::from_slice(&value).map(Some).map_err(|error| {
        LixError::new(
            LixError::CODE_INTERNAL_ERROR,
            format!("decode materialized sync state alias: {error}"),
        )
    })
}

pub(crate) async fn load_sync_commit_state_alias(
    store: &(impl StorageAdapterRead + ?Sized),
    commit_id: CommitId,
) -> Result<Option<SyncCommitStateAlias>, LixError> {
    if let Some(alias) = load_materialized_sync_state_alias(store, commit_id).await? {
        return Ok(Some(alias));
    }
    load_manifest_sync_state_alias(store, commit_id).await
}

/// Snapshot materialization preserves the authenticated alias separately from
/// its independently rooted native state. Absence is not a negative proof.
pub(crate) async fn load_complete_state_alias_source(
    store: &(impl StorageAdapterRead + ?Sized),
    commit_id: CommitId,
    native_source: Option<CommitId>,
) -> Result<Option<CommitId>, LixError> {
    if native_source.is_some() {
        return Ok(native_source);
    }
    load_materialized_sync_state_alias(store, commit_id)
        .await?
        .map(|alias| {
            CommitId::parse_lix(&alias.source_commit_id, "materialized state alias source")
        })
        .transpose()
}

async fn load_manifest_sync_state_alias(
    store: &(impl StorageAdapterRead + ?Sized),
    commit_id: CommitId,
) -> Result<Option<SyncCommitStateAlias>, LixError> {
    Ok(load_commit_state_manifest(store, commit_id)
        .await?
        .and_then(|manifest| manifest.snapshot_root)
        .filter(|root| root.complete_state_fence)
        .and_then(|root| {
            let source = root.parent_roots.into_iter().next()?;
            Some(SyncCommitStateAlias {
                source_commit_id: source.commit_id.to_string(),
                state_root_id: blake3::Hash::from_bytes(*root.root_id.as_bytes())
                    .to_hex()
                    .to_string(),
            })
        }))
}

/// One identity-ordered member of a commit delta.
///
/// Row lifecycle timestamps are distinct from the authored change timestamp:
/// selected merge members retain their source change while acquiring state
/// coordinates in the selecting commit.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncCommitMember {
    pub change_id: String,
    pub authored: bool,
    pub schema_key: String,
    pub file_id: Option<String>,
    pub row_pk: serde_json::Value,
    pub deleted: bool,
    pub snapshot: Option<serde_json::Value>,
    /// Canonical Schema v1 typed row payload, base64 encoded; absent for tombstones.
    pub snapshot_payload: Option<String>,
    pub metadata: Option<serde_json::Value>,
    pub row_created_at: String,
    pub row_updated_at: String,
    pub change_account_id: String,
    pub change_created_at: String,
    pub origin_key: Option<String>,
}

pub(crate) struct SyncCommitMemberRef<'a> {
    pub(crate) change_id: crate::changelog::ChangeId,
    pub(crate) authored: bool,
    pub(crate) schema_key: &'a str,
    pub(crate) file_id: Option<&'a str>,
    pub(crate) row_pk: &'a RowPk,
    pub(crate) deleted: bool,
    pub(crate) snapshot_json: Option<&'a str>,
    pub(crate) decoded_snapshot: Option<&'a crate::row_payload::TypedRow>,
    pub(crate) metadata_json: Option<&'a str>,
    pub(crate) row_created_at: LixTimestamp,
    pub(crate) row_updated_at: LixTimestamp,
    pub(crate) change_account_id: &'a str,
    pub(crate) change_created_at: LixTimestamp,
    pub(crate) origin_key: Option<&'a str>,
}

/// One canonical member encoder shared by staged authority preflight and
/// post-commit storage export. Keeping JSON parsing and typed primary-key
/// projection here makes byte-size admission test the exact public wire shape.
pub(crate) fn encode_sync_commit_member(
    member: SyncCommitMemberRef<'_>,
) -> Result<SyncCommitMember, LixError> {
    Ok(SyncCommitMember {
        change_id: member.change_id.to_string(),
        authored: member.authored,
        schema_key: member.schema_key.to_owned(),
        file_id: member.file_id.map(str::to_owned),
        row_pk: member.row_pk.as_typed_json_array_value()?,
        deleted: member.deleted,
        snapshot: parse_materialized_json(member.snapshot_json, member.change_id, "snapshot")?,
        snapshot_payload: member
            .decoded_snapshot
            .map(encode_sync_row_payload)
            .transpose()?,
        metadata: parse_materialized_json(member.metadata_json, member.change_id, "metadata")?,
        row_created_at: member.row_created_at.to_string(),
        row_updated_at: member.row_updated_at.to_string(),
        change_account_id: member.change_account_id.to_owned(),
        change_created_at: member.change_created_at.to_string(),
        origin_key: member.origin_key.map(str::to_owned),
    })
}

/// Re-encode the logical typed row so compression and storage caches do not
/// change immutable wire identity between preflight and later export.
pub(crate) fn encode_sync_row_payload(
    row: &crate::row_payload::TypedRow,
) -> Result<String, LixError> {
    use base64::Engine as _;
    let bytes = crate::plugin::wire::typed::encode_native_row_payload_with_identity(
        &row.schema_fingerprint,
        &row.row_pk,
        &row.row,
    )
    .map_err(|error| {
        LixError::new(
            LixError::CODE_SCHEMA_VALIDATION,
            format!("encode sync typed row: {error:?}"),
        )
    })?;
    Ok(base64::engine::general_purpose::STANDARD.encode(bytes))
}

pub(crate) fn decode_sync_row_payload(
    schema_key: &str,
    row_pk: &RowPk,
    snapshot: &serde_json::Value,
    payload: Option<&str>,
) -> Result<Vec<u8>, LixError> {
    use base64::Engine as _;
    let invalid = |message: String| LixError::new(LixError::CODE_INVALID_PARAM, message);
    let payload =
        payload.ok_or_else(|| invalid("live sync row is missing snapshotPayload".into()))?;
    let bytes = base64::engine::general_purpose::STANDARD
        .decode(payload)
        .map_err(|error| invalid(format!("invalid sync snapshotPayload: {error}")))?;
    let row = crate::row_payload::TypedRow::decode_durable_payload(
        std::sync::Arc::from(bytes.clone()),
        schema_key,
        row_pk,
    )
    .map_err(|error| invalid(format!("invalid sync snapshotPayload: {}", error.message)))?;
    if let Some((_, plan)) = crate::catalog::CatalogSnapshot::builtin().plan_for_key(schema_key) {
        let expected =
            crate::row_payload::TypedRow::from_normalized_json(plan, row_pk, snapshot)
                .map_err(|error| invalid(format!("invalid sync snapshot: {}", error.message)))?;
        if encode_sync_row_payload(&expected).map_err(|error| invalid(error.message))? != payload {
            return Err(invalid(format!(
                "sync row for schema '{schema_key}' does not match its built-in schema"
            )));
        }
    }
    let decoded_pk = RowPk::from_schema_values(&row.row_pk)
        .map_err(|error| invalid(format!("invalid sync primary key: {error:?}")))?;
    if decoded_pk != *row_pk
        || row
            .to_json_value()
            .map_err(|error| invalid(error.message))?
            != *snapshot
        || encode_sync_row_payload(&row).map_err(|error| invalid(error.message))? != payload
    {
        return Err(invalid(format!(
            "sync row for schema '{schema_key}' has different content than its declared Schema v1 identity"
        )));
    }
    Ok(bytes)
}

impl SyncCommit {
    /// Validates all untrusted identities before storage or graph code sees
    /// them. Member order is part of the canonical wire representation.
    pub(crate) fn validate(&self) -> Result<(), LixError> {
        let commit_id = CommitId::parse_lix(&self.commit_id, "sync commit id")?;
        parse_timestamp("sync commit createdAt", &self.created_at)?;
        if self.account_id.is_empty() {
            return invalid("sync commit accountId must not be empty");
        }

        let mut parents = BTreeSet::new();
        for parent in &self.parent_commit_ids {
            let parent = CommitId::parse_lix(parent, "sync parent commit id")?;
            if parent == commit_id {
                return invalid("sync commit cannot be its own parent");
            }
            if !parents.insert(parent) {
                return invalid("sync commit parent ids must be unique");
            }
        }
        let base_commit_id = self
            .base_commit_id
            .as_deref()
            .map(|base| CommitId::parse_lix(base, "sync base commit id"))
            .transpose()?;
        if base_commit_id == Some(commit_id) {
            return invalid("sync commit cannot use itself as its base");
        }
        if self.global_scope && base_commit_id.is_some() {
            return invalid("global sync commit must not have a base");
        }
        if !self.global_scope && base_commit_id.is_none() {
            return invalid("local sync commit must have a base");
        }
        let selected_source_commit_id = self
            .selected_source_commit_id
            .as_deref()
            .map(|source| CommitId::parse_lix(source, "sync selected source commit id"))
            .transpose()?;
        if selected_source_commit_id == Some(commit_id) {
            return invalid("sync commit cannot select itself as its source");
        }
        if let Some(source) = &self.complete_incorporation_source_commit_id {
            let source = CommitId::parse_lix(source, "sync incorporation source")?;
            if source == commit_id
                || self.incorporation_unknown
                || self.parent_commit_ids.len() != 1
                || self.selected_source_commit_id.is_some()
            {
                return invalid("sync compact incorporation has invalid ownership or source");
            }
            if self
                .state_alias
                .as_ref()
                .is_some_and(|alias| alias.source_commit_id != source)
            {
                return invalid("sync alias and incorporation sources disagree");
            }
        }
        if let Some(alias) = &self.state_alias {
            let source = CommitId::parse_lix(
                &alias.source_commit_id,
                "sync complete-state source commit id",
            )?;
            if source == commit_id {
                return invalid("sync commit cannot alias its own state");
            }
            super::validate_blake3_id(
                &alias.state_root_id,
                "sync complete-state alias stateRootId",
            )?;
            if self.selected_source_commit_id.is_some() {
                return invalid(
                    "sync commit cannot carry both selected and complete-state sources",
                );
            }
            if self.parent_commit_ids.len() != 1 {
                return invalid("sync complete-state alias must have exactly one semantic parent");
            }
            if !self.members.is_empty() {
                return invalid("sync complete-state alias must not carry commit members");
            }
        }

        let mut previous: Option<(String, Option<String>, RowPk)> = None;
        let mut authored_change_ids = BTreeSet::new();
        for member in &self.members {
            if member.schema_key.is_empty() {
                return invalid("sync commit member schemaKey must not be empty");
            }
            if member.change_account_id.is_empty() {
                return invalid("sync commit member changeAccountId must not be empty");
            }
            let change_id = crate::changelog::ChangeId::parse_lix(
                &member.change_id,
                "sync commit member change id",
            )?;
            if member.authored && member.change_account_id != self.account_id {
                return invalid("authored sync member account must match its commit account");
            }
            if member.authored && !authored_change_ids.insert(change_id) {
                return invalid("authored sync member change ids must be unique");
            }
            let row_created_at =
                parse_timestamp("sync member rowCreatedAt", &member.row_created_at)?;
            let row_updated_at =
                parse_timestamp("sync member rowUpdatedAt", &member.row_updated_at)?;
            if row_created_at > row_updated_at {
                return invalid("sync member rowCreatedAt must not follow rowUpdatedAt");
            }
            parse_timestamp("sync member changeCreatedAt", &member.change_created_at)?;
            if member.deleted == member.snapshot.is_some()
                || member.deleted == member.snapshot_payload.is_some()
            {
                return invalid(
                    "sync commit member must have a snapshot exactly when it is not deleted",
                );
            }
            let row_pk = RowPk::from_typed_json_array_value(&member.row_pk).map_err(|error| {
                LixError::new(
                    LixError::CODE_INVALID_PARAM,
                    format!("sync commit member rowPk is invalid: {error}"),
                )
            })?;
            let identity = (member.schema_key.clone(), member.file_id.clone(), row_pk);
            if previous
                .as_ref()
                .is_some_and(|previous| previous >= &identity)
            {
                return invalid("sync commit members must be strictly identity ordered");
            }
            previous = Some(identity);
        }
        let has_selected_members = self.members.iter().any(|member| !member.authored);
        let is_merge = self.parent_commit_ids.len() > 1;
        if is_merge && has_selected_members {
            if self.selected_source_commit_id.as_deref()
                != self.parent_commit_ids.get(1).map(String::as_str)
            {
                return invalid("merge selectedSourceCommitId must equal its second parent");
            }
        } else if selected_source_commit_id.is_some() {
            return invalid(
                "selectedSourceCommitId is allowed only for a merge with selected members",
            );
        }
        Ok(())
    }
}

fn parse_timestamp(context: &str, value: &str) -> Result<LixTimestamp, LixError> {
    LixTimestamp::parse(value).map_err(|error| {
        LixError::new(
            LixError::CODE_INVALID_PARAM,
            format!("{context} is invalid: {error}"),
        )
    })
}

fn invalid<T>(message: impl Into<String>) -> Result<T, LixError> {
    Err(LixError::new(LixError::CODE_INVALID_PARAM, message.into()))
}

/// Exports one complete logical commit from a repository.
///
/// Missing ids are ordinary history results, so they remain `None` instead of
/// being collapsed into a transport error.
pub(crate) async fn export_sync_commit<StorageImpl>(
    lix: &Lix<StorageImpl>,
    commit_id: &str,
) -> Result<Option<SyncCommit>, LixError>
where
    StorageImpl: Storage + Clone + Send + Sync + 'static,
{
    let commit_id = CommitId::parse_lix(commit_id, "sync commit id")?;
    let adapter = lix.storage_adapter();
    let read = adapter.begin_read(StorageReadOptions::default()).await?;
    load_sync_commit(&read, commit_id).await
}

pub(crate) async fn load_sync_commit<S>(
    store: &S,
    commit_id: CommitId,
) -> Result<Option<SyncCommit>, LixError>
where
    S: StorageAdapterRead + ?Sized,
{
    #[cfg(test)]
    super::upload_metrics::record_commit_payload_load();
    let requested = [commit_id];
    let record = ChangelogContext::new()
        .reader(store)
        .load_commits(CommitLoadRequest {
            commit_ids: &requested,
        })
        .await?
        .into_iter()
        .next()
        .and_then(|(_, record)| record);
    let Some(record) = record else {
        if crate::tracked_state::commit_history_is_omitted(store, commit_id).await? {
            return Err(crate::tracked_state::NativeMetadataRef::CommitGraphRecord(
                commit_id.to_string(),
            )
            .annotate_missing(LixError::new(
                "LIX_SYNC_HISTORY_REQUIRED",
                "snapshot semantic source graph must be hydrated before its history body",
            )));
        }
        return Ok(None);
    };

    let materialized_state_alias = load_materialized_sync_state_alias(store, commit_id).await?;
    let state_alias = match materialized_state_alias.clone() {
        Some(alias) => Some(alias),
        None => load_manifest_sync_state_alias(store, commit_id).await?,
    };
    let delta = if materialized_state_alias.is_some() {
        Vec::new()
    } else if state_alias.is_some() {
        load_local_commit_delta_members_with_payloads(store, commit_id).await?
    } else {
        load_commit_delta_members_with_payloads(store, commit_id).await?
    };
    let payloads = materialize_known_change_payloads_in_order(
        delta.iter().map(|member| member.change.clone()),
        ChangeRecordProjection::full(),
    )?;
    if payloads.len() != delta.len() {
        return Err(LixError::new(
            LixError::CODE_INTERNAL_ERROR,
            "sync commit payload count does not match its delta membership",
        ));
    }

    let members = delta
        .into_iter()
        .zip(payloads)
        .map(|(member, (change_id, payload))| {
            if change_id != member.value.change_id || change_id != member.change.change_id {
                return Err(LixError::new(
                    LixError::CODE_INTERNAL_ERROR,
                    format!(
                        "sync commit '{commit_id}' member payload identity does not match its delta"
                    ),
                ));
            }
            encode_sync_commit_member(SyncCommitMemberRef {
                change_id,
                authored: member.authored,
                schema_key: &member.key.schema_key,
                file_id: member.key.file_id.as_deref(),
                row_pk: &member.key.row_pk,
                deleted: member.value.deleted,
                snapshot_json: payload.snapshot_content.as_deref(),
                decoded_snapshot: payload.decoded_snapshot.as_deref(),
                metadata_json: payload.metadata.as_deref(),
                row_created_at: member.value.created_at,
                row_updated_at: member.value.updated_at,
                change_account_id: &member.change.account_id,
                change_created_at: member.change.created_at,
                origin_key: member.change.origin_key.as_deref(),
            })
        })
        .collect::<Result<Vec<_>, LixError>>()?;

    // Merge provenance is already canonical in the commit graph: parent zero
    // is the target and parent one is the selected source. Non-merge
    // checkpoints carry complete selected members and need no source pointer.
    let has_selected_members = members.iter().any(|member| !member.authored);
    let selected_source_commit_id = (has_selected_members && record.parent_commit_ids.len() > 1)
        .then(|| record.parent_commit_ids[1]);

    let topology = crate::tracked_state::load_published_commit_state_topology(store, commit_id)
        .await?
        .ok_or_else(|| {
            LixError::unknown(format!(
                "sync commit '{commit_id}' has no tracked-state authority"
            ))
        })?;
    let (complete_incorporation_source_commit_id, incorporation_unknown) =
        match topology.incorporation() {
            crate::tracked_state::CommitStateIncorporation::None => (None, false),
            crate::tracked_state::CommitStateIncorporation::Complete(source) => {
                (Some(source.to_string()), false)
            }
            crate::tracked_state::CommitStateIncorporation::LegacyUnknown => (None, true),
        };
    let exported = SyncCommit {
        is_checkpoint: record.is_checkpoint,
        commit_id: record.commit_id.to_string(),
        parent_commit_ids: record
            .parent_commit_ids
            .into_iter()
            .map(|parent| parent.to_string())
            .collect(),
        base_commit_id: record.base_commit_id.map(|base| base.to_string()),
        account_id: record.account_id,
        created_at: record.created_at.to_string(),
        global_scope: topology.global_scope(),
        selected_source_commit_id: selected_source_commit_id.map(|source| source.to_string()),
        state_alias,
        complete_incorporation_source_commit_id,
        incorporation_unknown,
        members,
    };
    exported.validate()?;
    Ok(Some(exported))
}

fn parse_materialized_json(
    json: Option<&str>,
    change_id: crate::changelog::ChangeId,
    field: &str,
) -> Result<Option<serde_json::Value>, LixError> {
    json.map(|json| {
        serde_json::from_str(json).map_err(|error| {
            LixError::new(
                LixError::CODE_INTERNAL_ERROR,
                format!("sync commit change '{change_id}' has invalid {field} JSON: {error}"),
            )
        })
    })
    .transpose()
}

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

    #[test]
    fn typed_sync_payload_preserves_custom_schema_and_rejects_tampering() {
        let schema = serde_json::json!({
            "$schema": "https://lix.dev/schema-v1.json", "key": "custom_sync",
            "columns": [{"name":"id","type":"text","nullable":false}, {"name":"count","type":"int8","nullable":false}],
            "primary_key": ["id"]
        });
        let catalog = crate::catalog::CatalogSnapshot::from_visible_schemas(&[schema]).unwrap();
        let (_, plan) = catalog.plan_for_key("custom_sync").unwrap();
        let pk = RowPk::single("one");
        let json = serde_json::json!({"id":"one", "count":42});
        let row = crate::row_payload::TypedRow::from_normalized_json(plan, &pk, &json).unwrap();
        let payload = encode_sync_row_payload(&row).unwrap();
        decode_sync_row_payload("custom_sync", &pk, &json, Some(&payload)).unwrap();
        assert!(decode_sync_row_payload("custom_sync", &pk, &json, None).is_err());
        assert!(decode_sync_row_payload("custom_sync", &pk, &json, Some("invalid")).is_err());
        assert!(
            decode_sync_row_payload(
                "custom_sync",
                &pk,
                &serde_json::json!({"id":"one", "count":43}),
                Some(&payload)
            )
            .is_err()
        );
        assert_eq!(
            decode_sync_row_payload(
                "custom_sync",
                &RowPk::single("other"),
                &json,
                Some(&payload)
            )
            .unwrap_err()
            .code,
            LixError::CODE_INVALID_PARAM,
        );
        assert_eq!(
            decode_sync_row_payload("custom_sync", &pk, &json, Some("Ag=="))
                .unwrap_err()
                .code,
            LixError::CODE_INVALID_PARAM,
            "malformed typed bytes are caller input errors, not internal failures",
        );
    }

    async fn exported_key_value_commit() -> (Lix, SyncCommit) {
        let lix = crate::open_lix().await.expect("open lix");
        lix.execute(
            "INSERT INTO lix_key_value (key, value) VALUES ($1, CAST($2 AS JSONB))",
            &[
                crate::Value::Text("sync-commit-codec".to_owned()),
                crate::Value::Text("{\"answer\":42}".to_owned()),
            ],
        )
        .with_origin_key("sync-commit-codec-origin")
        .await
        .expect("write commit fixture");
        let commit_id = lix
            .execute("SELECT lix_active_branch_commit_id() AS id", &[])
            .await
            .expect("load fixture head")
            .rows()[0]
            .get::<String>("id")
            .expect("fixture head id");
        let adapter = lix.storage_adapter();
        let read = adapter
            .begin_read(StorageReadOptions::default())
            .await
            .expect("open export snapshot");
        let exported = load_sync_commit(
            &read,
            CommitId::parse_lix(&commit_id, "test commit").expect("parse fixture head"),
        )
        .await
        .expect("export fixture commit")
        .expect("fixture commit exists");
        (lix, exported)
    }

    #[tokio::test]
    async fn export_is_complete_deterministic_and_idempotent() {
        let (lix, exported) = exported_key_value_commit().await;
        assert!(!exported.members.is_empty());
        assert!(exported.members.iter().any(|member| {
            member.schema_key == "lix_key_value"
                && member.origin_key.as_deref() == Some("sync-commit-codec-origin")
                && member.snapshot.as_ref().is_some_and(|snapshot| {
                    snapshot["key"] == "sync-commit-codec" && snapshot["value"]["answer"] == 42
                })
        }));
        let adapter = lix.storage_adapter();
        let read = adapter
            .begin_read(StorageReadOptions::default())
            .await
            .expect("open comparison snapshot");
        let again = load_sync_commit(
            &read,
            CommitId::parse_lix(&exported.commit_id, "test commit").expect("parse fixture commit"),
        )
        .await
        .expect("re-export fixture commit")
        .expect("fixture commit still exists");
        assert_eq!(exported, again);
        assert_eq!(
            export_sync_commit(&lix, &exported.commit_id)
                .await
                .expect("repository export"),
            Some(exported)
        );
        lix.close().await.expect("close fixture");
    }

    #[tokio::test]
    async fn ordinary_merge_exports_graph_derived_selection_provenance() {
        let lix = crate::open_lix().await.expect("open merge fixture");
        let source_branch = lix
            .create_branch(crate::CreateBranchOptions {
                id: None,
                name: "sync codec selected source".to_owned(),
                from_commit_id: None,
            })
            .await
            .expect("create source branch");
        let source = lix
            .open_another_session()
            .await
            .expect("open source session");
        source
            .switch_branch(crate::SwitchBranchOptions {
                branch_id: source_branch.id.clone(),
            })
            .await
            .expect("switch source branch");
        source
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('codec-source', 'selected')",
                &[],
            )
            .await
            .expect("write source branch");
        lix.execute(
            "INSERT INTO lix_key_value (key, value) VALUES ('codec-target', 'authored')",
            &[],
        )
        .await
        .expect("write target branch");
        let merge = lix
            .merge_branch(crate::MergeBranchOptions {
                source_branch_id: source_branch.id,
            })
            .await
            .expect("merge disjoint branches");
        let merge_commit_id = merge
            .created_merge_commit_id
            .expect("divergence should create a merge commit");
        let exported = export_sync_commit(&lix, &merge_commit_id)
            .await
            .expect("export merge commit")
            .expect("merge commit exists");
        assert_eq!(
            exported.selected_source_commit_id,
            Some(merge.source_head_before_commit_id)
        );
        assert!(exported.members.iter().any(|member| !member.authored));
        source.close().await.expect("close source session");
        lix.close().await.expect("close merge fixture");
    }

    #[tokio::test]
    async fn public_nested_merge_exports_a_valid_complete_sync_commit() {
        let lix = crate::open_lix().await.expect("open nested merge fixture");
        let main_branch_id = lix.active_branch_id().await.expect("load main branch");
        let source_branch = lix
            .create_branch(crate::CreateBranchOptions {
                id: None,
                name: "nested merge source".to_owned(),
                from_commit_id: None,
            })
            .await
            .expect("create first source branch");
        let second_target = lix
            .create_branch(crate::CreateBranchOptions {
                id: None,
                name: "nested merge target".to_owned(),
                from_commit_id: None,
            })
            .await
            .expect("create second target branch");

        let source = lix
            .open_another_session()
            .await
            .expect("open source session");
        source
            .switch_branch(crate::SwitchBranchOptions {
                branch_id: source_branch.id.clone(),
            })
            .await
            .expect("switch first source branch");
        source
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('nested-source', 'selected')",
                &[],
            )
            .await
            .expect("write first source branch");
        lix.execute(
            "INSERT INTO lix_key_value (key, value) VALUES ('nested-main', 'authored')",
            &[],
        )
        .await
        .expect("write main branch");
        let first = lix
            .merge_branch(crate::MergeBranchOptions {
                source_branch_id: source_branch.id,
            })
            .await
            .expect("create first merge");
        let first_id = first
            .created_merge_commit_id
            .expect("first merge should create a commit");
        let first_export = export_sync_commit(&lix, &first_id)
            .await
            .expect("export first merge")
            .expect("first merge exists");
        first_export
            .validate()
            .expect("first merge must be a valid sync commit");
        assert!(first_export.selected_source_commit_id.is_some());

        let target = lix
            .open_another_session()
            .await
            .expect("open second target");
        target
            .switch_branch(crate::SwitchBranchOptions {
                branch_id: second_target.id,
            })
            .await
            .expect("switch second target branch");
        target
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('nested-target', 'authored')",
                &[],
            )
            .await
            .expect("write second target branch");
        let nested = target
            .merge_branch(crate::MergeBranchOptions {
                source_branch_id: main_branch_id,
            })
            .await
            .expect("public merge should accept a source head that is itself a merge");
        let nested_id = nested
            .created_merge_commit_id
            .expect("nested divergence should create a merge commit");
        let nested_export = export_sync_commit(&lix, &nested_id)
            .await
            .expect("export nested merge")
            .expect("nested merge exists");
        nested_export
            .validate()
            .expect("every public merge must export as a valid complete sync commit");

        target.close().await.expect("close second target");
        source.close().await.expect("close source session");
        lix.close().await.expect("close nested merge fixture");
    }

    #[tokio::test]
    async fn checkpoint_exports_bounded_authenticated_state_alias() {
        let lix = crate::open_lix().await.expect("open checkpoint fixture");
        lix.execute(
            "INSERT INTO lix_key_value (key, value) VALUES ('checkpoint-source', 'selected')",
            &[],
        )
        .await
        .expect("write checkpoint interval");
        lix.create_checkpoint().await.expect("create checkpoint");
        let checkpoint_id = lix
            .execute("SELECT lix_active_branch_commit_id() AS id", &[])
            .await
            .expect("load checkpoint head")
            .rows()[0]
            .get::<String>("id")
            .expect("checkpoint id");
        let exported = export_sync_commit(&lix, &checkpoint_id)
            .await
            .expect("export checkpoint")
            .expect("checkpoint exists");
        exported
            .validate()
            .expect("checkpoint alias must remain a valid sync commit");
        assert!(exported.members.iter().all(|member| member.authored));
        assert!(
            exported.members.is_empty(),
            "the branch checkpoint boundary has no local row mutations"
        );
        assert_eq!(exported.selected_source_commit_id, None);
        let alias = exported
            .state_alias
            .as_ref()
            .expect("checkpoint export must carry its physical state source");
        super::super::validate_blake3_id(&alias.state_root_id, "checkpoint state root")
            .expect("checkpoint root id must be canonical");
        lix.close().await.expect("close checkpoint fixture");
    }

    #[tokio::test]
    async fn materialized_state_alias_sidecar_retires_by_commit_identity() {
        let lix = crate::open_lix().await.expect("open sidecar fixture");
        let commit_id = CommitId::for_test_label("materialized-alias-owner");
        let alias = SyncCommitStateAlias {
            source_commit_id: CommitId::for_test_label("materialized-alias-source").to_string(),
            state_root_id: blake3::hash(b"materialized-alias-root")
                .to_hex()
                .to_string(),
        };
        let adapter = lix.storage_adapter();
        let mut writes = adapter.new_write_set();
        stage_materialized_sync_state_alias(&mut writes, commit_id, &alias).expect("stage sidecar");
        adapter
            .commit_certified_replica_write_set(
                crate::sync::certified_replica_write_capability(),
                writes,
                crate::storage_adapter::StorageWriteOptions::default(),
            )
            .await
            .expect("publish sidecar");
        let read = adapter
            .begin_read(StorageReadOptions::default())
            .await
            .expect("open sidecar read");
        assert_eq!(
            load_materialized_sync_state_alias(&read, commit_id)
                .await
                .expect("load sidecar"),
            Some(alias),
        );
        drop(read);

        let mut writes = adapter.new_write_set();
        stage_delete_materialized_sync_state_alias(&mut writes, commit_id);
        adapter
            .commit_certified_replica_write_set(
                crate::sync::certified_replica_write_capability(),
                writes,
                crate::storage_adapter::StorageWriteOptions::default(),
            )
            .await
            .expect("retire sidecar");
        let read = adapter
            .begin_read(StorageReadOptions::default())
            .await
            .expect("open retired sidecar read");
        assert_eq!(
            load_materialized_sync_state_alias(&read, commit_id)
                .await
                .expect("load retired sidecar"),
            None,
        );
        drop(read);
        lix.close().await.expect("close sidecar fixture");
    }

    #[test]
    fn incorporation_wire_preserves_unknown_and_rejects_contradictory_proofs() {
        let mut commit = SyncCommit {
            is_checkpoint: true,
            commit_id: CommitId::for_test_label("incorporation-target").to_string(),
            parent_commit_ids: vec![CommitId::for_test_label("incorporation-parent").to_string()],
            base_commit_id: Some(CommitId::for_test_label("incorporation-catalog").to_string()),
            account_id: crate::ANONYMOUS_ACCOUNT_ID.to_owned(),
            created_at: "2026-08-19T00:00:00Z".to_owned(),
            global_scope: false,
            selected_source_commit_id: None,
            state_alias: None,
            complete_incorporation_source_commit_id: None,
            incorporation_unknown: true,
            members: Vec::new(),
        };
        commit.validate().expect("legacy uncertainty is explicit");
        let wire = serde_json::to_value(&commit).expect("serialize unknown proof");
        assert_eq!(wire["incorporationUnknown"], true);
        assert!(wire.get("completeIncorporationSourceCommitId").is_none());
        let decoded: SyncCommit = serde_json::from_value(wire).expect("decode unknown proof");
        assert_eq!(decoded, commit);
        commit.complete_incorporation_source_commit_id =
            Some(CommitId::for_test_label("incorporation-source").to_string());
        commit
            .validate()
            .expect_err("complete and unknown are exclusive");
        commit.incorporation_unknown = false;
        commit
            .validate()
            .expect("complete source has valid wire shape");
        commit.complete_incorporation_source_commit_id = Some(commit.commit_id.clone());
        commit
            .validate()
            .expect_err("self incorporation is invalid");
    }

    #[test]
    fn validation_rejects_noncanonical_member_order_and_authorship() {
        let commit_id = CommitId::for_test_label("sync-validation");
        let member = |label: &str, ordinal: u32| {
            let mut change_bytes = *commit_id.as_uuid().as_bytes();
            change_bytes[12..].copy_from_slice(&ordinal.to_be_bytes());
            SyncCommitMember {
                change_id: crate::changelog::ChangeId::new(uuid::Uuid::from_bytes(change_bytes))
                    .to_string(),
                authored: true,
                schema_key: "schema".to_owned(),
                file_id: None,
                row_pk: serde_json::json!([{ "type": "string", "value": label }]),
                deleted: false,
                snapshot: Some(serde_json::json!({"id": label})),
                snapshot_payload: Some(String::new()),
                metadata: None,
                row_created_at: "2026-08-19T00:00:00Z".to_owned(),
                row_updated_at: "2026-08-19T00:00:00Z".to_owned(),
                change_account_id: crate::ANONYMOUS_ACCOUNT_ID.to_owned(),
                change_created_at: "2026-08-19T00:00:00Z".to_owned(),
                origin_key: None,
            }
        };
        let mut commit = SyncCommit {
            is_checkpoint: false,
            commit_id: commit_id.to_string(),
            parent_commit_ids: Vec::new(),
            base_commit_id: Some(CommitId::for_test_label("validation-base").to_string()),
            account_id: crate::ANONYMOUS_ACCOUNT_ID.to_owned(),
            created_at: "2026-08-19T00:00:00Z".to_owned(),
            global_scope: false,
            selected_source_commit_id: None,
            state_alias: None,
            complete_incorporation_source_commit_id: None,
            incorporation_unknown: false,
            members: vec![member("b", 1), member("a", 2)],
        };
        assert!(
            commit
                .validate()
                .expect_err("descending identities must fail")
                .message
                .contains("strictly identity ordered")
        );
        commit.members = vec![member("a", 1)];
        commit.members[0].change_account_id = crate::SYSTEM_ACCOUNT_ID.to_owned();
        assert!(
            commit
                .validate()
                .expect_err("foreign authored account must fail")
                .message
                .contains("account")
        );

        commit.members[0].authored = false;
        commit.members[0].change_account_id = crate::ANONYMOUS_ACCOUNT_ID.to_owned();
        commit
            .validate()
            .expect("non-merge checkpoint members are self-contained");
        commit.selected_source_commit_id = Some(CommitId::for_test_label("source").to_string());
        assert!(
            commit
                .validate()
                .expect_err("a non-merge source pointer must fail")
                .message
                .contains("allowed only")
        );

        commit.parent_commit_ids = vec![
            CommitId::for_test_label("target").to_string(),
            CommitId::for_test_label("source").to_string(),
        ];
        commit
            .validate()
            .expect("merge selected source equals the second parent");
        commit.selected_source_commit_id = None;
        assert!(
            commit
                .validate()
                .expect_err("merge selected members require the second parent source")
                .message
                .contains("second parent")
        );
    }

    #[test]
    fn validation_rejects_non_metadata_only_state_aliases() {
        let commit_id = CommitId::for_test_label("alias-validation");
        let parent = CommitId::for_test_label("alias-parent");
        let source = CommitId::for_test_label("alias-source");
        let mut commit = SyncCommit {
            is_checkpoint: false,
            commit_id: commit_id.to_string(),
            parent_commit_ids: vec![parent.to_string()],
            base_commit_id: Some(CommitId::for_test_label("alias-validation-base").to_string()),
            account_id: crate::ANONYMOUS_ACCOUNT_ID.to_owned(),
            created_at: "2026-08-19T00:00:00Z".to_owned(),
            global_scope: false,
            selected_source_commit_id: None,
            state_alias: Some(SyncCommitStateAlias {
                source_commit_id: source.to_string(),
                state_root_id: blake3::hash(b"alias-root").to_hex().to_string(),
            }),
            complete_incorporation_source_commit_id: None,
            incorporation_unknown: false,
            members: Vec::new(),
        };
        commit.validate().expect("canonical state alias validates");

        commit.parent_commit_ids.clear();
        assert!(
            commit
                .validate()
                .expect_err("alias without one semantic parent must fail")
                .message
                .contains("exactly one")
        );
        commit.parent_commit_ids = vec![parent.to_string()];
        commit.members.push(SyncCommitMember {
            change_id: crate::changelog::ChangeId::for_test_label("alias-member").to_string(),
            authored: true,
            schema_key: "schema".to_owned(),
            file_id: None,
            row_pk: serde_json::json!([{ "type": "string", "value": "row" }]),
            deleted: false,
            snapshot: Some(serde_json::json!({ "id": "row" })),
            snapshot_payload: Some(String::new()),
            metadata: None,
            row_created_at: "2026-08-19T00:00:00Z".to_owned(),
            row_updated_at: "2026-08-19T00:00:00Z".to_owned(),
            change_account_id: crate::ANONYMOUS_ACCOUNT_ID.to_owned(),
            change_created_at: "2026-08-19T00:00:00Z".to_owned(),
            origin_key: None,
        });
        assert!(
            commit
                .validate()
                .expect_err("alias with members must fail")
                .message
                .contains("must not carry")
        );
    }
}