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
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
use std::collections::BTreeSet;

use crate::LixError;
use crate::changelog::{ChangeRecordProjection, CommitId};
use crate::checkpoint::CHECKPOINT_SCHEMA_KEY;
use crate::row_pk::RowPk;
use crate::sql2::{
    DiffCommand, DiffCommandOutcome, DiffCommandSelection, SqlWriteExecutionContext,
};
use crate::storage_adapter::Storage;
use crate::tracked_state::{TrackedStateDiffRequest, TrackedStateKey};
use crate::transaction::Transaction;
use crate::transaction_types::{
    RawWriteBatch, TransactionJson, TransactionWrite, TransactionWriteMode, TransactionWriteRow,
};
use crate::undo_redo::{
    UNDO_REDO_MARKER_SCHEMA_KEY, UNDO_STATE_SCHEMA_KEY, UndoRedoKind, UndoRedoMarker,
    marker_stage_row,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Default)]
struct SemanticState {
    undo_top: Option<CommitId>,
    redo_top: Option<CommitId>,
}

/// One point-addressable state row per original target or undo receipt. No
/// operation rewrites the complete branch history to advance an undo cursor.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum UndoState {
    Epoch {
        checkpoint: CommitId,
    },
    Target {
        epoch: Option<CommitId>,
        parent: CommitId,
        checkpoint: bool,
        all: BTreeSet<String>,
        undone: BTreeSet<String>,
        retired: bool,
    },
    Receipt {
        epoch: Option<CommitId>,
        pending: BTreeSet<String>,
        metadata_pending: bool,
    },
}

fn no_change() -> DiffCommandOutcome {
    DiffCommandOutcome {
        rows_affected: 0,
        commit_id: None,
        parent_commit_id: None,
    }
}

pub(crate) fn is_undo_metadata(schema: &str) -> bool {
    matches!(
        schema,
        CHECKPOINT_SCHEMA_KEY | UNDO_REDO_MARKER_SCHEMA_KEY | UNDO_STATE_SCHEMA_KEY
    )
}

#[tracing::instrument(target = "lix_perf", name = "lix.perf.undo_redo", skip_all, fields(redo, explicit_target = requested.is_some(), selected = scope.is_some()))]
pub(crate) async fn execute<S: Storage + Clone + Send + Sync + 'static>(
    tx: &mut Transaction<S>,
    redo: bool,
    requested: Option<CommitId>,
    scope: Option<Vec<DiffCommandSelection>>,
) -> Result<DiffCommandOutcome, LixError> {
    let whole_scope = scope.is_none();
    let branch = tx.active_branch_id().to_string();
    let head = tx
        .load_branch_head(&branch)
        .await?
        .ok_or_else(|| LixError::branch_not_found(&branch, "undo/redo", "branch"))?;
    let mut state = semantic_state_at(tx, &branch, head).await?;
    if requested.is_none() {
        normalize_cursor(tx, &branch, head, redo, &mut state).await?;
    }
    let Some(requested_id) = requested.or(if redo { state.redo_top } else { state.undo_top })
    else {
        return Ok(no_change());
    };
    if requested.is_some()
        && tx
            .commit_graph_reader()
            .await?
            .merge_base(&head, &requested_id)
            .await?
            != requested_id
        && load_state(tx, head, requested_id).await?.is_none()
    {
        return Err(LixError::new(
            if redo {
                "LIX_INVALID_REDO_TARGET"
            } else {
                "LIX_INVALID_UNDO_TARGET"
            },
            "undo/redo target is not incorporated on this branch",
        ));
    }
    // Resolve an internal logical frame separately from public target roles.
    let requested_marker = operation_marker_any(tx, requested_id).await?;
    let (target, frame_effects, undo_next) = if !redo && requested.is_none() {
        if let Some(marker) = &requested_marker {
            if marker.kind != UndoRedoKind::Redo {
                return Err(LixError::unknown("invalid logical undo frame"));
            }
            (
                marker.target_commit_id,
                Some(marker.effects.clone()),
                marker.undo_target_after,
            )
        } else {
            let node = load_node(tx, requested_id).await?;
            let parent = only_parent(&node.parent_commit_ids, requested_id, "undo")?;
            (
                requested_id,
                None,
                semantic_state_at(tx, &branch, parent).await?.undo_top,
            )
        }
    } else if redo {
        let marker = requested_marker
            .as_ref()
            .filter(|m| m.kind == UndoRedoKind::Undo)
            .ok_or_else(|| {
                LixError::new(
                    "LIX_INVALID_REDO_TARGET",
                    "redo requires the commit returned by undo",
                )
            })?;
        (marker.target_commit_id, None, state.undo_top)
    } else {
        if requested_marker.is_some() {
            return Err(LixError::new(
                "LIX_INVALID_UNDO_TARGET",
                "undo requires an original action, not an undo/redo receipt",
            ));
        }
        let node = load_node(tx, requested_id).await?;
        let parent = only_parent(&node.parent_commit_ids, requested_id, "undo")?;
        let next = if state.undo_top == Some(requested_id) && scope.is_some() {
            Some(requested_id)
        } else if state.undo_top == Some(requested_id) {
            semantic_state_at(tx, &branch, parent).await?.undo_top
        } else {
            state.undo_top
        };
        (requested_id, None, next)
    };
    let node = load_node(tx, target).await?;
    let parent = only_parent(
        &node.parent_commit_ids,
        target,
        if redo { "redo" } else { "undo" },
    )?;
    let mut target_state = load_state(tx, head, target).await?;
    let baseline = tx.load_branch_working_base(&branch).await?;
    let epoch_id =
        CommitId::parse("00000000-0000-0000-0000-000000000001").expect("internal epoch UUID");
    let epoch = if node.is_checkpoint {
        let stored = match load_state(tx, head, epoch_id).await? {
            Some(UndoState::Epoch { checkpoint }) => Some(checkpoint),
            _ => None,
        };
        let mut latest = stored;
        if let Some(baseline) = baseline {
            let baseline_node = load_node(tx, baseline).await?;
            if baseline_node.is_checkpoint
                && match stored {
                    Some(stored) => {
                        tx.commit_graph_reader().await?.merge_base(&baseline, &stored).await? != baseline
                    }
                    None => true,
                }
            {
                latest = Some(baseline);
            }
        }
        latest.or(Some(target))
    } else {
        None
    };
    let mut receipt_state = if redo {
        load_state(tx, head, requested_id).await?
    } else {
        None
    };
    if node.is_checkpoint {
        let same_epoch = matches!(&target_state, Some(UndoState::Target { epoch: previous, .. }) if *previous == epoch);
        if !redo && baseline == Some(target) && !same_epoch {
            // A formerly superseded checkpoint can start a fresh undo cycle
            // when it is again the active baseline. Old receipts keep their epoch.
            target_state = None;
        }
        let receipt_current = !redo
            || matches!(&receipt_state, Some(UndoState::Receipt { epoch: previous, .. }) if *previous == epoch);
        let eligible = receipt_current
            && match &target_state {
                Some(UndoState::Target {
                    epoch: previous,
                    retired: true,
                    parent,
                    ..
                }) => *previous == epoch && baseline == Some(*parent),
                _ => baseline == Some(target),
            };
        if !eligible {
            return Err(LixError::new(
                "LIX_STALE_CHECKPOINT_UNDO",
                "checkpoint undo cycle was superseded; use restore or revert for historical content",
            ));
        }
    }
    if scope.as_ref().is_some_and(Vec::is_empty) {
        return Ok(no_change());
    }
    let from = target.to_string();
    let to = parent.to_string();
    let mut selected_files = BTreeSet::new();
    if redo && target_state.is_none() {
        return Err(LixError::new("LIX_INVALID_REDO_TARGET", "undo receipt lacks its incorporated target state"));
    }
    let full = if redo && scope.is_none() {
        selected_files = requested_marker.as_ref().expect("validated undo receipt").selected_files.clone();
        match &receipt_state {
            Some(UndoState::Receipt { pending, .. }) => Some(pending.clone()),
            _ => None,
        }
    } else if scope.is_none() || (node.is_checkpoint && target_state.is_none()) {
        let diff = tx
            .tracked_state_reader()
            .await?
            .diff_commits(&from, &to, &TrackedStateDiffRequest { retain_payloads: false, ..TrackedStateDiffRequest::default() })
            .await?;
        let mut ids = BTreeSet::new();
        for entry in diff.entries {
            if is_undo_metadata(entry.identity.schema_key()) {
                continue;
            }
            if entry.identity.schema_key() == "lix_file_descriptor" {
                selected_files.insert(entry.identity.row_pk().as_single_string_owned()?);
            }
            ids.insert(entry.diff_id()?);
        }
        Some(ids)
    } else {
        None
    };
    let selected = if let Some(mut scope) = scope {
        selected_files.clear();
        for row in &mut scope {
            row.source_commits = Some((from.clone(), to.clone()));
            if row.relation == "lix_file" {
                selected_files.insert(row.row_pk.as_single_string_owned()?);
            }
        }
        tx.resolve_diff_command_selections(DiffCommand::Apply, &scope)
            .await?
            .into_iter()
            .collect::<BTreeSet<_>>()
    } else {
        full.clone().unwrap_or_default()
    };
    if target_state.is_none() {
        target_state = Some(UndoState::Target {
            epoch,
            parent,
            checkpoint: node.is_checkpoint,
            all: if node.is_checkpoint {
                full.unwrap_or_default()
            } else {
                BTreeSet::new()
            },
            undone: BTreeSet::new(),
            retired: false,
        });
    }
    let Some(UndoState::Target {
        all,
        undone,
        retired,
        ..
    }) = target_state.as_mut()
    else {
        return Err(LixError::unknown("invalid undo target state"));
    };
    let effects = if redo {
        let Some(UndoState::Receipt { pending, .. }) = &receipt_state else {
            return Err(LixError::new(
                "LIX_INVALID_REDO_TARGET",
                "undo receipt is not incorporated on this branch",
            ));
        };
        selected
            .intersection(pending)
            .cloned()
            .collect::<BTreeSet<_>>()
    } else {
        let mut effects = selected
            .difference(undone)
            .cloned()
            .collect::<BTreeSet<_>>();
        if let Some(frame_effects) = frame_effects {
            effects.retain(|id| frame_effects.contains(id));
        }
        effects
    };
    let metadata_only = whole_scope
        && node.is_checkpoint
        && all.is_empty()
        && if redo {
            matches!(
                receipt_state,
                Some(UndoState::Receipt {
                    metadata_pending: true,
                    ..
                })
            )
        } else {
            !*retired
        };
    if effects.is_empty() && !metadata_only {
        return Ok(no_change());
    }
    let old_retired = *retired;
    if redo {
        for effect in &effects {
            undone.remove(effect);
        }
        if metadata_only || undone.is_empty() {
            *retired = false;
        }
    } else {
        undone.extend(effects.iter().cloned());
        if node.is_checkpoint && (metadata_only || all.is_subset(undone)) {
            *retired = true;
        }
    }
    let new_retired = *retired;
    let patch = effects
        .iter()
        .map(|id| {
            if redo {
                let sides = crate::tracked_state::decode_diff_id(id)?;
                crate::tracked_state::encode_diff_id(sides.after, sides.before)
            } else {
                Ok(id.clone())
            }
        })
        .collect::<Result<Vec<_>, LixError>>()?;
    if redo {
        selected_files = selected_files
            .intersection(&requested_marker.as_ref().expect("receipt").selected_files)
            .cloned()
            .collect();
    }
    let mut outcome = if patch.is_empty() {
        no_change()
    } else {
        tx.execute_undo_patch(patch, selected_files.clone()).await?
    };
    let redo_next = if redo {
        let marker = requested_marker.as_ref().expect("receipt");
        let Some(UndoState::Receipt {
            pending,
            metadata_pending,
            ..
        }) = receipt_state.as_mut()
        else {
            unreachable!()
        };
        for effect in &effects {
            pending.remove(effect);
        }
        if metadata_only {
            *metadata_pending = false;
        }
        if state.redo_top == Some(requested_id) && pending.is_empty() && !*metadata_pending {
            marker.redo_next
        } else {
            state.redo_top
        }
    } else {
        state.redo_top
    };
    stage_marker(
        tx,
        UndoRedoMarker {
            branch_id: branch.clone(),
            kind: if redo {
                UndoRedoKind::Redo
            } else {
                UndoRedoKind::Undo
            },
            target_commit_id: target,
            undo_target_after: undo_next,
            redo_top_after: if redo { redo_next } else { None },
            redo_next,
            effects: effects.iter().cloned().collect(),
            selected_files,
            checkpoint: node.is_checkpoint,
            source_undo_commit_id: redo.then_some(requested_id),
            baseline_before: (node.is_checkpoint && old_retired != new_retired)
                .then_some(baseline)
                .flatten(),
            baseline_after: (node.is_checkpoint && old_retired != new_retired)
                .then_some(if new_retired { parent } else { target }),
        },
    )
    .await?;
    let commit = tx.staged_undo_commit_id()?;
    if redo {
        stage_state(
            tx,
            requested_id,
            receipt_state.as_ref().expect("receipt state"),
        )
        .await?;
    } else {
        stage_state(
            tx,
            commit,
            &UndoState::Receipt {
                epoch,
                pending: effects,
                metadata_pending: metadata_only,
            },
        )
        .await?;
    }
    stage_state(tx, target, target_state.as_ref().expect("target state")).await?;
    if let Some(checkpoint) = epoch {
        stage_state(tx, epoch_id, &UndoState::Epoch { checkpoint }).await?;
    }
    if node.is_checkpoint && old_retired != new_retired {
        tx.stage_undo_baseline(
            baseline.expect("validated checkpoint baseline"),
            if new_retired { parent } else { target },
        )
        .await?;
    }
    outcome.commit_id = Some(commit.to_string());
    outcome.rows_affected = outcome.rows_affected.max(1);
    Ok(outcome)
}

// Explicit operations can consume a non-top receipt or only part of an action.
// Resolve those durable frames lazily for editor navigation, without enumerating
// unrelated target effects on the explicit, row-scoped write path.
async fn normalize_cursor<S: Storage + Clone + Send + Sync + 'static>(
    tx: &mut Transaction<S>,
    branch: &str,
    head: CommitId,
    redo: bool,
    state: &mut SemanticState,
) -> Result<(), LixError> {
    let cursor = if redo {
        &mut state.redo_top
    } else {
        &mut state.undo_top
    };
    let mut visited = BTreeSet::new();
    while let Some(id) = *cursor {
        if !visited.insert(id) {
            return Err(LixError::unknown("cyclic undo/redo cursor"));
        }
        let marker = operation_marker_any(tx, id).await?;
        if redo {
            if matches!(load_state(tx, head, id).await?, Some(UndoState::Receipt { pending, metadata_pending: false, .. }) if pending.is_empty())
            {
                *cursor = marker.and_then(|marker| marker.redo_next);
                continue;
            }
            break;
        }
        let target = marker.as_ref().map_or(id, |marker| marker.target_commit_id);
        let Some(UndoState::Target { undone, .. }) = load_state(tx, head, target).await? else {
            break;
        };
        if let Some(marker) = marker {
            if marker.kind == UndoRedoKind::Redo
                && marker.effects.iter().all(|effect| undone.contains(effect))
                && !marker.effects.is_empty()
            {
                *cursor = marker.undo_target_after;
                continue;
            }
            break;
        }
        let node = load_node(tx, target).await?;
        let parent = only_parent(&node.parent_commit_ids, target, "undo")?;
        let diff = tx
            .tracked_state_reader()
            .await?
            .diff_commits(
                &target.to_string(),
                &parent.to_string(),
                &TrackedStateDiffRequest { retain_payloads: false, ..TrackedStateDiffRequest::default() },
            )
            .await?;
        let mut remaining = false;
        for entry in diff.entries {
            if !is_undo_metadata(entry.identity.schema_key()) && !undone.contains(&entry.diff_id()?)
            {
                remaining = true;
                break;
            }
        }
        if remaining {
            break;
        }
        *cursor = semantic_state_at(tx, branch, parent).await?.undo_top;
    }
    Ok(())
}

async fn load_state<S: Storage + Clone + Send + Sync + 'static>(
    tx: &mut Transaction<S>,
    head: CommitId,
    id: CommitId,
) -> Result<Option<UndoState>, LixError> {
    let rows = tx
        .tracked_state_reader()
        .await?
        .load_projected_batch_at_commit(
            &head.to_string(),
            &[TrackedStateKey {
                schema_key: UNDO_STATE_SCHEMA_KEY.into(),
                file_id: None,
                row_pk: RowPk::uuid_from_canonical(&id.to_string())
                    .map_err(|e| LixError::unknown(e.to_string()))?,
            }],
            &ChangeRecordProjection::from_columns(&["snapshot_content".into()]),
        )
        .await?;
    let Some(row) = rows.row(0).filter(|row| !row.deleted()) else {
        return Ok(None);
    };
    #[derive(Deserialize)]
    struct StateSnapshot {
        state: UndoState,
    }
    let snapshot: StateSnapshot = serde_json::from_str(
        row.snapshot_content()
            .ok_or_else(|| LixError::unknown("missing undo state"))?
            .as_str(),
    )
    .map_err(|e| LixError::unknown(format!("invalid undo state: {e}")))?;
    Ok(Some(snapshot.state))
}

async fn stage_state<S: Storage + Clone + Send + Sync + 'static>(
    tx: &mut Transaction<S>,
    id: CommitId,
    state: &UndoState,
) -> Result<(), LixError> {
    let mut rows = RawWriteBatch::with_capacity(1);
    rows.push(TransactionWriteRow {
        row_pk: None,
        schema_key: UNDO_STATE_SCHEMA_KEY.into(),
        file_id: None,
        snapshot: Some(TransactionJson::from_value_unchecked(
            serde_json::json!({"id": id, "state": state}),
        )),
        metadata: None,
        origin: None,
        created_at: None,
        updated_at: None,
        global: false,
        change_id: None,
        commit_id: None,
        untracked: false,
        branch_id: tx.active_branch_id().to_string().into(),
    });
    tx.stage_write(TransactionWrite::Rows {
        mode: TransactionWriteMode::Replace,
        rows,
    })
    .await
    .map(|_| ())
}

async fn semantic_state_at<S: Storage + Clone + Send + Sync + 'static>(
    tx: &mut Transaction<S>,
    branch: &str,
    id: CommitId,
) -> Result<SemanticState, LixError> {
    let node = load_node(tx, id).await?;
    if node.parent_commit_ids.len() != 1 || tx.is_current_checkpoint_commit(branch, id).await? {
        return Ok(SemanticState::default());
    }
    match operation_marker_any(tx, id).await? {
        Some(marker) if marker.branch_id != branch => Ok(SemanticState::default()),
        Some(marker) => Ok(match marker.kind {
            UndoRedoKind::Undo => SemanticState {
                undo_top: marker.undo_target_after,
                redo_top: Some(id),
            },
            UndoRedoKind::Redo => SemanticState {
                undo_top: Some(id),
                redo_top: marker.redo_top_after,
            },
        }),
        None => Ok(SemanticState {
            undo_top: Some(id),
            redo_top: None,
        }),
    }
}

/// Recovery writes new change identities. Only a recorded undo/redo effect can
/// prove that such a change represents an older revision; equal bytes from an
/// unrelated edit are deliberately insufficient.
pub(crate) async fn replayed_change_matches<S: Storage + Clone + Send + Sync + 'static>(
    tx: &mut Transaction<S>,
    writer: CommitId,
    expected: crate::changelog::ChangeId,
    cache: &mut std::collections::BTreeMap<CommitId, BTreeSet<crate::changelog::ChangeId>>,
) -> Result<bool, LixError> {
    if let std::collections::btree_map::Entry::Vacant(entry) = cache.entry(writer) {
        let mut restored = BTreeSet::new();
        if let Some(marker) = operation_marker_any(tx, writer).await? {
            for effect in marker.effects {
                let sides = crate::tracked_state::decode_diff_id(&effect)?;
                restored.extend(match marker.kind {
                    UndoRedoKind::Undo => sides.after,
                    UndoRedoKind::Redo => sides.before,
                });
            }
        }
        entry.insert(restored);
    }
    Ok(cache[&writer].contains(&expected))
}

async fn operation_marker_any<S: Storage + Clone + Send + Sync + 'static>(
    tx: &mut Transaction<S>,
    id: CommitId,
) -> Result<Option<UndoRedoMarker>, LixError> {
    let rows = tx
        .tracked_state_reader()
        .await?
        .commit_delta_values_for_schemas(id, &[UNDO_REDO_MARKER_SCHEMA_KEY.into()])
        .await?;
    let Some(entry) = rows.iter().find(|row| !row.value().deleted) else {
        return Ok(None);
    };
    let key = entry.key_ref();
    let key = TrackedStateKey {
        schema_key: key.schema_key.into(),
        file_id: key.file_id.map(str::to_string),
        row_pk: key.row_pk.clone(),
    };
    let projected = tx
        .tracked_state_reader()
        .await?
        .load_projected_batch_at_commit(
            &id.to_string(),
            &[key],
            &ChangeRecordProjection::from_columns(&["snapshot_content".into()]),
        )
        .await?;
    let snapshot = projected
        .row(0)
        .and_then(|row| row.snapshot_content())
        .ok_or_else(|| LixError::unknown("missing undo marker"))?;
    serde_json::from_str(snapshot.as_str())
        .map(Some)
        .map_err(|e| LixError::unknown(format!("invalid undo marker: {e}")))
}

async fn load_node<S: Storage + Clone + Send + Sync + 'static>(
    tx: &mut Transaction<S>,
    id: CommitId,
) -> Result<crate::commit_graph::CommitGraphNode, LixError> {
    tx.commit_graph_reader()
        .await?
        .load_node(&id)
        .await?
        .ok_or_else(|| {
            LixError::new(
                LixError::CODE_COMMIT_NOT_FOUND,
                format!("commit '{id}' does not exist"),
            )
        })
}

fn only_parent(parents: &[CommitId], id: CommitId, operation: &str) -> Result<CommitId, LixError> {
    match parents {
        [parent] => Ok(*parent),
        [] => Err(LixError::new(
            LixError::CODE_INVALID_PARAM,
            format!("cannot {operation} root commit '{id}'"),
        )),
        _ => Err(LixError::new(
            LixError::CODE_INVALID_MERGE,
            format!("cannot {operation} merge commit '{id}'"),
        )),
    }
}

async fn stage_marker<S: Storage + Clone + Send + Sync + 'static>(
    tx: &mut Transaction<S>,
    marker: UndoRedoMarker,
) -> Result<(), LixError> {
    let mut rows = RawWriteBatch::with_capacity(1);
    rows.push(marker_stage_row(&marker));
    tx.stage_write(TransactionWrite::Rows {
        mode: TransactionWriteMode::Replace,
        rows,
    })
    .await
    .map(|_| ())
}
#[cfg(test)]
mod tests {
    use crate::engine::Engine;
    use crate::storage::Memory;
    use crate::{
        Blob, CreateBranchOptions, ExecuteBatchStatement, LixError, MergeBranchOptions,
        MergeBranchOutcome, Value,
    };

    async fn setup_engine() -> Engine<Memory> {
        let storage = Memory::new();
        Engine::initialize(storage.clone())
            .await
            .expect("storage initializes");
        Engine::new(storage).await.expect("engine opens")
    }

    async fn setup() -> crate::session::SessionContext<Memory> {
        setup_engine()
            .await
            .open_session()
            .await
            .expect("session opens")
    }

    async fn value(session: &crate::session::SessionContext<Memory>, key: &str) -> Option<String> {
        let result = session
            .execute(
                "SELECT value FROM lix_key_value WHERE key = $1",
                &[Value::Text(key.to_string())],
            )
            .await
            .expect("value reads");
        result
            .rows()
            .first()
            .and_then(|row| row.get::<Value>("value").ok())
            .and_then(|value| match value {
                Value::Text(value) => Some(value),
                Value::Jsonb(value) => value.as_json_string(),
                _ => None,
            })
    }

    #[tokio::test]
    async fn undo_redo_tracks_branch_actions_without_rewinding_history() {
        let session = setup().await;
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('theme', 'light')",
                &[],
            )
            .await
            .expect("insert commits");
        session
            .execute(
                "UPDATE lix_key_value SET value = 'dark' WHERE key = 'theme'",
                &[],
            )
            .await
            .expect("update commits");

        let update = session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("update undoes");
        assert_eq!(value(&session, "theme").await.as_deref(), Some("light"));
        let insert = session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("insert undoes");
        assert_ne!(
            update.rows()[0].get::<String>("commit_id").unwrap(),
            insert.rows()[0].get::<String>("commit_id").unwrap()
        );
        assert_eq!(value(&session, "theme").await, None);

        session
            .execute("SELECT commit_id FROM lix_redo()", &[])
            .await
            .expect("insert redoes");
        assert_eq!(value(&session, "theme").await.as_deref(), Some("light"));
        session
            .execute("SELECT commit_id FROM lix_redo()", &[])
            .await
            .expect("update redoes");
        assert_eq!(value(&session, "theme").await.as_deref(), Some("dark"));
    }

    #[tokio::test]
    async fn ordinary_commit_after_undo_discards_old_redo_path() {
        let session = setup().await;
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('one', '1')",
                &[],
            )
            .await
            .expect("first insert commits");
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('two', '2')",
                &[],
            )
            .await
            .expect("second insert commits");
        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("second insert undoes");
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('three', '3')",
                &[],
            )
            .await
            .expect("replacement action commits");

        let error = session
            .execute("SELECT commit_id FROM lix_redo()", &[])
            .await
            .expect("old redo is discarded");
        assert_eq!(
            error.rows()[0].get::<Value>("commit_id").unwrap(),
            Value::Null
        );
        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("replacement action undoes");
        assert_eq!(value(&session, "three").await, None);
        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("first action remains undoable");
        assert_eq!(value(&session, "one").await, None);
    }

    #[tokio::test]
    async fn checkpoint_is_an_undo_floor() {
        let session = setup().await;
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('before', 'checkpoint')",
                &[],
            )
            .await
            .expect("pre-checkpoint insert commits");
        session
            .create_checkpoint()
            .await
            .expect("checkpoint commits");
        let error = session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("checkpoint blocks undo");
        assert_eq!(
            error.rows()[0].get::<Value>("commit_id").unwrap(),
            Value::Null
        );

        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('after', 'checkpoint')",
                &[],
            )
            .await
            .expect("post-checkpoint insert commits");
        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("post-checkpoint action undoes");
        let error = session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("undo stops at checkpoint");
        assert_eq!(
            error.rows()[0].get::<Value>("commit_id").unwrap(),
            Value::Null
        );
        assert_eq!(
            value(&session, "before").await.as_deref(),
            Some("checkpoint")
        );
    }

    #[tokio::test]
    async fn branch_forked_at_checkpoint_starts_at_an_undo_floor() {
        let engine = setup_engine().await;
        let session = engine.open_session().await.expect("session opens");
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('before-fork', 'kept')",
                &[],
            )
            .await
            .expect("pre-checkpoint insert commits");
        let checkpoint = session
            .create_checkpoint()
            .await
            .expect("checkpoint commits");
        let branch = session
            .create_branch(CreateBranchOptions {
                id: Some("01930000-0000-7000-8000-0000000000a1".to_string()),
                name: "checkpoint-fork".to_string(),
                from_commit_id: Some(checkpoint.commit_id),
            })
            .await
            .expect("branch creates");
        let fork = engine.open_session_at(branch.id).await.expect("fork opens");

        let error = fork
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("foreign checkpoint is a floor");
        assert_eq!(
            error.rows()[0].get::<Value>("commit_id").unwrap(),
            Value::Null
        );
        assert_eq!(value(&fork, "before-fork").await.as_deref(), Some("kept"));
    }

    #[tokio::test]
    async fn branch_forked_at_undo_commit_resets_undo_history() {
        let engine = setup_engine().await;
        let session = engine.open_session().await.expect("session opens");
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('abandoned', 'change')",
                &[],
            )
            .await
            .expect("insert commits");
        let undone = session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("insert undoes");
        let branch = session
            .create_branch(CreateBranchOptions {
                id: Some("01930000-0000-7000-8000-0000000000a2".to_string()),
                name: "undo-fork".to_string(),
                from_commit_id: Some(undone.rows()[0].get::<String>("commit_id").unwrap()),
            })
            .await
            .expect("branch creates");
        let fork = engine.open_session_at(branch.id).await.expect("fork opens");

        let error = fork
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("foreign operation commit is a floor");
        assert_eq!(
            error.rows()[0].get::<Value>("commit_id").unwrap(),
            Value::Null
        );
        assert_eq!(value(&fork, "abandoned").await, None);
    }

    #[tokio::test]
    async fn atomic_batch_is_one_undo_unit() {
        let session = setup().await;
        session
            .execute_batch(&[
                ExecuteBatchStatement {
                    label: None,
                    sql: "INSERT INTO lix_key_value (key, value) VALUES ('left', '1')".into(),
                    params: vec![],
                },
                ExecuteBatchStatement {
                    label: None,
                    sql: "INSERT INTO lix_key_value (key, value) VALUES ('right', '2')".into(),
                    params: vec![],
                },
            ])
            .await
            .expect("batch commits");
        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("batch undoes");
        assert_eq!(value(&session, "left").await, None);
        assert_eq!(value(&session, "right").await, None);
        let error = session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("batch was one unit");
        assert_eq!(
            error.rows()[0].get::<Value>("commit_id").unwrap(),
            Value::Null
        );
    }

    #[tokio::test]
    async fn mixed_transaction_leaves_untracked_state_untouched() {
        let session = setup().await;
        let mut transaction = session
            .begin_transaction()
            .await
            .expect("transaction opens");
        transaction
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('tracked', 'yes')",
                &[],
            )
            .await
            .expect("tracked row stages");
        transaction
            .execute(
                "INSERT INTO lix_key_value (key, value, lixcol_untracked) VALUES ('ui', 'open', true)",
                &[],
            )
            .await
            .expect("untracked row stages");
        transaction
            .commit()
            .await
            .expect("mixed transaction commits");

        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("tracked portion undoes");
        assert_eq!(value(&session, "tracked").await, None);
        assert_eq!(value(&session, "ui").await.as_deref(), Some("open"));
    }

    #[tokio::test]
    async fn file_create_and_update_roundtrip_through_undo_redo() {
        let session = setup().await;
        session
            .upsert_file_content("/note.txt".into(), Blob::from("one".as_bytes()))
            .await
            .expect("file creates");
        session
            .upsert_file_content("/note.txt".into(), Blob::from("two".as_bytes()))
            .await
            .expect("file updates");

        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("file update undoes");
        assert_eq!(
            session
                .read_file_content("/note.txt".into(), None)
                .await
                .expect("file reads")
                .expect("file exists")
                .content()
                .as_ref(),
            b"one"
        );
        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("file create undoes");
        assert_eq!(
            session
                .read_file_content("/note.txt".into(), None)
                .await
                .expect("file reads"),
            None
        );
        session
            .execute("SELECT commit_id FROM lix_redo()", &[])
            .await
            .expect("file create redoes");
        session
            .execute("SELECT commit_id FROM lix_redo()", &[])
            .await
            .expect("file update redoes");
        assert_eq!(
            session
                .read_file_content("/note.txt".into(), None)
                .await
                .expect("file reads")
                .expect("file exists")
                .content()
                .as_ref(),
            b"two"
        );
    }

    #[tokio::test]
    async fn file_delete_roundtrips_exact_tracked_dependency_closure() {
        let session = setup().await;
        session
            .upsert_file_content("/deleted.txt".into(), Blob::from("restored".as_bytes()))
            .await
            .expect("file creates");
        session
            .upsert_file_content("/unrelated.txt".into(), Blob::from("untouched".as_bytes()))
            .await
            .expect("unrelated file creates");
        let files = session
            .execute(
                "SELECT id, path FROM lix_file WHERE path IN ('/deleted.txt', '/unrelated.txt')",
                &[],
            )
            .await
            .expect("file identities read");
        let file_id = |path: &str| {
            files
                .rows()
                .iter()
                .find(|row| row.get::<String>("path").ok().as_deref() == Some(path))
                .and_then(|row| row.get::<String>("id").ok())
                .expect("file identity exists")
        };
        let deleted_file_id = file_id("/deleted.txt");
        let unrelated_file_id = file_id("/unrelated.txt");
        session
            .execute_batch(&[
                ExecuteBatchStatement {
                    label: None,
                    sql: "INSERT INTO lix_key_value (key, value, lixcol_file_id) VALUES ('deleted-state', 'restore-me', $1)".to_string(),
                    params: vec![Value::Text(deleted_file_id)],
                },
                ExecuteBatchStatement {
                    label: None,
                    sql: "INSERT INTO lix_key_value (key, value, lixcol_file_id) VALUES ('unrelated-state', 'keep-me', $1)".to_string(),
                    params: vec![Value::Text(unrelated_file_id)],
                },
                ExecuteBatchStatement {
                    label: None,
                    sql: "INSERT INTO lix_key_value (key, value) VALUES ('global-state', 'keep-me')"
                        .to_string(),
                    params: vec![],
                },
            ])
            .await
            .expect("tracked dependency rows write");
        session
            .execute("DELETE FROM lix_file WHERE path = '/deleted.txt'", &[])
            .await
            .expect("file deletes");
        assert_eq!(
            session
                .read_file_content("/deleted.txt".into(), None)
                .await
                .expect("deleted file reads"),
            None
        );
        assert_eq!(value(&session, "deleted-state").await, None);
        assert_eq!(
            value(&session, "unrelated-state").await.as_deref(),
            Some("keep-me")
        );
        assert_eq!(
            value(&session, "global-state").await.as_deref(),
            Some("keep-me")
        );

        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("file deletion undoes");
        assert_eq!(
            session
                .read_file_content("/deleted.txt".into(), None)
                .await
                .expect("restored file reads")
                .expect("file is restored")
                .content()
                .as_ref(),
            b"restored"
        );
        assert_eq!(
            value(&session, "deleted-state").await.as_deref(),
            Some("restore-me")
        );
        assert_eq!(
            value(&session, "unrelated-state").await.as_deref(),
            Some("keep-me")
        );
        assert_eq!(
            value(&session, "global-state").await.as_deref(),
            Some("keep-me")
        );
        session
            .execute("SELECT commit_id FROM lix_redo()", &[])
            .await
            .expect("file deletion redoes");
        assert_eq!(
            session
                .read_file_content("/deleted.txt".into(), None)
                .await
                .expect("redeleted file reads"),
            None
        );
        assert_eq!(value(&session, "deleted-state").await, None);
        assert_eq!(
            value(&session, "unrelated-state").await.as_deref(),
            Some("keep-me")
        );
        assert_eq!(
            value(&session, "global-state").await.as_deref(),
            Some("keep-me")
        );
    }

    /// The mixed state that made undo lossy can no longer be created.
    ///
    /// This test used to write an untracked row into a *tracked* file and then
    /// assert that `undo` refused to proceed — a guard
    /// (`reject_untracked_descriptor_cascade`) standing in for an invariant the
    /// engine did not enforce. PR D enforces the invariant at the write, so the
    /// INSERT is now rejected and the guard has nothing left to catch.
    #[tokio::test]
    async fn untracked_row_cannot_be_owned_by_a_tracked_file() {
        let session = setup().await;
        session
            .upsert_file_content("/owned.txt".into(), Blob::from("tracked".as_bytes()))
            .await
            .expect("file creates");
        let file = session
            .execute("SELECT id FROM lix_file WHERE path = '/owned.txt'", &[])
            .await
            .expect("file id reads");
        let file_id = match file.rows()[0].get::<Value>("id").expect("id projects") {
            Value::Text(value) => value,
            value => panic!("expected text file id, got {value:?}"),
        };
        let error = session
            .execute(
                "INSERT INTO lix_key_value (key, value, lixcol_file_id, lixcol_untracked) \
                 VALUES ('file-ui', 'open', $1, true)",
                &[Value::Text(file_id)],
            )
            .await
            .expect_err("an untracked row must not be owned by a tracked file");
        assert_eq!(error.code, LixError::CODE_CONSTRAINT_VIOLATION);
        assert!(
            error.message.contains("which exists but is tracked"),
            "the rejection must name the file's lane, got: {}",
            error.message
        );

        // With the mixed state unreachable, undo is ordinary again: it removes
        // the file it created, and there is no untracked state to strand.
        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("undo removes the tracked file");
        assert!(
            session
                .read_file_content("/owned.txt".into(), None)
                .await
                .expect("file reads")
                .is_none(),
            "undo must remove the file its target commit created"
        );
        assert_eq!(value(&session, "file-ui").await, None);
    }

    #[tokio::test]
    async fn redo_cursor_is_durable_across_fresh_sessions() {
        let storage = Memory::new();
        Engine::initialize(storage.clone())
            .await
            .expect("storage initializes");
        let engine = Engine::new(storage).await.expect("engine opens");
        let first = engine.open_session().await.expect("first session opens");
        first
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('durable', 'yes')",
                &[],
            )
            .await
            .expect("insert commits");
        first
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("insert undoes");
        let branch_id = first.active_branch_id().await.expect("branch resolves");
        drop(first);

        let reopened = engine
            .open_session_at(branch_id)
            .await
            .expect("fresh pinned session opens");
        reopened
            .execute("SELECT commit_id FROM lix_redo()", &[])
            .await
            .expect("redo survives session loss");
        assert_eq!(value(&reopened, "durable").await.as_deref(), Some("yes"));
    }

    #[tokio::test]
    async fn merge_commit_is_an_undo_floor() {
        let storage = Memory::new();
        Engine::initialize(storage.clone())
            .await
            .expect("storage initializes");
        let engine = Engine::new(storage).await.expect("engine opens");
        let main = engine.open_session().await.expect("main session opens");
        let draft = main
            .create_branch(CreateBranchOptions {
                id: Some("01930000-0000-7000-8000-000000000099".to_string()),
                name: "draft".to_string(),
                from_commit_id: None,
            })
            .await
            .expect("draft creates");
        let draft_session = engine
            .open_session_at(draft.id.clone())
            .await
            .expect("draft session opens");
        draft_session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('draft', 'change')",
                &[],
            )
            .await
            .expect("draft diverges");
        main.execute(
            "INSERT INTO lix_key_value (key, value) VALUES ('main', 'change')",
            &[],
        )
        .await
        .expect("main diverges");
        let merge = main
            .merge_branch(MergeBranchOptions {
                source_branch_id: draft.id,
            })
            .await
            .expect("merge commits");
        assert_eq!(merge.outcome, MergeBranchOutcome::MergeCommitted);

        let error = main
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("merge blocks undo");
        assert_eq!(
            error.rows()[0].get::<Value>("commit_id").unwrap(),
            Value::Null
        );
    }

    #[tokio::test]
    async fn observers_receive_undo_and_redo_state() {
        let session = setup().await;
        let mut events = session
            .observe(
                "SELECT value FROM lix_key_value WHERE key = 'observed'",
                &[],
            )
            .expect("observation opens");
        events.next().await.expect("initial event reads");
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('observed', 'yes')",
                &[],
            )
            .await
            .expect("insert commits");
        assert_eq!(
            events
                .next()
                .await
                .expect("insert event reads")
                .expect("insert event exists")
                .rows
                .rows()
                .len(),
            1
        );
        session
            .execute("SELECT commit_id FROM lix_undo()", &[])
            .await
            .expect("insert undoes");
        assert_eq!(
            events
                .next()
                .await
                .expect("undo event reads")
                .expect("undo event exists")
                .rows
                .rows()
                .len(),
            0
        );
        session
            .execute("SELECT commit_id FROM lix_redo()", &[])
            .await
            .expect("insert redoes");
        assert_eq!(
            events
                .next()
                .await
                .expect("redo event reads")
                .expect("redo event exists")
                .rows
                .rows()
                .len(),
            1
        );
    }
}