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
//! EE-039: Diagnostic command to verify stdout/stderr stream separation.
//!
//! The `ee diag streams` command helps agents verify that the CLI correctly
//! separates machine data (stdout) from diagnostics (stderr). This is critical
//! for agent-native operation where stdout must be parseable as JSON.
use std::io::Write;
/// Report from the streams diagnostic command.
#[derive(Clone, Debug)]
pub struct StreamsReport {
/// Whether stdout is correctly isolated for machine data.
pub stdout_isolated: bool,
/// Whether stderr received the diagnostic probe.
pub stderr_received_probe: bool,
/// The diagnostic message sent to stderr during the test.
pub stderr_probe_message: String,
/// Version of the ee binary.
pub version: &'static str,
}
impl StreamsReport {
/// Gather the streams report by probing stderr.
///
/// This writes a diagnostic message to stderr and reports success.
/// The stdout output will contain only the JSON report.
pub fn gather<W: Write>(stderr: &mut W) -> Self {
let probe_message = "ee diag streams: stderr probe for stream isolation verification";
// Write probe to stderr - this should NOT appear in stdout
let stderr_ok = writeln!(stderr, "{}", probe_message).is_ok();
Self {
stdout_isolated: true, // If we get here, stdout is isolated (JSON goes to stdout)
stderr_received_probe: stderr_ok,
stderr_probe_message: probe_message.to_string(),
version: env!("CARGO_PKG_VERSION"),
}
}
/// Check if the stream separation is working correctly.
#[must_use]
pub const fn is_healthy(&self) -> bool {
self.stdout_isolated && self.stderr_received_probe
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn streams_report_gather_succeeds() {
let mut stderr = Vec::new();
let report = StreamsReport::gather(&mut stderr);
assert!(report.stdout_isolated);
assert!(report.stderr_received_probe);
assert!(!report.stderr_probe_message.is_empty());
assert!(!stderr.is_empty(), "stderr should have received probe");
}
#[test]
fn streams_report_is_healthy_when_both_streams_work() {
let report = StreamsReport {
stdout_isolated: true,
stderr_received_probe: true,
stderr_probe_message: "test".to_string(),
version: "0.1.0",
};
assert!(report.is_healthy());
}
#[test]
fn streams_report_unhealthy_when_stderr_fails() {
let report = StreamsReport {
stdout_isolated: true,
stderr_received_probe: false,
stderr_probe_message: "test".to_string(),
version: "0.1.0",
};
assert!(!report.is_healthy());
}
}