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
use lix::Value;
use serde_json::json;

use super::select_rows;

simulation_test!(
    relation_diff_pairs_schema_columns_and_inverts_sides,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );
        let baseline = sim.initial_commit_id().to_string();

        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('note', 'first')",
                &[],
            )
            .await
            .expect("tracked insert should succeed");
        let inserted = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("inserted head should load")
            .expect("inserted head should exist")
            .to_string();

        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT key, diff_type, from_value, to_value \
                 FROM lix_diff('lix_key_value', '{baseline}', '{inserted}')"
                ),
            )
            .await,
            vec![vec![
                Value::Text("note".to_string()),
                Value::Text("added".to_string()),
                Value::Null,
                Value::Jsonb(json!("first").into()),
            ]]
        );

        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT key, diff_type, from_value, to_value \
                 FROM lix_diff('lix_key_value', '{inserted}', '{baseline}')"
                ),
            )
            .await,
            vec![vec![
                Value::Text("note".to_string()),
                Value::Text("removed".to_string()),
                Value::Jsonb(json!("first").into()),
                Value::Null,
            ]]
        );

        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT count(*) FROM lix_diff('lix_key_value', '{inserted}', '{inserted}')"
                ),
            )
            .await,
            vec![vec![Value::Integer(0)]]
        );

        assert_eq!(
            select_rows(
                &session,
                "SELECT count(*) FROM lix_diff(\
                 'lix_key_value', lix_root_commit_id(), lix_active_branch_commit_id()\
             ) WHERE key = 'note'",
            )
            .await,
            vec![vec![Value::Integer(1)]],
            "commit graph accessors are valid explicit table-function arguments",
        );

        session
            .execute(
                "UPDATE lix_key_value SET value = 'second' WHERE key = 'note'",
                &[],
            )
            .await
            .expect("tracked update should succeed");
        let updated = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("updated head should load")
            .expect("updated head should exist")
            .to_string();
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT diff_type, from_value, to_value \
                 FROM lix_diff('lix_key_value', '{inserted}', '{updated}') \
                 WHERE key = 'note'"
                ),
            )
            .await,
            vec![vec![
                Value::Text("modified".to_string()),
                Value::Jsonb(json!("first").into()),
                Value::Jsonb(json!("second").into()),
            ]]
        );
    }
);

simulation_test!(
    relation_diff_preserves_global_scope_on_both_sides,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );
        let local_before = sim.initial_commit_id().to_string();
        let global_before = engine
            .load_branch_head_commit_id(lix::GLOBAL_BRANCH_ID)
            .await
            .expect("global head should load")
            .expect("global head should exist")
            .to_string();

        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('local-scope', 'value')",
                &[],
            )
            .await
            .expect("branch-local insert should succeed");
        let local_after = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("local head should load")
            .expect("local head should exist")
            .to_string();
        session
            .execute("INSERT INTO lix_key_value (key, value, lixcol_global) VALUES ('global-scope', 'value', TRUE)", &[])
            .await
            .expect("global row should publish");
        let global_after = engine
            .load_branch_head_commit_id(lix::GLOBAL_BRANCH_ID)
            .await
            .expect("updated global head should load")
            .expect("updated global head should exist")
            .to_string();

        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT from_lixcol_global, to_lixcol_global \
                 FROM lix_diff('lix_key_value', '{global_before}', '{global_after}')"
                ),
            )
            .await,
            vec![vec![Value::Null, Value::Boolean(true)]],
            "global additions preserve the source relation's scope",
        );
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT from_lixcol_global, to_lixcol_global \
                 FROM lix_diff('lix_key_value', '{global_after}', '{global_before}')"
                ),
            )
            .await,
            vec![vec![Value::Boolean(true), Value::Null]],
            "reversing a global diff preserves its global before-side",
        );
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT from_lixcol_global, to_lixcol_global \
                 FROM lix_diff('lix_key_value', '{local_before}', '{local_after}')"
                ),
            )
            .await,
            vec![vec![Value::Null, Value::Boolean(false)]],
            "branch-local relation rows remain local in diff metadata",
        );
    }
);

simulation_test!(
    working_diff_span_reports_global_provenance,
    |sim| async move {
        // Regression: the checkpoint-to-head working span with an identity-only
        // provenance projection selected the HOT fast path, which carries no
        // provenance sets — a local edit shadowing an inherited global row
        // reported its global before-side as local.
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );
        session
            .execute(
                "INSERT INTO lix_key_value (key, value, lixcol_global) \
             VALUES ('shadowed', 'global-value', TRUE)",
                &[],
            )
            .await
            .expect("global insert should succeed");
        session
            .create_checkpoint()
            .await
            .expect("checkpoint should pin the working-diff cursor");
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('shadowed', 'local-shadow') \
             ON CONFLICT(key) DO UPDATE SET value = excluded.value",
                &[],
            )
            .await
            .expect("active upsert should shadow the inherited global row");

        assert_eq!(
            select_rows(&session,
                "SELECT diff_type, from_value, to_value FROM lix_diff('lix_key_value') WHERE key = 'shadowed'",
            ).await,
            vec![vec![Value::Text("modified".into()), Value::Jsonb(json!("global-value").into()), Value::Jsonb(json!("local-shadow").into())]],
            "effective classification and values do not depend on projecting provenance",
        );
        assert_eq!(
            select_rows(
                &session,
                "SELECT from_lixcol_global, to_lixcol_global \
             FROM lix_diff('lix_key_value') \
             WHERE key = 'shadowed'",
            )
            .await,
            vec![vec![Value::Boolean(true), Value::Boolean(false)]],
            "the shadowed row's before-side is the inherited global version",
        );
    }
);

simulation_test!(
    relation_diff_aggregates_files_and_preserves_removed_paths,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );
        let baseline = sim.initial_commit_id().to_string();

        session
            .execute(
                "INSERT INTO lix_file (path, content) VALUES ('/note.txt', CAST('hello' AS BYTEA))",
                &[],
            )
            .await
            .expect("file insert should succeed");
        let inserted = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("inserted head should load")
            .expect("inserted head should exist")
            .to_string();
        let added = select_rows(
            &session,
            &format!(
                "SELECT id, diff_type, from_path, to_path \
             FROM lix_diff('lix_file', '{baseline}', '{inserted}')"
            ),
        )
        .await;
        assert_eq!(added.len(), 1, "descriptor and content form one file diff");
        assert_eq!(added[0][1], Value::Text("added".to_string()));
        assert_eq!(added[0][2], Value::Null);
        assert_eq!(added[0][3], Value::Text("/note.txt".to_string()));
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT diff_type, from_path, to_path \
                 FROM lix_diff('lix_file', '{inserted}', '{baseline}')"
                ),
            )
            .await,
            vec![vec![
                Value::Text("removed".to_string()),
                Value::Text("/note.txt".to_string()),
                Value::Null,
            ]],
            "reversing a file addition swaps sides and inverts its classification",
        );
        let file_id = match &added[0][0] {
            Value::Text(id) => id.clone(),
            other => panic!("file row identity should be text, got {other:?}"),
        };
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT count(*) FROM lix_diff('lix_file', '{baseline}', '{inserted}') \
                 WHERE id = '{file_id}'"
                ),
            )
            .await,
            vec![vec![Value::Integer(1)]],
            "typed primary-key equality remains a pushed file-scoped filter",
        );
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT count(*) \
                 FROM lix_diff('lix_file', '{baseline}', '{inserted}')"
                ),
            )
            .await,
            vec![vec![Value::Integer(1)]],
            "counts operate on logical file rows",
        );

        session
            .execute(
                "UPDATE lix_file SET path = '/renamed.txt' WHERE path = '/note.txt'",
                &[],
            )
            .await
            .expect("file rename should succeed");
        let renamed = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("renamed head should load")
            .expect("renamed head should exist")
            .to_string();
        assert_eq!(
        select_rows(
            &session,
            &format!(
                "SELECT diff_type, from_path, to_path FROM lix_diff('lix_file', '{inserted}', '{renamed}')"
            ),
        )
        .await,
        vec![vec![
            Value::Text("modified".to_string()),
            Value::Text("/note.txt".to_string()),
            Value::Text("/renamed.txt".to_string()),
        ]]
    );

        session
            .execute("DELETE FROM lix_file WHERE path = '/renamed.txt'", &[])
            .await
            .expect("file removal should succeed");
        let removed = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("removed head should load")
            .expect("removed head should exist")
            .to_string();
        assert_eq!(
        select_rows(
            &session,
            &format!(
                "SELECT diff_type, from_path, to_path FROM lix_diff('lix_file', '{renamed}', '{removed}')"
            ),
        )
        .await,
        vec![vec![
            Value::Text("removed".to_string()),
            Value::Text("/renamed.txt".to_string()),
            Value::Null,
        ]]
    );
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT diff_type, from_path, to_path \
                 FROM lix_diff('lix_file', '{removed}', '{renamed}')"
                ),
            )
            .await,
            vec![vec![
                Value::Text("added".to_string()),
                Value::Null,
                Value::Text("/renamed.txt".to_string()),
            ]],
            "reversing a file removal restores its historical path",
        );
    }
);

simulation_test!(
    default_range_diff_uses_certified_hot_epoch_without_changing_its_schema,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );
        session
            .execute(
                "INSERT INTO lix_file (path, content) VALUES \
             ('/one.txt', CAST('one' AS BYTEA)), ('/two.txt', CAST('two' AS BYTEA))",
                &[],
            )
            .await
            .expect("file inserts should succeed");
        let rows = select_rows(
            &session,
            "SELECT coalesce(to_path, from_path) AS path, diff_type \
             FROM lix_diff('lix_file') ORDER BY coalesce(to_path, from_path)",
        )
        .await;
        assert_eq!(rows.len(), 2);
        for (index, path) in ["/one.txt", "/two.txt"].into_iter().enumerate() {
            assert_eq!(rows[index][0], Value::Text(path.to_string()));
            assert_eq!(rows[index][1], Value::Text("added".to_string()));
        }

        assert_eq!(
            select_rows(
                &session,
                "SELECT from_content, to_content FROM lix_diff('lix_file') ORDER BY to_path"
            )
            .await,
            vec![
                vec![Value::Null, Value::Blob(b"one".to_vec().into())],
                vec![Value::Null, Value::Blob(b"two".to_vec().into())]
            ]
        );
    }
);

simulation_test!(
    working_diff_remains_available_after_interval_local_add_then_delete,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );

        session
            .execute(
                "INSERT INTO lix_file (id, path, content) VALUES \
                 ('87851c4d-9308-46af-9046-5aa5351250fd', '/net-zero.md', CAST('temporary' AS BYTEA))",
                &[],
            )
            .await
            .expect("file insert should succeed");
        session
            .execute(
                "DELETE FROM lix_file WHERE id = '87851c4d-9308-46af-9046-5aa5351250fd'",
                &[],
            )
            .await
            .expect("file delete should succeed");

        assert!(
            select_rows(&session, "SELECT id, diff_type FROM lix_diff('lix_file')",)
                .await
                .is_empty(),
            "net-zero file churn must not appear in working review",
        );
    }
);

simulation_test!(
    relation_diff_tracks_directory_descriptor_add_rename_and_remove,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );
        let baseline = sim.initial_commit_id().to_string();

        session
            .execute("INSERT INTO lix_directory (path) VALUES ('/docs')", &[])
            .await
            .expect("directory insert should succeed");
        let inserted = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("inserted head should load")
            .expect("inserted head should exist")
            .to_string();
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT diff_type, from_path, to_path \
                 FROM lix_diff('lix_directory', '{baseline}', '{inserted}')"
                ),
            )
            .await,
            vec![vec![
                Value::Text("added".to_string()),
                Value::Null,
                Value::Text("/docs".to_string()),
            ]]
        );

        session
            .execute(
                "UPDATE lix_directory SET path = '/archive' WHERE path = '/docs'",
                &[],
            )
            .await
            .expect("directory rename should succeed");
        let renamed = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("renamed head should load")
            .expect("renamed head should exist")
            .to_string();
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT diff_type, from_path, to_path \
                 FROM lix_diff('lix_directory', '{inserted}', '{renamed}')"
                ),
            )
            .await,
            vec![vec![
                Value::Text("modified".to_string()),
                Value::Text("/docs".to_string()),
                Value::Text("/archive".to_string()),
            ]]
        );

        session
            .execute("DELETE FROM lix_directory WHERE path = '/archive'", &[])
            .await
            .expect("directory deletion should succeed");
        let removed = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("removed head should load")
            .expect("removed head should exist")
            .to_string();
        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT diff_type, from_path, to_path \
                 FROM lix_diff('lix_directory', '{renamed}', '{removed}')"
                ),
            )
            .await,
            vec![vec![
                Value::Text("removed".to_string()),
                Value::Text("/archive".to_string()),
                Value::Null,
            ]]
        );
    }
);

simulation_test!(
    relation_diff_accepts_default_range_and_rejects_partial_range,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );

        let missing_relation = session
            .execute("SELECT * FROM lix_diff('a', 'b')", &[])
            .await
            .expect_err("legacy two-argument lix_diff must be rejected");
        assert!(
            missing_relation
                .message
                .contains("requires a relation and either zero or two commit ID arguments")
        );

        let unsupported = session
            .execute("SELECT * FROM lix_diff('not_a_relation', 'a', 'b')", &[])
            .await
            .expect_err("unknown relations must be rejected");
        assert!(unsupported.message.contains("does not support relation"));

        let file_content = session.execute(
            "SELECT from_content, to_content FROM lix_diff('lix_file', lix_root_commit_id(), lix_active_branch_commit_id())", &[])
            .await.expect("empty file diffs support content projection");
        assert_eq!(file_content.columns(), ["from_content", "to_content"]);
    }
);

simulation_test!(
    relation_diff_fences_machinery_from_user_column_names,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );

        let reserved = session
        .execute(
            "INSERT INTO lix_registered_schema (value, lixcol_global, lixcol_untracked) \
             VALUES (CAST('{\"$schema\":\"https://lix.dev/schema-v1.json\",\"key\":\"reserved_name_collision\",\"columns\":[{\"name\":\"id\",\"type\":\"text\",\"nullable\":false},{\"name\":\"from_lixcol_user_value\",\"type\":\"text\",\"nullable\":true}],\"primary_key\":[\"id\"]}' AS JSONB), false, false)",
            &[],
        )
        .await
        .expect_err("schema registration must reject the reserved lixcol_ segment");
        assert!(reserved.message.contains("reserved lixcol_ segment"));

        session
        .execute(
            "INSERT INTO lix_registered_schema (value, lixcol_global, lixcol_untracked) \
             VALUES (CAST('{\"$schema\":\"https://lix.dev/schema-v1.json\",\"key\":\"diff_name_collision\",\"columns\":[{\"name\":\"id\",\"type\":\"text\",\"nullable\":false},{\"name\":\"diff_type\",\"type\":\"text\",\"nullable\":false},{\"name\":\"row_count\",\"type\":\"int8\",\"nullable\":false},{\"name\":\"depth\",\"type\":\"int8\",\"nullable\":false},{\"name\":\"from_path\",\"type\":\"text\",\"nullable\":false}],\"primary_key\":[\"id\"]}' AS JSONB), false, false)",
            &[],
        )
        .await
        .expect("collision schema should register");
        let baseline = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("baseline head should load")
            .expect("baseline head should exist")
            .to_string();

        session
            .execute(
                "INSERT INTO diff_name_collision (id, diff_type, row_count, depth, from_path) \
             VALUES ('row-1', 'user-kind', 7, 3, '/user-path')",
                &[],
            )
            .await
            .expect("collision row should insert");
        let head = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("updated head should load")
            .expect("updated head should exist")
            .to_string();

        assert_eq!(
            select_rows(
                &session,
                &format!(
                    "SELECT id, diff_type, \
                 from_diff_type, to_diff_type, from_row_count, to_row_count, \
                 from_depth, to_depth, from_from_path, to_from_path \
                 FROM lix_diff('diff_name_collision', '{baseline}', '{head}')"
                ),
            )
            .await,
            vec![vec![
                Value::Text("row-1".to_string()),
                Value::Text("added".to_string()),
                Value::Null,
                Value::Text("user-kind".to_string()),
                Value::Null,
                Value::Integer(7),
                Value::Null,
                Value::Integer(3),
                Value::Null,
                Value::Text("/user-path".to_string()),
            ]]
        );

        for retired in ["lixcol_diff_type", "lixcol_row_count", "row_count"] {
            let error = session
            .execute(
                &format!(
                    "SELECT {retired} FROM lix_diff('diff_name_collision', '{baseline}', '{head}')"
                ),
                &[],
            )
            .await
            .expect_err("retired diff machinery alias must not exist");
            assert!(
                error.message.contains(retired),
                "error should identify retired column {retired}: {}",
                error.message
            );
            assert!(
                retired == "row_count" || error.message.contains("Did you mean"),
                "renamed diff columns should preserve DataFusion's near-match guidance: {}",
                error.message
            );
        }

        let error = session
        .execute(
            &format!(
                "SELECT not_a_diff_column FROM lix_diff('diff_name_collision', '{baseline}', '{head}')"
            ),
            &[],
        )
        .await
        .expect_err("unknown diff column must not exist");
        assert!(
            error.message.contains("Valid fields are")
                && error.message.contains("diff_type")
                && error.message.contains("row_count"),
            "unknown diff columns should preserve DataFusion's discoverable field listing: {}",
            error.message
        );
    }
);

simulation_test!(
    working_diff_retains_relation_payloads_on_both_sides,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );
        session
            .execute(
                "INSERT INTO lix_key_value (key, value, lixcol_metadata) VALUES \
                 ('modified', 'before', '{\"stage\":\"before\"}'), ('removed', 'deleted', NULL)",
                &[],
            )
            .await
            .expect("baseline rows should insert");
        session
            .create_checkpoint()
            .await
            .expect("checkpoint should succeed");
        for sql in [
            "INSERT INTO lix_key_value (key, value) VALUES ('added', 'new')",
            "UPDATE lix_key_value SET value = 'after', lixcol_metadata = '{\"stage\":\"after\"}' WHERE key = 'modified'",
            "DELETE FROM lix_key_value WHERE key = 'removed'",
        ] {
            session
                .execute(sql, &[])
                .await
                .expect("working edit should succeed");
        }
        assert_eq!(
            select_rows(
                &session,
                "SELECT key, diff_type FROM lix_diff('lix_key_value') ORDER BY key"
            )
            .await,
            vec![
                vec![Value::Text("added".into()), Value::Text("added".into())],
                vec![
                    Value::Text("modified".into()),
                    Value::Text("modified".into())
                ],
                vec![Value::Text("removed".into()), Value::Text("removed".into())],
            ],
            "identity-only projections must preserve the same changed rows",
        );
        let projection =
            "key, diff_type, from_value, to_value, from_lixcol_global, to_lixcol_global";
        let expected = vec![
            vec![
                Value::Text("added".into()),
                Value::Text("added".into()),
                Value::Null,
                Value::Jsonb(json!("new").into()),
                Value::Null,
                Value::Boolean(false),
            ],
            vec![
                Value::Text("modified".into()),
                Value::Text("modified".into()),
                Value::Jsonb(json!("before").into()),
                Value::Jsonb(json!("after").into()),
                Value::Boolean(false),
                Value::Boolean(false),
            ],
            vec![
                Value::Text("removed".into()),
                Value::Text("removed".into()),
                Value::Jsonb(json!("deleted").into()),
                Value::Null,
                Value::Boolean(false),
                Value::Null,
            ],
        ];
        let coordinates = session.execute("SELECT working_base_commit_id, commit_id FROM lix_branch WHERE id = lix_active_branch_id()", &[]).await.unwrap();
        let from = coordinates.rows()[0]
            .get::<String>("working_base_commit_id")
            .unwrap();
        let to = coordinates.rows()[0].get::<String>("commit_id").unwrap();
        for arguments in [
            "'lix_key_value'".to_string(),
            format!("'lix_key_value', '{from}', '{to}'"),
        ] {
            assert_eq!(
                select_rows(
                    &session,
                    &format!("SELECT {projection} FROM lix_diff({arguments}) ORDER BY key")
                )
                .await,
                expected,
                "working and explicit ranges must preserve payload values and side presence: {arguments}",
            );
        }
        assert_eq!(
            select_rows(
                &session,
                "SELECT from_lixcol_metadata, to_lixcol_metadata \
                 FROM lix_diff('lix_key_value') WHERE key = 'modified'"
            )
            .await,
            vec![vec![
                Value::Jsonb(json!({"stage": "before"}).into()),
                Value::Jsonb(json!({"stage": "after"}).into()),
            ]],
            "metadata must be loaded from each side's exact change",
        );
        assert_eq!(
            select_rows(&session,
                "SELECT key FROM lix_diff('lix_key_value') WHERE from_value IS NOT NULL AND to_value IS NOT NULL"
            ).await,
            vec![vec![Value::Text("modified".into())]],
            "payloads used only by a predicate must also be retained",
        );
    }
);

simulation_test!(
    working_diff_retains_directory_descriptors_on_both_sides,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine.open_session().await.expect("session should open"),
            &engine,
        );
        session
            .execute(
                "INSERT INTO lix_directory (path) VALUES ('/docs/original'), ('/docs/removed')",
                &[],
            )
            .await
            .expect("nested baseline directories should insert");
        session
            .create_checkpoint()
            .await
            .expect("checkpoint should succeed");
        for sql in [
            "INSERT INTO lix_directory (path) VALUES ('/docs/added')",
            "UPDATE lix_directory SET path = '/docs/renamed' WHERE path = '/docs/original'",
            "DELETE FROM lix_directory WHERE path = '/docs/removed'",
        ] {
            session
                .execute(sql, &[])
                .await
                .expect("working edit should succeed");
        }
        let expected = vec![
            vec![
                Value::Text("added".into()),
                Value::Null,
                Value::Text("added".into()),
                Value::Null,
                Value::Text("/docs/added".into()),
            ],
            vec![
                Value::Text("removed".into()),
                Value::Text("removed".into()),
                Value::Null,
                Value::Text("/docs/removed".into()),
                Value::Null,
            ],
            vec![
                Value::Text("modified".into()),
                Value::Text("original".into()),
                Value::Text("renamed".into()),
                Value::Text("/docs/original".into()),
                Value::Text("/docs/renamed".into()),
            ],
        ];
        let coordinates = session.execute("SELECT working_base_commit_id, commit_id FROM lix_branch WHERE id = lix_active_branch_id()", &[]).await.unwrap();
        let from = coordinates.rows()[0]
            .get::<String>("working_base_commit_id")
            .unwrap();
        let to = coordinates.rows()[0].get::<String>("commit_id").unwrap();
        for arguments in [
            "'lix_directory'".to_string(),
            format!("'lix_directory', '{from}', '{to}'"),
        ] {
            assert_eq!(
                select_rows(
                    &session,
                    &format!(
                        "SELECT diff_type, from_name, to_name, from_path, to_path \
                     FROM lix_diff({arguments}) ORDER BY coalesce(to_path, from_path)"
                    )
                )
                .await,
                expected,
                "working and explicit ranges must reconstruct paths through unchanged parents: {arguments}",
            );
        }
    }
);

simulation_test!(
    file_diff_content_preserves_absent_empty_and_renamed_sides,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(engine.open_session().await.unwrap(), &engine);
        let id = "0193182b-2a72-7ed5-9015-76bf271af333";
        let steps = [
            (
                "INSERT INTO lix_file(id,path,content) VALUES ($1, '/empty', CAST('' AS BYTEA))",
                "added",
                Value::Null,
                Value::Blob(Vec::new().into()),
            ),
            (
                "UPDATE lix_file SET path='/renamed' WHERE id=$1",
                "modified",
                Value::Blob(Vec::new().into()),
                Value::Blob(Vec::new().into()),
            ),
            (
                "UPDATE lix_file SET content=CAST('next' AS BYTEA) WHERE id=$1",
                "modified",
                Value::Blob(Vec::new().into()),
                Value::Blob(b"next".to_vec().into()),
            ),
            (
                "DELETE FROM lix_file WHERE id=$1",
                "removed",
                Value::Blob(b"next".to_vec().into()),
                Value::Null,
            ),
        ];
        for (sql, kind, old, new) in steps {
            let before = engine
                .load_branch_head_commit_id(sim.main_branch_id())
                .await
                .unwrap()
                .unwrap()
                .to_string();
            session
                .execute(sql, &[Value::Text(id.into())])
                .await
                .unwrap();
            let after = engine
                .load_branch_head_commit_id(sim.main_branch_id())
                .await
                .unwrap()
                .unwrap()
                .to_string();
            // Snapshot absence is no row, while a present empty file retains its
            // zero-length bytes. Each diff side must agree with that same read.
            for (commit, content) in [(&before, &old), (&after, &new)] {
                let expected = if *content == Value::Null {
                    Vec::new()
                } else {
                    vec![vec![content.clone()]]
                };
                assert_eq!(
                    select_rows(
                        &session,
                        &format!(
                            "SELECT content FROM lix_as_of('lix_file', '{commit}') WHERE id='{id}'"
                        ),
                    )
                    .await,
                    expected,
                    "snapshot and diff content must agree at {commit}",
                );
            }
            for source in [
                format!("lix_diff('lix_file', '{before}', '{after}')"),
                format!("lix_history('lix_file', '{after}')"),
            ] {
                assert_eq!(select_rows(&session, &format!("SELECT diff_type, from_content, to_content FROM {source} WHERE id='{id}' AND lixcol_to_commit_id='{after}'")).await,
                vec![vec![Value::Text(kind.into()), old.clone(), new.clone()]]);
            }
            assert_eq!(select_rows(&session, &format!("SELECT from_content, to_content FROM lix_diff('lix_file', '{after}', '{before}') WHERE id='{id}'")).await,
            vec![vec![new, old]]);
        }
    }
);