lix 0.12.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
use std::collections::BTreeSet;

use lix::{CreateBranchOptions, Value};

use super::select_rows;

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

        let initial_head = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("branch head should load")
            .expect("branch head should exist");

        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('commit-surface', 'one')",
                &[],
            )
            .await
            .expect("first tracked write should succeed");
        let first_head = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("branch head should load")
            .expect("branch head should exist");

        session
            .execute(
                "UPDATE lix_key_value SET value = 'two' WHERE key = 'commit-surface'",
                &[],
            )
            .await
            .expect("second tracked write should succeed");
        let second_head = engine
            .load_branch_head_commit_id(sim.main_branch_id())
            .await
            .expect("branch head should load")
            .expect("branch head should exist");

        let commit_rows = select_rows(
            &session,
            &format!(
                "SELECT id, lixcol_global, lixcol_untracked \
                 FROM lix_commit WHERE id = '{second_head}'"
            ),
        )
        .await;
        assert_eq!(
            commit_rows,
            vec![vec![
                Value::Text(second_head.clone()),
                Value::Boolean(true),
                Value::Boolean(false),
            ]]
        );

        let edge_rows = select_rows(
            &session,
            &format!(
                "SELECT parent_id, child_id, parent_order, lixcol_global, lixcol_untracked \
                 FROM lix_commit_edge WHERE child_id = '{second_head}'"
            ),
        )
        .await;
        assert_eq!(
            edge_rows,
            vec![vec![
                Value::Text(first_head.clone()),
                Value::Text(second_head.clone()),
                Value::Integer(0),
                Value::Boolean(true),
                Value::Boolean(false),
            ]]
        );

        let by_branch_rows = select_rows(
            &session,
            &format!(
                "SELECT id, lixcol_branch_id, lixcol_global, lixcol_untracked \
                 FROM lix_commit_by_branch \
                 WHERE id IN ('{initial_head}', '{first_head}', '{second_head}') \
                 ORDER BY id, lixcol_branch_id"
            ),
        )
        .await;
        assert!(by_branch_rows.contains(&vec![
            Value::Text(initial_head.clone()),
            Value::Text(sim.main_branch_id().to_string()),
            Value::Boolean(true),
            Value::Boolean(false),
        ]));
        assert!(by_branch_rows.contains(&vec![
            Value::Text(initial_head),
            Value::Text("ffffffff-ffff-7fff-bfff-ffffffffffff".to_string()),
            Value::Boolean(true),
            Value::Boolean(false),
        ]));
        assert!(by_branch_rows.contains(&vec![
            Value::Text(first_head.clone()),
            Value::Text(sim.main_branch_id().to_string()),
            Value::Boolean(true),
            Value::Boolean(false),
        ]));
        assert!(by_branch_rows.contains(&vec![
            Value::Text(first_head.clone()),
            Value::Text("ffffffff-ffff-7fff-bfff-ffffffffffff".to_string()),
            Value::Boolean(true),
            Value::Boolean(false),
        ]));
        assert!(by_branch_rows.contains(&vec![
            Value::Text(second_head.clone()),
            Value::Text(sim.main_branch_id().to_string()),
            Value::Boolean(true),
            Value::Boolean(false),
        ]));
        assert!(by_branch_rows.contains(&vec![
            Value::Text(second_head.clone()),
            Value::Text("ffffffff-ffff-7fff-bfff-ffffffffffff".to_string()),
            Value::Boolean(true),
            Value::Boolean(false),
        ]));

        let edge_by_branch_rows = select_rows(
            &session,
            &format!(
                "SELECT parent_id, child_id, parent_order, lixcol_branch_id, lixcol_global, lixcol_untracked \
                 FROM lix_commit_edge_by_branch \
                 WHERE child_id = '{second_head}' \
                 ORDER BY lixcol_branch_id"
            ),
        )
        .await;
        assert_eq!(
            edge_by_branch_rows,
            vec![
                vec![
                    Value::Text(first_head.clone()),
                    Value::Text(second_head.clone()),
                    Value::Integer(0),
                    Value::Text(sim.main_branch_id().to_string()),
                    Value::Boolean(true),
                    Value::Boolean(false),
                ],
                vec![
                    Value::Text(first_head),
                    Value::Text(second_head),
                    Value::Integer(0),
                    Value::Text("ffffffff-ffff-7fff-bfff-ffffffffffff".to_string()),
                    Value::Boolean(true),
                    Value::Boolean(false),
                ],
            ]
        );
    }
);

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

        main.execute(
            "INSERT INTO lix_key_value (key, value) VALUES ('main-only', 'main')",
            &[],
        )
        .await
        .expect("main write should succeed");

        main.create_branch(CreateBranchOptions {
            id: Some("01930000-0000-7000-8000-000000000006".to_string()),
            name: "Commit branch".to_string(),
            from_commit_id: None,
        })
        .await
        .expect("branch branch should be created");

        let branch = sim.wrap_session(
            engine
                .open_session_at("01930000-0000-7000-8000-000000000006")
                .await
                .expect("branch session should open"),
            &engine,
        );
        branch
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('6272616e-6368-8d6f-8e6c-790000000000', 'branch')",
                &[],
            )
            .await
            .expect("branch write should succeed");

        let branch_head = engine
            .load_branch_head_commit_id("01930000-0000-7000-8000-000000000006")
            .await
            .expect("branch head should load")
            .expect("branch head should exist");

        let main_commit_rows = select_rows(
            &main,
            &format!("SELECT id FROM lix_commit WHERE id = '{branch_head}'"),
        )
        .await;
        let branch_commit_rows = select_rows(
            &branch,
            &format!("SELECT id FROM lix_commit WHERE id = '{branch_head}'"),
        )
        .await;
        assert_eq!(
            main_commit_rows, branch_commit_rows,
            "lix_commit should not depend on the active branch"
        );
        assert_eq!(
            main_commit_rows,
            vec![vec![Value::Text(branch_head.clone())]]
        );

        let main_edge_rows = select_rows(
            &main,
            &format!("SELECT child_id FROM lix_commit_edge WHERE child_id = '{branch_head}'"),
        )
        .await;
        let branch_edge_rows = select_rows(
            &branch,
            &format!("SELECT child_id FROM lix_commit_edge WHERE child_id = '{branch_head}'"),
        )
        .await;
        assert_eq!(
            main_edge_rows, branch_edge_rows,
            "derived commit surfaces should also expose global commit-derived rows"
        );
        assert_eq!(main_edge_rows, vec![vec![Value::Text(branch_head)]]);
    }
);

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

        main.execute(
            "INSERT INTO lix_key_value (key, value) VALUES ('main-edge-probe', 'main')",
            &[],
        )
        .await
        .expect("main write should succeed");

        main.create_branch(CreateBranchOptions {
            id: Some("01930000-0000-7000-8000-000000000007".to_string()),
            name: "Edge Probe A".to_string(),
            from_commit_id: Some(sim.initial_commit_id().to_string()),
        })
        .await
        .expect("01930000-0000-7000-8000-000000000007 should be created from the initial commit");
        main.create_branch(CreateBranchOptions {
            id: Some("01930000-0000-7000-8000-000000000008".to_string()),
            name: "Edge Probe B".to_string(),
            from_commit_id: Some(sim.initial_commit_id().to_string()),
        })
        .await
        .expect("01930000-0000-7000-8000-000000000008 should be created from the initial commit");

        let branch_a = sim.wrap_session(
            engine
                .open_session_at("01930000-0000-7000-8000-000000000007")
                .await
                .expect("01930000-0000-7000-8000-000000000007 session should open"),
            &engine,
        );
        branch_a
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('edge-probe-a-only', 'a')",
                &[],
            )
            .await
            .expect("01930000-0000-7000-8000-000000000007 write should succeed");

        let branch_b = sim.wrap_session(
            engine
                .open_session_at("01930000-0000-7000-8000-000000000008")
                .await
                .expect("01930000-0000-7000-8000-000000000008 session should open"),
            &engine,
        );
        branch_b
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('edge-probe-b-only', 'b')",
                &[],
            )
            .await
            .expect("01930000-0000-7000-8000-000000000008 write should succeed");

        let global_edges =
            commit_edges_by_branch(&main, "ffffffff-ffff-7fff-bfff-ffffffffffff").await;
        for branch_id in [
            sim.main_branch_id(),
            "01930000-0000-7000-8000-000000000007",
            "01930000-0000-7000-8000-000000000008",
        ] {
            let actual_edges = commit_edges_by_branch(&main, branch_id).await;
            assert_eq!(
                actual_edges, global_edges,
                "lix_commit_edge_by_branch should project derived global edges for {branch_id}"
            );
        }
    }
);

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

        for (schema_key, tables) in [
            ("lix_commit", vec!["lix_commit", "lix_commit_by_branch"]),
            (
                "lix_commit_edge",
                vec!["lix_commit_edge", "lix_commit_edge_by_branch"],
            ),
        ] {
            let schema_properties = builtin_schema_property_names(schema_key);
            for table in tables {
                let surface_columns = non_system_column_names(&session, table).await;
                assert_eq!(
                    surface_columns, schema_properties,
                    "{table} data columns should match {schema_key} properties"
                );
            }
        }
    }
);

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

        for table in [
            "lix_commit",
            "lix_commit_by_branch",
            "lix_commit_edge",
            "lix_commit_edge_by_branch",
        ] {
            let rows = select_rows(&session, &format!("SELECT count(*) FROM {table}")).await;
            assert_single_count(rows, table);
        }
    }
);

fn assert_single_count(rows: Vec<Vec<Value>>, table: &str) {
    assert_eq!(rows.len(), 1, "{table} should return one count row");
    assert_eq!(rows[0].len(), 1, "{table} should return one count column");
    let Value::Integer(count) = rows[0][0] else {
        panic!(
            "{table} should return an integer count, got {:?}",
            rows[0][0]
        );
    };
    assert!(count >= 0, "{table} count should be non-negative");
}

fn text_value(value: &Value) -> String {
    let Value::Text(value) = value else {
        panic!("expected text value, got {value:?}");
    };
    value.clone()
}

async fn commit_edges_by_branch(
    session: &crate::support::simulation_test::engine::SimSession,
    branch_id: &str,
) -> BTreeSet<(String, String)> {
    select_rows(
        session,
        &format!(
            "SELECT parent_id, child_id \
             FROM lix_commit_edge_by_branch \
             WHERE lixcol_branch_id = '{branch_id}'"
        ),
    )
    .await
    .into_iter()
    .map(|row| (text_value(&row[0]), text_value(&row[1])))
    .collect()
}

fn builtin_schema_property_names(schema_key: &str) -> BTreeSet<String> {
    let schema = match schema_key {
        "lix_commit" => include_str!("../../../src/schema/builtin/lix_commit.json"),
        "lix_commit_edge" => include_str!("../../../src/schema/builtin/lix_commit_edge.json"),
        other => panic!("unexpected builtin schema key: {other}"),
    };
    let schema = serde_json::from_str::<serde_json::Value>(schema)
        .expect("builtin schema fixture should parse");
    schema
        .get("columns")
        .and_then(serde_json::Value::as_array)
        .expect("builtin schema should define columns")
        .iter()
        .map(|column| column["name"].as_str().expect("column name").to_string())
        .collect::<BTreeSet<_>>()
}

async fn non_system_column_names(
    session: &crate::support::simulation_test::engine::SimSession,
    table_name: &str,
) -> BTreeSet<String> {
    let rows = select_rows(
        session,
        &format!(
            "SELECT column_name \
             FROM information_schema.columns \
             WHERE table_name = '{table_name}'"
        ),
    )
    .await;
    rows.into_iter()
        .map(|row| text_value(&row[0]))
        .filter(|column_name| !column_name.starts_with("lixcol_"))
        .collect()
}