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
//! The crate's error type.
use std::time::Duration;
/// Errors produced when launching or running a child process.
///
/// Spawn failures, a non-zero exit ([`Exit`](Error::Exit)), timeouts, and IO
/// errors fold into one structured enum, so callers can pattern-match on the
/// failure mode instead of parsing strings.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// The child process could not be started (binary not found, permission
/// denied, …).
#[error("could not start `{program}`: {source}")]
Spawn {
/// The program we tried to launch.
program: String,
/// The underlying OS error.
#[source]
source: std::io::Error,
},
/// The process ran to completion but exited with a non-zero status.
///
/// Produced by the `ensure_success` helpers; the raw exit code is otherwise
/// reported without erroring (a non-zero exit is not inherently a failure).
///
/// Both captured streams are carried **in full**: `git`/`jj` write decisive
/// diagnostics to **stdout** on failure (`CONFLICT (content): …`, `nothing to
/// commit, working tree clean`), so a caller building a user-facing message
/// wants stdout as a fallback when stderr is empty — see
/// [`diagnostic`](Self::diagnostic). Consumers also classify on these fields
/// (grep for a marker, parse a sub-code), so they are never truncated before
/// the caller sees them; only the `Display` message below is bounded.
///
/// The one-line `Display` message appends the **last non-empty line** of
/// [`diagnostic`](Self::diagnostic), capped at 200 bytes — `` `git` exited
/// with code 2: fatal: boom `` — actionable in a log line without dumping
/// multi-KiB streams into it.
#[error("{}", display_exit(program, *code, stdout, stderr))]
Exit {
/// The program that exited non-zero.
program: String,
/// The raw process exit code.
code: i32,
/// Captured standard output, in full. Not shown in the `Display`
/// message; kept for callers that need a stdout-borne failure message.
/// For the raw-bytes helper (`output_bytes`) this is a lossy UTF-8 decode
/// of stdout — the exact bytes remain on the originating `ProcessResult`.
stdout: String,
/// Captured standard error, in full. Only its **last non-empty
/// line** (bounded) appears in the `Display` message — the complete
/// captured text lives here, never poisoning a log line.
stderr: String,
},
/// The process exceeded its configured timeout and was killed.
#[error("`{program}` timed out after {timeout:?}")]
Timeout {
/// The program that timed out.
program: String,
/// The deadline that elapsed.
timeout: Duration,
},
/// A readiness probe ([`RunningProcess::wait_for_line`],
/// [`wait_for_port`](crate::RunningProcess::wait_for_port),
/// [`wait_for`](crate::RunningProcess::wait_for)) did not pass within its
/// deadline — the line never appeared, the port never accepted, the check
/// never returned `true`, or the child exited before becoming ready.
///
/// Distinct from [`Timeout`](Error::Timeout): a probe deadline is separate
/// from the run's own [`Command::timeout`](crate::Command::timeout), and a
/// failed probe does **not** kill the child — the caller decides what
/// happens next.
///
/// [`RunningProcess::wait_for_line`]: crate::RunningProcess::wait_for_line
#[error("`{program}` was not ready after {timeout:?}")]
NotReady {
/// The program that did not become ready.
program: String,
/// The probe deadline that elapsed (or would have — an early child
/// exit fails the probe immediately).
timeout: Duration,
},
/// The process succeeded but its output could not be parsed into the
/// expected shape (e.g. malformed `--json`). Produced by the fallible-parse
/// helpers on [`CliClient`](crate::CliClient).
#[error("failed to parse `{program}` output: {message}")]
Parse {
/// The program whose output failed to parse.
program: String,
/// What went wrong.
message: String,
},
/// A requested resource limit could not be enforced.
///
/// Produced by [`ProcessGroup::with_options`](crate::ProcessGroup::with_options)
/// when a [`ResourceLimits`](crate::ResourceLimits) cap was set but the active
/// mechanism can't honor it — either the platform has no whole-tree container
/// (macOS/BSD, the Linux process-group fallback, the no-containment target), or
/// the OS rejected the request (e.g. a Linux cgroup without controller
/// delegation). An unenforced limit is no protection, so this is raised rather
/// than leaving the tree silently unbounded.
#[cfg(feature = "limits")]
#[error("could not enforce resource limits: {0}")]
ResourceLimit(String),
/// An operation is not supported by the active containment mechanism on
/// this platform.
///
/// Raised by `ProcessGroup::signal` for any signal other than
/// `Signal::Kill` on Windows (Job Objects have no POSIX signals), and by
/// `signal`/`suspend`/`resume` on the no-containment target, which has no
/// process tree to act on.
#[error("operation `{operation}` is not supported on this platform")]
Unsupported {
/// A short description of the operation, e.g. `"signal(Hup)"` or
/// `"suspend"`.
operation: String,
},
/// The run was cancelled via its `CancellationToken`
/// ([`Command::cancel_on`](crate::Command::cancel_on)) and its process
/// tree was killed.
///
/// Asymmetric with [`Timeout`](Error::Timeout) by design: a timeout is
/// *captured* (`ProcessResult::timed_out`) on the non-checking paths,
/// whereas a cancellation is **always** raised on every consuming path.
/// When a run both times out and is cancelled, cancellation wins (it is
/// checked first).
#[cfg(feature = "cancellation")]
#[error("`{program}` was cancelled")]
Cancelled {
/// The program that was cancelled.
program: String,
},
/// An IO error occurred while driving the process (reading a pipe, writing
/// stdin, waiting for exit).
#[error(transparent)]
Io(#[from] std::io::Error),
}
impl Error {
/// The best human-facing message for a failed run, trimmed of surrounding
/// whitespace: captured standard error if it carries text, otherwise the
/// captured standard output (where `git` puts `CONFLICT …` and `git commit`
/// puts `nothing to commit`). Returns `None` when there is no captured output
/// to show — a silent [`Exit`](Error::Exit) (both streams blank) or any
/// non-`Exit` variant ([`Spawn`](Error::Spawn), [`Timeout`](Error::Timeout),
/// [`Parse`](Error::Parse), [`Io`](Error::Io)) — so a caller can fall back to
/// the [`Display`](std::fmt::Display) message. For the raw, untrimmed stream
/// match on [`Exit`](Error::Exit)'s fields directly.
pub fn diagnostic(&self) -> Option<&str> {
match self {
Error::Exit { stdout, stderr, .. } => exit_diagnostic(stdout, stderr),
_ => None,
}
}
/// Whether this is a spawn/IO **"not found"** — the program (or a path it
/// needs) doesn't exist (`ENOENT`). True for [`Spawn`](Error::Spawn) /
/// [`Io`](Error::Io) carrying [`NotFound`](std::io::ErrorKind::NotFound);
/// `false` for every other variant. Lets a caller surface a "command not
/// installed?" hint without reaching into the raw [`std::io::Error`].
pub fn is_not_found(&self) -> bool {
self.io_source()
.is_some_and(|e| e.kind() == std::io::ErrorKind::NotFound)
}
/// Whether this is a spawn/IO **permission denial** (`EACCES`/`EPERM`): the
/// binary isn't executable, or the OS refused the launch. True for
/// [`Spawn`](Error::Spawn) / [`Io`](Error::Io) carrying
/// [`PermissionDenied`](std::io::ErrorKind::PermissionDenied); `false`
/// otherwise.
pub fn is_permission_denied(&self) -> bool {
self.io_source()
.is_some_and(|e| e.kind() == std::io::ErrorKind::PermissionDenied)
}
/// Whether this is a **transient** spawn/IO condition a bare retry can clear
/// — interrupted (`EINTR`), would-block (`EAGAIN`), a busy resource, a
/// text-file-busy executable mid-write (`ETXTBSY`), or a Windows sharing/lock
/// violation. Classifies the [`Spawn`](Error::Spawn)/[`Io`](Error::Io) IO
/// error only.
///
/// **Scope: IO/spawn-level, never exit codes.** Whether a tool's non-zero
/// [`Exit`](Error::Exit) is retryable is domain-specific (a `git` 128 is not
/// generically transient) — that stays the caller's call. [`Timeout`](Error::Timeout)
/// is also excluded by design; compose it if wanted:
/// `e.is_transient() || matches!(e, Error::Timeout { .. })`.
///
/// Pairs with [`Command::retry`](crate::Command::retry):
/// `cmd.retry(3, backoff, |e| e.is_transient())`.
pub fn is_transient(&self) -> bool {
self.io_source().is_some_and(is_transient_io)
}
/// The underlying [`std::io::Error`] for the variants that carry one
/// ([`Spawn`](Error::Spawn), [`Io`](Error::Io)) — the basis for the io-level
/// classifiers above.
fn io_source(&self) -> Option<&std::io::Error> {
match self {
Error::Spawn { source, .. } => Some(source),
Error::Io(source) => Some(source),
_ => None,
}
}
}
/// io-level "retry as-is" conditions: transient kernel/filesystem states a bare
/// retry can clear, distinct from a permanent failure (not-found, permission).
/// Kept deliberately narrow.
fn is_transient_io(e: &std::io::Error) -> bool {
use std::io::ErrorKind;
// `ExecutableFileBusy` is std's stable mapping of `ETXTBSY` — the executable
// is being written; the launch clears once the writer closes it.
if matches!(
e.kind(),
ErrorKind::Interrupted
| ErrorKind::WouldBlock
| ErrorKind::ResourceBusy
| ErrorKind::ExecutableFileBusy
) {
return true;
}
// Windows sharing/lock violations std leaves `Uncategorized`, so match the
// raw codes: ERROR_SHARING_VIOLATION (32) / ERROR_LOCK_VIOLATION (33) — a
// file the launch needs is briefly locked by another process.
#[cfg(windows)]
{
matches!(e.raw_os_error(), Some(32 | 33))
}
#[cfg(not(windows))]
{
false
}
}
/// The stream a failed run's message should quote: stderr when it carries
/// text, else stdout (where `git` puts `CONFLICT …`), else nothing.
fn exit_diagnostic<'a>(stdout: &'a str, stderr: &'a str) -> Option<&'a str> {
[stderr, stdout]
.into_iter()
.map(str::trim)
.find(|text| !text.is_empty())
}
/// `Exit`'s one-line `Display`: program + code, plus a bounded excerpt of the
/// diagnostic — its **last** non-empty line (the actionable one: `git push`
/// ends with `remote: permission denied`, not starts), capped at 200 bytes on
/// a char boundary so a binary-garbage or one-enormous-line stream can never
/// poison a log line.
fn display_exit(program: &str, code: i32, stdout: &str, stderr: &str) -> String {
const TAIL_CAP: usize = 200;
let mut message = format!("`{program}` exited with code {code}");
let tail = exit_diagnostic(stdout, stderr)
.and_then(|text| text.lines().rev().map(str::trim).find(|l| !l.is_empty()));
if let Some(tail) = tail {
message.push_str(": ");
if tail.len() <= TAIL_CAP {
message.push_str(tail);
} else {
let mut cut = TAIL_CAP;
while !tail.is_char_boundary(cut) {
cut -= 1;
}
message.push_str(&tail[..cut]);
message.push('…');
}
}
message
}
/// Crate result alias.
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exit_display_appends_a_bounded_diagnostic_tail() {
// The policy guard (deliberately rewritten when the tail was added):
// the Display stays one actionable line — program + code + the LAST
// non-empty diagnostic line — never the full captured streams.
let err = Error::Exit {
program: "git".into(),
code: 2,
stdout: "CONFLICT (content): merge conflict in a.rs".into(),
stderr: "warning: something\nfatal: boom\n".into(),
};
assert_eq!(err.to_string(), "`git` exited with code 2: fatal: boom");
// stderr blank → the stdout-borne message (git's CONFLICT) is used.
let err = Error::Exit {
program: "git".into(),
code: 2,
stdout: "CONFLICT (content): merge conflict in a.rs".into(),
stderr: " ".into(),
};
assert_eq!(
err.to_string(),
"`git` exited with code 2: CONFLICT (content): merge conflict in a.rs"
);
}
#[test]
fn exit_display_with_blank_streams_has_no_trailing_colon() {
let err = Error::Exit {
program: "git".into(),
code: 2,
stdout: String::new(),
stderr: " \n ".into(),
};
assert_eq!(err.to_string(), "`git` exited with code 2");
}
#[test]
fn exit_display_tail_is_capped_and_never_leaks_the_stream() {
// A multi-KiB single-line stderr must not poison the log line: the
// tail is cut at 200 bytes on a char boundary, with an ellipsis.
let huge = "é".repeat(3000); // 2 bytes per char — exercises the boundary
let err = Error::Exit {
program: "x".into(),
code: 1,
stdout: String::new(),
stderr: huge,
};
let message = err.to_string();
assert!(message.len() < 250, "capped, got {} bytes", message.len());
assert!(message.ends_with('…'), "got: {message}");
assert!(message.starts_with("`x` exited with code 1: éé"));
}
#[test]
fn diagnostic_is_none_for_non_exit_variants() {
let timeout = Error::Timeout {
program: "git".into(),
timeout: Duration::from_secs(1),
};
assert_eq!(timeout.diagnostic(), None);
let unsupported = Error::Unsupported {
operation: "suspend".into(),
};
assert_eq!(unsupported.diagnostic(), None);
let not_ready = Error::NotReady {
program: "server".into(),
timeout: Duration::from_secs(10),
};
assert_eq!(not_ready.diagnostic(), None);
#[cfg(feature = "cancellation")]
{
let cancelled = Error::Cancelled {
program: "job".into(),
};
assert_eq!(cancelled.diagnostic(), None);
}
#[cfg(feature = "limits")]
{
let limit = Error::ResourceLimit("cgroup controller delegation unavailable".into());
assert_eq!(limit.diagnostic(), None);
}
}
#[cfg(feature = "cancellation")]
#[test]
fn cancelled_display_names_the_program() {
let err = Error::Cancelled {
program: "long-job".into(),
};
assert_eq!(err.to_string(), "`long-job` was cancelled");
}
#[test]
fn not_ready_display_names_program_and_timeout() {
let err = Error::NotReady {
program: "my-server".into(),
timeout: Duration::from_secs(10),
};
assert_eq!(err.to_string(), "`my-server` was not ready after 10s");
}
#[test]
fn unsupported_display_names_the_operation() {
let err = Error::Unsupported {
operation: "signal(Hup)".into(),
};
assert_eq!(
err.to_string(),
"operation `signal(Hup)` is not supported on this platform"
);
}
#[cfg(feature = "limits")]
#[test]
fn resource_limit_display_carries_reason() {
let err = Error::ResourceLimit("no cgroup or Job Object available".into());
assert_eq!(
err.to_string(),
"could not enforce resource limits: no cgroup or Job Object available"
);
}
fn spawn(kind: std::io::ErrorKind) -> Error {
Error::Spawn {
program: "x".into(),
source: std::io::Error::from(kind),
}
}
#[test]
fn not_found_and_permission_denied_are_classified_on_spawn_and_io() {
use std::io::ErrorKind::{NotFound, PermissionDenied};
assert!(spawn(NotFound).is_not_found());
assert!(Error::Io(std::io::Error::from(NotFound)).is_not_found());
assert!(!spawn(NotFound).is_permission_denied());
assert!(spawn(PermissionDenied).is_permission_denied());
assert!(!spawn(PermissionDenied).is_not_found());
// Neither permanent failure counts as transient.
assert!(!spawn(NotFound).is_transient());
assert!(!spawn(PermissionDenied).is_transient());
}
#[test]
fn transient_kinds_are_classified() {
// Includes ExecutableFileBusy built straight from the kind (no raw
// errno) — the classifier must recognize it by `ErrorKind` alone.
for kind in [
std::io::ErrorKind::Interrupted,
std::io::ErrorKind::WouldBlock,
std::io::ErrorKind::ResourceBusy,
std::io::ErrorKind::ExecutableFileBusy,
] {
assert!(spawn(kind).is_transient(), "{kind:?} should be transient");
assert!(
Error::Io(std::io::Error::from(kind)).is_transient(),
"{kind:?} (Io) should be transient"
);
}
}
#[cfg(unix)]
#[test]
fn etxtbsy_is_transient_on_unix() {
let err = Error::Spawn {
program: "busy".into(),
source: std::io::Error::from_raw_os_error(libc::ETXTBSY),
};
assert!(err.is_transient());
assert!(!err.is_not_found() && !err.is_permission_denied());
}
#[cfg(windows)]
#[test]
fn sharing_and_lock_violations_are_transient_on_windows() {
for code in [32, 33] {
let err = Error::Spawn {
program: "locked".into(),
source: std::io::Error::from_raw_os_error(code),
};
assert!(
err.is_transient(),
"raw os error {code} should be transient"
);
}
}
#[test]
fn classifiers_are_false_for_non_io_variants() {
// A tool's non-zero exit is never an io-level classification (its
// retryability is the caller's domain), and Timeout is excluded too.
let exit = Error::Exit {
program: "git".into(),
code: 128,
stdout: String::new(),
stderr: "could not resolve host".into(),
};
assert!(!exit.is_not_found() && !exit.is_permission_denied() && !exit.is_transient());
let timeout = Error::Timeout {
program: "x".into(),
timeout: Duration::from_secs(1),
};
assert!(
!timeout.is_transient(),
"Timeout is excluded from is_transient by design"
);
}
}