lix 0.17.1

Embeddable version control for apps and AI agents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Staging of root-backed opening coordinates for a partial replica.
//!
//! This helper is only for fresh local storage. It publishes no complete-state
//! certificate, materializes no native objects, and does not activate an engine.
//! A dedicated opener must also bind repository identity, admission and object
//! ownership before exposing these coordinates to SQL or garbage collection.

use bytes::Bytes;

use crate::LixError;
use crate::branch::{
    BranchHeadControl, branch_head_control_precondition, stage_branch_head_control,
};
use crate::changelog::{ChangeId, CommitId};
use crate::common::LixTimestamp;
use crate::hot_state::{ROOT_CURRENT_BASE_SPACE, TrackedHeadContext, hot_generation_scope_prefix};
use crate::storage_adapter::{
    StorageAdapterRead, StorageKey, StoragePrecondition, StorageWriteSet,
};

use super::partial_state::{PartialReplicaState, stage_partial_replica_state};

/// Canonical local control for an admitted immutable authority basis. The
/// local serving generation is separate from authority identity/revision.
pub(super) fn partial_branch_control(
    state: &PartialReplicaState,
    branch: &super::partial_replica::PartialReplicaBranch,
) -> Result<BranchHeadControl, LixError> {
    let invalid = |field: &str| {
        LixError::new(
            "LIX_PARTIAL_REPLICA_STATE_INVALID",
            format!("partial replica has invalid {field}"),
        )
    };
    let head = CommitId::parse(&branch.head.commit_id).map_err(|_| invalid("head ID"))?;
    let checkpoint =
        CommitId::parse(&branch.checkpoint.commit_id).map_err(|_| invalid("checkpoint ID"))?;
    Ok(BranchHeadControl {
        head_commit_id: head,
        tracked_generation: state.serving_generation(&branch.branch_id)?,
        current_state_revision: 0,
        working_diff_checkpoint_commit_id: Some(checkpoint),
        created_at: LixTimestamp::parse(&branch.created_at).map_err(|_| invalid("createdAt"))?,
        updated_at: LixTimestamp::parse(&branch.updated_at).map_err(|_| invalid("updatedAt"))?,
        ref_change_id: ChangeId::parse(&branch.ref_change_id)
            .map_err(|_| invalid("refChangeId"))?,
        schema_presence_bloom: [u64::MAX; 4],
    })
}

/// Stage the opening receipt, selected/global controls and root-base markers
/// in one caller-owned write set. Commit every returned precondition atomically
/// with that set. They reject existing controls/markers/receipts; this is not a
/// replacement, migration, reopen or branch-reset API.
///
/// Head IDs select fresh *local* serving generations. The authority's mutable
/// generation/revision is never transferred. All-one schema bloom bits avoid
/// claiming absence for schemas whose objects have not been loaded.
/// On error the caller must discard the write set.
#[must_use = "opening writes must be committed with every returned precondition"]
pub(crate) fn stage_partial_bootstrap(
    read: &(impl StorageAdapterRead + ?Sized),
    writes: &mut StorageWriteSet,
    state: &PartialReplicaState,
) -> Result<Vec<StoragePrecondition>, LixError> {
    let descriptor = state.descriptor();
    descriptor.validate(
        state.repository_id(),
        Some(&descriptor.selected_branch.branch_id),
    )?;
    let mut controls = Vec::with_capacity(2);
    let mut preconditions = Vec::with_capacity(8);
    for branch in [&descriptor.selected_branch, &descriptor.global_branch] {
        if controls
            .iter()
            .any(|(id, _)| *id == branch.branch_id.as_str())
        {
            continue;
        }
        let control = partial_branch_control(state, branch)?;
        preconditions.push(branch_head_control_precondition(&branch.branch_id, None)?);
        preconditions.push(StoragePrecondition::KeyAbsent {
            space: ROOT_CURRENT_BASE_SPACE,
            key: StorageKey(Bytes::from(hot_generation_scope_prefix(
                &branch.branch_id,
                control.tracked_generation,
            ))),
        });
        controls.push((branch.branch_id.as_str(), control));
    }
    preconditions.push(stage_partial_replica_state(writes, state, None)?);
    // This fresh local token identifies the fixed baseline's catalog; it does
    // not certify catalog coverage. Compilation still completes its native
    // scans before caching. Local schema commits rotate it atomically. Any
    // future remote baseline adoption must also rotate it in the same write.
    crate::catalog::stage_catalog_revision(writes);
    // Account proofs are disposable and revision-bound. A new partial epoch
    // needs its own unique token just like full initialization; actual account
    // mutations and remote global publication rotate this token atomically.
    crate::account::stage_account_revision(writes);
    preconditions
        .push(super::partial_interest_journal::stage_initial_read_interest_journal(writes, state)?);
    preconditions
        .extend(super::partial_push_state::stage_initial_partial_push_states(writes, state)?);
    for (branch_id, control) in controls {
        stage_branch_head_control(writes, branch_id, control)?;
        TrackedHeadContext::new()
            .writer(read, writes)
            .stage_empty_root_deterministic_witness(branch_id, control.tracked_generation)?;
        TrackedHeadContext::new()
            .writer(read, writes)
            .stage_root_current_base(
                branch_id,
                control.tracked_generation,
                control.head_commit_id,
            );
    }
    Ok(preconditions)
}

#[cfg(test)]
mod tests {
    use super::super::partial_state::load_partial_replica_state;
    use super::*;
    use crate::branch::BranchHeadControlContext;
    use crate::storage_adapter::{
        PointReadPlan, StorageAdapter, StorageGetOptions, StorageProjectedValue,
        StorageWriteOptions,
    };
    use crate::{CreateBranchOptions, Memory, open_lix};

    async fn state(select_global: bool) -> PartialReplicaState {
        let authority = open_lix().await.unwrap();
        let selected = if select_global {
            crate::GLOBAL_BRANCH_ID.to_owned()
        } else {
            authority
                .create_branch(CreateBranchOptions {
                    id: None,
                    name: "partial-selected".into(),
                    from_commit_id: None,
                })
                .await
                .unwrap()
                .id
        };
        PartialReplicaState::new(
            format!("https://example.test/lix/{}", authority.lix_id()),
            crate::ANONYMOUS_ACCOUNT_ID.to_owned(),
            "00000000-0000-7000-8000-000000000199".to_owned(),
            authority
                .partial_replica_descriptor(Some(&selected))
                .await
                .unwrap(),
        )
        .unwrap()
    }

    #[tokio::test]
    async fn partial_engine_open_requires_exact_admission_without_native_rows() {
        let state = state(false).await;
        let adapter = StorageAdapter::new(Memory::new());
        let read = adapter.begin_read(Default::default()).await.unwrap();
        let mut writes = adapter.new_write_set();
        let preconditions = stage_partial_bootstrap(&read, &mut writes, &state).unwrap();
        crate::init::stage_partial_repository_protocol(&mut writes);
        drop(read);
        adapter
            .commit_write_set(
                writes,
                StorageWriteOptions {
                    preconditions,
                    ..Default::default()
                },
            )
            .await
            .unwrap();

        // No native row, commit header, graph record or payload is installed.
        // Construction therefore cannot fall back to SQL identity/account reads.
        let (engine, session) = crate::engine::Engine::new_partial_replica(
            adapter.clone(),
            crate::engine::EngineOptions::new(),
            &state,
        )
        .await
        .unwrap();
        assert_eq!(engine.lix_id(), state.repository_id());
        assert_eq!(session.active_account_id(), state.active_account_id());
        assert_eq!(
            session.active_branch_id().await.unwrap(),
            state.descriptor().selected_branch.branch_id
        );

        let ordinary = crate::engine::Engine::new_with_adapter(
            adapter.clone(),
            crate::engine::EngineOptions::new(),
        )
        .await;
        assert!(
            matches!(ordinary, Err(error) if error.code == "LIX_PARTIAL_REPLICA_REQUIRES_ON_DEMAND_SYNC")
        );

        for (remote, account, epoch) in [
            (
                "https://different.test/repo",
                state.active_account_id(),
                state.epoch_id(),
            ),
            (
                state.remote_id(),
                crate::SYSTEM_ACCOUNT_ID,
                state.epoch_id(),
            ),
            (
                state.remote_id(),
                state.active_account_id(),
                "00000000-0000-7000-8000-000000000299",
            ),
        ] {
            let different = PartialReplicaState::new(
                remote.into(),
                account.into(),
                epoch.into(),
                state.descriptor().clone(),
            )
            .unwrap();
            let opened = crate::engine::Engine::new_partial_replica(
                adapter.clone(),
                crate::engine::EngineOptions::new(),
                &different,
            )
            .await;
            assert!(
                matches!(opened, Err(error) if error.code == "LIX_PARTIAL_REPLICA_ADMISSION_MISMATCH")
            );
        }
        // Engine admission alone must not grant permission to publish writes.
        let mut unauthorized = adapter.new_write_set();
        crate::init::stage_repository_protocol(&mut unauthorized);
        assert!(
            adapter
                .commit_write_set(unauthorized, Default::default())
                .await
                .is_err()
        );
        let branch = &state.descriptor().selected_branch;
        let base = CommitId::parse(&branch.head.commit_id).unwrap();
        let key = StorageKey(Bytes::from(hot_generation_scope_prefix(
            &branch.branch_id,
            base,
        )));
        let mut corrupt = adapter.new_write_set();
        corrupt.put(
            ROOT_CURRENT_BASE_SPACE,
            key,
            crate::storage_adapter::StorageValue {
                bytes: Bytes::from(vec![7u8; 16]),
            },
        );
        adapter
            .commit_partial_replica_write_set(
                super::super::partial_replica_write_capability(),
                corrupt,
                Default::default(),
            )
            .await
            .unwrap();
        let opened = crate::engine::Engine::new_partial_replica(
            adapter,
            crate::engine::EngineOptions::new(),
            &state,
        )
        .await;
        assert!(
            matches!(opened, Err(error) if error.code == "LIX_PARTIAL_REPLICA_ADMISSION_MISMATCH"),
            "a valid but unrelated root UUID must not be admitted"
        );
    }

    #[tokio::test]
    async fn partial_bootstrap_stages_only_canonical_root_backed_coordinates() {
        for select_global in [false, true] {
            let state = state(select_global).await;
            let adapter = StorageAdapter::new(Memory::new());
            let read = adapter.begin_read(Default::default()).await.unwrap();
            let mut writes = adapter.new_write_set();
            let preconditions = stage_partial_bootstrap(&read, &mut writes, &state).unwrap();
            assert_eq!(preconditions.len(), if select_global { 5 } else { 8 });
            drop(read);
            adapter
                .commit_write_set(
                    writes,
                    StorageWriteOptions {
                        preconditions,
                        await_durable: true,
                        ..Default::default()
                    },
                )
                .await
                .unwrap();
            let read = adapter.begin_read(Default::default()).await.unwrap();
            assert_eq!(
                load_partial_replica_state(&read).await.unwrap().unwrap().0,
                state
            );
            let account_revision = crate::account::load_account_revision(&read)
                .await
                .unwrap()
                .expect("partial bootstrap must seed the account proof token");
            assert_eq!(account_revision.len(), 16);
            for branch in [
                &state.descriptor().selected_branch,
                &state.descriptor().global_branch,
            ] {
                let control = BranchHeadControlContext::new()
                    .reader(&read)
                    .load(&branch.branch_id)
                    .await
                    .unwrap()
                    .unwrap();
                assert_eq!(control.head_commit_id.to_string(), branch.head.commit_id);
                assert_eq!(control.tracked_generation, control.head_commit_id);
                assert_eq!(control.current_state_revision, 0);
                assert_eq!(
                    control
                        .working_diff_checkpoint_commit_id
                        .unwrap()
                        .to_string(),
                    branch.checkpoint.commit_id
                );
                assert_eq!(control.created_at.to_string(), branch.created_at);
                assert_eq!(control.updated_at.to_string(), branch.updated_at);
                assert_eq!(control.ref_change_id.to_string(), branch.ref_change_id);
                assert!(
                    control
                        .schema_presence_bloom
                        .iter()
                        .all(|bits| *bits == u64::MAX)
                );
                let marker_key = StorageKey(Bytes::from(hot_generation_scope_prefix(
                    &branch.branch_id,
                    control.tracked_generation,
                )));
                let marker = PointReadPlan::new(ROOT_CURRENT_BASE_SPACE, &[marker_key])
                    .materialize(&read, StorageGetOptions::default())
                    .await
                    .unwrap()
                    .value
                    .pop()
                    .flatten()
                    .unwrap();
                assert!(
                    matches!(marker, StorageProjectedValue::FullValue(bytes) if bytes.as_ref() == control.head_commit_id.as_uuid().as_bytes())
                );
            }
            let full_receipt = PointReadPlan::new(
                super::super::SYNC_REPLICA_STATE_SPACE,
                &[super::super::replica_state_key()],
            )
            .materialize(&read, StorageGetOptions::default())
            .await
            .unwrap();
            assert!(
                full_receipt.value.into_iter().all(|value| value.is_none()),
                "opening must not create a full-sync receipt"
            );
            let controls = BranchHeadControlContext::new().reader(&read);
            assert_eq!(
                controls.scan().await.unwrap_err().code,
                "LIX_SYNC_BRANCH_INVENTORY_REQUIRED"
            );
            let unknown_branch = controls
                .load("00000000-0000-7000-8000-000000000299")
                .await
                .expect_err("unloaded native descriptor cannot prove branch absence");
            assert_eq!(unknown_branch.code, "LIX_COMMIT_NOT_FOUND");
            assert_eq!(
                crate::tracked_state::NativeMetadataRef::from_missing_error(&unknown_branch)
                    .unwrap(),
                Some(crate::tracked_state::NativeMetadataRef::CommitGraphRecord(
                    state.descriptor().global_branch.head.commit_id.clone(),
                )),
                "descriptor-only opening must demand the exact global native graph record, not invent an absent branch"
            );
            assert!(
                super::super::repository::has_any_sync_replica_state(&read)
                    .await
                    .unwrap()
            );
            assert_eq!(
                super::super::repository::inspect_sync_replica_binding(&read)
                    .await
                    .unwrap_err()
                    .code,
                "LIX_PARTIAL_REPLICA_REQUIRES_ON_DEMAND_SYNC"
            );
            let mut duplicate = adapter.new_write_set();
            let preconditions = stage_partial_bootstrap(&read, &mut duplicate, &state).unwrap();
            drop(read);
            assert_eq!(
                super::super::repository::admit_sync_authority_storage(&adapter, None)
                    .await
                    .unwrap_err()
                    .code,
                super::super::SYNC_PROTOCOL_MISMATCH_CODE
            );
            assert!(
                adapter
                    .commit_write_set(
                        duplicate,
                        StorageWriteOptions {
                            preconditions,
                            ..Default::default()
                        }
                    )
                    .await
                    .is_err(),
                "installer must not reset an existing partial replica"
            );
        }
    }
}