Skip to main content

oxdock_ssh_plugin/
diagnostics.rs

1//! SSH user-facing errors behind single constructors with stable keys.
2//!
3//! This module owns every SSH template and key (`ssh.*`). It builds on
4//! the generic [`oxdock_diagnostics::Diagnostic`] transport but owns its
5//! wording: changing SSH text happens here, never at the call sites.
6//! Call sites pass structured data only, never `format!` strings.
7//! Lock-poison and cancellation failures are parameterless (the wording
8//! is fixed); I/O failures take the concrete `std::io::Error`; worker
9//! and function labels ride as structured fields with rendering here.
10
11use oxdock_diagnostics::Diagnostic;
12use oxdock_parser::{HANDLE_TYPE_NAME, KEYWORD_ASYNC, KEYWORD_LET};
13
14/// Structured field keys. Single spelling for the whole module:
15/// constructors reference these, never literals, so a key rename is
16/// one edit.
17const FIELD_FUNC: &str = "func";
18const FIELD_WORKER: &str = "worker";
19const FIELD_NAME: &str = "name";
20const FIELD_RAW: &str = "raw";
21const FIELD_PATH: &str = "path";
22const FIELD_ALGORITHM: &str = "algorithm";
23const FIELD_MODE: &str = "mode";
24const FIELD_ERR: &str = "err";
25const FIELD_CODE: &str = "code";
26const FIELD_OPTION: &str = "option";
27const FIELD_GOT: &str = "got";
28
29/// `key_path` escapes the workspace guard. Takes the invoking func
30/// and the raw path; quoting lives here.
31pub fn key_path_escapes(func: &str, raw: &str) -> Diagnostic {
32    let message = format!("{func} key_path escapes the workspace: {raw}");
33    Diagnostic::new(
34        "ssh.key.escapes_workspace",
35        message,
36        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_RAW, raw)]),
37    )
38}
39
40/// Pre-existing host key is not Ed25519. Takes the invoking func,
41/// the display path, and the algorithm (`Debug` rendering lives
42/// here).
43pub fn host_key_must_be_ed25519(
44    func: &str,
45    path: &str,
46    algorithm: &impl std::fmt::Debug,
47) -> Diagnostic {
48    let algorithm_text = format!("{algorithm:?}");
49    let message = format!("{func} host key {path} must be Ed25519, found {algorithm_text}");
50    Diagnostic::new(
51        "ssh.key.must_be_ed25519",
52        message,
53        Diagnostic::make_fields(&[
54            (FIELD_FUNC, func),
55            (FIELD_PATH, path),
56            (FIELD_ALGORITHM, algorithm_text.as_str()),
57        ]),
58    )
59}
60
61/// Pre-existing host key file has unsafe permissions. Takes the
62/// invoking func, the display path, and the raw mode; octal
63/// rendering lives here.
64pub fn host_key_insecure_permissions(func: &str, path: &str, mode: u32) -> Diagnostic {
65    let mode_text = format!("0{mode:o}");
66    let message = format!(
67        "{func} insecure permissions on host key file {path}: expected 0600, got {mode_text}"
68    );
69    Diagnostic::new(
70        "ssh.key.insecure_permissions",
71        message,
72        Diagnostic::make_fields(&[
73            (FIELD_FUNC, func),
74            (FIELD_PATH, path),
75            (FIELD_MODE, mode_text.as_str()),
76        ]),
77    )
78}
79
80/// Workspace resolver for the host key cannot open. Takes the
81/// invoking func and the underlying failure; the outer message
82/// stays bare while the cause rides a chained source, so
83/// `{err:#}` keeps the legacy `": "` join. Call sites stay
84/// `format!`-free.
85pub fn host_key_resolver_failed(func: &str, reason: &anyhow::Error) -> Diagnostic {
86    let reason_text = format!("{reason:#}");
87    Diagnostic::new(
88        "ssh.key.resolver_failed",
89        format!("{func} cannot open the workspace resolver"),
90        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
91    )
92    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
93}
94
95/// Ed25519 host key generation failed. Takes the concrete failure
96/// by value; the outer message stays bare while the cause rides a
97/// chained source.
98pub fn host_key_generate_failed(
99    reason: impl std::error::Error + Send + Sync + 'static,
100) -> Diagnostic {
101    let reason_text = reason.to_string();
102    Diagnostic::new(
103        "ssh.key.generate_failed",
104        "generate Ed25519 host key".to_string(),
105        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
106    )
107    .with_source(std::sync::Arc::new(reason))
108}
109
110/// Ed25519 host key PEM encoding failed. Takes the concrete failure
111/// by value; bare outer message plus chained source.
112pub fn host_key_encode_failed(
113    reason: impl std::error::Error + Send + Sync + 'static,
114) -> Diagnostic {
115    let reason_text = reason.to_string();
116    Diagnostic::new(
117        "ssh.key.encode_failed",
118        "encode Ed25519 host key".to_string(),
119        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
120    )
121    .with_source(std::sync::Arc::new(reason))
122}
123
124/// `key_path` parent guard cannot anchor the workspace root. Takes
125/// the invoking func and the underlying failure; bare outer message
126/// plus chained source.
127pub fn key_guard_failed(func: &str, reason: &anyhow::Error) -> Diagnostic {
128    let reason_text = format!("{reason:#}");
129    Diagnostic::new(
130        "ssh.key.guard_failed",
131        format!("{func} cannot guard the workspace root"),
132        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
133    )
134    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
135}
136
137/// Host key file metadata cannot be read. Takes the display path
138/// and the underlying failure; bare outer message plus chained
139/// source.
140pub fn host_key_stat_failed(path: &str, reason: &anyhow::Error) -> Diagnostic {
141    let reason_text = format!("{reason:#}");
142    Diagnostic::new(
143        "ssh.key.stat_failed",
144        format!("cannot stat host key {path}"),
145        Diagnostic::make_fields(&[(FIELD_PATH, path), (FIELD_ERR, &reason_text)]),
146    )
147    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
148}
149
150/// Host key file bytes cannot be read. Takes the invoking func, the
151/// display path, and the underlying failure; bare outer message
152/// plus chained source.
153pub fn host_key_read_failed(func: &str, path: &str, reason: &anyhow::Error) -> Diagnostic {
154    let reason_text = format!("{reason:#}");
155    Diagnostic::new(
156        "ssh.key.read_failed",
157        format!("{func} cannot read host key {path}"),
158        Diagnostic::make_fields(&[
159            (FIELD_FUNC, func),
160            (FIELD_PATH, path),
161            (FIELD_ERR, &reason_text),
162        ]),
163    )
164    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
165}
166
167/// Host key bytes do not parse as OpenSSH. Takes the invoking func,
168/// the display path, and the concrete failure by value; bare outer
169/// message plus chained source.
170pub fn host_key_parse_failed(
171    func: &str,
172    path: &str,
173    reason: impl std::error::Error + Send + Sync + 'static,
174) -> Diagnostic {
175    let reason_text = reason.to_string();
176    Diagnostic::new(
177        "ssh.key.parse_failed",
178        format!("{func} cannot parse host key {path}"),
179        Diagnostic::make_fields(&[
180            (FIELD_FUNC, func),
181            (FIELD_PATH, path),
182            (FIELD_ERR, &reason_text),
183        ]),
184    )
185    .with_source(std::sync::Arc::new(reason))
186}
187
188/// Host key file write failed past the first-start race. Takes the
189/// underlying failure verbatim: the file layer already names the
190/// path, so the message passes through unchanged.
191pub fn host_key_write_failed(reason: &anyhow::Error) -> Diagnostic {
192    let reason_text = format!("{reason:#}");
193    Diagnostic::new(
194        "ssh.key.write_failed",
195        reason_text.clone(),
196        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
197    )
198}
199
200/// Guarded join of a `key_path` escapes the workspace. Takes the
201/// invoking func, the raw path, and the guard failure; the outer
202/// message names the escape while the resolved-path detail rides a
203/// chained source, so it keeps its own key.
204pub fn key_path_join_failed(func: &str, raw: &str, reason: &anyhow::Error) -> Diagnostic {
205    let reason_text = format!("{reason:#}");
206    Diagnostic::new(
207        "ssh.key.join_failed",
208        format!("{func} key_path escapes the workspace: {raw}"),
209        Diagnostic::make_fields(&[
210            (FIELD_FUNC, func),
211            (FIELD_RAW, raw),
212            (FIELD_ERR, &reason_text),
213        ]),
214    )
215    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
216}
217
218/// SSH pump output lock poisoned. Parameterless: the wording is
219/// fixed.
220pub fn pump_output_lock_poisoned() -> Diagnostic {
221    Diagnostic::new(
222        "ssh.pump.output_lock_poisoned",
223        "SSH pump output lock poisoned".to_string(),
224        Diagnostic::make_fields(&[]),
225    )
226}
227
228/// SSH pump input pipe read failed. Takes the I/O failure;
229/// rendering lives here.
230pub fn pump_input_read_failed(err: &std::io::Error) -> Diagnostic {
231    let err_text = err.to_string();
232    let message = format!("SSH pump input pipe read failed: {err_text}");
233    Diagnostic::new(
234        "ssh.pump.input_read_failed",
235        message,
236        Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
237    )
238}
239
240/// SSH pump worker panicked. Takes the worker label; rendering
241/// lives here.
242pub fn pump_worker_panicked(worker: &str) -> Diagnostic {
243    let message = format!("SSH pump {worker} panicked");
244    Diagnostic::new(
245        "ssh.pump.worker_panicked",
246        message,
247        Diagnostic::make_fields(&[(FIELD_WORKER, worker)]),
248    )
249}
250
251/// SSH pump interrupted by cancellation. Parameterless: the wording
252/// is fixed.
253pub fn pump_interrupted_by_cancellation() -> Diagnostic {
254    Diagnostic::new(
255        "ssh.pump.interrupted_by_cancellation",
256        "SSH pump interrupted by cancellation".to_string(),
257        Diagnostic::make_fields(&[]),
258    )
259}
260
261/// `SSH_PUMP` source pipe read failed. Takes the I/O failure;
262/// rendering lives here.
263pub fn pump_source_read_failed(err: &std::io::Error) -> Diagnostic {
264    let err_text = err.to_string();
265    let message = format!("SSH_PUMP source pipe read failed: {err_text}");
266    Diagnostic::new(
267        "ssh.pump.source_read_failed",
268        message,
269        Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
270    )
271}
272
273/// `SSH_PUMP` target lock poisoned. Parameterless: the wording is
274/// fixed.
275pub fn pump_target_lock_poisoned() -> Diagnostic {
276    Diagnostic::new(
277        "ssh.pump.target_lock_poisoned",
278        "SSH_PUMP target lock poisoned".to_string(),
279        Diagnostic::make_fields(&[]),
280    )
281}
282
283/// Pipe-to-sink input read failed. Takes the caller label plus the
284/// I/O failure; rendering lives here.
285pub fn pump_func_input_read_failed(func: &str, err: &std::io::Error) -> Diagnostic {
286    let err_text = err.to_string();
287    let message = format!("{func} input pipe read failed: {err_text}");
288    Diagnostic::new(
289        "ssh.pump.func_input_read_failed",
290        message,
291        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, err_text.as_str())]),
292    )
293}
294
295/// SSH pump output pipe write failed. Takes the concrete I/O
296/// failure by value; bare outer message plus chained source so
297/// `{err:#}` keeps the legacy `": "` join.
298pub fn pump_output_write_failed(
299    reason: impl std::error::Error + Send + Sync + 'static,
300) -> Diagnostic {
301    let reason_text = reason.to_string();
302    Diagnostic::new(
303        "ssh.pump.output_write_failed",
304        "SSH pump output pipe write failed".to_string(),
305        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
306    )
307    .with_source(std::sync::Arc::new(reason))
308}
309
310/// SSH pump output pipe flush failed. Takes the concrete I/O
311/// failure by value; bare outer plus chained source.
312pub fn pump_output_flush_failed(
313    reason: impl std::error::Error + Send + Sync + 'static,
314) -> Diagnostic {
315    let reason_text = reason.to_string();
316    Diagnostic::new(
317        "ssh.pump.output_flush_failed",
318        "SSH pump output pipe flush failed".to_string(),
319        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
320    )
321    .with_source(std::sync::Arc::new(reason))
322}
323
324/// SSH pump cannot borrow the input pipe. Takes the underlying
325/// typed failure by value; bare outer plus chained source so the
326/// inner key survives traversal.
327pub fn pump_borrow_input_failed(reason: Diagnostic) -> Diagnostic {
328    let reason_text = reason.to_string();
329    Diagnostic::new(
330        "ssh.pump.borrow_input_failed",
331        "SSH pump cannot borrow the input pipe".to_string(),
332        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
333    )
334    .with_source(std::sync::Arc::new(reason))
335}
336
337/// SSH pump cannot borrow the output pipe. Takes the underlying
338/// typed failure by value; bare outer plus chained source.
339pub fn pump_borrow_output_failed(reason: Diagnostic) -> Diagnostic {
340    let reason_text = reason.to_string();
341    Diagnostic::new(
342        "ssh.pump.borrow_output_failed",
343        "SSH pump cannot borrow the output pipe".to_string(),
344        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
345    )
346    .with_source(std::sync::Arc::new(reason))
347}
348
349/// SSH pump wait runtime failed to build. Takes the concrete
350/// failure by value; bare outer plus chained source.
351pub fn pump_wait_runtime_failed(
352    reason: impl std::error::Error + Send + Sync + 'static,
353) -> Diagnostic {
354    let reason_text = reason.to_string();
355    Diagnostic::new(
356        "ssh.pump.wait_runtime_failed",
357        "SSH pump wait runtime failed".to_string(),
358        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
359    )
360    .with_source(std::sync::Arc::new(reason))
361}
362
363/// `SSH_PUMP` cannot borrow the source pipe. Takes the underlying
364/// typed failure by value; bare outer plus chained source.
365pub fn pump_borrow_source_failed(reason: Diagnostic) -> Diagnostic {
366    let reason_text = reason.to_string();
367    Diagnostic::new(
368        "ssh.pump.borrow_source_failed",
369        "SSH_PUMP cannot borrow the source pipe".to_string(),
370        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
371    )
372    .with_source(std::sync::Arc::new(reason))
373}
374
375/// `SSH_PUMP` cannot borrow the target pipe. Takes the underlying
376/// typed failure by value; bare outer plus chained source.
377pub fn pump_borrow_target_failed(reason: Diagnostic) -> Diagnostic {
378    let reason_text = reason.to_string();
379    Diagnostic::new(
380        "ssh.pump.borrow_target_failed",
381        "SSH_PUMP cannot borrow the target pipe".to_string(),
382        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
383    )
384    .with_source(std::sync::Arc::new(reason))
385}
386
387/// `SSH_PUMP` target pipe write failed. Takes the concrete failure
388/// by value; bare outer plus chained source.
389pub fn pump_target_write_failed(
390    reason: impl std::error::Error + Send + Sync + 'static,
391) -> Diagnostic {
392    let reason_text = reason.to_string();
393    Diagnostic::new(
394        "ssh.pump.target_write_failed",
395        "SSH_PUMP target pipe write failed".to_string(),
396        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
397    )
398    .with_source(std::sync::Arc::new(reason))
399}
400
401/// `SSH_PUMP` target pipe flush failed. Takes the concrete failure
402/// by value; bare outer plus chained source.
403pub fn pump_target_flush_failed(
404    reason: impl std::error::Error + Send + Sync + 'static,
405) -> Diagnostic {
406    let reason_text = reason.to_string();
407    Diagnostic::new(
408        "ssh.pump.target_flush_failed",
409        "SSH_PUMP target pipe flush failed".to_string(),
410        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
411    )
412    .with_source(std::sync::Arc::new(reason))
413}
414
415/// Authentication rejected. Takes the invoking func (`SSH_EXEC` /
416/// `SSH_CONNECT`: same failure mode, one key); rendering lives
417/// here.
418pub fn authentication_rejected(func: &str) -> Diagnostic {
419    let message = format!("{func} authentication rejected");
420    Diagnostic::new(
421        "ssh.auth.rejected",
422        message,
423        Diagnostic::make_fields(&[(FIELD_FUNC, func)]),
424    )
425}
426
427/// Dial failed. Takes the invoking func (`SSH_EXEC` /
428/// `SSH_CONNECT`: same failure mode, one key) plus the underlying
429/// failure; the russh connect boundary surfaces `anyhow::Error`,
430/// so the outer stays bare while the cause rides an I/O-chained
431/// source. Call sites stay `format!`-free.
432pub fn dial_failed(func: &str, reason: &anyhow::Error) -> Diagnostic {
433    let reason_text = format!("{reason:#}");
434    Diagnostic::new(
435        "ssh.dial_failed",
436        format!("{func} dial failed"),
437        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
438    )
439    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
440}
441
442/// Authentication exchange failed. Takes the invoking func
443/// (`SSH_EXEC` / `SSH_CONNECT`: same failure mode, one key) plus
444/// the concrete failure by value; bare outer plus chained source.
445pub fn auth_exchange_failed(
446    func: &str,
447    reason: impl std::error::Error + Send + Sync + 'static,
448) -> Diagnostic {
449    let reason_text = reason.to_string();
450    Diagnostic::new(
451        "ssh.auth.exchange_failed",
452        format!("{func} authentication exchange failed"),
453        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
454    )
455    .with_source(std::sync::Arc::new(reason))
456}
457
458/// Channel open failed. Takes the invoking func (`SSH_EXEC` /
459/// `SSH_CONNECT`: same failure mode, one key) plus the concrete
460/// failure by value; bare outer plus chained source.
461pub fn channel_open_failed(
462    func: &str,
463    reason: impl std::error::Error + Send + Sync + 'static,
464) -> Diagnostic {
465    let reason_text = reason.to_string();
466    Diagnostic::new(
467        "ssh.channel.open_failed",
468        format!("{func} channel open failed"),
469        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
470    )
471    .with_source(std::sync::Arc::new(reason))
472}
473
474/// `SSH_EXEC` exec request failed. Takes the concrete failure by
475/// value; bare outer plus chained source.
476pub fn exec_request_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
477    let reason_text = reason.to_string();
478    Diagnostic::new(
479        "ssh.exec.request_failed",
480        "SSH_EXEC exec request failed".to_string(),
481        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
482    )
483    .with_source(std::sync::Arc::new(reason))
484}
485
486/// `SSH_CONNECT` shell request failed. Takes the concrete failure
487/// by value; bare outer plus chained source.
488pub fn shell_request_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
489    let reason_text = reason.to_string();
490    Diagnostic::new(
491        "ssh.connect.shell_request_failed",
492        "SSH_CONNECT shell request failed".to_string(),
493        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
494    )
495    .with_source(std::sync::Arc::new(reason))
496}
497
498/// Listener socket cannot go non-blocking for the runtime. Takes
499/// the concrete failure by value; bare outer plus chained source.
500/// The legacy wording stays byte-identical (no func prefix).
501pub fn listener_prepare_failed(
502    reason: impl std::error::Error + Send + Sync + 'static,
503) -> Diagnostic {
504    let reason_text = reason.to_string();
505    Diagnostic::new(
506        "ssh.serve.listener_prepare_failed",
507        "prepare listener for the runtime".to_string(),
508        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
509    )
510    .with_source(std::sync::Arc::new(reason))
511}
512
513/// Listener socket cannot hand to the runtime. Takes the concrete
514/// failure by value; bare outer plus chained source.
515pub fn listener_handoff_failed(
516    reason: impl std::error::Error + Send + Sync + 'static,
517) -> Diagnostic {
518    let reason_text = reason.to_string();
519    Diagnostic::new(
520        "ssh.serve.listener_handoff_failed",
521        "hand listener to the runtime".to_string(),
522        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
523    )
524    .with_source(std::sync::Arc::new(reason))
525}
526
527/// SSH listener failed. Takes the concrete failure by value; bare
528/// outer plus chained source.
529pub fn listener_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
530    let reason_text = reason.to_string();
531    Diagnostic::new(
532        "ssh.serve.listener_failed",
533        "SSH listener failed".to_string(),
534        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
535    )
536    .with_source(std::sync::Arc::new(reason))
537}
538
539/// SSH client runtime failed to build. Takes the concrete failure
540/// by value; bare outer plus chained source.
541pub fn client_runtime_build_failed(
542    reason: impl std::error::Error + Send + Sync + 'static,
543) -> Diagnostic {
544    let reason_text = reason.to_string();
545    Diagnostic::new(
546        "ssh.client_runtime.build_failed",
547        "build SSH client runtime".to_string(),
548        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
549    )
550    .with_source(std::sync::Arc::new(reason))
551}
552
553/// Session pump ends already taken by `SSH_SEND_EXIT_STATUS`.
554/// Parameterless: the wording is fixed.
555pub fn session_pump_already_finished() -> Diagnostic {
556    Diagnostic::new(
557        "ssh.session.pump_already_finished",
558        "SSH session pump already finished".to_string(),
559        Diagnostic::make_fields(&[]),
560    )
561}
562
563/// Exit-status queue saturated under backpressure. Parameterless:
564/// the wording is fixed.
565pub fn session_wire_queue_saturated() -> Diagnostic {
566    Diagnostic::new(
567        "ssh.session.wire_queue_saturated",
568        "SSH session wire queue saturated under backpressure".to_string(),
569        Diagnostic::make_fields(&[]),
570    )
571}
572
573/// Exit-status queue receiver gone. Parameterless: the wording is
574/// fixed.
575pub fn session_wire_writer_gone() -> Diagnostic {
576    Diagnostic::new(
577        "ssh.session.wire_writer_gone",
578        "SSH session wire writer is gone".to_string(),
579        Diagnostic::make_fields(&[]),
580    )
581}
582
583/// Second pump on the same session. Parameterless: the wording is
584/// fixed.
585pub fn session_already_pumped() -> Diagnostic {
586    Diagnostic::new(
587        "ssh.session.already_pumped",
588        "SSH session already pumped".to_string(),
589        Diagnostic::make_fields(&[]),
590    )
591}
592
593/// Wire data for an unknown channel. Parameterless: the wording is
594/// fixed.
595pub fn channel_data_unknown() -> Diagnostic {
596    Diagnostic::new(
597        "ssh.channel.data_unknown",
598        "data for unknown channel".to_string(),
599        Diagnostic::make_fields(&[]),
600    )
601}
602
603/// Pump side vanished mid-send. Parameterless: the wording is fixed.
604pub fn channel_pump_gone() -> Diagnostic {
605    Diagnostic::new(
606        "ssh.channel.pump_gone",
607        "pump went away".to_string(),
608        Diagnostic::make_fields(&[]),
609    )
610}
611
612/// Wire side vanished during EOF close. Parameterless: the wording
613/// is fixed.
614pub fn channel_wire_gone_eof_close() -> Diagnostic {
615    Diagnostic::new(
616        "ssh.channel.wire_gone_eof_close",
617        "wire gone during EOF close".to_string(),
618        Diagnostic::make_fields(&[]),
619    )
620}
621
622/// Wire request for an unknown channel. Parameterless: the wording
623/// is fixed.
624pub fn channel_request_unknown() -> Diagnostic {
625    Diagnostic::new(
626        "ssh.channel.request_unknown",
627        "request for unknown channel".to_string(),
628        Diagnostic::make_fields(&[]),
629    )
630}
631
632/// Channel announced twice. Parameterless: the wording is fixed.
633pub fn channel_already_announced() -> Diagnostic {
634    Diagnostic::new(
635        "ssh.channel.already_announced",
636        "channel already announced".to_string(),
637        Diagnostic::make_fields(&[]),
638    )
639}
640
641/// Child stream read failed. Takes the stream name plus the I/O
642/// failure; rendering lives here.
643pub fn run_child_read_failed(name: &str, err: &std::io::Error) -> Diagnostic {
644    let err_text = err.to_string();
645    let message = format!("SSH run child {name} read failed: {err_text}");
646    Diagnostic::new(
647        "ssh.run.child_read_failed",
648        message,
649        Diagnostic::make_fields(&[(FIELD_NAME, name), (FIELD_ERR, err_text.as_str())]),
650    )
651}
652
653/// SSH run output pipe write failed. Takes the concrete failure by
654/// value; bare outer plus chained source.
655pub fn run_output_write_failed(
656    reason: impl std::error::Error + Send + Sync + 'static,
657) -> Diagnostic {
658    let reason_text = reason.to_string();
659    Diagnostic::new(
660        "ssh.run.output_write_failed",
661        "SSH run output pipe write failed".to_string(),
662        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
663    )
664    .with_source(std::sync::Arc::new(reason))
665}
666
667/// SSH run output pipe flush failed. Takes the concrete failure by
668/// value; bare outer plus chained source.
669pub fn run_output_flush_failed(
670    reason: impl std::error::Error + Send + Sync + 'static,
671) -> Diagnostic {
672    let reason_text = reason.to_string();
673    Diagnostic::new(
674        "ssh.run.output_flush_failed",
675        "SSH run output pipe flush failed".to_string(),
676        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
677    )
678    .with_source(std::sync::Arc::new(reason))
679}
680
681/// SSH run output lock poisoned. Parameterless: the wording is
682/// fixed.
683pub fn run_output_lock_poisoned() -> Diagnostic {
684    Diagnostic::new(
685        "ssh.run.output_lock_poisoned",
686        "SSH run output lock poisoned".to_string(),
687        Diagnostic::make_fields(&[]),
688    )
689}
690
691/// `SSH_RUN` with no argv. Parameterless: the wording is fixed.
692pub fn run_needs_program() -> Diagnostic {
693    Diagnostic::new(
694        "ssh.run.needs_program",
695        "SSH_RUN needs at least a program".to_string(),
696        Diagnostic::make_fields(&[]),
697    )
698}
699
700/// Spawned child lost its stderr handle. Parameterless: the wording
701/// is fixed.
702pub fn run_spawn_lost_stderr() -> Diagnostic {
703    Diagnostic::new(
704        "ssh.run.spawn_lost_stderr",
705        "SSH run spawn lost stderr".to_string(),
706        Diagnostic::make_fields(&[]),
707    )
708}
709
710/// SSH run interrupted by cancellation. Parameterless: the wording
711/// is fixed.
712pub fn run_interrupted_by_cancellation() -> Diagnostic {
713    Diagnostic::new(
714        "ssh.run.interrupted_by_cancellation",
715        "SSH run interrupted by cancellation".to_string(),
716        Diagnostic::make_fields(&[]),
717    )
718}
719
720/// SSH run cannot borrow the input pipe. Takes the underlying
721/// typed failure by value; bare outer plus chained source.
722pub fn run_borrow_input_failed(reason: Diagnostic) -> Diagnostic {
723    let reason_text = reason.to_string();
724    Diagnostic::new(
725        "ssh.run.borrow_input_failed",
726        "SSH run cannot borrow the input pipe".to_string(),
727        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
728    )
729    .with_source(std::sync::Arc::new(reason))
730}
731
732/// SSH run cannot borrow the output pipe. Takes the underlying
733/// typed failure by value; bare outer plus chained source.
734pub fn run_borrow_output_failed(reason: Diagnostic) -> Diagnostic {
735    let reason_text = reason.to_string();
736    Diagnostic::new(
737        "ssh.run.borrow_output_failed",
738        "SSH run cannot borrow the output pipe".to_string(),
739        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
740    )
741    .with_source(std::sync::Arc::new(reason))
742}
743
744/// SSH run cannot borrow the error pipe. Takes the underlying
745/// typed failure by value; bare outer plus chained source.
746pub fn run_borrow_error_failed(reason: Diagnostic) -> Diagnostic {
747    let reason_text = reason.to_string();
748    Diagnostic::new(
749        "ssh.run.borrow_error_failed",
750        "SSH run cannot borrow the error pipe".to_string(),
751        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
752    )
753    .with_source(std::sync::Arc::new(reason))
754}
755
756/// SSH run spawn failed. Takes the underlying typed failure by
757/// value; bare outer plus chained source.
758pub fn run_spawn_failed(reason: Diagnostic) -> Diagnostic {
759    let reason_text = reason.to_string();
760    Diagnostic::new(
761        "ssh.run.spawn_failed",
762        "SSH run spawn failed".to_string(),
763        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
764    )
765    .with_source(std::sync::Arc::new(reason))
766}
767
768/// SSH pty output lock poisoned. Parameterless: the wording is
769/// fixed.
770pub fn pty_output_lock_poisoned() -> Diagnostic {
771    Diagnostic::new(
772        "ssh.pty.output_lock_poisoned",
773        "SSH pty output lock poisoned".to_string(),
774        Diagnostic::make_fields(&[]),
775    )
776}
777
778/// SSH pty run interrupted by cancellation. Parameterless: the
779/// wording is fixed.
780pub fn pty_interrupted_by_cancellation() -> Diagnostic {
781    Diagnostic::new(
782        "ssh.pty.interrupted_by_cancellation",
783        "SSH pty run interrupted by cancellation".to_string(),
784        Diagnostic::make_fields(&[]),
785    )
786}
787
788/// SSH pty cannot borrow the input pipe. Takes the underlying
789/// typed failure by value; bare outer plus chained source.
790pub fn pty_borrow_input_failed(reason: Diagnostic) -> Diagnostic {
791    let reason_text = reason.to_string();
792    Diagnostic::new(
793        "ssh.pty.borrow_input_failed",
794        "SSH pty cannot borrow the input pipe".to_string(),
795        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
796    )
797    .with_source(std::sync::Arc::new(reason))
798}
799
800/// SSH pty cannot borrow the output pipe. Takes the underlying
801/// typed failure by value; bare outer plus chained source.
802pub fn pty_borrow_output_failed(reason: Diagnostic) -> Diagnostic {
803    let reason_text = reason.to_string();
804    Diagnostic::new(
805        "ssh.pty.borrow_output_failed",
806        "SSH pty cannot borrow the output pipe".to_string(),
807        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
808    )
809    .with_source(std::sync::Arc::new(reason))
810}
811
812/// SSH pty session init write failed. Takes the concrete failure
813/// by value; bare outer plus chained source.
814pub fn pty_session_init_write_failed(
815    reason: impl std::error::Error + Send + Sync + 'static,
816) -> Diagnostic {
817    let reason_text = reason.to_string();
818    Diagnostic::new(
819        "ssh.pty.session_init_write_failed",
820        "SSH pty session init write failed".to_string(),
821        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
822    )
823    .with_source(std::sync::Arc::new(reason))
824}
825
826/// SSH pty session init flush failed. Takes the concrete failure
827/// by value; bare outer plus chained source.
828pub fn pty_session_init_flush_failed(
829    reason: impl std::error::Error + Send + Sync + 'static,
830) -> Diagnostic {
831    let reason_text = reason.to_string();
832    Diagnostic::new(
833        "ssh.pty.session_init_flush_failed",
834        "SSH pty session init flush failed".to_string(),
835        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
836    )
837    .with_source(std::sync::Arc::new(reason))
838}
839
840/// Dequeue wait cancelled. Takes the invoking func (`SSH_DEQUEUE` /
841/// `SSH_ACCEPT`: same failure mode, one key); rendering lives here.
842pub fn dequeue_task_cancelled(func: &str) -> Diagnostic {
843    let message = format!("{func}: task cancelled");
844    Diagnostic::new(
845        "ssh.dequeue.task_cancelled",
846        message,
847        Diagnostic::make_fields(&[(FIELD_FUNC, func)]),
848    )
849}
850
851/// Queue shut down while waiting. Takes the invoking func (same
852/// failure mode, one key); rendering lives here.
853pub fn dequeue_server_closed(func: &str) -> Diagnostic {
854    let message = format!("{func}: server is closed");
855    Diagnostic::new(
856        "ssh.dequeue.server_closed",
857        message,
858        Diagnostic::make_fields(&[(FIELD_FUNC, func)]),
859    )
860}
861
862/// `SSH_DEQUEUE` outside `ASYNC`. Parameterless: the wrap hint lives
863/// here.
864pub fn dequeue_requires_async() -> Diagnostic {
865    Diagnostic::new(
866        "ssh.dequeue.requires_async",
867        format!(
868            "SSH_DEQUEUE requires {KEYWORD_ASYNC}: wrap it as {KEYWORD_LET} $t: {HANDLE_TYPE_NAME}<T> = {KEYWORD_ASYNC} {{ SSH_DEQUEUE($server.server) }}"
869        ),
870        Diagnostic::make_fields(&[]),
871    )
872}
873
874/// `SSH_ACCEPT` outside `ASYNC`. Parameterless: the wrap hint lives
875/// here.
876pub fn accept_requires_async() -> Diagnostic {
877    Diagnostic::new(
878        "ssh.accept.requires_async",
879        format!(
880            "SSH_ACCEPT requires {KEYWORD_ASYNC}: wrap it as {KEYWORD_LET} $t: {HANDLE_TYPE_NAME}<T> = {KEYWORD_ASYNC} {{ SSH_ACCEPT($server, $in, $out) }}"
881        ),
882        Diagnostic::make_fields(&[]),
883    )
884}
885
886/// `SSH_PUMP_CHANNEL` outside `ASYNC`. Parameterless: the wording is
887/// fixed.
888pub fn pump_channel_requires_async() -> Diagnostic {
889    Diagnostic::new(
890        "ssh.pump_channel.requires_async",
891        format!(
892            "SSH_PUMP_CHANNEL requires {KEYWORD_ASYNC}: pump it in its own task after SSH_DEQUEUE"
893        ),
894        Diagnostic::make_fields(&[]),
895    )
896}
897
898/// `SSH_CONNECT` outside `ASYNC`. Parameterless: the wording is
899/// fixed.
900pub fn connect_requires_async() -> Diagnostic {
901    Diagnostic::new(
902        "ssh.connect.requires_async",
903        format!(
904            "SSH_CONNECT requires {KEYWORD_ASYNC}: run it in its own task beside the SSH_PUMP tasks"
905        ),
906        Diagnostic::make_fields(&[]),
907    )
908}
909
910/// `SSH_CONNECT` while offline. Parameterless: the wording is fixed.
911pub fn connect_offline() -> Diagnostic {
912    Diagnostic::new(
913        "ssh.connect.offline",
914        "SSH_CONNECT failed: engine running in --offline mode".to_string(),
915        Diagnostic::make_fields(&[]),
916    )
917}
918
919/// `SSH_CONNECT` with an empty username. Parameterless: the wording
920/// is fixed.
921pub fn connect_username_empty() -> Diagnostic {
922    Diagnostic::new(
923        "ssh.connect.username_empty",
924        "SSH_CONNECT username must not be empty".to_string(),
925        Diagnostic::make_fields(&[]),
926    )
927}
928
929/// `SSH_CONNECT` session leg failed. Takes the leg's typed failure
930/// by value; bare outer plus chained source.
931pub fn connect_failed(reason: Diagnostic) -> Diagnostic {
932    let reason_text = reason.to_string();
933    Diagnostic::new(
934        "ssh.connect.failed",
935        "SSH_CONNECT failed".to_string(),
936        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
937    )
938    .with_source(std::sync::Arc::new(reason))
939}
940
941/// `SSH_PTY_RUN` outside `ASYNC`. Parameterless: the wording is
942/// fixed.
943pub fn pty_run_requires_async() -> Diagnostic {
944    Diagnostic::new(
945        "ssh.pty_run.requires_async",
946        format!(
947            "SSH_PTY_RUN requires {KEYWORD_ASYNC}: run it in its own task beside the session pump task"
948        ),
949        Diagnostic::make_fields(&[]),
950    )
951}
952
953/// `SSH_RUN` outside `ASYNC`. Parameterless: the wording is fixed.
954pub fn run_requires_async() -> Diagnostic {
955    Diagnostic::new(
956        "ssh.run.requires_async",
957        format!(
958            "SSH_RUN requires {KEYWORD_ASYNC}: run it in its own task beside the session pump task"
959        ),
960        Diagnostic::make_fields(&[]),
961    )
962}
963
964/// `SSH_EXEC` with no argv. Parameterless: the wording is fixed.
965pub fn exec_needs_program() -> Diagnostic {
966    Diagnostic::new(
967        "ssh.exec.needs_program",
968        "SSH_EXEC needs at least a program".to_string(),
969        Diagnostic::make_fields(&[]),
970    )
971}
972
973/// `SSH_EXEC` with an empty username. Parameterless: the wording is
974/// fixed.
975pub fn exec_username_empty() -> Diagnostic {
976    Diagnostic::new(
977        "ssh.exec.username_empty",
978        "SSH_EXEC username must not be empty".to_string(),
979        Diagnostic::make_fields(&[]),
980    )
981}
982
983/// `SSH_EXEC` while offline. Parameterless: the wording is fixed.
984pub fn exec_offline() -> Diagnostic {
985    Diagnostic::new(
986        "ssh.exec.offline",
987        "SSH_EXEC failed: engine running in --offline mode".to_string(),
988        Diagnostic::make_fields(&[]),
989    )
990}
991
992/// `SSH_SERVE` option empty. Takes the option name (callers pass the
993/// `FIELD_*` / `OPT_*` constants); quoting lives here. Shared by
994/// every `SSH_SERVE` string option: one key, one mode.
995pub fn serve_option_empty(option: &str) -> Diagnostic {
996    let message = format!("SSH_SERVE option '{option}' must not be empty");
997    Diagnostic::new(
998        "ssh.serve.option_empty",
999        message,
1000        Diagnostic::make_fields(&[(FIELD_OPTION, option)]),
1001    )
1002}
1003
1004/// `SSH_SERVE` option missing. Takes the option name; quoting lives
1005/// here. Shared by every required `SSH_SERVE` option: one key, one
1006/// mode.
1007pub fn serve_option_required(option: &str) -> Diagnostic {
1008    let message = format!("SSH_SERVE option '{option}' is required");
1009    Diagnostic::new(
1010        "ssh.serve.option_required",
1011        message,
1012        Diagnostic::make_fields(&[(FIELD_OPTION, option)]),
1013    )
1014}
1015
1016/// Ephemeral Ed25519 host key generation failed. Takes the concrete
1017/// failure by value; bare outer plus chained source. The legacy
1018/// wording stays byte-identical.
1019pub fn ephemeral_key_generate_failed(
1020    reason: impl std::error::Error + Send + Sync + 'static,
1021) -> Diagnostic {
1022    let reason_text = reason.to_string();
1023    Diagnostic::new(
1024        "ssh.serve.ephemeral_key_generate_failed",
1025        "generate ephemeral Ed25519 host key".to_string(),
1026        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
1027    )
1028    .with_source(std::sync::Arc::new(reason))
1029}
1030
1031/// `SSH_SERVE` cannot clone its listener. Takes the concrete
1032/// failure by value; bare outer plus chained source.
1033pub fn serve_listener_clone_failed(
1034    reason: impl std::error::Error + Send + Sync + 'static,
1035) -> Diagnostic {
1036    let reason_text = reason.to_string();
1037    Diagnostic::new(
1038        "ssh.serve.listener_clone_failed",
1039        "SSH_SERVE cannot clone its listener".to_string(),
1040        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
1041    )
1042    .with_source(std::sync::Arc::new(reason))
1043}
1044
1045/// `SSH_SERVE` cannot spawn the server thread. Takes the concrete
1046/// failure by value; bare outer plus chained source.
1047pub fn serve_spawn_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
1048    let reason_text = reason.to_string();
1049    Diagnostic::new(
1050        "ssh.serve.spawn_failed",
1051        "SSH_SERVE cannot spawn the server thread".to_string(),
1052        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
1053    )
1054    .with_source(std::sync::Arc::new(reason))
1055}
1056
1057/// `SSH_EXEC` numeric option not positive. Takes the option name
1058/// (callers pass the `OPT_*` constants); quoting lives here.
1059/// Shared by `timeout` / `max_bytes`: one key, one mode.
1060pub fn exec_option_must_be_positive(option: &str) -> Diagnostic {
1061    let message = format!("SSH_EXEC option '{option}' must be positive");
1062    Diagnostic::new(
1063        "ssh.exec.option_must_be_positive",
1064        message,
1065        Diagnostic::make_fields(&[(FIELD_OPTION, option)]),
1066    )
1067}
1068
1069/// Duration option holds a non-duration string. Takes the invoking
1070/// func (`SSH_SERVE` / `SSH_EXEC`: same failure mode, one key),
1071/// the option name, the offending text, and the parse failure by
1072/// value; quoting lives here. Bare outer plus chained source.
1073pub fn option_must_be_duration(
1074    func: &str,
1075    option: &str,
1076    got: &str,
1077    reason: Diagnostic,
1078) -> Diagnostic {
1079    let reason_text = reason.to_string();
1080    let message = format!("{func} option '{option}' must be a duration, got {got:?}");
1081    Diagnostic::new(
1082        "ssh.option.must_be_duration",
1083        message,
1084        Diagnostic::make_fields(&[
1085            (FIELD_FUNC, func),
1086            (FIELD_OPTION, option),
1087            (FIELD_GOT, got),
1088            (FIELD_ERR, &reason_text),
1089        ]),
1090    )
1091    .with_source(std::sync::Arc::new(reason))
1092}
1093
1094/// `SSH_EXEC` remote leg failed. Takes the leg's typed failure by
1095/// value; bare outer plus chained source.
1096pub fn exec_failed(reason: Diagnostic) -> Diagnostic {
1097    let reason_text = reason.to_string();
1098    Diagnostic::new(
1099        "ssh.exec.failed",
1100        "SSH_EXEC failed".to_string(),
1101        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
1102    )
1103    .with_source(std::sync::Arc::new(reason))
1104}
1105
1106/// `SSH_SEND_EXIT_STATUS` with a negative code. Takes the code;
1107/// rendering lives here.
1108pub fn send_exit_status_negative(code: i64) -> Diagnostic {
1109    let message = format!("SSH_SEND_EXIT_STATUS code must not be negative: {code}");
1110    Diagnostic::new(
1111        "ssh.send_exit_status.negative",
1112        message,
1113        Diagnostic::make_fields(&[(FIELD_CODE, &code.to_string())]),
1114    )
1115}
1116
1117/// `SSH_SEND_EXIT_STATUS` code outside `u32`. Takes the code;
1118/// rendering lives here.
1119pub fn send_exit_status_out_of_range(code: i64) -> Diagnostic {
1120    let message = format!("SSH_SEND_EXIT_STATUS code out of range: {code}");
1121    Diagnostic::new(
1122        "ssh.send_exit_status.out_of_range",
1123        message,
1124        Diagnostic::make_fields(&[(FIELD_CODE, &code.to_string())]),
1125    )
1126}
1127
1128#[cfg(test)]
1129mod tests {
1130    use super::*;
1131
1132    fn io_err(msg: &str) -> std::io::Error {
1133        std::io::Error::other(msg.to_string())
1134    }
1135
1136    #[test]
1137    fn each_failure_has_a_distinct_key() {
1138        let err = io_err("boom");
1139        let keys = [
1140            key_path_escapes("SSH_SERVE", "k").key(),
1141            host_key_resolver_failed("SSH_SERVE", &anyhow::anyhow!("boom")).key(),
1142            host_key_generate_failed(io_err("boom")).key(),
1143            host_key_encode_failed(io_err("boom")).key(),
1144            key_guard_failed("SSH_SERVE", &anyhow::anyhow!("boom")).key(),
1145            key_path_join_failed("SSH_SERVE", "k", &anyhow::anyhow!("boom")).key(),
1146            host_key_stat_failed("p", &anyhow::anyhow!("boom")).key(),
1147            host_key_read_failed("SSH_SERVE", "p", &anyhow::anyhow!("boom")).key(),
1148            host_key_parse_failed("SSH_SERVE", "p", io_err("boom")).key(),
1149            host_key_write_failed(&anyhow::anyhow!("boom")).key(),
1150            host_key_must_be_ed25519("SSH_SERVE", "p", &"Ed25519").key(),
1151            host_key_insecure_permissions("SSH_SERVE", "p", 0o644).key(),
1152            pump_output_lock_poisoned().key(),
1153            pump_input_read_failed(&err).key(),
1154            pump_worker_panicked("input worker").key(),
1155            pump_interrupted_by_cancellation().key(),
1156            pump_source_read_failed(&err).key(),
1157            pump_target_lock_poisoned().key(),
1158            pump_func_input_read_failed("SSH run", &err).key(),
1159            pump_output_write_failed(io_err("boom")).key(),
1160            pump_output_flush_failed(io_err("boom")).key(),
1161            pump_borrow_input_failed(pump_output_lock_poisoned()).key(),
1162            pump_borrow_output_failed(pump_output_lock_poisoned()).key(),
1163            pump_wait_runtime_failed(io_err("boom")).key(),
1164            pump_borrow_source_failed(pump_output_lock_poisoned()).key(),
1165            pump_borrow_target_failed(pump_output_lock_poisoned()).key(),
1166            pump_target_write_failed(io_err("boom")).key(),
1167            pump_target_flush_failed(io_err("boom")).key(),
1168            authentication_rejected("SSH_EXEC").key(),
1169            dial_failed("SSH_EXEC", &anyhow::anyhow!("boom")).key(),
1170            auth_exchange_failed("SSH_EXEC", io_err("boom")).key(),
1171            channel_open_failed("SSH_EXEC", io_err("boom")).key(),
1172            exec_request_failed(io_err("boom")).key(),
1173            shell_request_failed(io_err("boom")).key(),
1174            listener_prepare_failed(io_err("boom")).key(),
1175            listener_handoff_failed(io_err("boom")).key(),
1176            listener_failed(io_err("boom")).key(),
1177            client_runtime_build_failed(io_err("boom")).key(),
1178            session_pump_already_finished().key(),
1179            session_wire_queue_saturated().key(),
1180            session_wire_writer_gone().key(),
1181            session_already_pumped().key(),
1182            channel_data_unknown().key(),
1183            channel_pump_gone().key(),
1184            channel_wire_gone_eof_close().key(),
1185            channel_request_unknown().key(),
1186            channel_already_announced().key(),
1187            run_child_read_failed("stdout", &err).key(),
1188            run_output_write_failed(io_err("boom")).key(),
1189            run_output_flush_failed(io_err("boom")).key(),
1190            run_output_lock_poisoned().key(),
1191            run_needs_program().key(),
1192            run_spawn_lost_stderr().key(),
1193            run_interrupted_by_cancellation().key(),
1194            run_borrow_input_failed(pump_output_lock_poisoned()).key(),
1195            run_borrow_output_failed(pump_output_lock_poisoned()).key(),
1196            run_borrow_error_failed(pump_output_lock_poisoned()).key(),
1197            run_spawn_failed(pump_output_lock_poisoned()).key(),
1198            pty_output_lock_poisoned().key(),
1199            pty_interrupted_by_cancellation().key(),
1200            pty_borrow_input_failed(pump_output_lock_poisoned()).key(),
1201            pty_borrow_output_failed(pump_output_lock_poisoned()).key(),
1202            pty_session_init_write_failed(io_err("boom")).key(),
1203            pty_session_init_flush_failed(io_err("boom")).key(),
1204            dequeue_task_cancelled("SSH_DEQUEUE").key(),
1205            dequeue_server_closed("SSH_DEQUEUE").key(),
1206            dequeue_requires_async().key(),
1207            accept_requires_async().key(),
1208            pump_channel_requires_async().key(),
1209            connect_requires_async().key(),
1210            connect_offline().key(),
1211            connect_username_empty().key(),
1212            connect_failed(pump_output_lock_poisoned()).key(),
1213            pty_run_requires_async().key(),
1214            run_requires_async().key(),
1215            exec_needs_program().key(),
1216            exec_username_empty().key(),
1217            exec_offline().key(),
1218            exec_failed(pump_output_lock_poisoned()).key(),
1219            serve_option_empty("username").key(),
1220            serve_option_required("username").key(),
1221            option_must_be_duration(
1222                "SSH_SERVE",
1223                "idle_timeout",
1224                "x",
1225                pump_output_lock_poisoned(),
1226            )
1227            .key(),
1228            ephemeral_key_generate_failed(io_err("boom")).key(),
1229            serve_listener_clone_failed(io_err("boom")).key(),
1230            serve_spawn_failed(io_err("boom")).key(),
1231            exec_option_must_be_positive("timeout").key(),
1232            send_exit_status_negative(-1).key(),
1233            send_exit_status_out_of_range(1 << 40).key(),
1234        ];
1235        let mut seen = std::collections::HashSet::new();
1236        for k in keys {
1237            assert!(seen.insert(k), "duplicate key {k}");
1238        }
1239    }
1240
1241    #[test]
1242    fn shared_modes_keep_one_key() {
1243        assert_eq!(
1244            authentication_rejected("SSH_EXEC").key(),
1245            authentication_rejected("SSH_CONNECT").key()
1246        );
1247        assert_eq!(
1248            dial_failed("SSH_EXEC", &anyhow::anyhow!("boom")).key(),
1249            dial_failed("SSH_CONNECT", &anyhow::anyhow!("boom")).key()
1250        );
1251        assert_eq!(
1252            auth_exchange_failed("SSH_EXEC", io_err("boom")).key(),
1253            auth_exchange_failed("SSH_CONNECT", io_err("boom")).key()
1254        );
1255        assert_eq!(
1256            channel_open_failed("SSH_EXEC", io_err("boom")).key(),
1257            channel_open_failed("SSH_CONNECT", io_err("boom")).key()
1258        );
1259        assert_eq!(
1260            dequeue_task_cancelled("SSH_DEQUEUE").key(),
1261            dequeue_task_cancelled("SSH_ACCEPT").key()
1262        );
1263        assert_eq!(
1264            dequeue_server_closed("SSH_DEQUEUE").key(),
1265            dequeue_server_closed("SSH_ACCEPT").key()
1266        );
1267        assert_eq!(
1268            serve_option_empty("username").key(),
1269            serve_option_empty("password").key()
1270        );
1271        assert_eq!(
1272            serve_option_required("username").key(),
1273            serve_option_required("password").key()
1274        );
1275        assert_eq!(
1276            option_must_be_duration(
1277                "SSH_SERVE",
1278                "idle_timeout",
1279                "x",
1280                pump_output_lock_poisoned(),
1281            )
1282            .key(),
1283            option_must_be_duration("SSH_EXEC", "timeout", "y", pump_output_lock_poisoned()).key()
1284        );
1285        assert_eq!(
1286            exec_option_must_be_positive("timeout").key(),
1287            exec_option_must_be_positive("max_bytes").key()
1288        );
1289    }
1290
1291    #[test]
1292    fn messages_match_canonical_wording() {
1293        let err = io_err("boom");
1294        assert_eq!(
1295            key_path_escapes("SSH_SERVE", "k").message(),
1296            "SSH_SERVE key_path escapes the workspace: k"
1297        );
1298        assert_eq!(
1299            host_key_resolver_failed("SSH_SERVE", &anyhow::anyhow!("boom")).message(),
1300            "SSH_SERVE cannot open the workspace resolver"
1301        );
1302        assert_eq!(
1303            format!(
1304                "{:#}",
1305                anyhow::Error::new(host_key_resolver_failed(
1306                    "SSH_SERVE",
1307                    &anyhow::anyhow!("boom")
1308                ))
1309            ),
1310            "SSH_SERVE cannot open the workspace resolver: boom"
1311        );
1312        assert_eq!(
1313            host_key_generate_failed(io_err("boom")).message(),
1314            "generate Ed25519 host key"
1315        );
1316        assert_eq!(
1317            format!(
1318                "{:#}",
1319                anyhow::Error::new(host_key_generate_failed(io_err("boom")))
1320            ),
1321            "generate Ed25519 host key: boom"
1322        );
1323        assert_eq!(
1324            host_key_encode_failed(io_err("boom")).message(),
1325            "encode Ed25519 host key"
1326        );
1327        assert_eq!(
1328            format!(
1329                "{:#}",
1330                anyhow::Error::new(host_key_encode_failed(io_err("boom")))
1331            ),
1332            "encode Ed25519 host key: boom"
1333        );
1334        assert_eq!(
1335            key_guard_failed("SSH_SERVE", &anyhow::anyhow!("boom")).message(),
1336            "SSH_SERVE cannot guard the workspace root"
1337        );
1338        assert_eq!(
1339            format!(
1340                "{:#}",
1341                anyhow::Error::new(key_guard_failed("SSH_SERVE", &anyhow::anyhow!("boom")))
1342            ),
1343            "SSH_SERVE cannot guard the workspace root: boom"
1344        );
1345        assert_eq!(
1346            key_path_join_failed("SSH_SERVE", "k", &anyhow::anyhow!("boom")).message(),
1347            "SSH_SERVE key_path escapes the workspace: k"
1348        );
1349        assert_eq!(
1350            format!(
1351                "{:#}",
1352                anyhow::Error::new(key_path_join_failed(
1353                    "SSH_SERVE",
1354                    "k",
1355                    &anyhow::anyhow!("boom")
1356                ))
1357            ),
1358            "SSH_SERVE key_path escapes the workspace: k: boom"
1359        );
1360        assert_eq!(
1361            host_key_stat_failed("p", &anyhow::anyhow!("boom")).message(),
1362            "cannot stat host key p"
1363        );
1364        assert_eq!(
1365            format!(
1366                "{:#}",
1367                anyhow::Error::new(host_key_stat_failed("p", &anyhow::anyhow!("boom")))
1368            ),
1369            "cannot stat host key p: boom"
1370        );
1371        assert_eq!(
1372            host_key_read_failed("SSH_SERVE", "p", &anyhow::anyhow!("boom")).message(),
1373            "SSH_SERVE cannot read host key p"
1374        );
1375        assert_eq!(
1376            format!(
1377                "{:#}",
1378                anyhow::Error::new(host_key_read_failed(
1379                    "SSH_SERVE",
1380                    "p",
1381                    &anyhow::anyhow!("boom")
1382                ))
1383            ),
1384            "SSH_SERVE cannot read host key p: boom"
1385        );
1386        assert_eq!(
1387            host_key_parse_failed("SSH_SERVE", "p", io_err("boom")).message(),
1388            "SSH_SERVE cannot parse host key p"
1389        );
1390        assert_eq!(
1391            format!(
1392                "{:#}",
1393                anyhow::Error::new(host_key_parse_failed("SSH_SERVE", "p", io_err("boom")))
1394            ),
1395            "SSH_SERVE cannot parse host key p: boom"
1396        );
1397        assert_eq!(
1398            host_key_write_failed(&anyhow::anyhow!("boom")).message(),
1399            "boom"
1400        );
1401        assert_eq!(
1402            host_key_must_be_ed25519("SSH_SERVE", "p", &"Ed25519").message(),
1403            "SSH_SERVE host key p must be Ed25519, found \"Ed25519\""
1404        );
1405        assert_eq!(
1406            host_key_insecure_permissions("SSH_SERVE", "p", 0o644).message(),
1407            "SSH_SERVE insecure permissions on host key file p: expected 0600, got 0644"
1408        );
1409        assert_eq!(
1410            pump_output_lock_poisoned().message(),
1411            "SSH pump output lock poisoned"
1412        );
1413        assert_eq!(
1414            pump_input_read_failed(&err).message(),
1415            "SSH pump input pipe read failed: boom"
1416        );
1417        assert_eq!(
1418            pump_worker_panicked("input worker").message(),
1419            "SSH pump input worker panicked"
1420        );
1421        assert_eq!(
1422            pump_interrupted_by_cancellation().message(),
1423            "SSH pump interrupted by cancellation"
1424        );
1425        assert_eq!(
1426            pump_source_read_failed(&err).message(),
1427            "SSH_PUMP source pipe read failed: boom"
1428        );
1429        assert_eq!(
1430            pump_target_lock_poisoned().message(),
1431            "SSH_PUMP target lock poisoned"
1432        );
1433        assert_eq!(
1434            pump_func_input_read_failed("SSH run", &err).message(),
1435            "SSH run input pipe read failed: boom"
1436        );
1437        assert_eq!(
1438            pump_output_write_failed(io_err("boom")).message(),
1439            "SSH pump output pipe write failed"
1440        );
1441        assert_eq!(
1442            format!(
1443                "{:#}",
1444                anyhow::Error::new(pump_output_write_failed(io_err("boom")))
1445            ),
1446            "SSH pump output pipe write failed: boom"
1447        );
1448        assert_eq!(
1449            pump_output_flush_failed(io_err("boom")).message(),
1450            "SSH pump output pipe flush failed"
1451        );
1452        assert_eq!(
1453            format!(
1454                "{:#}",
1455                anyhow::Error::new(pump_output_flush_failed(io_err("boom")))
1456            ),
1457            "SSH pump output pipe flush failed: boom"
1458        );
1459        assert_eq!(
1460            pump_borrow_input_failed(pump_output_lock_poisoned()).message(),
1461            "SSH pump cannot borrow the input pipe"
1462        );
1463        assert_eq!(
1464            format!(
1465                "{:#}",
1466                anyhow::Error::new(pump_borrow_input_failed(pump_output_lock_poisoned()))
1467            ),
1468            "SSH pump cannot borrow the input pipe: SSH pump output lock poisoned"
1469        );
1470        assert_eq!(
1471            pump_borrow_output_failed(pump_output_lock_poisoned()).message(),
1472            "SSH pump cannot borrow the output pipe"
1473        );
1474        assert_eq!(
1475            format!(
1476                "{:#}",
1477                anyhow::Error::new(pump_borrow_output_failed(pump_output_lock_poisoned()))
1478            ),
1479            "SSH pump cannot borrow the output pipe: SSH pump output lock poisoned"
1480        );
1481        assert_eq!(
1482            pump_wait_runtime_failed(io_err("boom")).message(),
1483            "SSH pump wait runtime failed"
1484        );
1485        assert_eq!(
1486            format!(
1487                "{:#}",
1488                anyhow::Error::new(pump_wait_runtime_failed(io_err("boom")))
1489            ),
1490            "SSH pump wait runtime failed: boom"
1491        );
1492        assert_eq!(
1493            pump_borrow_source_failed(pump_output_lock_poisoned()).message(),
1494            "SSH_PUMP cannot borrow the source pipe"
1495        );
1496        assert_eq!(
1497            format!(
1498                "{:#}",
1499                anyhow::Error::new(pump_borrow_source_failed(pump_output_lock_poisoned()))
1500            ),
1501            "SSH_PUMP cannot borrow the source pipe: SSH pump output lock poisoned"
1502        );
1503        assert_eq!(
1504            pump_borrow_target_failed(pump_output_lock_poisoned()).message(),
1505            "SSH_PUMP cannot borrow the target pipe"
1506        );
1507        assert_eq!(
1508            format!(
1509                "{:#}",
1510                anyhow::Error::new(pump_borrow_target_failed(pump_output_lock_poisoned()))
1511            ),
1512            "SSH_PUMP cannot borrow the target pipe: SSH pump output lock poisoned"
1513        );
1514        assert_eq!(
1515            pump_target_write_failed(io_err("boom")).message(),
1516            "SSH_PUMP target pipe write failed"
1517        );
1518        assert_eq!(
1519            format!(
1520                "{:#}",
1521                anyhow::Error::new(pump_target_write_failed(io_err("boom")))
1522            ),
1523            "SSH_PUMP target pipe write failed: boom"
1524        );
1525        assert_eq!(
1526            pump_target_flush_failed(io_err("boom")).message(),
1527            "SSH_PUMP target pipe flush failed"
1528        );
1529        assert_eq!(
1530            format!(
1531                "{:#}",
1532                anyhow::Error::new(pump_target_flush_failed(io_err("boom")))
1533            ),
1534            "SSH_PUMP target pipe flush failed: boom"
1535        );
1536        assert_eq!(
1537            authentication_rejected("SSH_EXEC").message(),
1538            "SSH_EXEC authentication rejected"
1539        );
1540        assert_eq!(
1541            dial_failed("SSH_EXEC", &anyhow::anyhow!("boom")).message(),
1542            "SSH_EXEC dial failed"
1543        );
1544        assert_eq!(
1545            format!(
1546                "{:#}",
1547                anyhow::Error::new(dial_failed("SSH_EXEC", &anyhow::anyhow!("boom")))
1548            ),
1549            "SSH_EXEC dial failed: boom"
1550        );
1551        assert_eq!(
1552            auth_exchange_failed("SSH_EXEC", io_err("boom")).message(),
1553            "SSH_EXEC authentication exchange failed"
1554        );
1555        assert_eq!(
1556            format!(
1557                "{:#}",
1558                anyhow::Error::new(auth_exchange_failed("SSH_EXEC", io_err("boom")))
1559            ),
1560            "SSH_EXEC authentication exchange failed: boom"
1561        );
1562        assert_eq!(
1563            channel_open_failed("SSH_EXEC", io_err("boom")).message(),
1564            "SSH_EXEC channel open failed"
1565        );
1566        assert_eq!(
1567            format!(
1568                "{:#}",
1569                anyhow::Error::new(channel_open_failed("SSH_EXEC", io_err("boom")))
1570            ),
1571            "SSH_EXEC channel open failed: boom"
1572        );
1573        assert_eq!(
1574            exec_request_failed(io_err("boom")).message(),
1575            "SSH_EXEC exec request failed"
1576        );
1577        assert_eq!(
1578            format!(
1579                "{:#}",
1580                anyhow::Error::new(exec_request_failed(io_err("boom")))
1581            ),
1582            "SSH_EXEC exec request failed: boom"
1583        );
1584        assert_eq!(
1585            shell_request_failed(io_err("boom")).message(),
1586            "SSH_CONNECT shell request failed"
1587        );
1588        assert_eq!(
1589            format!(
1590                "{:#}",
1591                anyhow::Error::new(shell_request_failed(io_err("boom")))
1592            ),
1593            "SSH_CONNECT shell request failed: boom"
1594        );
1595        assert_eq!(
1596            listener_prepare_failed(io_err("boom")).message(),
1597            "prepare listener for the runtime"
1598        );
1599        assert_eq!(
1600            format!(
1601                "{:#}",
1602                anyhow::Error::new(listener_prepare_failed(io_err("boom")))
1603            ),
1604            "prepare listener for the runtime: boom"
1605        );
1606        assert_eq!(
1607            listener_handoff_failed(io_err("boom")).message(),
1608            "hand listener to the runtime"
1609        );
1610        assert_eq!(
1611            format!(
1612                "{:#}",
1613                anyhow::Error::new(listener_handoff_failed(io_err("boom")))
1614            ),
1615            "hand listener to the runtime: boom"
1616        );
1617        assert_eq!(
1618            listener_failed(io_err("boom")).message(),
1619            "SSH listener failed"
1620        );
1621        assert_eq!(
1622            format!("{:#}", anyhow::Error::new(listener_failed(io_err("boom")))),
1623            "SSH listener failed: boom"
1624        );
1625        assert_eq!(
1626            client_runtime_build_failed(io_err("boom")).message(),
1627            "build SSH client runtime"
1628        );
1629        assert_eq!(
1630            format!(
1631                "{:#}",
1632                anyhow::Error::new(client_runtime_build_failed(io_err("boom")))
1633            ),
1634            "build SSH client runtime: boom"
1635        );
1636        assert_eq!(
1637            session_pump_already_finished().message(),
1638            "SSH session pump already finished"
1639        );
1640        assert_eq!(
1641            session_wire_queue_saturated().message(),
1642            "SSH session wire queue saturated under backpressure"
1643        );
1644        assert_eq!(
1645            session_wire_writer_gone().message(),
1646            "SSH session wire writer is gone"
1647        );
1648        assert_eq!(
1649            session_already_pumped().message(),
1650            "SSH session already pumped"
1651        );
1652        assert_eq!(channel_data_unknown().message(), "data for unknown channel");
1653        assert_eq!(channel_pump_gone().message(), "pump went away");
1654        assert_eq!(
1655            channel_wire_gone_eof_close().message(),
1656            "wire gone during EOF close"
1657        );
1658        assert_eq!(
1659            channel_request_unknown().message(),
1660            "request for unknown channel"
1661        );
1662        assert_eq!(
1663            channel_already_announced().message(),
1664            "channel already announced"
1665        );
1666        assert_eq!(
1667            run_child_read_failed("stdout", &err).message(),
1668            "SSH run child stdout read failed: boom"
1669        );
1670        assert_eq!(
1671            run_output_write_failed(io_err("boom")).message(),
1672            "SSH run output pipe write failed"
1673        );
1674        assert_eq!(
1675            format!(
1676                "{:#}",
1677                anyhow::Error::new(run_output_write_failed(io_err("boom")))
1678            ),
1679            "SSH run output pipe write failed: boom"
1680        );
1681        assert_eq!(
1682            run_output_flush_failed(io_err("boom")).message(),
1683            "SSH run output pipe flush failed"
1684        );
1685        assert_eq!(
1686            format!(
1687                "{:#}",
1688                anyhow::Error::new(run_output_flush_failed(io_err("boom")))
1689            ),
1690            "SSH run output pipe flush failed: boom"
1691        );
1692        assert_eq!(
1693            run_output_lock_poisoned().message(),
1694            "SSH run output lock poisoned"
1695        );
1696        assert_eq!(
1697            run_needs_program().message(),
1698            "SSH_RUN needs at least a program"
1699        );
1700        assert_eq!(
1701            run_spawn_lost_stderr().message(),
1702            "SSH run spawn lost stderr"
1703        );
1704        assert_eq!(
1705            run_interrupted_by_cancellation().message(),
1706            "SSH run interrupted by cancellation"
1707        );
1708        assert_eq!(
1709            run_borrow_input_failed(pump_output_lock_poisoned()).message(),
1710            "SSH run cannot borrow the input pipe"
1711        );
1712        assert_eq!(
1713            format!(
1714                "{:#}",
1715                anyhow::Error::new(run_borrow_input_failed(pump_output_lock_poisoned()))
1716            ),
1717            "SSH run cannot borrow the input pipe: SSH pump output lock poisoned"
1718        );
1719        assert_eq!(
1720            run_borrow_output_failed(pump_output_lock_poisoned()).message(),
1721            "SSH run cannot borrow the output pipe"
1722        );
1723        assert_eq!(
1724            format!(
1725                "{:#}",
1726                anyhow::Error::new(run_borrow_output_failed(pump_output_lock_poisoned()))
1727            ),
1728            "SSH run cannot borrow the output pipe: SSH pump output lock poisoned"
1729        );
1730        assert_eq!(
1731            run_borrow_error_failed(pump_output_lock_poisoned()).message(),
1732            "SSH run cannot borrow the error pipe"
1733        );
1734        assert_eq!(
1735            format!(
1736                "{:#}",
1737                anyhow::Error::new(run_borrow_error_failed(pump_output_lock_poisoned()))
1738            ),
1739            "SSH run cannot borrow the error pipe: SSH pump output lock poisoned"
1740        );
1741        assert_eq!(
1742            run_spawn_failed(pump_output_lock_poisoned()).message(),
1743            "SSH run spawn failed"
1744        );
1745        assert_eq!(
1746            format!(
1747                "{:#}",
1748                anyhow::Error::new(run_spawn_failed(pump_output_lock_poisoned()))
1749            ),
1750            "SSH run spawn failed: SSH pump output lock poisoned"
1751        );
1752        assert_eq!(
1753            pty_output_lock_poisoned().message(),
1754            "SSH pty output lock poisoned"
1755        );
1756        assert_eq!(
1757            pty_interrupted_by_cancellation().message(),
1758            "SSH pty run interrupted by cancellation"
1759        );
1760        assert_eq!(
1761            pty_borrow_input_failed(pump_output_lock_poisoned()).message(),
1762            "SSH pty cannot borrow the input pipe"
1763        );
1764        assert_eq!(
1765            format!(
1766                "{:#}",
1767                anyhow::Error::new(pty_borrow_input_failed(pump_output_lock_poisoned()))
1768            ),
1769            "SSH pty cannot borrow the input pipe: SSH pump output lock poisoned"
1770        );
1771        assert_eq!(
1772            pty_borrow_output_failed(pump_output_lock_poisoned()).message(),
1773            "SSH pty cannot borrow the output pipe"
1774        );
1775        assert_eq!(
1776            format!(
1777                "{:#}",
1778                anyhow::Error::new(pty_borrow_output_failed(pump_output_lock_poisoned()))
1779            ),
1780            "SSH pty cannot borrow the output pipe: SSH pump output lock poisoned"
1781        );
1782        assert_eq!(
1783            pty_session_init_write_failed(io_err("boom")).message(),
1784            "SSH pty session init write failed"
1785        );
1786        assert_eq!(
1787            format!(
1788                "{:#}",
1789                anyhow::Error::new(pty_session_init_write_failed(io_err("boom")))
1790            ),
1791            "SSH pty session init write failed: boom"
1792        );
1793        assert_eq!(
1794            pty_session_init_flush_failed(io_err("boom")).message(),
1795            "SSH pty session init flush failed"
1796        );
1797        assert_eq!(
1798            format!(
1799                "{:#}",
1800                anyhow::Error::new(pty_session_init_flush_failed(io_err("boom")))
1801            ),
1802            "SSH pty session init flush failed: boom"
1803        );
1804        assert_eq!(
1805            dequeue_task_cancelled("SSH_DEQUEUE").message(),
1806            "SSH_DEQUEUE: task cancelled"
1807        );
1808        assert_eq!(
1809            dequeue_server_closed("SSH_ACCEPT").message(),
1810            "SSH_ACCEPT: server is closed"
1811        );
1812        assert_eq!(
1813            dequeue_requires_async().message(),
1814            "SSH_DEQUEUE requires ASYNC: wrap it as LET $t: HANDLE<T> = ASYNC { SSH_DEQUEUE($server.server) }"
1815        );
1816        assert_eq!(
1817            accept_requires_async().message(),
1818            "SSH_ACCEPT requires ASYNC: wrap it as LET $t: HANDLE<T> = ASYNC { SSH_ACCEPT($server, $in, $out) }"
1819        );
1820        assert_eq!(
1821            pump_channel_requires_async().message(),
1822            "SSH_PUMP_CHANNEL requires ASYNC: pump it in its own task after SSH_DEQUEUE"
1823        );
1824        assert_eq!(
1825            connect_requires_async().message(),
1826            "SSH_CONNECT requires ASYNC: run it in its own task beside the SSH_PUMP tasks"
1827        );
1828        assert_eq!(
1829            connect_offline().message(),
1830            "SSH_CONNECT failed: engine running in --offline mode"
1831        );
1832        assert_eq!(
1833            connect_username_empty().message(),
1834            "SSH_CONNECT username must not be empty"
1835        );
1836        assert_eq!(
1837            connect_failed(pump_output_lock_poisoned()).message(),
1838            "SSH_CONNECT failed"
1839        );
1840        assert_eq!(
1841            format!(
1842                "{:#}",
1843                anyhow::Error::new(connect_failed(pump_output_lock_poisoned()))
1844            ),
1845            "SSH_CONNECT failed: SSH pump output lock poisoned"
1846        );
1847        assert_eq!(
1848            pty_run_requires_async().message(),
1849            "SSH_PTY_RUN requires ASYNC: run it in its own task beside the session pump task"
1850        );
1851        assert_eq!(
1852            run_requires_async().message(),
1853            "SSH_RUN requires ASYNC: run it in its own task beside the session pump task"
1854        );
1855        assert_eq!(
1856            exec_needs_program().message(),
1857            "SSH_EXEC needs at least a program"
1858        );
1859        assert_eq!(
1860            exec_username_empty().message(),
1861            "SSH_EXEC username must not be empty"
1862        );
1863        assert_eq!(
1864            exec_offline().message(),
1865            "SSH_EXEC failed: engine running in --offline mode"
1866        );
1867        assert_eq!(
1868            exec_failed(pump_output_lock_poisoned()).message(),
1869            "SSH_EXEC failed"
1870        );
1871        assert_eq!(
1872            format!(
1873                "{:#}",
1874                anyhow::Error::new(exec_failed(pump_output_lock_poisoned()))
1875            ),
1876            "SSH_EXEC failed: SSH pump output lock poisoned"
1877        );
1878        assert_eq!(
1879            serve_option_empty("username").message(),
1880            "SSH_SERVE option 'username' must not be empty"
1881        );
1882        assert_eq!(
1883            serve_option_required("password").message(),
1884            "SSH_SERVE option 'password' is required"
1885        );
1886        assert_eq!(
1887            option_must_be_duration(
1888                "SSH_SERVE",
1889                "idle_timeout",
1890                "soon",
1891                pump_output_lock_poisoned(),
1892            )
1893            .message(),
1894            "SSH_SERVE option 'idle_timeout' must be a duration, got \"soon\""
1895        );
1896        assert_eq!(
1897            format!(
1898                "{:#}",
1899                anyhow::Error::new(option_must_be_duration(
1900                    "SSH_SERVE",
1901                    "idle_timeout",
1902                    "soon",
1903                    pump_output_lock_poisoned(),
1904                ))
1905            ),
1906            "SSH_SERVE option 'idle_timeout' must be a duration, got \"soon\": SSH pump output lock poisoned"
1907        );
1908        assert_eq!(
1909            option_must_be_duration("SSH_EXEC", "timeout", "soon", pump_output_lock_poisoned())
1910                .message(),
1911            "SSH_EXEC option 'timeout' must be a duration, got \"soon\""
1912        );
1913        assert_eq!(
1914            ephemeral_key_generate_failed(io_err("boom")).message(),
1915            "generate ephemeral Ed25519 host key"
1916        );
1917        assert_eq!(
1918            format!(
1919                "{:#}",
1920                anyhow::Error::new(ephemeral_key_generate_failed(io_err("boom")))
1921            ),
1922            "generate ephemeral Ed25519 host key: boom"
1923        );
1924        assert_eq!(
1925            serve_listener_clone_failed(io_err("boom")).message(),
1926            "SSH_SERVE cannot clone its listener"
1927        );
1928        assert_eq!(
1929            format!(
1930                "{:#}",
1931                anyhow::Error::new(serve_listener_clone_failed(io_err("boom")))
1932            ),
1933            "SSH_SERVE cannot clone its listener: boom"
1934        );
1935        assert_eq!(
1936            serve_spawn_failed(io_err("boom")).message(),
1937            "SSH_SERVE cannot spawn the server thread"
1938        );
1939        assert_eq!(
1940            format!(
1941                "{:#}",
1942                anyhow::Error::new(serve_spawn_failed(io_err("boom")))
1943            ),
1944            "SSH_SERVE cannot spawn the server thread: boom"
1945        );
1946        assert_eq!(
1947            exec_option_must_be_positive("timeout").message(),
1948            "SSH_EXEC option 'timeout' must be positive"
1949        );
1950        assert_eq!(
1951            send_exit_status_negative(-1).message(),
1952            "SSH_SEND_EXIT_STATUS code must not be negative: -1"
1953        );
1954        assert_eq!(
1955            send_exit_status_out_of_range(1 << 40).message(),
1956            "SSH_SEND_EXIT_STATUS code out of range: 1099511627776"
1957        );
1958    }
1959}