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
//! Suspend/resume detection (#167 ask 2): the watcher that pushes a [`StreamFrame::Resumed`] when
//! this machine wakes from a sleep, so an embedder holding long-lived sessions can re-dial
//! deliberately instead of discovering the loss on its next send.
//!
//! While a machine is suspended it sends nothing, so the PEER's idle timer runs out and tears the
//! connection down before the lid is even reopened. `[network].keep_alive_secs` cannot help — a
//! suspended process emits no PINGs — and `idle_timeout_secs` is negotiated to the minimum of both
//! peers, so surviving a lid close would need every peer in the mesh raised in lockstep to a value
//! covering the longest expected sleep. #167 says that plainly: this needs reconnection semantics,
//! not transport tuning. The frame is the signal half of those semantics.
//!
//! [`StreamFrame::Resumed`]: mcpmesh_local_api::StreamFrame::Resumed
use Arc;
use ;
use crateepoch_now_i64;
use MeshState;
/// How often the watcher compares its two clocks.
///
/// This is now the daemon's most frequent periodic timer, on a feature whose entire audience is
/// battery-powered laptops — so the cost that matters is not the tick's work (two clock reads and a
/// subtraction) but the WAKEUP, which defeats deep idle and OS timer coalescing. Five seconds keeps
/// that rare while still landing the frame far inside the 30s default idle timeout.
///
/// The frame arrives up to one full TICK after the machine is back, not immediately: tokio's timer
/// deadline is monotonic, so a sleep outstanding across a suspend has its remaining time frozen and
/// still has to elapse once the machine wakes.
const TICK: Duration = from_secs;
/// How far the wall clock must outrun the monotonic clock in ONE tick before we call it a suspend.
///
/// Above [`TICK`] by enough that scheduling jitter, a slow fsync or a busy runtime cannot reach it,
/// and below the 30s default QUIC idle timeout, so the signal arrives for the suspends that
/// actually kill connections. A three-second lid close does not emit — and nothing was lost either.
pub const RESUME_THRESHOLD_SECS: i64 = 10;
/// The loop's clock arithmetic, split out so the PAIRING of the two deltas is pinned by a test.
///
/// The struct below makes transposing them a deliberate mislabel rather than an invisible argument
/// swap, which is most of the protection — but "deliberate" is not "caught", and the loop that
/// builds it is the one part of this module no test drives. Reducing that gap to the two `now()`
/// reads (which cannot be swapped: they are different types) is the difference between a rule and
/// an enforced rule.
///
/// Both arguments are `(monotonic, wall)` pairs, in the same order as the fields they feed.
pub
/// One tick's two clock deltas.
///
/// A struct rather than two `i64` parameters because the whole rule is a SUBTRACTION of one from
/// the other, so transposing them at the call site inverts it — and the only production call site
/// is a loop no test can drive. This gate proved that exactly: with the arguments swapped, the
/// feature is 100% dead in production (the skew goes strongly negative on a real suspend, so
/// nothing ever emits) and the entire workspace stays green. Named fields make the wrong pairing
/// something you have to write out deliberately.
/// The whole detection rule, PURE — given one tick's clock deltas, how long the machine was away,
/// or `None` if this was an ordinary tick.
///
/// `Instant` is `CLOCK_MONOTONIC` on Linux and `CLOCK_UPTIME_RAW` on Apple targets, and **neither
/// advances while the machine is suspended**; the wall clock does. So the three cases separate
/// cleanly:
///
/// | | monotonic Δ | wall Δ | skew |
/// |---|---|---|---|
/// | ordinary tick | ≈ TICK | ≈ TICK | ≈ 0 |
/// | suspend/resume | ≈ TICK | ≫ TICK | **the sleep** |
/// | starved runtime | ≫ TICK | ≫ TICK | ≈ 0 |
///
/// Subtracting the monotonic delta rather than comparing the wall delta to [`TICK`] is what
/// distinguishes the last row from the middle one. A watcher that looked only at the wall clock
/// would emit a false resume every time the machine got busy, and a liveness signal that fires
/// under load is one consumers learn to ignore.
///
/// A BACKWARDS wall-clock step (an NTP correction) yields a negative skew and emits nothing: it is
/// not evidence of a suspend, and reporting `0` seconds away would be a frame asserting a wake that
/// did not happen. A FORWARD step is a different matter — it is indistinguishable from a suspend by
/// this method and DOES emit; see `a_forward_clock_step_is_reported_as_a_suspend_and_that_is_documented`
/// for why that is stated rather than papered over.
pub
/// What the watcher broadcasts. Carries the detection stamp rather than letting the subscriber
/// stamp it, so every subscriber reports the same `at_epoch` for one wake.
/// ONE tick's decision and its effect — split out of the loop so it is reachable from a test.
///
/// The loop itself cannot be: staging a real suspend would mean suspending the machine running the
/// suite. What a test CAN drive is everything downstream of the two clock reads, which is where the
/// mistakes live — a detected suspend that is never broadcast, or one broadcast with the tick's
/// duration in place of the sleep's. Leaving that inside the loop would have meant a module whose
/// only test was [`detect_suspend`], and a `send` no test would miss if it were deleted.
pub
/// Spawn the suspend watcher: tick, compare both clocks, and broadcast a [`ResumeEvent`] on a skew
/// past [`RESUME_THRESHOLD_SECS`].
///
/// `pub` for symmetry with [`spawn_self_net_watch`](super::self_net::spawn_self_net_watch) and so
/// an embedder driving `boot_node`'s pieces itself can install it. It is NOT pub for a test: the
/// loop reads two real clocks and no test drives it — [`tick`] is the seam, reached through
/// [`MeshState::resume_tick_for_test`].