public snapshot · areas/examples/raft-nib/ · system@main · commit da2bf624f03d · shared 2026-11-23T17:23:12Z· frozen: this page does not change when the repository does
all files
  1<!doctype html>
  2<html lang="en">
  3<head>
  4<meta charset="utf-8">
  5<meta name="viewport" content="width=device-width, initial-scale=1">
  6<title>Raft, checked from its own journal</title>
  7<meta name="description" content="Raft written in nib, run on a simulated network that loses, reorders and crashes, and checked entry by entry against a TLA+ safety monitor.">
  8<link rel="stylesheet" href="style.css">
  9</head>
 10<body>
 11<header class="top">
 12  <p class="crumb"><a href="https://nib.app.jahfer.link">nib</a> / examples / raft</p>
 13  <h1>Raft, checked from its own journal</h1>
 14  <p class="lede">Three Raft nodes run in one nib process on a simulated network
 15  that drops, delays and duplicates messages and crashes machines. Every effect
 16  the program performs is written to a journal, and a TLA+ monitor reads that
 17  journal entry by entry, refusing the run at the first step that breaks one of
 18  Raft's safety rules.</p>
 19</header>
 20
 21<figure class="run hero">
 22<div class="scroll"><svg class="timeline" viewBox="0 0 900 236" role="img" aria-labelledby="t-Main">
 23<title id="t-Main">A run of the correct program under three crashes</title>
 24<defs><pattern id="hatch" width="6" height="6" patternUnits="userSpaceOnUse" patternTransform="rotate(45)"><line x1="0" y1="0" x2="0" y2="6" class="hatch"/></pattern></defs>
 25<line class="grid" x1="92.0" x2="92.0" y1="22" y2="214"/>
 26<text class="tick" x="92.0" y="230" text-anchor="middle">0 ms</text>
 27<line class="grid" x1="221.0" x2="221.0" y1="22" y2="214"/>
 28<text class="tick" x="221.0" y="230" text-anchor="middle">500 ms</text>
 29<line class="grid" x1="350.0" x2="350.0" y1="22" y2="214"/>
 30<text class="tick" x="350.0" y="230" text-anchor="middle">1000 ms</text>
 31<line class="grid" x1="479.0" x2="479.0" y1="22" y2="214"/>
 32<text class="tick" x="479.0" y="230" text-anchor="middle">1500 ms</text>
 33<line class="grid" x1="608.0" x2="608.0" y1="22" y2="214"/>
 34<text class="tick" x="608.0" y="230" text-anchor="middle">2000 ms</text>
 35<line class="grid" x1="737.0" x2="737.0" y1="22" y2="214"/>
 36<text class="tick" x="737.0" y="230" text-anchor="middle">2500 ms</text>
 37<line class="grid" x1="866.0" x2="866.0" y1="22" y2="214"/>
 38<text class="tick" x="866.0" y="230" text-anchor="middle">3000 ms</text>
 39<text class="host" x="0" y="51.0">10.0.0.1</text>
 40<line class="lane n1" x1="92" x2="866" y1="47.0" y2="47.0"/>
 41<rect class="candidate n1" x="146.2" y="39" width="10.6" height="16" rx="2"/>
 42<rect class="leader n1" x="156.8" y="34" width="115.8" height="26" rx="2"/>
 43<text class="role" x="164.8" y="51.0">leads term 1</text>
 44<rect class="down" x="272.6" y="30" width="129.0" height="34" fill="url(#hatch)"/>
 45<text class="downlabel" x="337.1" y="25" text-anchor="middle">crashed</text>
 46<rect class="candidate n1" x="528.0" y="39" width="13.7" height="16" rx="2"/>
 47<rect class="leader n1" x="541.7" y="34" width="324.3" height="26" rx="2"/>
 48<text class="role" x="549.7" y="51.0">leads term 3</text>
 49<line class="apply n1" x1="179.7" x2="179.7" y1="66" y2="73"/>
 50<line class="apply n1" x1="197.8" x2="197.8" y1="66" y2="73"/>
 51<line class="apply n1" x1="204.0" x2="204.0" y1="66" y2="73"/>
 52<line class="apply n1" x1="220.5" x2="220.5" y1="66" y2="73"/>
 53<line class="apply n1" x1="233.9" x2="233.9" y1="66" y2="73"/>
 54<line class="apply n1" x1="253.0" x2="253.0" y1="66" y2="73"/>
 55<text class="host" x="0" y="111.0">10.0.0.2</text>
 56<line class="lane n2" x1="92" x2="866" y1="107.0" y2="107.0"/>
 57<rect class="candidate n2" x="148.8" y="99" width="16.0" height="16" rx="2"/>
 58<rect class="candidate n2" x="321.6" y="99" width="4.1" height="16" rx="2"/>
 59<rect class="leader n2" x="325.7" y="94" width="153.3" height="26" rx="2"/>
 60<text class="role" x="333.7" y="111.0">leads term 2</text>
 61<rect class="down" x="479.0" y="90" width="103.2" height="34" fill="url(#hatch)"/>
 62<text class="downlabel" x="530.6" y="85" text-anchor="middle">crashed</text>
 63<line class="apply n2" x1="189.3" x2="189.3" y1="126" y2="133"/>
 64<line class="apply n2" x1="218.7" x2="218.7" y1="126" y2="133"/>
 65<line class="apply n2" x1="218.7" x2="218.7" y1="126" y2="133"/>
 66<line class="apply n2" x1="229.3" x2="229.3" y1="126" y2="133"/>
 67<line class="apply n2" x1="253.2" x2="253.2" y1="126" y2="133"/>
 68<text class="host" x="0" y="171.0">10.0.0.3</text>
 69<line class="lane n3" x1="92" x2="866" y1="167.0" y2="167.0"/>
 70<rect class="down" x="685.4" y="150" width="77.4" height="34" fill="url(#hatch)"/>
 71<text class="downlabel" x="724.1" y="145" text-anchor="middle">crashed</text>
 72<line class="apply n3" x1="191.8" x2="191.8" y1="186" y2="193"/>
 73<line class="apply n3" x1="219.5" x2="219.5" y1="186" y2="193"/>
 74<line class="apply n3" x1="219.5" x2="219.5" y1="186" y2="193"/>
 75<line class="apply n3" x1="229.8" x2="229.8" y1="186" y2="193"/>
 76<line class="apply n3" x1="246.0" x2="246.0" y1="186" y2="193"/>
 77<line class="apply n3" x1="267.2" x2="267.2" y1="186" y2="193"/>
 78</svg></div>
 79<figcaption>
 80<p>The correct program, seed 3 of <code>worlds/crash.json</code>:
 81each node crashes once, for 300 to 500 ms. 10.0.0.1 leads term 1 from 251 ms, 10.0.0.2 leads term 2 from 906 ms, 10.0.0.1 leads term 3 from 1743 ms. Short ticks under a
 82lane are entries that node applied. The run is 7,460 journal
 83entries, and the monitor <strong class="ok">admits</strong> all of them.</p>
 84<p class="key"><span class="sw leader"></span> leads a term
 85<span class="sw candidate"></span> stands for election
 86<span class="sw down"></span> crashed</p>
 87<p>Download <a href="runs/main-crash-3.jsonl.gz">the journal</a> and
 88<a href="runs/main-crash-3.world.json">the world file that makes it</a>.</p>
 89</figcaption>
 90</figure>
 91
 92<main>
 93<section id="pipeline">
 94<h2>How a run becomes a verdict</h2>
 95<p>Nothing here inspects the program's memory or instruments its code. The
 96program runs as written, and the journal of what it did is the only thing
 97checked. Five steps, each a section below:</p>
 98<ol class="pipeline">
 99  <li><a href="#program"><b>The program</b></a> names every effect it can perform in its types.</li>
100  <li><a href="#world"><b>The world</b></a> answers those effects, and makes every network and timing decision.</li>
101  <li><a href="#journal"><b>The journal</b></a> records each answer and each decision, in order.</li>
102  <li><a href="#monitor"><b>The monitor</b></a> replays the journal against five safety properties.</li>
103  <li><a href="#verdict"><b>The verdict</b></a>: admitted, or refused at a named entry.</li>
104</ol>
105</section>
106
107<section id="program">
108<h2><span class="n">1</span> The program</h2>
109<p><a href="raft.nib.txt"><code>raft.nib</code></a> is leader election and
110log replication for three nodes, in 496 lines. Each node listens on its own
111address, dials its peers, and feeds every message and a timer's ticks into one
112mailbox; its main loop takes one event at a time. A function's type says which
113effects it may perform, so the step that handles one event says it touches the
114store, the network, randomness, the state machine and the voting rules, and
115nothing else:</p>
116<figure class="code"><pre class="nib"><code>  <span class='keyword'>func</span> <span class='function'>handle</span> <span class='punctuation delimiter'>::</span> <span class='variable parameter'>n</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='constructor'>Node</span></span><span class='punctuation'>,</span> <span class='variable parameter'>ev</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='constructor'>Ev</span></span><span class='punctuation'>,</span> <span class='variable parameter'>now</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>Int</span></span> <span class='operator'>-&gt;</span> <span class='type'><span class='constructor'>Node</span></span> <span class='operator'>!</span> <span class='type'><span class='constructor'>Store</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Net</span><span class='punctuation delimiter'>.</span><span class='constructor'>Socket</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Random</span><span class='punctuation delimiter'>.</span><span class='constructor'>Source</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Machine</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Rules</span></span> <span class='operator'>=</span><span class='variable'></span></code></pre><figcaption><code>raft.nib</code>: the event handler's signature. Everything after <code>!</code> is an effect.</figcaption></figure>
117<p>The effects are interfaces. What survives a crash (the current term, the
118vote, the log) goes through <code>Store</code>, and every write is a recorded
119operation made before the message it justifies is sent:</p>
120<figure class="code"><pre class="nib"><code><span class='keyword'>interface</span> <span class='constructor'>Store</span> <span class='keyword'>where</span>
121  <span class='function'>recover</span><span class='punctuation bracket'>(</span><span class='variable parameter'>node</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>String</span></span><span class='punctuation bracket'>)</span> <span class='operator'>-&gt;</span> <span class='type'><span class='constructor'>Saved</span></span><span class='punctuation'>;</span>
122  <span class='function'>save</span><span class='punctuation bracket'>(</span><span class='variable parameter'>node</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>String</span></span><span class='punctuation'>,</span> <span class='variable parameter'>term</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>Int</span></span><span class='punctuation'>,</span> <span class='variable parameter'>voted</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>String</span></span><span class='punctuation bracket'>)</span> <span class='operator'>-&gt;</span> <span class='punctuation bracket'>(</span><span class='punctuation bracket'>)</span><span class='punctuation'>;</span>
123  <span class='function'>append</span><span class='punctuation bracket'>(</span><span class='variable parameter'>node</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>String</span></span><span class='punctuation'>,</span> <span class='variable parameter'>keep</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>Int</span></span><span class='punctuation'>,</span> <span class='variable parameter'>entries</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>Vec</span></span><span class='operator'>&lt;</span><span class='type'><span class='constructor'>Entry</span></span><span class='operator'>&gt;</span><span class='punctuation bracket'>)</span> <span class='operator'>-&gt;</span> <span class='punctuation bracket'>(</span><span class='punctuation bracket'>)</span></code></pre><figcaption><code>raft.nib</code>: the persistent state, as an interface.</figcaption></figure>
124<p>A <em>wire</em> binds each interface to an implementation. Swapping one
125binding gives a program with a known bug and leaves the rest of the protocol
126alone, so the checks have something to refuse. <code>Forgetful</code> binds a
127store whose <code>recover</code> answers nothing, as a node that never synced
128its writes would:</p>
129<figure class="code"><pre class="nib"><code><span class='keyword'>wire</span> <span class='constructor'>Main</span> <span class='keyword'>runs</span> Raft<span class='punctuation delimiter'>.</span><span class='variable'>main</span> <span class='keyword'>where</span>
130  <span class='type'><span class='constructor'>Store</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Disk</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Machine</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Kv</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Rules</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Strict</span></span><span class='punctuation'>,</span>
131  <span class='type'><span class='constructor'>Net</span><span class='punctuation delimiter'>.</span><span class='constructor'>Socket</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Net</span><span class='punctuation delimiter'>.</span><span class='constructor'>Sim</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Time</span><span class='punctuation delimiter'>.</span><span class='constructor'>Clock</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Time</span><span class='punctuation delimiter'>.</span><span class='constructor'>Sim</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Random</span><span class='punctuation delimiter'>.</span><span class='constructor'>Source</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Random</span><span class='punctuation delimiter'>.</span><span class='constructor'>Sim</span></span><span class='punctuation'>,</span>
132  <span class='type'><span class='constructor'>Io</span><span class='punctuation delimiter'>.</span><span class='constructor'>Print</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Io</span><span class='punctuation delimiter'>.</span><span class='constructor'>Stdout</span></span>
133
134<span class='keyword'>wire</span> <span class='constructor'>Forgetful</span> <span class='keyword'>runs</span> Raft<span class='punctuation delimiter'>.</span><span class='variable'>main</span> <span class='keyword'>where</span>
135  <span class='type'><span class='constructor'>Store</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Ram</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Machine</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Kv</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Rules</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Strict</span></span><span class='punctuation'>,</span>
136  <span class='type'><span class='constructor'>Net</span><span class='punctuation delimiter'>.</span><span class='constructor'>Socket</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Net</span><span class='punctuation delimiter'>.</span><span class='constructor'>Sim</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Time</span><span class='punctuation delimiter'>.</span><span class='constructor'>Clock</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Time</span><span class='punctuation delimiter'>.</span><span class='constructor'>Sim</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Random</span><span class='punctuation delimiter'>.</span><span class='constructor'>Source</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Random</span><span class='punctuation delimiter'>.</span><span class='constructor'>Sim</span></span><span class='punctuation'>,</span>
137  <span class='type'><span class='constructor'>Io</span><span class='punctuation delimiter'>.</span><span class='constructor'>Print</span></span> <span class='operator'>=</span> <span class='type'><span class='constructor'>Io</span><span class='punctuation delimiter'>.</span><span class='constructor'>Stdout</span></span></code></pre><figcaption><code>raft.nib</code>: two of the four wires. <code>Mutant</code> votes twice in a term; <code>Careless</code> ignores the candidate's log.</figcaption></figure>
138<figure class="code"><pre class="nib"><code><span class='keyword'>effector</span> <span class='constructor'>Ram</span> <span class='keyword'>is</span> <span class='type'><span class='constructor'>Store</span></span> <span class='keyword'>where</span>
139  unkeyed <span class='keyword'>var</span> <span class='variable'>logs</span> <span class='punctuation delimiter'>::</span> <span class='type'><span class='type builtin'>Map</span></span><span class='operator'>&lt;</span><span class='type'><span class='type builtin'>String</span></span><span class='punctuation'>,</span> <span class='type'><span class='type builtin'>Vec</span></span><span class='operator'>&lt;</span><span class='type'><span class='constructor'>Entry</span></span><span class='operator'>&gt;</span><span class='operator'>&gt;</span> <span class='operator'>=</span> <span class='punctuation bracket'>{</span><span class='punctuation bracket'>}</span><span class='punctuation'>;</span>
140  <span class='function'>recover</span><span class='punctuation bracket'>(</span><span class='variable parameter'>node</span><span class='punctuation bracket'>)</span> <span class='operator'>=</span> <span class='constructor'>Saved</span><span class='punctuation bracket'>{</span> term<span class='punctuation delimiter'>:</span> <span class='number'>0</span><span class='punctuation'>,</span> voted<span class='punctuation delimiter'>:</span> <span class='string'>&quot;&quot;</span><span class='punctuation'>,</span> log<span class='punctuation delimiter'>:</span> <span class='punctuation delimiter'>#</span><span class='punctuation bracket'>[</span><span class='punctuation bracket'>]</span> <span class='punctuation bracket'>}</span>
141  <span class='function'>save</span><span class='punctuation bracket'>(</span><span class='variable parameter'>node</span><span class='punctuation'>,</span> <span class='variable parameter'>term</span><span class='punctuation'>,</span> <span class='variable parameter'>voted</span><span class='punctuation bracket'>)</span> <span class='operator'>=</span> <span class='punctuation bracket'>(</span><span class='punctuation bracket'>)</span>
142  <span class='function'>append</span><span class='punctuation bracket'>(</span><span class='variable parameter'>node</span><span class='punctuation'>,</span> <span class='variable parameter'>keep</span><span class='punctuation'>,</span> <span class='variable parameter'>entries</span><span class='punctuation bracket'>)</span> <span class='operator'>=</span>
143    <span class='variable'>logs</span> <span class='operator'>:=</span> <span class='constructor'>Map</span><span class='punctuation delimiter'>.</span><span class='function'>set</span><span class='punctuation bracket'>(</span><span class='variable'>logs</span><span class='punctuation'>,</span> <span class='variable'>node</span><span class='punctuation'>,</span> <span class='constructor'>Vec</span><span class='punctuation delimiter'>.</span><span class='function'>append</span><span class='punctuation bracket'>(</span><span class='constructor'>Vec</span><span class='punctuation delimiter'>.</span><span class='function'>take</span><span class='punctuation bracket'>(</span><span class='constructor'>Map</span><span class='punctuation delimiter'>.</span><span class='function'>get_or</span><span class='punctuation bracket'>(</span><span class='variable'>logs</span><span class='punctuation'>,</span> <span class='variable'>node</span><span class='punctuation'>,</span> <span class='punctuation delimiter'>#</span><span class='punctuation bracket'>[</span><span class='punctuation bracket'>]</span><span class='punctuation bracket'>)</span><span class='punctuation'>,</span> <span class='variable'>keep</span><span class='punctuation bracket'>)</span><span class='punctuation'>,</span> <span class='variable'>entries</span><span class='punctuation bracket'>)</span><span class='punctuation bracket'>)</span><span class='punctuation'>;</span> <span class='punctuation bracket'>(</span><span class='punctuation bracket'>)</span></code></pre><figcaption><code>raft.nib</code>: the store that forgets.</figcaption></figure>
144</section>
145
146<section id="world">
147<h2><span class="n">2</span> The world</h2>
148<p><code>Net.Sim</code>, <code>Time.Sim</code> and <code>Random.Sim</code> are
149simulated implementations of the network, clock and randomness interfaces.
150All three nodes are fibres in one process; virtual time moves only when every
151fibre is waiting, so three seconds of cluster time take a fraction of a second.
152A world file says what the world does:</p>
153<figure class="code"><pre class="json"><code>{ &quot;schedule&quot;: &quot;random&quot;,
154  &quot;net&quot;: { &quot;delay_ms&quot;: [1, 40], &quot;drop_permille&quot;: 50, &quot;duplicate_permille&quot;: 20 },
155  &quot;crashes&quot;: [ { &quot;node&quot;: &quot;10.0.0.1&quot;, &quot;at_ms&quot;: 700, &quot;restart_ms&quot;: 1200 },
156               { &quot;node&quot;: &quot;10.0.0.2&quot;, &quot;at_ms&quot;: 1500, &quot;restart_ms&quot;: 1900 },
157               { &quot;node&quot;: &quot;10.0.0.3&quot;, &quot;at_ms&quot;: 2300, &quot;restart_ms&quot;: 2600 } ] }</code></pre><figcaption><code>worlds/crash.json</code>: a random schedule, 1 to 40 ms delivery, 5% loss, 2% duplication, and a crash of each node.</figcaption></figure>
158<p>Every decision the world makes is a <em>choice</em>: when each message
159arrives (or whether it is lost), which ready fibre runs next, each random
160draw, each crash. A seed makes them; a world file can also state them
161outright. A crash kills every fibre of that host where it stands and closes
162its connections. Effector state survives, because it belongs to the wire,
163not to a fibre, so it is the program's disk. Restarting is the program's own
164work: a supervisor outside the node sees it killed and runs it again.</p>
165<figure class="code"><pre class="nib"><code>  <span class='keyword'>func</span> <span class='function'>keep</span> <span class='punctuation delimiter'>::</span> <span class='variable parameter'>me</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>String</span></span><span class='punctuation'>,</span> <span class='variable parameter'>stop</span><span class='punctuation delimiter'>:</span> <span class='type'><span class='type builtin'>Int</span></span> <span class='operator'>-&gt;</span> <span class='punctuation bracket'>(</span><span class='punctuation bracket'>)</span> <span class='operator'>!</span> <span class='type'><span class='constructor'>Store</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Net</span><span class='punctuation delimiter'>.</span><span class='constructor'>Socket</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Random</span><span class='punctuation delimiter'>.</span><span class='constructor'>Source</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Machine</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Rules</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Time</span><span class='punctuation delimiter'>.</span><span class='constructor'>Clock</span></span><span class='punctuation'>,</span> <span class='type'><span class='constructor'>Io</span><span class='punctuation delimiter'>.</span><span class='constructor'>Print</span></span> <span class='operator'>=</span>
166    <span class='keyword'>let</span> <span class='variable'>crashed</span> <span class='operator'>=</span>
167      <span class='keyword'>try</span> <span class='constructor'>Switch</span><span class='punctuation delimiter'>.</span>run <span class='punctuation bracket'>{</span> <span class='variable parameter'>sw</span> <span class='operator'>-&gt;</span> <span class='keyword'>let</span> _ <span class='operator'>=</span> <span class='constructor'>Fiber</span><span class='punctuation delimiter'>.</span><span class='function'>await</span><span class='punctuation bracket'>(</span><span class='function'>fork</span><span class='punctuation bracket'>(</span><span class='variable'>sw</span><span class='punctuation bracket'>)</span> <span class='punctuation bracket'>{</span> <span class='operator'>-&gt;</span> <span class='function'>node</span><span class='punctuation bracket'>(</span><span class='variable'>me</span><span class='punctuation'>,</span> <span class='variable'>stop</span><span class='punctuation bracket'>)</span> <span class='punctuation bracket'>}</span><span class='punctuation bracket'>)</span><span class='punctuation'>;</span> false <span class='punctuation bracket'>}</span>
168      <span class='keyword'>rescue</span> <span class='constructor'>HandleRefused</span><span class='punctuation bracket'>(</span>_<span class='punctuation bracket'>)</span> <span class='operator'>=&gt;</span> true
169      <span class='keyword'>end</span><span class='punctuation'>;</span>
170    <span class='keyword'>if</span> <span class='variable'>crashed</span> <span class='operator'>&amp;&amp;</span> <span class='operator'>@</span><span class='constructor'>Time</span><span class='punctuation delimiter'>.</span><span class='constructor'>Clock</span><span class='punctuation delimiter'>.</span><span class='function'>now</span><span class='punctuation bracket'>(</span><span class='punctuation bracket'>)</span> <span class='operator'>&lt;</span> <span class='variable'>stop</span> <span class='keyword'>then</span> <span class='function'>keep</span><span class='punctuation bracket'>(</span><span class='variable'>me</span><span class='punctuation'>,</span> <span class='variable'>stop</span><span class='punctuation bracket'>)</span> <span class='keyword'>else</span> <span class='punctuation bracket'>(</span><span class='punctuation bracket'>)</span></code></pre><figcaption><code>raft.nib</code>: the supervisor. Its next <code>listen</code> waits until the world brings the host back.</figcaption></figure>
171</section>
172
173<section id="journal">
174<h2><span class="n">3</span> The journal</h2>
175<p>Every operation and every choice is a line of JSON, carrying the node that
176performed it and the answer it got. The hero run is 7,460 lines,
1773,648 of them clock reads and sleeps. Messages are sent as JSON, so
178the checker can read a message's kind and term out of the bytes. Here is the
179crash, and the two writes the monitor compares in the refused run below
180(fields trimmed):</p>
181<figure class="code"><pre class="journal"><code>{&quot;seq&quot;:2079,&quot;kind&quot;:&quot;choice&quot;,&quot;op&quot;:&quot;World.crash&quot;,&quot;argv&quot;:[&quot;10.0.0.1&quot;,700],&quot;node&quot;:&quot;&quot;,&quot;outcome&quot;:{&quot;value&quot;:null}}
182{&quot;seq&quot;:566,&quot;kind&quot;:&quot;operation&quot;,&quot;op&quot;:&quot;Raft.Store.save&quot;,&quot;argv&quot;:[&quot;10.0.0.1&quot;,1,&quot;10.0.0.3&quot;],&quot;node&quot;:&quot;10.0.0.1:7000&quot;,&quot;outcome&quot;:{&quot;value&quot;:null}}
183{&quot;seq&quot;:3071,&quot;kind&quot;:&quot;operation&quot;,&quot;op&quot;:&quot;Raft.Store.save&quot;,&quot;argv&quot;:[&quot;10.0.0.1&quot;,1,&quot;&quot;],&quot;node&quot;:&quot;10.0.0.1:7000&quot;,&quot;outcome&quot;:{&quot;value&quot;:null}}</code></pre>
184<figcaption>Entries 2079, 566 and 3071 of
185<a href="runs/forgetful-crash-1.jsonl.gz">the refused run</a>.</figcaption></figure>
186<p>The same journal is also a replay: run the program against it, and each
187recorded answer and choice is served back in order, so the run happens again
188exactly, crashes included, with no world file needed. <code>nib-tla
189world</code> turns a journal's choices back into a world file, so a failing
190run can be edited and cut down.</p>
191</section>
192
193<section id="monitor">
194<h2><span class="n">4</span> The monitor</h2>
195<p><a href="RaftTrace.tla.txt"><code>RaftTrace.tla</code></a> is not a model
196of Raft. It rebuilds what the journal shows (who led which term, each node's
197log, what was applied and saved) and refuses the journal at the first entry
198that breaks one of these:</p>
199<dl class="props">
200<dt>Election safety</dt><dd>At most one node leads a term. A node leads term
201<i>t</i> when it sends an append of term <i>t</i>.</dd>
202<dt>Log matching</dt><dd>Two logs holding an entry of the same term at the
203same index agree up to that index, checked on every <code>Store.append</code>.</dd>
204<dt>Leader completeness</dt><dd>An entry applied before a node stood for term
205<i>t</i> is in that node's log whenever it leads <i>t</i>.</dd>
206<dt>State machine safety</dt><dd>No two nodes apply different commands at one
207index.</dd>
208<dt>Durability</dt><dd>A node's saved term never goes back, and its vote in a
209term, once saved, never changes.</dd>
210</dl>
211<figure class="code"><pre class="tla"><code>Durable(t0, v0, t, v) == t &gt; t0 \/ (t = t0 /\ (v0 = &quot;&quot; \/ v0 = v))
212
213  \/ /\ e.op = &quot;Raft.Store.save&quot;
214     /\ e.argv[1] \in Nodes
215     /\ Durable(saved[e.argv[1]][1], saved[e.argv[1]][2], e.argv[2], e.argv[3])
216     /\ saved&#x27; = [saved EXCEPT ![e.argv[1]] = &lt;&lt;e.argv[2], e.argv[3]&gt;&gt;]
217     /\ candidacies&#x27; = IF e.argv[3] = e.argv[1]
218                         THEN candidacies \cup {&lt;&lt;e.argv[1], e.argv[2], l&gt;&gt;}
219                         ELSE candidacies
220     /\ UNCHANGED &lt;&lt;leaders, applied, logs&gt;&gt;</code></pre><figcaption><code>RaftTrace.tla</code>: Durability, and the step that checks it on each save.</figcaption></figure>
221<p>Any operation the monitor does not name is refused too, so a new effect in
222the program cannot pass unexamined. <code>nib-tla</code> checks this with a
223probe: it appends an operation nobody mapped and requires the monitor to
224refuse it.</p>
225</section>
226
227<section id="verdict">
228<h2><span class="n">5</span> The verdict</h2>
229<p>The forgetful store, seed 1 of the same world. Node
23010.0.0.1 voted for 10.0.0.3 in term 1 at 201 ms. It
231crashed, came back with nothing, and at 1213 ms heard of term
2321 again and saved it with no vote: free to vote a second time in a
233term it has already voted in. The monitor refuses the run there, at entry
2343071, before a second leader exists to show for it.</p>
235<figure class="run">
236<div class="scroll"><svg class="timeline" viewBox="0 0 900 236" role="img" aria-labelledby="t-Forgetful">
237<title id="t-Forgetful">The forgetful store, refused at the save that erased a vote</title>
238<defs><pattern id="hatch" width="6" height="6" patternUnits="userSpaceOnUse" patternTransform="rotate(45)"><line x1="0" y1="0" x2="0" y2="6" class="hatch"/></pattern></defs>
239<line class="grid" x1="92.0" x2="92.0" y1="22" y2="214"/>
240<text class="tick" x="92.0" y="230" text-anchor="middle">0 ms</text>
241<line class="grid" x1="221.0" x2="221.0" y1="22" y2="214"/>
242<text class="tick" x="221.0" y="230" text-anchor="middle">500 ms</text>
243<line class="grid" x1="350.0" x2="350.0" y1="22" y2="214"/>
244<text class="tick" x="350.0" y="230" text-anchor="middle">1000 ms</text>
245<line class="grid" x1="479.0" x2="479.0" y1="22" y2="214"/>
246<text class="tick" x="479.0" y="230" text-anchor="middle">1500 ms</text>
247<line class="grid" x1="608.0" x2="608.0" y1="22" y2="214"/>
248<text class="tick" x="608.0" y="230" text-anchor="middle">2000 ms</text>
249<line class="grid" x1="737.0" x2="737.0" y1="22" y2="214"/>
250<text class="tick" x="737.0" y="230" text-anchor="middle">2500 ms</text>
251<line class="grid" x1="866.0" x2="866.0" y1="22" y2="214"/>
252<text class="tick" x="866.0" y="230" text-anchor="middle">3000 ms</text>
253<text class="host" x="0" y="51.0">10.0.0.1</text>
254<line class="lane n1" x1="92" x2="866" y1="47.0" y2="47.0"/>
255<rect class="down" x="272.6" y="30" width="129.0" height="34" fill="url(#hatch)"/>
256<text class="downlabel" x="337.1" y="25" text-anchor="middle">crashed</text>
257<line class="apply n1" x1="178.2" x2="178.2" y1="66" y2="73"/>
258<line class="apply n1" x1="189.8" x2="189.8" y1="66" y2="73"/>
259<line class="apply n1" x1="207.3" x2="207.3" y1="66" y2="73"/>
260<line class="apply n1" x1="228.5" x2="228.5" y1="66" y2="73"/>
261<line class="apply n1" x1="228.5" x2="228.5" y1="66" y2="73"/>
262<line class="apply n1" x1="243.4" x2="243.4" y1="66" y2="73"/>
263<text class="host" x="0" y="111.0">10.0.0.2</text>
264<line class="lane n2" x1="92" x2="866" y1="107.0" y2="107.0"/>
265<rect class="down" x="479.0" y="90" width="103.2" height="34" fill="url(#hatch)"/>
266<text class="downlabel" x="530.6" y="85" text-anchor="middle">crashed</text>
267<rect class="candidate n2" x="721.5" y="99" width="13.4" height="16" rx="2"/>
268<rect class="leader n2" x="734.9" y="94" width="131.1" height="26" rx="2"/>
269<text class="role" x="742.9" y="111.0">leads term 2</text>
270<line class="apply n2" x1="172.2" x2="172.2" y1="126" y2="133"/>
271<line class="apply n2" x1="190.0" x2="190.0" y1="126" y2="133"/>
272<line class="apply n2" x1="206.3" x2="206.3" y1="126" y2="133"/>
273<line class="apply n2" x1="226.2" x2="226.2" y1="126" y2="133"/>
274<line class="apply n2" x1="226.2" x2="226.2" y1="126" y2="133"/>
275<line class="apply n2" x1="245.8" x2="245.8" y1="126" y2="133"/>
276<text class="host" x="0" y="171.0">10.0.0.3</text>
277<line class="lane n3" x1="92" x2="866" y1="167.0" y2="167.0"/>
278<rect class="candidate n3" x="143.6" y="159" width="2.3" height="16" rx="2"/>
279<rect class="leader n3" x="145.9" y="154" width="539.5" height="26" rx="2"/>
280<text class="role" x="153.9" y="171.0">leads term 1</text>
281<rect class="down" x="685.4" y="150" width="77.4" height="34" fill="url(#hatch)"/>
282<text class="downlabel" x="724.1" y="145" text-anchor="middle">crashed</text>
283<line class="apply n3" x1="164.2" x2="164.2" y1="186" y2="193"/>
284<line class="apply n3" x1="182.3" x2="182.3" y1="186" y2="193"/>
285<line class="apply n3" x1="190.8" x2="190.8" y1="186" y2="193"/>
286<line class="apply n3" x1="211.2" x2="211.2" y1="186" y2="193"/>
287<line class="apply n3" x1="215.1" x2="215.1" y1="186" y2="193"/>
288<line class="apply n3" x1="234.2" x2="234.2" y1="186" y2="193"/>
289<rect class="unread" x="405.0" y="20" width="461.0" height="194"/>
290<text class="unreadlabel" x="862.0" y="14" text-anchor="end">not checked past the refusal</text>
291<line class="refuse" x1="405.0" x2="405.0" y1="16" y2="214"/>
292<circle class="refuse-dot" cx="143.9" cy="47.0" r="5"/>
293<circle class="refuse-dot" cx="405.0" cy="47.0" r="5"/>
294<text class="refuselabel" x="413.0" y="14">refused at entry 3071</text>
295</svg></div>
296<figcaption><p>Dots mark the two saves: the vote, and the save that erased
297it. The program's own end-of-run check, which compares what every node
298applied, printed <code>safe: applied 6 6 6</code>.</p></figcaption>
299</figure>
300<p>That last line is the point. Of the 21 refused runs in the
301table below, 15 printed <code>safe</code>: the nodes agreed at
302the end, and the bug was invisible to "it ran and agreed with itself". On
303every commit, CI runs <code>nib-tla verify
304areas/examples/raft-nib</code>: one calm run, then each sweep below over a range of
305seeds. The correct program must be admitted on every seed, and each broken
306wire must be refused on at least one.</p>
307<div class="scroll"><table>
308<thead><tr><th>wire</th><th>world</th><th>expected</th><th>result</th></tr></thead>
309<tbody>
310<tr><td><code>Main</code></td><td><code>worlds/calm.json</code></td><td>admitted</td><td class="held">admitted, 5,498 entries</td></tr>
311<tr><td><code>Main</code></td><td><code>worlds/faults.json</code></td><td>admitted</td><td class="held">10 of 10 admitted</td></tr>
312<tr><td><code>Mutant</code></td><td><code>worlds/split.json</code></td><td>refused</td><td class="held">8 of 10 refused</td></tr>
313<tr><td><code>Careless</code></td><td><code>worlds/faults.json</code></td><td>refused</td><td class="held">8 of 10 refused</td></tr>
314<tr><td><code>Main</code></td><td><code>worlds/crash.json</code></td><td>admitted</td><td class="held">10 of 10 admitted</td></tr>
315<tr><td><code>Forgetful</code></td><td><code>worlds/crash.json</code></td><td>refused</td><td class="held">5 of 5 refused</td></tr>
316</tbody></table></div>
317<p class="small">The whole check took 125 s on a laptop.</p>
318</section>
319
320<section id="platform">
321<h2>What nib provides</h2>
322<p>None of this is special to Raft. It follows from four decisions in the
323language and its runtime:</p>
324<dl class="props">
325<dt>Effects are in the types</dt><dd>A function lists what it may do. So
326every way the program touches the world is a named operation with a
327signature, which is exactly what a journal can record and a monitor can
328name.</dd>
329<dt>Wires choose implementations</dt><dd>The same program runs on real
330sockets or on <code>Net.Sim</code>, and a known-wrong variant is one binding
331away, with no mocks and no flags in the protocol code.</dd>
332<dt>The scheduler has one choice point</dt><dd>Fibres change hands only
333where one waits, and every such switch asks the world which ready fibre runs
334next. Under a seed that choice is random; in a journal it is recorded; in a
335replay it is served back.</dd>
336<dt>The journal is the run</dt><dd>Operations are recorded when they complete,
337each with its node, fibre and message id, and chained by digest. The same
338file drives replay, the checker, and the world file that reproduces it.</dd>
339</dl>
340</section>
341
342<section id="limits">
343<h2>What this does not show</h2>
344<ul>
345<li>It checks the runs it is given: tens of seeds, not every interleaving.
346A bug that needs a schedule no seed draws goes unseen.</li>
347<li>The monitor checks safety only. A run in which no leader is ever elected is
348admitted.</li>
349<li>Leader completeness is checked only as far as the journal can settle it:
350commits are inferred from applies.</li>
351<li>Everything runs on the simulated world. Real sockets can be recorded, but
352not yet replayed with concurrent servers.</li>
353</ul>
354</section>
355
356<section id="run">
357<h2>Run it yourself</h2>
358<p>From a checkout of the repository, with <code>nibc</code> and
359<code>nib-tla</code> built:</p>
360<pre class="shell"><code>nibc --run areas/examples/raft-nib/raft.nib --wire Main \
361  --world areas/examples/raft-nib/worlds/crash.json --journal-out run.jsonl
362nib-tla check --spec areas/examples/raft-nib/RaftTrace.tla --journal run.jsonl
363nib-tla verify areas/examples/raft-nib</code></pre>
364<p><code>explore.sh</code> runs any wire under many seeds and keeps each
365refused run with the world file that reproduces it.</p>
366</section>
367</main>
368
369<footer>
370<p>Generated from runs of the program in
371<code>areas/examples/raft-nib</code> by <code>gen/build.py</code>; every
372number and journal line on this page comes from those runs. The source is
373also here as text: <a href="raft.nib.txt">raft.nib</a>,
374<a href="RaftTrace.tla.txt">RaftTrace.tla</a>.
375About nib: <a href="https://nib.app.jahfer.link">nib.app.jahfer.link</a>.</p>
376</footer>
377</body>
378</html>