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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! One declared logical clock: the authority every deadline in this crate names.
//!
//! Three clocks exist in any async system and they are not the same clock. The
//! engine's timer wheel, `std::time::Instant`, and a test's notion of "now" all
//! advance independently, and a bot that mixes them produces deadlines that
//! disagree with each other by exactly the amount nobody measured. This module
//! makes one of them authoritative and makes the difference *nameable*.
//!
//! # The three clocks, and what each governs
//!
//! | Clock | Governs | Sourced from |
//! |---|---|---|
//! | [`Clock`] (logical) | every deadline this crate evaluates: retry, sleep, budget, readiness, continuation | a caller-advanceable counter |
//! | the engine timer | when a `sleep` future actually resolves | the engine's driver |
//! | [`WallClock`] | the watchdog on things a logical clock cannot stop | `std::time::Instant` |
//!
//! # Why the wall clock is never governed by the logical one
//!
//! A logical clock is the right authority for anything a test can model: a retry
//! that backs off, a sleep, a readiness wait, a continuation deadline. It is the
//! wrong authority for a subprocess that has stopped responding, a blocking
//! callback that will never return, or a store whose `fsync` is stuck — none of
//! those are waiting for time to pass, they have stopped making progress
//! entirely, and a paused logical clock would wait on them forever. That is why
//! [`WallClock`] exists, is always on, and is never reachable from a paused
//! logical clock: [`Clock::wall`] hands out a wall watchdog that keeps running
//! with logical time frozen.
//!
//! The rule this encodes, which the module docs make a contract: **pausing
//! logical time never disables the wall-clock watchdog.** A test that pauses
//! time to examine a three-day outage still cannot hang on a process that
//! stopped responding, because the watchdog it was given reads real elapsed
//! time, not the counter it paused.
//!
//! # Determinism, and what is *not* deterministic
//!
//! Advancing a [`Clock`] is deterministic in the order work becomes eligible,
//! which is what makes a seeded replay meaningful. It is not a claim about
//! anything outside this process:
//!
//! - Poll *order* across worker threads is not deterministic, and a clock does
//! not make it so. What a clock determinizes is which deadline is *eligible*,
//! not which task the OS scheduler runs first.
//! - Actual observed external order — the order a remote peer saw two requests
//! in, the order bytes hit a socket — is never deterministic and is not
//! claimed here.
//! - Clock skew between hosts is real and unmeasured. A logical clock has no
//! cross-host notion at all.
//! - Real-time liveness is the [`WallClock`]'s domain and is the only thing that
//! says a subprocess is still answering.
//!
//! # Duration and remaining-deadline semantics across a restart
//!
//! A [`Clock`] is a **process-local** counter and it is explicitly *not*
//! serializable. A `std::time::Instant` is an opaque monotonic reading with an
//! undefined epoch, and writing it to a durable store as though it were a
//! timestamp produces a value that means nothing on another host, or after the
//! host's clock source changes. What survives a restart is the **remaining
//! duration**, never the instant:
//!
//! ```
//! # use std::time::Duration;
//! # use lgwks_bot::rt::clock::Clock;
//! let clock = Clock::virtual_at(Duration::ZERO);
//! let budget = Duration::from_secs(5);
//!
//! // A run records how much of its budget it has spent, not a wall instant.
//! clock.advance(Duration::from_secs(2))?;
//! let recorded = clock.snapshot();
//!
//! // A restart re-establishes the origin from the recorded elapsed time, and
//! // the remaining budget is the same fact on either host.
//! let resumed = Clock::virtual_at(recorded.elapsed());
//! assert_eq!(resumed.snapshot().remaining_from(budget), Duration::from_secs(3));
//! # Ok::<(), lgwks_bot::rt::clock::ClockError>(())
//! ```
//!
//! [`ClockSnapshot::remaining_from`] gives the remaining duration of a budget at
//! the recorded point, and a restarted clock restored to that point has the same
//! remaining budget. Nothing on this type can be mistaken for a wall-clock
//! timestamp, because it carries no epoch and no host identity.
//!
//! # No-runtime construction
//!
//! A [`Clock`] is built, advanced and read entirely from `std`. It does not
//! require a runtime, and constructing one inside an existing runtime is
//! allowed. Nothing here starts a runtime, promotes local work to remote
//! execution, or nests one runtime per task.
//!
//! [`Clock`]: crate::clock::Clock
//! [`Clock::wall`]: crate::clock::Clock::wall
//! [`WallClock`]: crate::clock::WallClock
//! [`ClockSnapshot::remaining_from`]: crate::clock::ClockSnapshot::remaining_from
use fmt;
use Arc;
use ;
use ;
/// Largest duration a logical instant can represent, saturating rather than
/// wrapping.
///
/// One [`Duration`] below the saturation point. A clock that reached it would
/// be reporting a deadline nobody could wait out in a human lifetime, so the
/// value is a documented ceiling rather than a silently wrong number.
const MAX_ELAPSED: Duration = from_nanos;
/// Which time source a [`Clock`] reads, and therefore what "advance" means.
///
/// A sum type rather than a boolean, because the two sources have different
/// contracts — one can be driven by a test and the other cannot — and a
/// `bool` field would let a caller construct a combination the module cannot
/// honour.
/// The one logical clock a caller declares, and the authority for every deadline
/// this crate evaluates.
///
/// Cheap to clone: every clone shares the same counter, so a body can hold one
/// and a test can drive it from outside. This is what makes it a *declared*
/// clock rather than a value copied around.
/// The shared state behind every [`Clock`] clone.
/// Why a clock operation was refused.
///
/// One error type rather than a `bool`, so a caller that ignores the refusal has
/// a value whose name says what it lost, not a silent no-op.
/// A persistable reading of a [`Clock`]'s elapsed time.
///
/// Deliberately *not* a `std::time::Instant`. An `Instant` is a process-local
/// monotonic reading with an undefined epoch; persisting one as a timestamp
/// produces a value that means nothing on another host. This carries only a
/// duration, and [`Clock::virtual_at`] takes exactly this back.
/// The independent wall-clock watchdog.
///
/// Always real time, never the logical counter, and therefore never paused by a
/// test. Constructed through [`Clock::wall_watchdog`] so that every watchdog in a
/// program traces back to a declared clock, rather than being a free-floating
/// [`Instant`].
/// Nanoseconds in `duration`, saturating at the representable ceiling.
///
/// `u64::MAX` nanoseconds is about 584 years, so the ceiling is never reached by
/// a real budget; it exists so that a caller who somehow asks for more gets the
/// documented ceiling rather than a wrapped instant.
///
/// Split rather than widen-then-narrow: `Duration` reports seconds and
/// sub-second nanoseconds through two infallible projections, so the only
/// arithmetic left is the scaling, and `saturating_mul`/`saturating_add` put
/// the ceiling in that arithmetic instead of in a conversion that could fail
/// and leave a caller holding a default.
/// `nanos` nanoseconds as a [`Duration`].
///
/// The inverse of [`duration_to_nanos`] with no intermediate arithmetic, so
/// there is no quotient to truncate and no remainder to narrow.