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
//! The pure Hybrid Logical Clock fold: the crate's time algebra, extracted
//! from the concurrency shell that runs it (S188, ruling R-19).
//!
//! [`advance`] is the *entire* semantics of a clock step: given the last
//! committed `(physical, logical)` (or none), one time-source reading, and an
//! optional remote stamp's `(physical, logical)`, it computes the next
//! committed pair and reports the skew it observed along the way. It is a
//! total, pure, heap-free function over machine words: no atomics, no time
//! source, no allocation, no `Kairos` (the caller unpacks the two fields it
//! folds), so the shells that share it stay free to choose their own
//! concurrency story and the checkers reach it whole.
//!
//! Two shells run this fold today: [`Clock`](super::Clock) commits it through
//! a compare-and-swap loop (thread-safe, lock-free, backoff on contention)
//! and [`LocalClock`](super::LocalClock) commits it through a `Cell` (single
//! threaded, no fences, no retries). Their agreement is not a test
//! obligation but a *construction*: there is one fold, and a shell only
//! decides how its result is committed. The S86 Kani harnesses prove the
//! fold's laws (strict domination, monotonicity under arbitrary skew, the
//! ceiling-logical rollover) through the `Clock` shell, whose single-threaded
//! first-attempt CAS is exactly one fold application; the shell-agreement
//! property test pins that `LocalClock` tells the same story on every tape.
//!
//! The fold is deliberately self-contained (plain integer arguments, no
//! sibling imports) so it stays eligible for the dual-homed pure-core proof
//! treatment (ruling R-18) without restructuring, if a session ever takes
//! the clock into the Creusot rung.
/// One advance's outcome: the pair to commit, and the skew observed while
/// computing it (zero when none). The shell owns what to do with each half:
/// commit the pair (CAS or set), fold the skews into its running maxima.
pub
/// One run reservation's outcome: the first stamp of the run, the pair to
/// commit (the run's last stamp, so every subsequent mint strictly dominates
/// every run member), and the skew observed by the single reading the run
/// consumed. The shell owns commitment and bookkeeping exactly as with
/// [`Advance`].
pub
/// Rank slots per physical unit: logical values run `0..=u16::MAX - 1`
/// (`u16::MAX` is the rollover sentinel [`advance`] never commits), so each
/// physical unit carries exactly `u16::MAX as u64` successor positions.
const RANK_SLOTS: u64 = u16MAX as u64;
/// The pair `n` send-step successors past `(physical, logical)`: the closed
/// form of iterating the fold's frozen-reading send step (which is the
/// snapshot codec's `rank_successor`; the S197 harness proves those equal)
/// `n` times. Total and heap-free; the physical carry saturates at the
/// `u64::MAX` ceiling exactly as the iterated step does (the documented pin
/// edge, where domination is arithmetically impossible), and the logical
/// cursor keeps its modular position either way, again as the iterated step.
/// `logical` must be a committed value (below the sentinel), which every
/// fold output is.
pub const
/// The Hybrid Logical Clock *run* step: one fold application reserving `len`
/// consecutive send positions against a single time-source reading.
///
/// The first stamp is exactly `advance(last, reading, None)`; the
/// committed pair is the run's last stamp, `len - 1` send-step successors
/// past the first. Because a send step against a reading at or behind the
/// committed physical *is* the successor rule (the S197 harness's frozen
/// -reading identity), this equals folding [`advance`] `len` times over the
/// one frozen reading: the run members are the very stamps that many
/// individual mints would have committed, minus the re-reads.
///
/// The reported skews are the MAXIMA the iterated mints would have folded
/// into the shell's stats, not the first mint's alone: a run whose carry
/// crosses a physical unit runs the committed physical ahead of the one
/// reading (forward skew, the retained local lead), and every mint past the
/// crossing reads the source behind the committed physical (backward skew
/// off the penultimate member). Without this, `ClockStats` would
/// under-report exactly the gap the run created (the S211 gate review's
/// finding).
///
/// `len` arrives from a `NonZeroU32`, so it is at least one; the saturating
/// subtractions only keep the function total on the whole `u32` domain.
pub const
/// The Hybrid Logical Clock step. `last` is the shell's committed state
/// (`None` before the first mint), `reading` is one time-source sample, and
/// `remote` is the stamp being received, if any (its `(physical, logical)`;
/// send-only steps pass `None`).
///
/// The result strictly dominates both the last committed pair and any remote
/// below the physical ceiling: at `u64::MAX` the clock pins (saturates,
/// never wraps), the documented edge the proofs carry as a precondition. A
/// logical that would reach `u16::MAX` rolls into the next physical unit
/// instead (the S86 fold fix: the ceiling tie must not fall through to the
/// kairotic/station tiebreak).
pub const