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
//! Autostart: [`connect_or_spawn`], [`connect_or_spawn_with`], [`SpawnError`]
//!
//! A bound-but-not-accepting unix socket still completes `connect(2)` into
//! the kernel backlog, so a bare connect can never tell "nothing is
//! listening" apart from "something is listening and not yet answering".
//! [`connect_or_spawn_with`] therefore never treats a plain `connect`
//! success as readiness — every probe it makes, including the very first
//! one, is a full handshake via [`Client::connect_with_timeout`]. Only
//! [`ConnectError::Connect`] (the OS refusing the connect
//! outright — nothing bound at all) is read as "nothing to disturb, launch a
//! daemon"; [`ConnectError::HandshakeTimeout`] on that first probe means a
//! daemon is already bound and mid-boot, so this launches nothing and just
//! keeps probing.
use fmt;
use Path;
use ExitStatus;
use ;
use crateClient;
use crateConnectError;
/// How long the spawn-and-wait path will keep probing before giving up.
pub const SPAWN_DEADLINE: Duration = from_secs;
/// First retry gap; grows ×1.5 up to [`BACKOFF_CAP`] (spec §6).
pub const BACKOFF_START: Duration = from_millis;
/// Ceiling for the retry gap (spec §6).
pub const BACKOFF_CAP: Duration = from_secs;
/// The per-attempt handshake budget, defined in the (private) `connection`
/// module and surfaced here so the whole spawn budget reads from one place.
pub use crateHANDSHAKE_TIMEOUT;
/// Exit status a `shep daemon` child uses when another daemon already holds
/// this `$SHEP_HOME` (`shep-cli`'s `ExitCode::DaemonAlreadyRunning`).
///
/// This couples the client to the CLI's exit-code taxonomy, deliberately:
/// the client cannot inspect a `BootError` across a process boundary, and an
/// exit status is the only channel a dead child leaves behind. Changing
/// either side without the other reintroduces the race documented on
/// [`SpawnError::DaemonExited`].
pub const DAEMON_ALREADY_RUNNING: i32 = 10;
/// Every timing [`connect_or_spawn`] obeys, injectable so tests do not spend
/// 30 wall-clock seconds proving a probe is bounded.
/// What [`connect_or_spawn`] (or [`connect_or_spawn_with`]) had to do to hand
/// back a connected [`Client`].
///
/// No `#[non_exhaustive]`: `connect_or_spawn`'s algorithm has exactly two
/// steps — probe once, and if that fails, launch then probe on a backoff
/// loop until the deadline — and each of this enum's variants is the success
/// exit of one step. A third variant would need a third step in the
/// algorithm, not just a new label for one of these two, the same closed-set
/// reasoning `CronScheduleError` documents for its own single variant
/// (IR-20). Contrast [`SpawnError`], which does carry the attribute: a
/// failure can originate from either step, or from the launcher, or from
/// exhausting the deadline, and that list is exactly the kind of thing a
/// future revision adds another way to fail to.
/// Why [`connect_or_spawn`] (or [`connect_or_spawn_with`]) failed to hand
/// back a connected [`Client`].
///
/// Growth is expected — library-crate public error type (IR-20).
/// Connects to `socket`, launching a daemon via `launch` if nothing answers
/// the first probe.
///
/// Shorthand for [`connect_or_spawn_with`]`(socket, launch,
/// SpawnOptions::default())`. Production code calls this and never names a
/// timing; [`connect_or_spawn_with`] exists so tests can inject a much
/// shorter deadline.
///
/// # Errors
///
/// See [`connect_or_spawn_with`] — every error variant it can return, this
/// returns unchanged.
pub async
/// As [`connect_or_spawn`], with caller-supplied timing.
///
/// The contract, in order:
/// 1. Probe `socket` with a full handshake
/// ([`Client::connect_with_timeout`], bounded by
/// `opts.handshake_timeout`). Success hands back
/// [`SpawnOutcome::Connected`] without ever calling `launch`.
/// 2. On [`ConnectError::Connect`] — and only that — call `launch` (on a
/// blocking thread; see below). Nothing is listening, so nothing is there
/// to disturb.
/// 3. On [`ConnectError::HandshakeTimeout`] from that *first* probe, do
/// **not** launch: something is already bound and not yet answering, a
/// daemon caught between binding its socket and starting to accept.
/// Enter the probe loop against the same deadline without spawning a
/// second daemon.
/// 4. Any other [`ConnectError`] propagates immediately as
/// [`SpawnError::Connect`] — see that variant's own doc.
/// 5. Loop until `opts.deadline`: check the launched child (if any), sleep
/// the current backoff, then attempt a full handshake with
/// `opts.handshake_timeout`. Success hands back [`SpawnOutcome::Spawned`].
/// A [`ConnectError::ProtocolMismatch`] on one of these loop probes
/// propagates immediately as [`SpawnError::Connect`], same as step 4 — a
/// daemon that answers but refuses on version skew is still a daemon, and
/// there is nothing a longer wait would change. Every other
/// [`ConnectError`] is instead kept for [`SpawnError::DeadlineExpired`].
/// 6. Between attempts, the launched child (if any) is checked with
/// `try_wait()`. An exit carrying [`DAEMON_ALREADY_RUNNING`] is not a
/// failure — another process won the cold-start race, so probing
/// continues. Any other exit is fatal: probing stops immediately and
/// [`SpawnError::DaemonExited`] is returned rather than spending the rest
/// of the deadline on a corpse.
///
/// `launch` runs on a blocking thread ([`tokio::task::spawn_blocking`]),
/// never on the async runtime worker: a real launcher (`shep-cli`'s
/// `launch_daemon`) does a directory create, file creates and a
/// `Command::spawn` — several blocking syscalls with no bound — and running
/// those on the executor would stall every other task in the process for as
/// long as the filesystem takes.
///
/// Not a `# Panics` section, deliberately: this function contains no
/// panicking call of its own. But note that if `launch` itself panics, that
/// panic is resumed rather than converted into a `SpawnError` — a caller
/// that deliberately panics inside a test launcher (as several tests in
/// this crate do, to assert that a code path must never call it) needs that
/// panic to still fail the test, not be swallowed by the `JoinHandle`
/// `spawn_blocking` returns it through.
///
/// # Errors
///
/// - [`SpawnError::Connect`] — the first probe failed for a reason other
/// than "nothing listening" or "listening but not yet answering", or a
/// later loop probe hit [`ConnectError::ProtocolMismatch`].
/// - [`SpawnError::Launch`] — `launch` itself returned an `io::Error`.
/// - [`SpawnError::DaemonExited`] — the launched child exited with a status
/// other than [`DAEMON_ALREADY_RUNNING`] before any probe succeeded.
/// - [`SpawnError::DeadlineExpired`] — no probe succeeded before
/// `opts.deadline`.
pub async
/// The retry loop shared by both entry points: keep probing `socket` until
/// one attempt completes a handshake or `opts.deadline` elapses.
///
/// `child` is `None` only for the "already bound, don't launch" branch
/// ([`ConnectError::HandshakeTimeout`] on the very first probe); otherwise
/// it is the process `connect_or_spawn_with` just launched, owned here for
/// the rest of the call and dropped on every exit path (including the
/// success path — the caller gets a [`Client`], not the `Child`; in
/// production that `Child` *is* the daemon, and this function never
/// `wait()`s it, only `try_wait()`s).
///
/// `seed` carries the `ConnectError` that caused the loop to be entered in
/// the no-launch branch, so a `DeadlineExpired` reached in that branch still
/// names a real probe failure instead of `None`.
async