1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Kernel-level stderr stream for real-time error output from pipeline stages.
//!
//! In bash, stderr from ALL pipeline stages streams to the terminal simultaneously.
//! kaish achieves this with `StderrStream` — a cloneable, Send+Sync handle backed
//! by an unbounded mpsc channel. Pipeline stages and external commands write to it
//! concurrently; the kernel drains it to the actual stderr sink.
//!
//! The channel carries raw bytes (`Vec<u8>`), not strings. This avoids UTF-8
//! corruption when multi-byte characters are split across chunk boundaries in
//! external command stderr reads. Lossy decode happens once at the kernel drain
//! site — the presentation boundary.
//!
//! ```text
//! Stage 1 ──┐
//! Stage 2 ──┼──▶ StderrStream (mpsc, bytes) ──▶ drain ──▶ from_utf8_lossy ──▶ output
//! Stage 3 ──┘
//! ```
use mpsc;
/// Cloneable handle to the kernel's stderr output stream.
///
/// Multiple pipeline stages can write concurrently. The receiver side
/// is drained by the kernel after each statement (or by a background task).
///
/// Uses `UnboundedSender` which is `Clone + Send + Sync` — safe across
/// `tokio::spawn` boundaries without `Arc<Mutex<..>>`.
/// Receiving end of the stderr stream.
///
/// Owned by the kernel. Call `drain_lossy()` to collect all pending bytes
/// as a UTF-8 string (with lossy decode at this boundary).
/// Create a new stderr stream pair.