Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 4.3 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Deterministic simulation of many agents landing changes on one branch,
5//! checked against a reference model after every step.
6//!
7//! # What runs
8//!
9//! The system under test is the engine's [`commit`] and [`land`] over an
10//! object store, and a lander: a serialized section that owns the head of
11//! `main`, reads it, calls [`land`], and moves the branch with a
12//! compare-and-swap, as the command line will do inside
13//! [`Store::transact`]. The store is either in memory with a simulated ref
14//! ([`MemoryBackend`]) or the real local store in a directory
15//! ([`StoreBackend`]), where the lander's section is a real transaction and
16//! every agent has its own handle.
17//!
18//! Agents, eight by default, each loop: take a snapshot of `main`, make a
19//! small random change, commit it on the snapshot, and ask the lander to land
20//! it. Changes add, edit and delete files, often the same files other agents
21//! edit; create and remove directories; put files where directories are and
22//! the other way round; add names that differ from others only in case or
23//! Unicode normalisation; and, in some runs, move a directory of about a
24//! thousand files across the largest flat directory, 1,024 entries. On a
25//! conflict an agent takes a new snapshot and either redoes its edits there
26//! or puts its own files over the head's.
27//!
28//! The scheduler interleaves agents and the lander in an order the seed
29//! decides, with pauses long enough for bases to go stale, and injects
30//! faults: answers lost on the way back (the agent times out and sends the
31//! same changeset again), requests delivered twice, agents that send a
32//! changeset again after it landed, and lander crashes after the outcome is
33//! computed and before the compare-and-swap.
34//!
35//! # The model
36//!
37//! A state is a map from path to file. The model predicts every landing on
38//! its own: per path, the change's file where the head left the base's, the
39//! head's where it already has the change's, and otherwise a line merge or a
40//! conflict; then the structural rules of `docs/design.md`. Its line merge
41//! is written independently and must agree with the engine's `merge3`, hunks
42//! included. The engine's answer must equal the model's: the outcome, the
43//! merged paths, the rebased change list, the conflicts with their entries
44//! and hunks, and the tree.
45//!
46//! # Invariants
47//!
48//! Checked after every step, and once more at the end after the store is
49//! opened again:
50//!
51//! - I1: the head only moves forward; every new head has the old head as its
52//! first parent.
53//! - I2: `main`'s first-parent history, and its op log, are the sequence of
54//! successful landings.
55//! - I3: no lost update: the head's tree is the model's state, file by file.
56//! - I4: each submitted changeset lands at most once, by id or by
57//! `rebased-from`, whatever the retries and duplicates.
58//! - I5: a reported conflict leaves the head unchanged.
59//! - I6: every changeset on `main`, and every changeset an agent submits,
60//! passes `admit`.
61//! - I7: two changes on unrelated paths, built on the same base, land the
62//! same in either order: replayed away from the branch in both orders.
63//!
64//! A failure names the seed, the check and the step, with the events about
65//! the changesets involved; the same seed replays the same run.
66//!
67//! [`commit`]: braid_engine::commit
68//! [`land`]: braid_engine::land
69//! [`Store::transact`]: braid_store::Store::transact
70
71#![forbid(unsafe_code)]
72
73mod backend;
74mod model;
75mod ops;
76mod rng;
77mod sim;
78mod trace;
79
80use std::path::Path;
81
82pub use backend::{Backend, LandRequest, Landed, MAIN, MemoryBackend, Move, StoreBackend};
83pub use sim::{Config, Failure, Outcome, Report, run};
84
85/// Runs one seed with objects in memory and a simulated ref.
86pub fn run_memory(seed: u64, config: &Config) -> Result<Outcome, Failure> {
87 run(seed, config, MemoryBackend::new())
88}
89
90/// Runs one seed on a real store, created in `dir`.
91pub fn run_store(seed: u64, config: &Config, dir: &Path) -> Result<Outcome, Failure> {
92 let backend = StoreBackend::create(dir, config.agents).map_err(|error| Failure {
93 seed,
94 backend: "store",
95 step: 0,
96 invariant: "engine",
97 message: format!("cannot create the store: {error}"),
98 trace: Vec::new(),
99 full_trace: Vec::new(),
100 })?;
101 run(seed, config, backend)
102}