1use oxdock_diagnostics::{Diagnostic, flatten_chain};
12use oxdock_parser::{HANDLE_TYPE_NAME, KEYWORD_ASYNC, KEYWORD_LET};
13
14const 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
29pub 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
40pub 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
61pub 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
80pub fn host_key_resolver_failed(func: &str, reason: &Diagnostic) -> Diagnostic {
86 let reason_text = flatten_chain(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
95pub 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
110pub 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
124pub fn key_guard_failed(func: &str, reason: &Diagnostic) -> Diagnostic {
128 let reason_text = flatten_chain(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
137pub fn host_key_stat_failed(path: &str, reason: &Diagnostic) -> Diagnostic {
141 let reason_text = flatten_chain(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
150pub fn host_key_read_failed(func: &str, path: &str, reason: &Diagnostic) -> Diagnostic {
154 let reason_text = flatten_chain(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
167pub 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
188pub fn host_key_write_failed(reason: &Diagnostic) -> Diagnostic {
194 host_key_write_failed_fallback(&flatten_chain(reason))
195}
196
197pub fn host_key_write_failed_fallback(reason_text: &str) -> Diagnostic {
202 Diagnostic::new(
203 "ssh.key.write_failed",
204 reason_text.to_string(),
205 Diagnostic::make_fields(&[(FIELD_ERR, reason_text)]),
206 )
207}
208
209pub fn from_anyhow(err: anyhow::Error) -> Diagnostic {
218 if let Some(found) = err
219 .chain()
220 .find_map(|cause| cause.downcast_ref::<Diagnostic>())
221 .cloned()
222 {
223 let mut outer_frames: Vec<String> = Vec::new();
224 for cause in err.chain() {
225 if cause.downcast_ref::<Diagnostic>().is_some() {
226 break;
227 }
228 if cause.to_string() != found.message() {
232 outer_frames.push(cause.to_string());
233 }
234 }
235 return found.with_contexts(outer_frames);
236 }
237 host_key_write_failed_fallback(&format!("{err:#}"))
238}
239
240pub fn key_path_join_failed(func: &str, raw: &str, reason: &Diagnostic) -> Diagnostic {
245 let reason_text = flatten_chain(reason);
246 Diagnostic::new(
247 "ssh.key.join_failed",
248 format!("{func} key_path escapes the workspace: {raw}"),
249 Diagnostic::make_fields(&[
250 (FIELD_FUNC, func),
251 (FIELD_RAW, raw),
252 (FIELD_ERR, &reason_text),
253 ]),
254 )
255 .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
256}
257
258pub fn pump_output_lock_poisoned() -> Diagnostic {
261 Diagnostic::new(
262 "ssh.pump.output_lock_poisoned",
263 "SSH pump output lock poisoned".to_string(),
264 Diagnostic::make_fields(&[]),
265 )
266}
267
268pub fn pump_input_read_failed(err: &std::io::Error) -> Diagnostic {
271 let err_text = err.to_string();
272 let message = format!("SSH pump input pipe read failed: {err_text}");
273 Diagnostic::new(
274 "ssh.pump.input_read_failed",
275 message,
276 Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
277 )
278}
279
280pub fn pump_worker_panicked(worker: &str) -> Diagnostic {
283 let message = format!("SSH pump {worker} panicked");
284 Diagnostic::new(
285 "ssh.pump.worker_panicked",
286 message,
287 Diagnostic::make_fields(&[(FIELD_WORKER, worker)]),
288 )
289}
290
291pub fn pump_interrupted_by_cancellation() -> Diagnostic {
294 Diagnostic::new(
295 "ssh.pump.interrupted_by_cancellation",
296 "SSH pump interrupted by cancellation".to_string(),
297 Diagnostic::make_fields(&[]),
298 )
299}
300
301pub fn pump_source_read_failed(err: &std::io::Error) -> Diagnostic {
304 let err_text = err.to_string();
305 let message = format!("SSH_PUMP source pipe read failed: {err_text}");
306 Diagnostic::new(
307 "ssh.pump.source_read_failed",
308 message,
309 Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
310 )
311}
312
313pub fn pump_target_lock_poisoned() -> Diagnostic {
316 Diagnostic::new(
317 "ssh.pump.target_lock_poisoned",
318 "SSH_PUMP target lock poisoned".to_string(),
319 Diagnostic::make_fields(&[]),
320 )
321}
322
323pub fn pump_func_input_read_failed(func: &str, err: &std::io::Error) -> Diagnostic {
326 let err_text = err.to_string();
327 let message = format!("{func} input pipe read failed: {err_text}");
328 Diagnostic::new(
329 "ssh.pump.func_input_read_failed",
330 message,
331 Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, err_text.as_str())]),
332 )
333}
334
335pub fn pump_output_write_failed(
339 reason: impl std::error::Error + Send + Sync + 'static,
340) -> Diagnostic {
341 let reason_text = reason.to_string();
342 Diagnostic::new(
343 "ssh.pump.output_write_failed",
344 "SSH pump output pipe write failed".to_string(),
345 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
346 )
347 .with_source(std::sync::Arc::new(reason))
348}
349
350pub fn pump_output_flush_failed(
353 reason: impl std::error::Error + Send + Sync + 'static,
354) -> Diagnostic {
355 let reason_text = reason.to_string();
356 Diagnostic::new(
357 "ssh.pump.output_flush_failed",
358 "SSH pump output pipe flush failed".to_string(),
359 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
360 )
361 .with_source(std::sync::Arc::new(reason))
362}
363
364pub fn pump_borrow_input_failed(reason: Diagnostic) -> Diagnostic {
368 let reason_text = reason.to_string();
369 Diagnostic::new(
370 "ssh.pump.borrow_input_failed",
371 "SSH pump cannot borrow the input pipe".to_string(),
372 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
373 )
374 .with_source(std::sync::Arc::new(reason))
375}
376
377pub fn pump_borrow_output_failed(reason: Diagnostic) -> Diagnostic {
380 let reason_text = reason.to_string();
381 Diagnostic::new(
382 "ssh.pump.borrow_output_failed",
383 "SSH pump cannot borrow the output pipe".to_string(),
384 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
385 )
386 .with_source(std::sync::Arc::new(reason))
387}
388
389pub fn pump_wait_runtime_failed(
392 reason: impl std::error::Error + Send + Sync + 'static,
393) -> Diagnostic {
394 let reason_text = reason.to_string();
395 Diagnostic::new(
396 "ssh.pump.wait_runtime_failed",
397 "SSH pump wait runtime failed".to_string(),
398 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
399 )
400 .with_source(std::sync::Arc::new(reason))
401}
402
403pub fn pump_borrow_source_failed(reason: Diagnostic) -> Diagnostic {
406 let reason_text = reason.to_string();
407 Diagnostic::new(
408 "ssh.pump.borrow_source_failed",
409 "SSH_PUMP cannot borrow the source pipe".to_string(),
410 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
411 )
412 .with_source(std::sync::Arc::new(reason))
413}
414
415pub fn pump_borrow_target_failed(reason: Diagnostic) -> Diagnostic {
418 let reason_text = reason.to_string();
419 Diagnostic::new(
420 "ssh.pump.borrow_target_failed",
421 "SSH_PUMP cannot borrow the target pipe".to_string(),
422 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
423 )
424 .with_source(std::sync::Arc::new(reason))
425}
426
427pub fn pump_target_write_failed(
430 reason: impl std::error::Error + Send + Sync + 'static,
431) -> Diagnostic {
432 let reason_text = reason.to_string();
433 Diagnostic::new(
434 "ssh.pump.target_write_failed",
435 "SSH_PUMP target pipe write failed".to_string(),
436 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
437 )
438 .with_source(std::sync::Arc::new(reason))
439}
440
441pub fn pump_target_flush_failed(
444 reason: impl std::error::Error + Send + Sync + 'static,
445) -> Diagnostic {
446 let reason_text = reason.to_string();
447 Diagnostic::new(
448 "ssh.pump.target_flush_failed",
449 "SSH_PUMP target pipe flush failed".to_string(),
450 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
451 )
452 .with_source(std::sync::Arc::new(reason))
453}
454
455pub fn authentication_rejected(func: &str) -> Diagnostic {
459 let message = format!("{func} authentication rejected");
460 Diagnostic::new(
461 "ssh.auth.rejected",
462 message,
463 Diagnostic::make_fields(&[(FIELD_FUNC, func)]),
464 )
465}
466
467pub fn dial_failed(func: &str, reason: &Diagnostic) -> Diagnostic {
473 let reason_text = flatten_chain(reason);
474 Diagnostic::new(
475 "ssh.dial_failed",
476 format!("{func} dial failed"),
477 Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
478 )
479 .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
480}
481
482pub fn auth_exchange_failed(
486 func: &str,
487 reason: impl std::error::Error + Send + Sync + 'static,
488) -> Diagnostic {
489 let reason_text = reason.to_string();
490 Diagnostic::new(
491 "ssh.auth.exchange_failed",
492 format!("{func} authentication exchange failed"),
493 Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
494 )
495 .with_source(std::sync::Arc::new(reason))
496}
497
498pub fn channel_open_failed(
502 func: &str,
503 reason: impl std::error::Error + Send + Sync + 'static,
504) -> Diagnostic {
505 let reason_text = reason.to_string();
506 Diagnostic::new(
507 "ssh.channel.open_failed",
508 format!("{func} channel open failed"),
509 Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
510 )
511 .with_source(std::sync::Arc::new(reason))
512}
513
514pub fn exec_request_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
517 let reason_text = reason.to_string();
518 Diagnostic::new(
519 "ssh.exec.request_failed",
520 "SSH_EXEC exec request failed".to_string(),
521 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
522 )
523 .with_source(std::sync::Arc::new(reason))
524}
525
526pub fn shell_request_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
529 let reason_text = reason.to_string();
530 Diagnostic::new(
531 "ssh.connect.shell_request_failed",
532 "SSH_CONNECT shell request failed".to_string(),
533 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
534 )
535 .with_source(std::sync::Arc::new(reason))
536}
537
538pub fn listener_prepare_failed(
542 reason: impl std::error::Error + Send + Sync + 'static,
543) -> Diagnostic {
544 let reason_text = reason.to_string();
545 Diagnostic::new(
546 "ssh.serve.listener_prepare_failed",
547 "prepare listener for the runtime".to_string(),
548 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
549 )
550 .with_source(std::sync::Arc::new(reason))
551}
552
553pub fn listener_handoff_failed(
556 reason: impl std::error::Error + Send + Sync + 'static,
557) -> Diagnostic {
558 let reason_text = reason.to_string();
559 Diagnostic::new(
560 "ssh.serve.listener_handoff_failed",
561 "hand listener to the runtime".to_string(),
562 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
563 )
564 .with_source(std::sync::Arc::new(reason))
565}
566
567pub fn listener_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
570 let reason_text = reason.to_string();
571 Diagnostic::new(
572 "ssh.serve.listener_failed",
573 "SSH listener failed".to_string(),
574 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
575 )
576 .with_source(std::sync::Arc::new(reason))
577}
578
579pub fn client_runtime_build_failed(
582 reason: impl std::error::Error + Send + Sync + 'static,
583) -> Diagnostic {
584 let reason_text = reason.to_string();
585 Diagnostic::new(
586 "ssh.client_runtime.build_failed",
587 "build SSH client runtime".to_string(),
588 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
589 )
590 .with_source(std::sync::Arc::new(reason))
591}
592
593pub fn session_pump_already_finished() -> Diagnostic {
596 Diagnostic::new(
597 "ssh.session.pump_already_finished",
598 "SSH session pump already finished".to_string(),
599 Diagnostic::make_fields(&[]),
600 )
601}
602
603pub fn session_wire_queue_saturated() -> Diagnostic {
606 Diagnostic::new(
607 "ssh.session.wire_queue_saturated",
608 "SSH session wire queue saturated under backpressure".to_string(),
609 Diagnostic::make_fields(&[]),
610 )
611}
612
613pub fn session_wire_writer_gone() -> Diagnostic {
616 Diagnostic::new(
617 "ssh.session.wire_writer_gone",
618 "SSH session wire writer is gone".to_string(),
619 Diagnostic::make_fields(&[]),
620 )
621}
622
623pub fn session_already_pumped() -> Diagnostic {
626 Diagnostic::new(
627 "ssh.session.already_pumped",
628 "SSH session already pumped".to_string(),
629 Diagnostic::make_fields(&[]),
630 )
631}
632
633pub fn channel_data_unknown() -> Diagnostic {
636 Diagnostic::new(
637 "ssh.channel.data_unknown",
638 "data for unknown channel".to_string(),
639 Diagnostic::make_fields(&[]),
640 )
641}
642
643pub fn channel_pump_gone() -> Diagnostic {
645 Diagnostic::new(
646 "ssh.channel.pump_gone",
647 "pump went away".to_string(),
648 Diagnostic::make_fields(&[]),
649 )
650}
651
652pub fn channel_wire_gone_eof_close() -> Diagnostic {
655 Diagnostic::new(
656 "ssh.channel.wire_gone_eof_close",
657 "wire gone during EOF close".to_string(),
658 Diagnostic::make_fields(&[]),
659 )
660}
661
662pub fn channel_request_unknown() -> Diagnostic {
665 Diagnostic::new(
666 "ssh.channel.request_unknown",
667 "request for unknown channel".to_string(),
668 Diagnostic::make_fields(&[]),
669 )
670}
671
672pub fn channel_already_announced() -> Diagnostic {
674 Diagnostic::new(
675 "ssh.channel.already_announced",
676 "channel already announced".to_string(),
677 Diagnostic::make_fields(&[]),
678 )
679}
680
681pub fn run_child_read_failed(name: &str, err: &std::io::Error) -> Diagnostic {
684 let err_text = err.to_string();
685 let message = format!("SSH run child {name} read failed: {err_text}");
686 Diagnostic::new(
687 "ssh.run.child_read_failed",
688 message,
689 Diagnostic::make_fields(&[(FIELD_NAME, name), (FIELD_ERR, err_text.as_str())]),
690 )
691}
692
693pub fn run_output_write_failed(
696 reason: impl std::error::Error + Send + Sync + 'static,
697) -> Diagnostic {
698 let reason_text = reason.to_string();
699 Diagnostic::new(
700 "ssh.run.output_write_failed",
701 "SSH run output pipe write failed".to_string(),
702 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
703 )
704 .with_source(std::sync::Arc::new(reason))
705}
706
707pub fn run_output_flush_failed(
710 reason: impl std::error::Error + Send + Sync + 'static,
711) -> Diagnostic {
712 let reason_text = reason.to_string();
713 Diagnostic::new(
714 "ssh.run.output_flush_failed",
715 "SSH run output pipe flush failed".to_string(),
716 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
717 )
718 .with_source(std::sync::Arc::new(reason))
719}
720
721pub fn run_output_lock_poisoned() -> Diagnostic {
724 Diagnostic::new(
725 "ssh.run.output_lock_poisoned",
726 "SSH run output lock poisoned".to_string(),
727 Diagnostic::make_fields(&[]),
728 )
729}
730
731pub fn run_needs_program() -> Diagnostic {
733 Diagnostic::new(
734 "ssh.run.needs_program",
735 "SSH_RUN needs at least a program".to_string(),
736 Diagnostic::make_fields(&[]),
737 )
738}
739
740pub fn run_spawn_lost_stderr() -> Diagnostic {
743 Diagnostic::new(
744 "ssh.run.spawn_lost_stderr",
745 "SSH run spawn lost stderr".to_string(),
746 Diagnostic::make_fields(&[]),
747 )
748}
749
750pub fn run_interrupted_by_cancellation() -> Diagnostic {
753 Diagnostic::new(
754 "ssh.run.interrupted_by_cancellation",
755 "SSH run interrupted by cancellation".to_string(),
756 Diagnostic::make_fields(&[]),
757 )
758}
759
760pub fn run_borrow_input_failed(reason: Diagnostic) -> Diagnostic {
763 let reason_text = reason.to_string();
764 Diagnostic::new(
765 "ssh.run.borrow_input_failed",
766 "SSH run cannot borrow the input pipe".to_string(),
767 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
768 )
769 .with_source(std::sync::Arc::new(reason))
770}
771
772pub fn run_borrow_output_failed(reason: Diagnostic) -> Diagnostic {
775 let reason_text = reason.to_string();
776 Diagnostic::new(
777 "ssh.run.borrow_output_failed",
778 "SSH run cannot borrow the output pipe".to_string(),
779 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
780 )
781 .with_source(std::sync::Arc::new(reason))
782}
783
784pub fn run_borrow_error_failed(reason: Diagnostic) -> Diagnostic {
787 let reason_text = reason.to_string();
788 Diagnostic::new(
789 "ssh.run.borrow_error_failed",
790 "SSH run cannot borrow the error pipe".to_string(),
791 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
792 )
793 .with_source(std::sync::Arc::new(reason))
794}
795
796pub fn run_spawn_failed(reason: Diagnostic) -> Diagnostic {
799 let reason_text = reason.to_string();
800 Diagnostic::new(
801 "ssh.run.spawn_failed",
802 "SSH run spawn failed".to_string(),
803 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
804 )
805 .with_source(std::sync::Arc::new(reason))
806}
807
808pub fn pty_output_lock_poisoned() -> Diagnostic {
811 Diagnostic::new(
812 "ssh.pty.output_lock_poisoned",
813 "SSH pty output lock poisoned".to_string(),
814 Diagnostic::make_fields(&[]),
815 )
816}
817
818pub fn pty_interrupted_by_cancellation() -> Diagnostic {
821 Diagnostic::new(
822 "ssh.pty.interrupted_by_cancellation",
823 "SSH pty run interrupted by cancellation".to_string(),
824 Diagnostic::make_fields(&[]),
825 )
826}
827
828pub fn pty_borrow_input_failed(reason: Diagnostic) -> Diagnostic {
831 let reason_text = reason.to_string();
832 Diagnostic::new(
833 "ssh.pty.borrow_input_failed",
834 "SSH pty cannot borrow the input pipe".to_string(),
835 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
836 )
837 .with_source(std::sync::Arc::new(reason))
838}
839
840pub fn pty_borrow_output_failed(reason: Diagnostic) -> Diagnostic {
843 let reason_text = reason.to_string();
844 Diagnostic::new(
845 "ssh.pty.borrow_output_failed",
846 "SSH pty cannot borrow the output pipe".to_string(),
847 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
848 )
849 .with_source(std::sync::Arc::new(reason))
850}
851
852pub fn pty_session_init_write_failed(
855 reason: impl std::error::Error + Send + Sync + 'static,
856) -> Diagnostic {
857 let reason_text = reason.to_string();
858 Diagnostic::new(
859 "ssh.pty.session_init_write_failed",
860 "SSH pty session init write failed".to_string(),
861 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
862 )
863 .with_source(std::sync::Arc::new(reason))
864}
865
866pub fn pty_session_init_flush_failed(
869 reason: impl std::error::Error + Send + Sync + 'static,
870) -> Diagnostic {
871 let reason_text = reason.to_string();
872 Diagnostic::new(
873 "ssh.pty.session_init_flush_failed",
874 "SSH pty session init flush failed".to_string(),
875 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
876 )
877 .with_source(std::sync::Arc::new(reason))
878}
879
880pub fn dequeue_task_cancelled(func: &str) -> Diagnostic {
883 let message = format!("{func}: task cancelled");
884 Diagnostic::new(
885 "ssh.dequeue.task_cancelled",
886 message,
887 Diagnostic::make_fields(&[(FIELD_FUNC, func)]),
888 )
889}
890
891pub fn dequeue_server_closed(func: &str) -> Diagnostic {
894 let message = format!("{func}: server is closed");
895 Diagnostic::new(
896 "ssh.dequeue.server_closed",
897 message,
898 Diagnostic::make_fields(&[(FIELD_FUNC, func)]),
899 )
900}
901
902pub fn dequeue_requires_async() -> Diagnostic {
905 Diagnostic::new(
906 "ssh.dequeue.requires_async",
907 format!(
908 "SSH_DEQUEUE requires {KEYWORD_ASYNC}: wrap it as {KEYWORD_LET} $t: {HANDLE_TYPE_NAME}<T> = {KEYWORD_ASYNC} {{ SSH_DEQUEUE($server.server) }}"
909 ),
910 Diagnostic::make_fields(&[]),
911 )
912}
913
914pub fn accept_requires_async() -> Diagnostic {
917 Diagnostic::new(
918 "ssh.accept.requires_async",
919 format!(
920 "SSH_ACCEPT requires {KEYWORD_ASYNC}: wrap it as {KEYWORD_LET} $t: {HANDLE_TYPE_NAME}<T> = {KEYWORD_ASYNC} {{ SSH_ACCEPT($server, $in, $out) }}"
921 ),
922 Diagnostic::make_fields(&[]),
923 )
924}
925
926pub fn pump_channel_requires_async() -> Diagnostic {
929 Diagnostic::new(
930 "ssh.pump_channel.requires_async",
931 format!(
932 "SSH_PUMP_CHANNEL requires {KEYWORD_ASYNC}: pump it in its own task after SSH_DEQUEUE"
933 ),
934 Diagnostic::make_fields(&[]),
935 )
936}
937
938pub fn connect_requires_async() -> Diagnostic {
941 Diagnostic::new(
942 "ssh.connect.requires_async",
943 format!(
944 "SSH_CONNECT requires {KEYWORD_ASYNC}: run it in its own task beside the SSH_PUMP tasks"
945 ),
946 Diagnostic::make_fields(&[]),
947 )
948}
949
950pub fn connect_offline() -> Diagnostic {
952 Diagnostic::new(
953 "ssh.connect.offline",
954 "SSH_CONNECT failed: engine running in --offline mode".to_string(),
955 Diagnostic::make_fields(&[]),
956 )
957}
958
959pub fn connect_username_empty() -> Diagnostic {
962 Diagnostic::new(
963 "ssh.connect.username_empty",
964 "SSH_CONNECT username must not be empty".to_string(),
965 Diagnostic::make_fields(&[]),
966 )
967}
968
969pub fn connect_failed(reason: Diagnostic) -> Diagnostic {
972 let reason_text = reason.to_string();
973 Diagnostic::new(
974 "ssh.connect.failed",
975 "SSH_CONNECT failed".to_string(),
976 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
977 )
978 .with_source(std::sync::Arc::new(reason))
979}
980
981pub fn pty_run_requires_async() -> Diagnostic {
984 Diagnostic::new(
985 "ssh.pty_run.requires_async",
986 format!(
987 "SSH_PTY_RUN requires {KEYWORD_ASYNC}: run it in its own task beside the session pump task"
988 ),
989 Diagnostic::make_fields(&[]),
990 )
991}
992
993pub fn run_requires_async() -> Diagnostic {
995 Diagnostic::new(
996 "ssh.run.requires_async",
997 format!(
998 "SSH_RUN requires {KEYWORD_ASYNC}: run it in its own task beside the session pump task"
999 ),
1000 Diagnostic::make_fields(&[]),
1001 )
1002}
1003
1004pub fn exec_needs_program() -> Diagnostic {
1006 Diagnostic::new(
1007 "ssh.exec.needs_program",
1008 "SSH_EXEC needs at least a program".to_string(),
1009 Diagnostic::make_fields(&[]),
1010 )
1011}
1012
1013pub fn exec_username_empty() -> Diagnostic {
1016 Diagnostic::new(
1017 "ssh.exec.username_empty",
1018 "SSH_EXEC username must not be empty".to_string(),
1019 Diagnostic::make_fields(&[]),
1020 )
1021}
1022
1023pub fn exec_offline() -> Diagnostic {
1025 Diagnostic::new(
1026 "ssh.exec.offline",
1027 "SSH_EXEC failed: engine running in --offline mode".to_string(),
1028 Diagnostic::make_fields(&[]),
1029 )
1030}
1031
1032pub fn serve_option_empty(option: &str) -> Diagnostic {
1036 let message = format!("SSH_SERVE option '{option}' must not be empty");
1037 Diagnostic::new(
1038 "ssh.serve.option_empty",
1039 message,
1040 Diagnostic::make_fields(&[(FIELD_OPTION, option)]),
1041 )
1042}
1043
1044pub fn serve_option_required(option: &str) -> Diagnostic {
1048 let message = format!("SSH_SERVE option '{option}' is required");
1049 Diagnostic::new(
1050 "ssh.serve.option_required",
1051 message,
1052 Diagnostic::make_fields(&[(FIELD_OPTION, option)]),
1053 )
1054}
1055
1056pub fn ephemeral_key_generate_failed(
1060 reason: impl std::error::Error + Send + Sync + 'static,
1061) -> Diagnostic {
1062 let reason_text = reason.to_string();
1063 Diagnostic::new(
1064 "ssh.serve.ephemeral_key_generate_failed",
1065 "generate ephemeral Ed25519 host key".to_string(),
1066 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
1067 )
1068 .with_source(std::sync::Arc::new(reason))
1069}
1070
1071pub fn serve_listener_clone_failed(
1074 reason: impl std::error::Error + Send + Sync + 'static,
1075) -> Diagnostic {
1076 let reason_text = reason.to_string();
1077 Diagnostic::new(
1078 "ssh.serve.listener_clone_failed",
1079 "SSH_SERVE cannot clone its listener".to_string(),
1080 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
1081 )
1082 .with_source(std::sync::Arc::new(reason))
1083}
1084
1085pub fn serve_spawn_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
1088 let reason_text = reason.to_string();
1089 Diagnostic::new(
1090 "ssh.serve.spawn_failed",
1091 "SSH_SERVE cannot spawn the server thread".to_string(),
1092 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
1093 )
1094 .with_source(std::sync::Arc::new(reason))
1095}
1096
1097pub fn exec_option_must_be_positive(option: &str) -> Diagnostic {
1101 let message = format!("SSH_EXEC option '{option}' must be positive");
1102 Diagnostic::new(
1103 "ssh.exec.option_must_be_positive",
1104 message,
1105 Diagnostic::make_fields(&[(FIELD_OPTION, option)]),
1106 )
1107}
1108
1109pub fn option_must_be_duration(
1114 func: &str,
1115 option: &str,
1116 got: &str,
1117 reason: Diagnostic,
1118) -> Diagnostic {
1119 let reason_text = reason.to_string();
1120 let message = format!("{func} option '{option}' must be a duration, got {got:?}");
1121 Diagnostic::new(
1122 "ssh.option.must_be_duration",
1123 message,
1124 Diagnostic::make_fields(&[
1125 (FIELD_FUNC, func),
1126 (FIELD_OPTION, option),
1127 (FIELD_GOT, got),
1128 (FIELD_ERR, &reason_text),
1129 ]),
1130 )
1131 .with_source(std::sync::Arc::new(reason))
1132}
1133
1134pub fn exec_failed(reason: Diagnostic) -> Diagnostic {
1137 let reason_text = reason.to_string();
1138 Diagnostic::new(
1139 "ssh.exec.failed",
1140 "SSH_EXEC failed".to_string(),
1141 Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
1142 )
1143 .with_source(std::sync::Arc::new(reason))
1144}
1145
1146pub fn send_exit_status_negative(code: i64) -> Diagnostic {
1149 let message = format!("SSH_SEND_EXIT_STATUS code must not be negative: {code}");
1150 Diagnostic::new(
1151 "ssh.send_exit_status.negative",
1152 message,
1153 Diagnostic::make_fields(&[(FIELD_CODE, &code.to_string())]),
1154 )
1155}
1156
1157pub fn send_exit_status_out_of_range(code: i64) -> Diagnostic {
1160 let message = format!("SSH_SEND_EXIT_STATUS code out of range: {code}");
1161 Diagnostic::new(
1162 "ssh.send_exit_status.out_of_range",
1163 message,
1164 Diagnostic::make_fields(&[(FIELD_CODE, &code.to_string())]),
1165 )
1166}
1167
1168#[cfg(test)]
1169mod tests {
1170 use super::*;
1171
1172 fn io_err(msg: &str) -> std::io::Error {
1173 std::io::Error::other(msg.to_string())
1174 }
1175
1176 fn reason_sample(msg: &str) -> Diagnostic {
1177 Diagnostic::new(
1178 "test.reason.sample",
1179 msg.to_string(),
1180 Diagnostic::make_fields(&[]),
1181 )
1182 }
1183
1184 #[test]
1185 fn each_failure_has_a_distinct_key() {
1186 let err = io_err("boom");
1187 let keys = [
1188 key_path_escapes("SSH_SERVE", "k").key(),
1189 host_key_resolver_failed("SSH_SERVE", &reason_sample("boom")).key(),
1190 host_key_generate_failed(io_err("boom")).key(),
1191 host_key_encode_failed(io_err("boom")).key(),
1192 key_guard_failed("SSH_SERVE", &reason_sample("boom")).key(),
1193 key_path_join_failed("SSH_SERVE", "k", &reason_sample("boom")).key(),
1194 host_key_stat_failed("p", &reason_sample("boom")).key(),
1195 host_key_read_failed("SSH_SERVE", "p", &reason_sample("boom")).key(),
1196 host_key_parse_failed("SSH_SERVE", "p", io_err("boom")).key(),
1197 host_key_write_failed(&reason_sample("boom")).key(),
1198 host_key_must_be_ed25519("SSH_SERVE", "p", &"Ed25519").key(),
1199 host_key_insecure_permissions("SSH_SERVE", "p", 0o644).key(),
1200 pump_output_lock_poisoned().key(),
1201 pump_input_read_failed(&err).key(),
1202 pump_worker_panicked("input worker").key(),
1203 pump_interrupted_by_cancellation().key(),
1204 pump_source_read_failed(&err).key(),
1205 pump_target_lock_poisoned().key(),
1206 pump_func_input_read_failed("SSH run", &err).key(),
1207 pump_output_write_failed(io_err("boom")).key(),
1208 pump_output_flush_failed(io_err("boom")).key(),
1209 pump_borrow_input_failed(pump_output_lock_poisoned()).key(),
1210 pump_borrow_output_failed(pump_output_lock_poisoned()).key(),
1211 pump_wait_runtime_failed(io_err("boom")).key(),
1212 pump_borrow_source_failed(pump_output_lock_poisoned()).key(),
1213 pump_borrow_target_failed(pump_output_lock_poisoned()).key(),
1214 pump_target_write_failed(io_err("boom")).key(),
1215 pump_target_flush_failed(io_err("boom")).key(),
1216 authentication_rejected("SSH_EXEC").key(),
1217 dial_failed("SSH_EXEC", &reason_sample("boom")).key(),
1218 auth_exchange_failed("SSH_EXEC", io_err("boom")).key(),
1219 channel_open_failed("SSH_EXEC", io_err("boom")).key(),
1220 exec_request_failed(io_err("boom")).key(),
1221 shell_request_failed(io_err("boom")).key(),
1222 listener_prepare_failed(io_err("boom")).key(),
1223 listener_handoff_failed(io_err("boom")).key(),
1224 listener_failed(io_err("boom")).key(),
1225 client_runtime_build_failed(io_err("boom")).key(),
1226 session_pump_already_finished().key(),
1227 session_wire_queue_saturated().key(),
1228 session_wire_writer_gone().key(),
1229 session_already_pumped().key(),
1230 channel_data_unknown().key(),
1231 channel_pump_gone().key(),
1232 channel_wire_gone_eof_close().key(),
1233 channel_request_unknown().key(),
1234 channel_already_announced().key(),
1235 run_child_read_failed("stdout", &err).key(),
1236 run_output_write_failed(io_err("boom")).key(),
1237 run_output_flush_failed(io_err("boom")).key(),
1238 run_output_lock_poisoned().key(),
1239 run_needs_program().key(),
1240 run_spawn_lost_stderr().key(),
1241 run_interrupted_by_cancellation().key(),
1242 run_borrow_input_failed(pump_output_lock_poisoned()).key(),
1243 run_borrow_output_failed(pump_output_lock_poisoned()).key(),
1244 run_borrow_error_failed(pump_output_lock_poisoned()).key(),
1245 run_spawn_failed(pump_output_lock_poisoned()).key(),
1246 pty_output_lock_poisoned().key(),
1247 pty_interrupted_by_cancellation().key(),
1248 pty_borrow_input_failed(pump_output_lock_poisoned()).key(),
1249 pty_borrow_output_failed(pump_output_lock_poisoned()).key(),
1250 pty_session_init_write_failed(io_err("boom")).key(),
1251 pty_session_init_flush_failed(io_err("boom")).key(),
1252 dequeue_task_cancelled("SSH_DEQUEUE").key(),
1253 dequeue_server_closed("SSH_DEQUEUE").key(),
1254 dequeue_requires_async().key(),
1255 accept_requires_async().key(),
1256 pump_channel_requires_async().key(),
1257 connect_requires_async().key(),
1258 connect_offline().key(),
1259 connect_username_empty().key(),
1260 connect_failed(pump_output_lock_poisoned()).key(),
1261 pty_run_requires_async().key(),
1262 run_requires_async().key(),
1263 exec_needs_program().key(),
1264 exec_username_empty().key(),
1265 exec_offline().key(),
1266 exec_failed(pump_output_lock_poisoned()).key(),
1267 serve_option_empty("username").key(),
1268 serve_option_required("username").key(),
1269 option_must_be_duration(
1270 "SSH_SERVE",
1271 "idle_timeout",
1272 "x",
1273 pump_output_lock_poisoned(),
1274 )
1275 .key(),
1276 ephemeral_key_generate_failed(io_err("boom")).key(),
1277 serve_listener_clone_failed(io_err("boom")).key(),
1278 serve_spawn_failed(io_err("boom")).key(),
1279 exec_option_must_be_positive("timeout").key(),
1280 send_exit_status_negative(-1).key(),
1281 send_exit_status_out_of_range(1 << 40).key(),
1282 ];
1283 let mut seen = std::collections::HashSet::new();
1284 for k in keys {
1285 assert!(seen.insert(k), "duplicate key {k}");
1286 }
1287 }
1288
1289 #[test]
1290 fn shared_modes_keep_one_key() {
1291 assert_eq!(
1292 authentication_rejected("SSH_EXEC").key(),
1293 authentication_rejected("SSH_CONNECT").key()
1294 );
1295 assert_eq!(
1296 dial_failed("SSH_EXEC", &reason_sample("boom")).key(),
1297 dial_failed("SSH_CONNECT", &reason_sample("boom")).key()
1298 );
1299 assert_eq!(
1300 auth_exchange_failed("SSH_EXEC", io_err("boom")).key(),
1301 auth_exchange_failed("SSH_CONNECT", io_err("boom")).key()
1302 );
1303 assert_eq!(
1304 channel_open_failed("SSH_EXEC", io_err("boom")).key(),
1305 channel_open_failed("SSH_CONNECT", io_err("boom")).key()
1306 );
1307 assert_eq!(
1308 dequeue_task_cancelled("SSH_DEQUEUE").key(),
1309 dequeue_task_cancelled("SSH_ACCEPT").key()
1310 );
1311 assert_eq!(
1312 dequeue_server_closed("SSH_DEQUEUE").key(),
1313 dequeue_server_closed("SSH_ACCEPT").key()
1314 );
1315 assert_eq!(
1316 serve_option_empty("username").key(),
1317 serve_option_empty("password").key()
1318 );
1319 assert_eq!(
1320 serve_option_required("username").key(),
1321 serve_option_required("password").key()
1322 );
1323 assert_eq!(
1324 option_must_be_duration(
1325 "SSH_SERVE",
1326 "idle_timeout",
1327 "x",
1328 pump_output_lock_poisoned(),
1329 )
1330 .key(),
1331 option_must_be_duration("SSH_EXEC", "timeout", "y", pump_output_lock_poisoned()).key()
1332 );
1333 assert_eq!(
1334 exec_option_must_be_positive("timeout").key(),
1335 exec_option_must_be_positive("max_bytes").key()
1336 );
1337 }
1338
1339 #[test]
1340 fn messages_match_canonical_wording() {
1341 let err = io_err("boom");
1342 assert_eq!(
1343 key_path_escapes("SSH_SERVE", "k").message(),
1344 "SSH_SERVE key_path escapes the workspace: k"
1345 );
1346 assert_eq!(
1347 host_key_resolver_failed("SSH_SERVE", &reason_sample("boom")).message(),
1348 "SSH_SERVE cannot open the workspace resolver"
1349 );
1350 assert_eq!(
1351 format!(
1352 "{:#}",
1353 anyhow::Error::new(host_key_resolver_failed(
1354 "SSH_SERVE",
1355 &reason_sample("boom")
1356 ))
1357 ),
1358 "SSH_SERVE cannot open the workspace resolver: boom"
1359 );
1360 assert_eq!(
1361 host_key_generate_failed(io_err("boom")).message(),
1362 "generate Ed25519 host key"
1363 );
1364 assert_eq!(
1365 format!(
1366 "{:#}",
1367 anyhow::Error::new(host_key_generate_failed(io_err("boom")))
1368 ),
1369 "generate Ed25519 host key: boom"
1370 );
1371 assert_eq!(
1372 host_key_encode_failed(io_err("boom")).message(),
1373 "encode Ed25519 host key"
1374 );
1375 assert_eq!(
1376 format!(
1377 "{:#}",
1378 anyhow::Error::new(host_key_encode_failed(io_err("boom")))
1379 ),
1380 "encode Ed25519 host key: boom"
1381 );
1382 assert_eq!(
1383 key_guard_failed("SSH_SERVE", &reason_sample("boom")).message(),
1384 "SSH_SERVE cannot guard the workspace root"
1385 );
1386 assert_eq!(
1387 format!(
1388 "{:#}",
1389 anyhow::Error::new(key_guard_failed("SSH_SERVE", &reason_sample("boom")))
1390 ),
1391 "SSH_SERVE cannot guard the workspace root: boom"
1392 );
1393 assert_eq!(
1394 key_path_join_failed("SSH_SERVE", "k", &reason_sample("boom")).message(),
1395 "SSH_SERVE key_path escapes the workspace: k"
1396 );
1397 assert_eq!(
1398 format!(
1399 "{:#}",
1400 anyhow::Error::new(key_path_join_failed(
1401 "SSH_SERVE",
1402 "k",
1403 &reason_sample("boom")
1404 ))
1405 ),
1406 "SSH_SERVE key_path escapes the workspace: k: boom"
1407 );
1408 assert_eq!(
1409 host_key_stat_failed("p", &reason_sample("boom")).message(),
1410 "cannot stat host key p"
1411 );
1412 assert_eq!(
1413 format!(
1414 "{:#}",
1415 anyhow::Error::new(host_key_stat_failed("p", &reason_sample("boom")))
1416 ),
1417 "cannot stat host key p: boom"
1418 );
1419 assert_eq!(
1420 host_key_read_failed("SSH_SERVE", "p", &reason_sample("boom")).message(),
1421 "SSH_SERVE cannot read host key p"
1422 );
1423 assert_eq!(
1424 format!(
1425 "{:#}",
1426 anyhow::Error::new(host_key_read_failed(
1427 "SSH_SERVE",
1428 "p",
1429 &reason_sample("boom")
1430 ))
1431 ),
1432 "SSH_SERVE cannot read host key p: boom"
1433 );
1434 assert_eq!(
1435 host_key_parse_failed("SSH_SERVE", "p", io_err("boom")).message(),
1436 "SSH_SERVE cannot parse host key p"
1437 );
1438 assert_eq!(
1439 format!(
1440 "{:#}",
1441 anyhow::Error::new(host_key_parse_failed("SSH_SERVE", "p", io_err("boom")))
1442 ),
1443 "SSH_SERVE cannot parse host key p: boom"
1444 );
1445 assert_eq!(
1446 host_key_write_failed(&reason_sample("boom")).message(),
1447 "boom"
1448 );
1449 assert_eq!(host_key_write_failed_fallback("boom").message(), "boom");
1450 assert_eq!(from_anyhow(anyhow::anyhow!("boom")).message(), "boom");
1451 assert_eq!(
1452 from_anyhow(anyhow::anyhow!("boom")).key(),
1453 "ssh.key.write_failed"
1454 );
1455 let framed_keyed = pump_output_lock_poisoned();
1456 let framed_key = framed_keyed.key();
1457 let wrapped = anyhow::Error::new(framed_keyed).context("outer frame");
1458 let reclaimed_framed = from_anyhow(wrapped);
1459 assert_eq!(reclaimed_framed.key(), framed_key);
1460 assert_eq!(reclaimed_framed.contexts(), &["outer frame".to_string()]);
1461 assert_eq!(
1462 host_key_must_be_ed25519("SSH_SERVE", "p", &"Ed25519").message(),
1463 "SSH_SERVE host key p must be Ed25519, found \"Ed25519\""
1464 );
1465 assert_eq!(
1466 host_key_insecure_permissions("SSH_SERVE", "p", 0o644).message(),
1467 "SSH_SERVE insecure permissions on host key file p: expected 0600, got 0644"
1468 );
1469 assert_eq!(
1470 pump_output_lock_poisoned().message(),
1471 "SSH pump output lock poisoned"
1472 );
1473 assert_eq!(
1474 pump_input_read_failed(&err).message(),
1475 "SSH pump input pipe read failed: boom"
1476 );
1477 assert_eq!(
1478 pump_worker_panicked("input worker").message(),
1479 "SSH pump input worker panicked"
1480 );
1481 assert_eq!(
1482 pump_interrupted_by_cancellation().message(),
1483 "SSH pump interrupted by cancellation"
1484 );
1485 assert_eq!(
1486 pump_source_read_failed(&err).message(),
1487 "SSH_PUMP source pipe read failed: boom"
1488 );
1489 assert_eq!(
1490 pump_target_lock_poisoned().message(),
1491 "SSH_PUMP target lock poisoned"
1492 );
1493 assert_eq!(
1494 pump_func_input_read_failed("SSH run", &err).message(),
1495 "SSH run input pipe read failed: boom"
1496 );
1497 assert_eq!(
1498 pump_output_write_failed(io_err("boom")).message(),
1499 "SSH pump output pipe write failed"
1500 );
1501 assert_eq!(
1502 format!(
1503 "{:#}",
1504 anyhow::Error::new(pump_output_write_failed(io_err("boom")))
1505 ),
1506 "SSH pump output pipe write failed: boom"
1507 );
1508 assert_eq!(
1509 pump_output_flush_failed(io_err("boom")).message(),
1510 "SSH pump output pipe flush failed"
1511 );
1512 assert_eq!(
1513 format!(
1514 "{:#}",
1515 anyhow::Error::new(pump_output_flush_failed(io_err("boom")))
1516 ),
1517 "SSH pump output pipe flush failed: boom"
1518 );
1519 assert_eq!(
1520 pump_borrow_input_failed(pump_output_lock_poisoned()).message(),
1521 "SSH pump cannot borrow the input pipe"
1522 );
1523 assert_eq!(
1524 format!(
1525 "{:#}",
1526 anyhow::Error::new(pump_borrow_input_failed(pump_output_lock_poisoned()))
1527 ),
1528 "SSH pump cannot borrow the input pipe: SSH pump output lock poisoned"
1529 );
1530 assert_eq!(
1531 pump_borrow_output_failed(pump_output_lock_poisoned()).message(),
1532 "SSH pump cannot borrow the output pipe"
1533 );
1534 assert_eq!(
1535 format!(
1536 "{:#}",
1537 anyhow::Error::new(pump_borrow_output_failed(pump_output_lock_poisoned()))
1538 ),
1539 "SSH pump cannot borrow the output pipe: SSH pump output lock poisoned"
1540 );
1541 assert_eq!(
1542 pump_wait_runtime_failed(io_err("boom")).message(),
1543 "SSH pump wait runtime failed"
1544 );
1545 assert_eq!(
1546 format!(
1547 "{:#}",
1548 anyhow::Error::new(pump_wait_runtime_failed(io_err("boom")))
1549 ),
1550 "SSH pump wait runtime failed: boom"
1551 );
1552 assert_eq!(
1553 pump_borrow_source_failed(pump_output_lock_poisoned()).message(),
1554 "SSH_PUMP cannot borrow the source pipe"
1555 );
1556 assert_eq!(
1557 format!(
1558 "{:#}",
1559 anyhow::Error::new(pump_borrow_source_failed(pump_output_lock_poisoned()))
1560 ),
1561 "SSH_PUMP cannot borrow the source pipe: SSH pump output lock poisoned"
1562 );
1563 assert_eq!(
1564 pump_borrow_target_failed(pump_output_lock_poisoned()).message(),
1565 "SSH_PUMP cannot borrow the target pipe"
1566 );
1567 assert_eq!(
1568 format!(
1569 "{:#}",
1570 anyhow::Error::new(pump_borrow_target_failed(pump_output_lock_poisoned()))
1571 ),
1572 "SSH_PUMP cannot borrow the target pipe: SSH pump output lock poisoned"
1573 );
1574 assert_eq!(
1575 pump_target_write_failed(io_err("boom")).message(),
1576 "SSH_PUMP target pipe write failed"
1577 );
1578 assert_eq!(
1579 format!(
1580 "{:#}",
1581 anyhow::Error::new(pump_target_write_failed(io_err("boom")))
1582 ),
1583 "SSH_PUMP target pipe write failed: boom"
1584 );
1585 assert_eq!(
1586 pump_target_flush_failed(io_err("boom")).message(),
1587 "SSH_PUMP target pipe flush failed"
1588 );
1589 assert_eq!(
1590 format!(
1591 "{:#}",
1592 anyhow::Error::new(pump_target_flush_failed(io_err("boom")))
1593 ),
1594 "SSH_PUMP target pipe flush failed: boom"
1595 );
1596 assert_eq!(
1597 authentication_rejected("SSH_EXEC").message(),
1598 "SSH_EXEC authentication rejected"
1599 );
1600 assert_eq!(
1601 dial_failed("SSH_EXEC", &reason_sample("boom")).message(),
1602 "SSH_EXEC dial failed"
1603 );
1604 assert_eq!(
1605 format!(
1606 "{:#}",
1607 anyhow::Error::new(dial_failed("SSH_EXEC", &reason_sample("boom")))
1608 ),
1609 "SSH_EXEC dial failed: boom"
1610 );
1611 assert_eq!(
1612 auth_exchange_failed("SSH_EXEC", io_err("boom")).message(),
1613 "SSH_EXEC authentication exchange failed"
1614 );
1615 assert_eq!(
1616 format!(
1617 "{:#}",
1618 anyhow::Error::new(auth_exchange_failed("SSH_EXEC", io_err("boom")))
1619 ),
1620 "SSH_EXEC authentication exchange failed: boom"
1621 );
1622 assert_eq!(
1623 channel_open_failed("SSH_EXEC", io_err("boom")).message(),
1624 "SSH_EXEC channel open failed"
1625 );
1626 assert_eq!(
1627 format!(
1628 "{:#}",
1629 anyhow::Error::new(channel_open_failed("SSH_EXEC", io_err("boom")))
1630 ),
1631 "SSH_EXEC channel open failed: boom"
1632 );
1633 assert_eq!(
1634 exec_request_failed(io_err("boom")).message(),
1635 "SSH_EXEC exec request failed"
1636 );
1637 assert_eq!(
1638 format!(
1639 "{:#}",
1640 anyhow::Error::new(exec_request_failed(io_err("boom")))
1641 ),
1642 "SSH_EXEC exec request failed: boom"
1643 );
1644 assert_eq!(
1645 shell_request_failed(io_err("boom")).message(),
1646 "SSH_CONNECT shell request failed"
1647 );
1648 assert_eq!(
1649 format!(
1650 "{:#}",
1651 anyhow::Error::new(shell_request_failed(io_err("boom")))
1652 ),
1653 "SSH_CONNECT shell request failed: boom"
1654 );
1655 assert_eq!(
1656 listener_prepare_failed(io_err("boom")).message(),
1657 "prepare listener for the runtime"
1658 );
1659 assert_eq!(
1660 format!(
1661 "{:#}",
1662 anyhow::Error::new(listener_prepare_failed(io_err("boom")))
1663 ),
1664 "prepare listener for the runtime: boom"
1665 );
1666 assert_eq!(
1667 listener_handoff_failed(io_err("boom")).message(),
1668 "hand listener to the runtime"
1669 );
1670 assert_eq!(
1671 format!(
1672 "{:#}",
1673 anyhow::Error::new(listener_handoff_failed(io_err("boom")))
1674 ),
1675 "hand listener to the runtime: boom"
1676 );
1677 assert_eq!(
1678 listener_failed(io_err("boom")).message(),
1679 "SSH listener failed"
1680 );
1681 assert_eq!(
1682 format!("{:#}", anyhow::Error::new(listener_failed(io_err("boom")))),
1683 "SSH listener failed: boom"
1684 );
1685 assert_eq!(
1686 client_runtime_build_failed(io_err("boom")).message(),
1687 "build SSH client runtime"
1688 );
1689 assert_eq!(
1690 format!(
1691 "{:#}",
1692 anyhow::Error::new(client_runtime_build_failed(io_err("boom")))
1693 ),
1694 "build SSH client runtime: boom"
1695 );
1696 assert_eq!(
1697 session_pump_already_finished().message(),
1698 "SSH session pump already finished"
1699 );
1700 assert_eq!(
1701 session_wire_queue_saturated().message(),
1702 "SSH session wire queue saturated under backpressure"
1703 );
1704 assert_eq!(
1705 session_wire_writer_gone().message(),
1706 "SSH session wire writer is gone"
1707 );
1708 assert_eq!(
1709 session_already_pumped().message(),
1710 "SSH session already pumped"
1711 );
1712 assert_eq!(channel_data_unknown().message(), "data for unknown channel");
1713 assert_eq!(channel_pump_gone().message(), "pump went away");
1714 assert_eq!(
1715 channel_wire_gone_eof_close().message(),
1716 "wire gone during EOF close"
1717 );
1718 assert_eq!(
1719 channel_request_unknown().message(),
1720 "request for unknown channel"
1721 );
1722 assert_eq!(
1723 channel_already_announced().message(),
1724 "channel already announced"
1725 );
1726 assert_eq!(
1727 run_child_read_failed("stdout", &err).message(),
1728 "SSH run child stdout read failed: boom"
1729 );
1730 assert_eq!(
1731 run_output_write_failed(io_err("boom")).message(),
1732 "SSH run output pipe write failed"
1733 );
1734 assert_eq!(
1735 format!(
1736 "{:#}",
1737 anyhow::Error::new(run_output_write_failed(io_err("boom")))
1738 ),
1739 "SSH run output pipe write failed: boom"
1740 );
1741 assert_eq!(
1742 run_output_flush_failed(io_err("boom")).message(),
1743 "SSH run output pipe flush failed"
1744 );
1745 assert_eq!(
1746 format!(
1747 "{:#}",
1748 anyhow::Error::new(run_output_flush_failed(io_err("boom")))
1749 ),
1750 "SSH run output pipe flush failed: boom"
1751 );
1752 assert_eq!(
1753 run_output_lock_poisoned().message(),
1754 "SSH run output lock poisoned"
1755 );
1756 assert_eq!(
1757 run_needs_program().message(),
1758 "SSH_RUN needs at least a program"
1759 );
1760 assert_eq!(
1761 run_spawn_lost_stderr().message(),
1762 "SSH run spawn lost stderr"
1763 );
1764 assert_eq!(
1765 run_interrupted_by_cancellation().message(),
1766 "SSH run interrupted by cancellation"
1767 );
1768 assert_eq!(
1769 run_borrow_input_failed(pump_output_lock_poisoned()).message(),
1770 "SSH run cannot borrow the input pipe"
1771 );
1772 assert_eq!(
1773 format!(
1774 "{:#}",
1775 anyhow::Error::new(run_borrow_input_failed(pump_output_lock_poisoned()))
1776 ),
1777 "SSH run cannot borrow the input pipe: SSH pump output lock poisoned"
1778 );
1779 assert_eq!(
1780 run_borrow_output_failed(pump_output_lock_poisoned()).message(),
1781 "SSH run cannot borrow the output pipe"
1782 );
1783 assert_eq!(
1784 format!(
1785 "{:#}",
1786 anyhow::Error::new(run_borrow_output_failed(pump_output_lock_poisoned()))
1787 ),
1788 "SSH run cannot borrow the output pipe: SSH pump output lock poisoned"
1789 );
1790 assert_eq!(
1791 run_borrow_error_failed(pump_output_lock_poisoned()).message(),
1792 "SSH run cannot borrow the error pipe"
1793 );
1794 assert_eq!(
1795 format!(
1796 "{:#}",
1797 anyhow::Error::new(run_borrow_error_failed(pump_output_lock_poisoned()))
1798 ),
1799 "SSH run cannot borrow the error pipe: SSH pump output lock poisoned"
1800 );
1801 assert_eq!(
1802 run_spawn_failed(pump_output_lock_poisoned()).message(),
1803 "SSH run spawn failed"
1804 );
1805 assert_eq!(
1806 format!(
1807 "{:#}",
1808 anyhow::Error::new(run_spawn_failed(pump_output_lock_poisoned()))
1809 ),
1810 "SSH run spawn failed: SSH pump output lock poisoned"
1811 );
1812 assert_eq!(
1813 pty_output_lock_poisoned().message(),
1814 "SSH pty output lock poisoned"
1815 );
1816 assert_eq!(
1817 pty_interrupted_by_cancellation().message(),
1818 "SSH pty run interrupted by cancellation"
1819 );
1820 assert_eq!(
1821 pty_borrow_input_failed(pump_output_lock_poisoned()).message(),
1822 "SSH pty cannot borrow the input pipe"
1823 );
1824 assert_eq!(
1825 format!(
1826 "{:#}",
1827 anyhow::Error::new(pty_borrow_input_failed(pump_output_lock_poisoned()))
1828 ),
1829 "SSH pty cannot borrow the input pipe: SSH pump output lock poisoned"
1830 );
1831 assert_eq!(
1832 pty_borrow_output_failed(pump_output_lock_poisoned()).message(),
1833 "SSH pty cannot borrow the output pipe"
1834 );
1835 assert_eq!(
1836 format!(
1837 "{:#}",
1838 anyhow::Error::new(pty_borrow_output_failed(pump_output_lock_poisoned()))
1839 ),
1840 "SSH pty cannot borrow the output pipe: SSH pump output lock poisoned"
1841 );
1842 assert_eq!(
1843 pty_session_init_write_failed(io_err("boom")).message(),
1844 "SSH pty session init write failed"
1845 );
1846 assert_eq!(
1847 format!(
1848 "{:#}",
1849 anyhow::Error::new(pty_session_init_write_failed(io_err("boom")))
1850 ),
1851 "SSH pty session init write failed: boom"
1852 );
1853 assert_eq!(
1854 pty_session_init_flush_failed(io_err("boom")).message(),
1855 "SSH pty session init flush failed"
1856 );
1857 assert_eq!(
1858 format!(
1859 "{:#}",
1860 anyhow::Error::new(pty_session_init_flush_failed(io_err("boom")))
1861 ),
1862 "SSH pty session init flush failed: boom"
1863 );
1864 assert_eq!(
1865 dequeue_task_cancelled("SSH_DEQUEUE").message(),
1866 "SSH_DEQUEUE: task cancelled"
1867 );
1868 assert_eq!(
1869 dequeue_server_closed("SSH_ACCEPT").message(),
1870 "SSH_ACCEPT: server is closed"
1871 );
1872 assert_eq!(
1873 dequeue_requires_async().message(),
1874 "SSH_DEQUEUE requires ASYNC: wrap it as LET $t: HANDLE<T> = ASYNC { SSH_DEQUEUE($server.server) }"
1875 );
1876 assert_eq!(
1877 accept_requires_async().message(),
1878 "SSH_ACCEPT requires ASYNC: wrap it as LET $t: HANDLE<T> = ASYNC { SSH_ACCEPT($server, $in, $out) }"
1879 );
1880 assert_eq!(
1881 pump_channel_requires_async().message(),
1882 "SSH_PUMP_CHANNEL requires ASYNC: pump it in its own task after SSH_DEQUEUE"
1883 );
1884 assert_eq!(
1885 connect_requires_async().message(),
1886 "SSH_CONNECT requires ASYNC: run it in its own task beside the SSH_PUMP tasks"
1887 );
1888 assert_eq!(
1889 connect_offline().message(),
1890 "SSH_CONNECT failed: engine running in --offline mode"
1891 );
1892 assert_eq!(
1893 connect_username_empty().message(),
1894 "SSH_CONNECT username must not be empty"
1895 );
1896 assert_eq!(
1897 connect_failed(pump_output_lock_poisoned()).message(),
1898 "SSH_CONNECT failed"
1899 );
1900 assert_eq!(
1901 format!(
1902 "{:#}",
1903 anyhow::Error::new(connect_failed(pump_output_lock_poisoned()))
1904 ),
1905 "SSH_CONNECT failed: SSH pump output lock poisoned"
1906 );
1907 assert_eq!(
1908 pty_run_requires_async().message(),
1909 "SSH_PTY_RUN requires ASYNC: run it in its own task beside the session pump task"
1910 );
1911 assert_eq!(
1912 run_requires_async().message(),
1913 "SSH_RUN requires ASYNC: run it in its own task beside the session pump task"
1914 );
1915 assert_eq!(
1916 exec_needs_program().message(),
1917 "SSH_EXEC needs at least a program"
1918 );
1919 assert_eq!(
1920 exec_username_empty().message(),
1921 "SSH_EXEC username must not be empty"
1922 );
1923 assert_eq!(
1924 exec_offline().message(),
1925 "SSH_EXEC failed: engine running in --offline mode"
1926 );
1927 assert_eq!(
1928 exec_failed(pump_output_lock_poisoned()).message(),
1929 "SSH_EXEC failed"
1930 );
1931 assert_eq!(
1932 format!(
1933 "{:#}",
1934 anyhow::Error::new(exec_failed(pump_output_lock_poisoned()))
1935 ),
1936 "SSH_EXEC failed: SSH pump output lock poisoned"
1937 );
1938 assert_eq!(
1939 serve_option_empty("username").message(),
1940 "SSH_SERVE option 'username' must not be empty"
1941 );
1942 assert_eq!(
1943 serve_option_required("password").message(),
1944 "SSH_SERVE option 'password' is required"
1945 );
1946 assert_eq!(
1947 option_must_be_duration(
1948 "SSH_SERVE",
1949 "idle_timeout",
1950 "soon",
1951 pump_output_lock_poisoned(),
1952 )
1953 .message(),
1954 "SSH_SERVE option 'idle_timeout' must be a duration, got \"soon\""
1955 );
1956 assert_eq!(
1957 format!(
1958 "{:#}",
1959 anyhow::Error::new(option_must_be_duration(
1960 "SSH_SERVE",
1961 "idle_timeout",
1962 "soon",
1963 pump_output_lock_poisoned(),
1964 ))
1965 ),
1966 "SSH_SERVE option 'idle_timeout' must be a duration, got \"soon\": SSH pump output lock poisoned"
1967 );
1968 assert_eq!(
1969 option_must_be_duration("SSH_EXEC", "timeout", "soon", pump_output_lock_poisoned())
1970 .message(),
1971 "SSH_EXEC option 'timeout' must be a duration, got \"soon\""
1972 );
1973 assert_eq!(
1974 ephemeral_key_generate_failed(io_err("boom")).message(),
1975 "generate ephemeral Ed25519 host key"
1976 );
1977 assert_eq!(
1978 format!(
1979 "{:#}",
1980 anyhow::Error::new(ephemeral_key_generate_failed(io_err("boom")))
1981 ),
1982 "generate ephemeral Ed25519 host key: boom"
1983 );
1984 assert_eq!(
1985 serve_listener_clone_failed(io_err("boom")).message(),
1986 "SSH_SERVE cannot clone its listener"
1987 );
1988 assert_eq!(
1989 format!(
1990 "{:#}",
1991 anyhow::Error::new(serve_listener_clone_failed(io_err("boom")))
1992 ),
1993 "SSH_SERVE cannot clone its listener: boom"
1994 );
1995 assert_eq!(
1996 serve_spawn_failed(io_err("boom")).message(),
1997 "SSH_SERVE cannot spawn the server thread"
1998 );
1999 assert_eq!(
2000 format!(
2001 "{:#}",
2002 anyhow::Error::new(serve_spawn_failed(io_err("boom")))
2003 ),
2004 "SSH_SERVE cannot spawn the server thread: boom"
2005 );
2006 assert_eq!(
2007 exec_option_must_be_positive("timeout").message(),
2008 "SSH_EXEC option 'timeout' must be positive"
2009 );
2010 assert_eq!(
2011 send_exit_status_negative(-1).message(),
2012 "SSH_SEND_EXIT_STATUS code must not be negative: -1"
2013 );
2014 assert_eq!(
2015 send_exit_status_out_of_range(1 << 40).message(),
2016 "SSH_SEND_EXIT_STATUS code out of range: 1099511627776"
2017 );
2018 }
2019}