An experiment
NAIMA checks itself
NAIMA lets many AI agents work on one repository at the same time. Two small models of how it coordinates them were written in mCRL2 and checked with its model checker. This page shows them, with the properties checked and how to run the checks yourself.
This is only an experiment. The models and the formulas below are hard to read without knowing mCRL2, and we are working on making them understandable. For now they are shown as they are, with their comments.
What the models are about
Git records the history of a set of files as commits. Each commit is a snapshot of all the files with a reference to its parents. A branch is a name for a line of work. The trunk is the main line, to which work returns. A merge commit has two parents. Following only first parents back from the tip of the trunk gives its first-parent line. A worktree is a separate folder checked out on one branch.
In NAIMA, every agent works in a worktree of its own, on a branch of its own. It records the items it works on in a claim file on that branch. Its work lands on the trunk in two steps: first the trunk is merged into the branch, where conflicts are resolved and the tests are run again; then the coordinator, the only agent that writes the trunk, adds to it a marker, i.e., a merge commit with exactly the files of the branch, whose message names the items closed by the landing. The first-parent line of the trunk is thus the list of the landings.
Claims are read from the worktree of a branch while the branch exists. Afterwards they are read from the copy of its claim file on the trunk. Every commit carries a copy of every claim file, possibly stale. A stale copy could reach the trunk and hide a claim, or bring back one that was released. The protocol relies on two rules to prevent this:
- each agent writes only its own claim file;
- a branch lands only after taking in the trunk. When it lands, the copies it carries of the other claim files are therefore current.
What was checked
- No claim is ever lostFor every branch b and item i, once b claims i, and as long as b does not release i, every read of the claims of b shows i.
holdsfails with either rule turned off
- A released claim never reappearsOnce b releases i, and until b claims i again, no read of the claims of b shows i.
holds
- Every landing stays detectableOnce a landing l is on the trunk, however it was made and however many landings follow, no later read misses it.
holds with markersfails with fast-forward
With mCRL2 202607.0, the claims model has 73,984 states and 668,032 transitions. Both claims properties hold. Turning off either rule makes the first property fail, and mCRL2 returns a counterexample in which a branch lands a stale copy of the claim file of another branch.
The landings property holds with markers (85 states, 211 transitions). It fails when markers are replaced by plain fast-forward landings: a fast-forward moves the trunk to the tip of the branch without creating a commit, so the landing leaves no commit of its own on the trunk. A landing made by hand is then absorbed by the next one and disappears from the record.
A few notions of mCRL2
Data are described by sorts, i.e., types, built with struct, and by functions declared with map and defined by equations (eqn). FSet(D) is the sort of finite sets over D and List(D) that of lists, where x |> r is the list with head x and tail r, and ++ is concatenation.
Behaviour is described by processes (proc), which perform actions (act). A process is a choice (+) between summands. A summand c -> a . P may happen only when the condition c holds. It performs action a and continues as P. The operator sum ranges over all values of a sort. A process with parameters is called again with only the parameters that change, e.g., Repo(disk = …). The initial state is given by init.
The properties are formulas of the modal μ-calculus of mCRL2 with regular expressions over actions. A formula [R] false says that no run of the model contains a sequence of actions matching the regular expression R, i.e., that what R describes never happens. In R, true matches any action, * is repetition, . is sequence and ! is negation. A condition on the data of an action is written with val. The quantifier forall in front makes the formula hold for all values of its sort.
Model 1 · claims.mcrl2
Claims
The model abstracts the claim protocol to two branches, two items and the trunk. Only claim files are modelled: the other files of a branch play no role in the protocol. There are several copies of the claim file of a branch: one on the disk of its worktree, one in the last commit of every branch, and one on the trunk.
% The claim protocol of Naima. Two branches, b1 and b2, each in a worktree of % its own, and a trunk. Each branch owns one claim file. Every tree (the % working copy of a branch, the commit of a branch, the trunk) holds a copy of % both claim files.
Data
A tree is modelled by the sort Files, which holds the claim file of each branch, i.e., the set of items the branch claims. The sort Refs holds the last commit of each branch. The functions get and put read and write the claim file of a branch in a tree, and ref and setref do the same for the commit of a branch.
% Part 1. Data. sort Br = struct b1 | b2; % the branches It = struct i1 | i2; % the items that can be claimed Files = struct files(f1: FSet(It), f2: FSet(It)); % a tree: the claim file of b1, % the claim file of b2 Refs = struct refs(r1: Files, r2: Files); % the commit of b1, of b2 map get: Files # Br -> FSet(It); % the claim file of a branch put: Files # Br # FSet(It) -> Files; % in a tree, read and written ref: Refs # Br -> Files; % the commit of a branch, setref: Refs # Br # Files -> Refs; % read and written none: Files; % a tree with no claims var x, y, s: FSet(It); p, q, f: Files; eqn get(files(x, y), b1) = x; get(files(x, y), b2) = y; put(files(x, y), b1, s) = files(s, y); put(files(x, y), b2, s) = files(x, s); ref(refs(p, q), b1) = p; ref(refs(p, q), b2) = q; setref(refs(p, q), b1, f) = refs(f, q); setref(refs(p, q), b2, f) = refs(p, f); none = files({}, {});
Rules
The two rules of the protocol are Boolean constants. The first, ff_only, says that a branch lands only after taking in the trunk. The second, own_files_only, says that a branch writes only its own claim file. Both are true in the protocol. Setting one of them to false removes that rule from the model.
% Part 2. The rules of the protocol, as switches. Both are true in the % protocol. Setting one to false is the negative experiment. map ff_only: Bool; % a branch lands only after taking in the trunk own_files_only: Bool; % a branch writes only its own claim file eqn ff_only = true; own_files_only = true;
Landing strategy
A third constant, semi_linear, says how a branch lands. In NAIMA it is true: each landing is a marker. The marker is a new commit, so after a landing no branch contains the whole trunk, and every branch must take in the trunk before landing again. When it is false, a landing is a fast-forward, and the branch that just landed still contains the trunk.
% Part 3. The landing strategy. True: each landing is a marker, so after it % the branch no longer contains the trunk and must take it in again before % its next landing. False: a plain fast-forward, after which the commit of % the branch is the trunk. map semi_linear: Bool; eqn semi_linear = true;
Actions
The actions are the steps of the protocol: claiming and releasing an item, committing, landing (merge), taking in the trunk (sync) and retiring a branch, i.e., removing its worktree and deleting it. The action write_other is the only step that breaks a rule: a branch overwrites the claim file of another. The action read is an observation: it says which items a branch is read to claim, and it is the action on which the properties are stated.
% Part 4. Actions. act claim, release: Br # It; % the branch claims or releases an item, % on its working copy commit: Br; % the branch commits its claim file merge: Br; % the branch lands: the trunk takes its commit sync: Br; % the branch takes in the trunk write_other: Br # Br; % the branch overwrites the claim file of % another branch (only without own_files_only) retire: Br; % the worktree is removed, the branch deleted read: Br # FSet(It); % the claims the branch is read to hold
The repository
The process Repo holds the whole state: the working copies, the commits, the trunk, the set fresh of branches whose commit contains the trunk, and the set alive of branches not yet retired. Each summand is one step. A branch may commit only when its working copy differs from its commit. It may land only when its commit differs from the trunk and, under ff_only, when it is fresh. Landing replaces the tree of the trunk with the commit of the branch, which is what a marker does, since a marker has exactly the files of the branch. Taking in the trunk copies every claim file from the trunk into the commit of the branch, except its own. A branch may be retired only when its claims are the same on its disk, in its commit and on the trunk. Finally, read follows the protocol: the claims of a branch are read from its worktree while the branch exists, and from the copy of its claim file on the trunk afterwards. Initially both branches are alive and fresh, and nothing is claimed.
% Part 5. The repository. % disk: the claim files on the working copy of each branch % branch: the commit of each branch % trunk: the tree of the trunk % fresh: the branches whose commit contains the trunk % alive: the branches not yet retired proc Repo(disk: Files, branch: Refs, trunk: Files, fresh: FSet(Br), alive: FSet(Br)) = % An item is claimed or released on the working copy. sum b: Br, i: It. (b in alive && !(i in get(disk, b))) -> claim(b, i) . Repo(disk = put(disk, b, get(disk, b) + {i})) + sum b: Br, i: It. (b in alive && i in get(disk, b)) -> release(b, i) . Repo(disk = put(disk, b, get(disk, b) - {i})) % The branch commits its own claim file. + sum b: Br. (b in alive && get(ref(branch, b), b) != get(disk, b)) -> commit(b) . Repo(branch = setref(branch, b, put(ref(branch, b), b, get(disk, b)))) % The branch lands. Under ff_only, only if it contains the trunk. + sum b: Br. (b in alive && ref(branch, b) != trunk && (b in fresh || !ff_only)) -> merge(b) . Repo(trunk = ref(branch, b), fresh = if(semi_linear, {}, {b})) % The branch takes in the trunk: every claim file comes from the % trunk, except its own. + sum b: Br. (b in alive && !(b in fresh)) -> sync(b) . Repo(branch = setref(branch, b, put(trunk, b, get(ref(branch, b), b))), fresh = fresh + {b}) % Without own_files_only, a branch may empty the claim file of another. + sum b, o: Br. (!own_files_only && b in alive && o != b && get(ref(branch, b), o) != {}) -> write_other(b, o) . Repo(branch = setref(branch, b, put(ref(branch, b), o, {}))) % A branch whose claims are all on the trunk may be retired. + sum b: Br. (b in alive && get(disk, b) == get(trunk, b) && get(ref(branch, b), b) == get(trunk, b)) -> retire(b) . Repo(alive = alive - {b}) % Claims are read from the working copy while the branch exists, % and afterwards from the copy of its claim file on the trunk. + sum b: Br. read(b, if(b in alive, get(disk, b), get(trunk, b))) . Repo(); init Repo(none, refs(none, none), none, {b1, b2}, {b1, b2});
Properties
The first property says that no claim is ever lost. The forbidden sequence is: any prefix, then branch b claims item i, then any number of actions other than b releasing i, then a read of the claims of b that does not contain i. Between the claim and the read, the branch may commit, take in the trunk, land and be retired, and other branches may do the same. The claim must survive all of it.
% No claim is ever lost: once branch b claims item i, every read of the % claims of b shows i until b releases it. forall b: Br, i: It. [true* . claim(b, i) . (!release(b, i))* . (exists s: FSet(It). read(b, s) && val(!(i in s)))] false
The second property is the mirror image of the first: once b releases i, no read of the claims of b contains i until b claims it again. It rules out a stale copy of a claim file bringing back a released claim.
% A released claim never reappears: once branch b releases item i, no read % of the claims of b shows i until b claims it again. forall b: Br, i: It. [true* . release(b, i) . (!claim(b, i))* . (exists s: FSet(It). read(b, s) && val(i in s))] false
Both properties hold. When ff_only is false, a branch may land without taking in the trunk, and the copy it carries of the claim file of another branch may be stale. For instance, branch b1 claims an item, commits, lands and is retired; branch b2, which still holds the empty copy of the claim file of b1, then lands, and the next read of the claims of b1 misses the item. When own_files_only is false, the same happens with a copy that b2 emptied itself. In both cases mCRL2 reports that the first property fails and returns a counterexample of this kind.
Model 2 · landings.mcrl2
Landings
The model abstracts the trunk to its first-parent line, which is the record of the landings. It checks that NAIMA can always tell, from the record alone, which landings it holds, also when some of them are made by hand. The model is a single process: the trunk, written by one landing at a time, and an observer that reads it.
% The landings on the trunk of Naima. The first-parent line of the trunk is % the record of the landings. An observer reads it, as Naima does, to tell % which landings it holds. Up to three landings, l1, l2, l3, happen one after % another, since the trunk has a single writer. Each is made by Naima or by % hand. A landing names the item it closes. The model identifies the two, % since an item is closed by one landing.
Data
A landing is made in one of three ways: by NAIMA, by a plain fast-forward, or by a plain merge, both by hand. A commit on the first-parent line is modelled by the sort C. It records whether the commit is a merge and its trailer, i.e., the line of its message that names the item closed by the landing. These are the only things the observer can see. The commit also records, as a ghost field that the observer never reads, the landing that actually put it there. The ghost is what allows the property to compare what the observer reads with what really happened.
% Part 1. Data. A commit on the first-parent line carries what the observer % sees (whether it is a merge, and its trailer, i.e., the line of its message % naming the item it closes) and, as a ghost the observer never reads, the % landing that put it there. sort L = struct l1 | l2 | l3; % the landings, in order How = struct by_command | by_hand_ff | by_hand_merge; % made by Naima, by a plain % fast-forward, by a plain merge Tr = struct none | closes(item: L); % the trailer of a commit C = struct c(is_merge: Bool, trailer: Tr, owner: L); % a commit; owner is the ghost
Landing strategy
As in the claims model, semi_linear is true when landings by NAIMA are markers. When it is false, a landing by NAIMA is a fast-forward, and the trailer is written on the last commit of the branch. This is the negative experiment.
% Part 2. The landing strategy. True: each landing by Naima is a marker. % False, the negative experiment: a fast-forward, with the trailer on the % tip of the branch. map semi_linear: Bool; eqn semi_linear = true;
Landings
The function landed gives the commits that a landing adds to the first-parent line. Its last argument says whether the branch has one commit or two, which is enough to show a landing spread over several commits. A marker adds a single merge commit, whatever the number of commits of the branch, since those stay on the side of its second parent. A fast-forward adds all commits of the branch. A plain merge by hand adds a merge commit with the default message of git, which has no trailer.
% Part 3. What a landing adds to the first-parent line. The flag two says % whether the branch has one commit or two. map id: Nat -> L; landed: How # L # Bool -> List(C); var l: L; two: Bool; eqn id(0) = l1; id(1) = l2; id(2) = l3; % By Naima. With markers, one merge commit naming the item: the commits % of the branch stay on its second-parent side, off the first-parent % line. With fast-forward, the commits of the branch, the tip carrying % the trailer. landed(by_command, l, two) = if(semi_linear, [c(true, closes(l), l)], if(two, [c(false, none, l)], []) ++ [c(false, closes(l), l)]); % By hand. A plain fast-forward puts the commits of the branch on the % line, with no trailer. A plain merge makes a merge commit with the % message of git, with no trailer. landed(by_hand_ff, l, two) = if(two, [c(false, none, l), c(false, none, l)], [c(false, none, l)]); landed(by_hand_merge, l, two) = [c(true, none, l)];
The observer
The function owners gives the landings in the record, read from the ghosts. The functions lost_ff and lost_sl give the landings that the observer misreads under each strategy, i.e., those it neither identifies by a trailer nor flags as landed by hand. With fast-forward, the observer has no way to tell where a landing begins: it reads every commit since the previous trailer as part of the landing named by the next one. With markers, every commit on the first-parent line is a landing of its own, so the observer either identifies it by its trailer or flags it. The function accounted gives the landings that the observer reads correctly.
% Part 4. The observer. owners gives the landings the record holds (from the % ghosts). lost_ff and lost_sl give the landings the observer misreads, for % each strategy. accounted gives the landings it identifies or flags. map owners: List(C) -> FSet(L); lost_ff: List(C) # FSet(L) -> FSet(L); lost_sl: List(C) -> FSet(L); accounted: List(C) -> FSet(L); var x: C; r: List(C); p: FSet(L); eqn owners([]) = {}; owners(x |> r) = {owner(x)} + owners(r); % Fast-forward, oldest commit first. A commit with a trailer ends a % landing, and every commit since the previous trailer is read as part % of that landing. The commits after the last trailer are flagged as % landed without one. p holds the landings of the commits read so far % since the last trailer. lost_ff([], p) = {}; (trailer(x) == none) -> lost_ff(x |> r, p) = lost_ff(r, p + {owner(x)}); (trailer(x) != none) -> lost_ff(x |> r, p) = ((p + {owner(x)}) - {item(trailer(x))}) + lost_ff(r, {}); % Markers. Every commit on the first-parent line is a landing of its % own. A merge with a trailer is identified by it. A merge without one, % or a commit that is not a merge, is flagged as landed by hand. lost_sl([]) = {}; lost_sl(x |> r) = if(is_merge(x) && trailer(x) != none && item(trailer(x)) != owner(x), {owner(x)}, {}) + lost_sl(r); accounted(r) = owners(r) - if(semi_linear, lost_sl(r), lost_ff(r, {}));
The process
The process Main holds the record and the number of landings so far. At each step, either the next landing happens, in any of the three ways and with one or two commits, or the observer reads the record.
% Part 5. Actions and process. rec is the first-parent line, n the number % of landings so far. act land: L # How; % a landing on the trunk read: FSet(L); % the observer reads the record proc Main(rec: List(C), n: Nat) = sum h: How, two: Bool. (n < 3) -> land(id(n), h) . Main(rec ++ landed(h, id(n), two), n + 1) + read(accounted(rec)) . Main(); init Main([], 0);
Property
The property says that every landing stays detectable. The forbidden sequence is: any prefix, then landing l in any of the three ways, then any actions, then a read that does not account for l. Since any number of landings may happen between the landing and the read, the property covers a landing followed by others.
% Every landing stays detectable: once landing l is on the trunk, every later % read of the record identifies it by its trailer or flags it as landed by % hand, however many landings follow. forall l: L. [true* . (exists h: How. land(l, h)) . true* . (exists s: FSet(L). read(s) && val(!(l in s)))] false
The property holds with markers. It fails with fast-forward: if a landing by hand is followed by a landing by NAIMA, the observer reads the commits of both as the commits of the second, and the first disappears from the record.
Run the checks yourself
The two models and the three properties, as plain text files:
- claims.mcrl2the claims model
- landings.mcrl2the landings model
- no-claim-lost.mcfno claim is ever lost
- released-claim-never-reappears.mcfa released claim never reappears
- every-landing-detectable.mcfevery landing stays detectable
You need the mCRL2 toolset; the results above are from version 202607.0. Put the files in one folder and run, there, three tools for each property: mcrl22lps turns the model into a linear process, lps2pbes -f combines it with the formula into an equation system, and pbes2bool solves it and prints true when the property holds, false when it does not.
$ mcrl22lps claims.mcrl2 claims.lps $ lps2pbes -f no-claim-lost.mcf claims.lps no-claim-lost.pbes $ pbes2bool no-claim-lost.pbes true $ lps2pbes -f released-claim-never-reappears.mcf claims.lps released.pbes $ pbes2bool released.pbes true $ mcrl22lps landings.mcrl2 landings.lps $ lps2pbes -f every-landing-detectable.mcf landings.lps landings.pbes $ pbes2bool landings.pbes true
Each claims property takes about a minute; the landings property takes a moment. For the negative experiments, change one line of a model and run its checks again: in claims.mcrl2, set ff_only or own_files_only to false, and pbes2bool prints false for no-claim-lost.mcf; in landings.mcrl2, set semi_linear to false, and it prints false for every-landing-detectable.mcf.