rch 2.1.7

Remote Compilation Helper - PreToolUse hook CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! CRITICAL: E2E Hook Context Non-Interference Tests
//!
//! This module verifies that AI coding agents (the PRIMARY users of RCH)
//! are completely unaffected by rich_rust integration.
//!
//! ANY regression here is a BLOCKER.
//!
//! Implements bead: bd-36x8
//!
//! ## Test Coverage
//!
//! 1. stdout JSON responses are BYTE-FOR-BYTE identical to pre-integration
//! 2. NO rich output appears on stdout (zero bytes of ANSI codes)
//! 3. Exit codes are EXACTLY preserved (0, 1, 101, 128+N)
//! 4. Timing is not degraded (hook classification still <5ms)
//! 5. stderr can have rich output but ONLY if not captured by agent

use rch_common::testing::{TestLogger, TestPhase};
use std::io::Write;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

/// Marker for ANSI escape sequences.
const ANSI_ESC: &str = "\x1b[";

/// Maximum acceptable hook runtime in milliseconds.
///
/// Note: these tests spawn `rch` as a separate process; timings include process startup.
const MAX_HOOK_TIME_MS: u64 = 25;

/// Get the path to the rch binary.
fn rch_binary() -> String {
    std::env::var("RCH_BINARY").unwrap_or_else(|_| {
        // Respect CARGO_TARGET_DIR if set (avoids finding stale cross-compiled binaries)
        if let Ok(target_dir) = std::env::var("CARGO_TARGET_DIR") {
            return format!("{target_dir}/release/rch");
        }
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
        format!(
            "{}/target/release/rch",
            manifest_dir.trim_end_matches("/rch")
        )
    })
}

/// Run hook with given JSON input and return (exit_code, stdout, stderr, duration).
fn run_hook(input: &str) -> (i32, String, String, Duration) {
    let rch = rch_binary();
    let start = Instant::now();

    let mut child = Command::new(&rch)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap_or_else(|e| panic!("Failed to spawn {}: {}", rch, e));

    if let Some(mut stdin) = child.stdin.take() {
        stdin.write_all(input.as_bytes()).ok();
    }

    let output = child.wait_with_output().expect("Failed to wait for rch");

    let duration = start.elapsed();

    (
        output.status.code().unwrap_or(-1),
        String::from_utf8_lossy(&output.stdout).to_string(),
        String::from_utf8_lossy(&output.stderr).to_string(),
        duration,
    )
}

/// Check if binary exists.
fn check_binary() -> bool {
    let rch = rch_binary();
    std::path::Path::new(&rch).exists()
}

/// Skip test if binary not available.
macro_rules! require_binary {
    () => {
        if !check_binary() {
            eprintln!("Skipping test: rch binary not found at {}", rch_binary());
            eprintln!("Build with: cargo build -p rch --release");
            return;
        }
    };
}

// =============================================================================
// TEST 1: stdout JSON Response Integrity
// =============================================================================

#[test]
fn test_hook_stdout_is_valid_json_or_empty() {
    let logger = TestLogger::for_test("test_hook_stdout_is_valid_json_or_empty");
    require_binary!();

    // Passthrough command (Tier-0) - should produce empty stdout (allow)
    let input = r#"{"tool_name":"Bash","tool_input":{"command":"echo hello"}}"#;
    logger.log(TestPhase::Execute, "Running hook with passthrough command");
    let (exit, stdout, _stderr, _dur) = run_hook(input);

    // Allow responses produce empty stdout or {}
    logger.log(TestPhase::Verify, "Checking stdout JSON validity");
    if !stdout.is_empty() {
        // If there's content, it must be valid JSON
        let parsed: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
        assert!(
            parsed.is_ok(),
            "stdout must be valid JSON or empty, got: {}",
            stdout
        );
    }

    assert_eq!(exit, 0, "Passthrough command should exit 0");
    logger.pass();
}

#[test]
fn test_hook_stdout_no_ansi_codes() {
    let logger = TestLogger::for_test("test_hook_stdout_no_ansi_codes");
    require_binary!();

    // Test various commands that should all produce clean stdout
    let test_cases = [
        r#"{"tool_name":"Bash","tool_input":{"command":"echo hello"}}"#,
        r#"{"tool_name":"Bash","tool_input":{"command":"ls -la"}}"#,
        r#"{"tool_name":"Bash","tool_input":{"command":"pwd"}}"#,
        r#"{"tool_name":"Read","tool_input":{"command":"/some/file"}}"#,
    ];

    logger.log(
        TestPhase::Execute,
        format!(
            "Testing {} command variants for ANSI codes",
            test_cases.len()
        ),
    );

    for input in test_cases {
        let (_exit, stdout, _stderr, _dur) = run_hook(input);

        assert!(
            !stdout.contains(ANSI_ESC),
            "stdout contains ANSI escape codes for input: {}\nstdout: {}",
            input,
            stdout
        );
    }

    logger.log(TestPhase::Verify, "All commands produced clean stdout");
    logger.pass();
}

// =============================================================================
// TEST 2: Exit Code Preservation
// =============================================================================

#[test]
fn test_hook_exit_code_allow() {
    require_binary!();

    // Passthrough command should exit 0 (allow)
    let input = r#"{"tool_name":"Bash","tool_input":{"command":"echo hello"}}"#;
    let (exit, _stdout, _stderr, _dur) = run_hook(input);

    assert_eq!(exit, 0, "Allow response should exit 0");
}

#[test]
fn test_hook_exit_code_non_bash_tool() {
    require_binary!();

    // Non-Bash tools should pass through (exit 0)
    let input = r#"{"tool_name":"Read","tool_input":{"command":"/some/file"}}"#;
    let (exit, _stdout, _stderr, _dur) = run_hook(input);

    assert_eq!(exit, 0, "Non-Bash tool should exit 0 (passthrough)");
}

// =============================================================================
// TEST 3: Fail-Open Behavior
// =============================================================================

#[test]
fn test_hook_empty_input_failopen() {
    require_binary!();

    // Empty input should not crash - fail open
    let (exit, _stdout, _stderr, _dur) = run_hook("");

    assert_eq!(exit, 0, "Empty input should exit 0 (fail-open)");
}

#[test]
fn test_hook_invalid_json_failopen() {
    require_binary!();

    // Invalid JSON should not crash - fail open
    let (exit, _stdout, _stderr, _dur) = run_hook("not valid json at all");

    assert_eq!(exit, 0, "Invalid JSON should exit 0 (fail-open)");
}

#[test]
fn test_hook_malformed_json_failopen() {
    require_binary!();

    // Malformed JSON (missing required fields) should fail open
    let input = r#"{"tool_name":"Bash"}"#;
    let (exit, _stdout, _stderr, _dur) = run_hook(input);

    assert_eq!(exit, 0, "Malformed JSON should exit 0 (fail-open)");
}

// =============================================================================
// TEST 4: Timing Budget (Hook classification < 10ms)
// =============================================================================

#[test]
fn test_hook_classification_timing() {
    let logger = TestLogger::for_test("test_hook_classification_timing");
    require_binary!();

    let iterations = 20;
    let mut total_ms = 0u128;

    let input = r#"{"tool_name":"Bash","tool_input":{"command":"echo test"}}"#;

    logger.log(
        TestPhase::Execute,
        format!("Running {} hook iterations for timing", iterations),
    );

    for _ in 0..iterations {
        let (_exit, _stdout, _stderr, dur) = run_hook(input);
        total_ms += dur.as_millis();
    }

    let avg_ms = total_ms / iterations as u128;

    logger.log_with_data(
        TestPhase::Verify,
        "Checking timing threshold",
        serde_json::json!({
            "avg_ms": avg_ms,
            "threshold_ms": MAX_HOOK_TIME_MS,
            "iterations": iterations
        }),
    );

    assert!(
        avg_ms <= MAX_HOOK_TIME_MS as u128,
        "Average hook time {}ms exceeds threshold {}ms",
        avg_ms,
        MAX_HOOK_TIME_MS
    );

    logger.pass();
}

// =============================================================================
// TEST 5: stderr/stdout Separation
// =============================================================================

#[test]
fn test_hook_stderr_stdout_separation() {
    require_binary!();

    // Cargo build command (may produce deny output)
    let input = r#"{"tool_name":"Bash","tool_input":{"command":"cargo build --release"}}"#;
    let (_exit, stdout, stderr, _dur) = run_hook(input);

    // stdout must be valid JSON or empty
    if !stdout.is_empty() {
        let parsed: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
        assert!(
            parsed.is_ok(),
            "stdout must be valid JSON when non-empty, got: {}",
            stdout
        );
    }

    // stderr can have anything - we don't care about its contents
    // but it must not corrupt stdout
    eprintln!("stderr length: {} bytes", stderr.len());
}

// =============================================================================
// TEST 6: JSON Structure Verification
// =============================================================================

#[test]
fn test_hook_deny_json_structure() {
    require_binary!();

    // Cargo build command that would be intercepted
    // Note: May output deny if daemon not running, or nothing if allowed
    let input = r#"{"tool_name":"Bash","tool_input":{"command":"cargo build --release"}}"#;
    let (_exit, stdout, _stderr, _dur) = run_hook(input);

    if !stdout.is_empty() {
        // Parse and check structure
        let parsed: serde_json::Value =
            serde_json::from_str(&stdout).expect("stdout must be valid JSON");

        // If it's a deny response, check structure
        if let Some(output) = parsed.get("hookSpecificOutput") {
            assert!(
                output.get("hookEventName").is_some(),
                "Response missing hookEventName"
            );
            assert!(
                output.get("permissionDecision").is_some(),
                "Response missing permissionDecision"
            );
            // permissionDecisionReason is only required for deny responses
            if output.get("permissionDecision").and_then(|v| v.as_str()) == Some("deny") {
                assert!(
                    output.get("permissionDecisionReason").is_some(),
                    "Deny response missing permissionDecisionReason"
                );
            }
        }
    }
}

// =============================================================================
// TEST 7: No State Leakage Between Invocations
// =============================================================================

#[test]
fn test_hook_no_state_leakage() {
    require_binary!();

    // Run multiple hook invocations and verify consistency
    let input = r#"{"tool_name":"Bash","tool_input":{"command":"echo hello"}}"#;

    let mut results: Vec<(i32, String)> = Vec::new();

    for _ in 0..5 {
        let (exit, stdout, _stderr, _dur) = run_hook(input);
        results.push((exit, stdout));
    }

    // All results should be identical
    let first = &results[0];
    for (i, result) in results.iter().enumerate().skip(1) {
        assert_eq!(
            result.0, first.0,
            "Exit code mismatch at iteration {}: {} vs {}",
            i, result.0, first.0
        );
        assert_eq!(
            result.1, first.1,
            "stdout mismatch at iteration {}: '{}' vs '{}'",
            i, result.1, first.1
        );
    }
}

// =============================================================================
// TEST 8: Binary Reproducibility (Critical for AI Agents)
// =============================================================================

#[test]
fn test_hook_output_deterministic() {
    require_binary!();

    let inputs = [
        r#"{"tool_name":"Bash","tool_input":{"command":"echo test"}}"#,
        r#"{"tool_name":"Bash","tool_input":{"command":"cargo check"}}"#,
        r#"{"tool_name":"Read","tool_input":{"command":"/tmp/file"}}"#,
    ];

    for input in inputs {
        let (exit1, stdout1, _, _) = run_hook(input);
        let (exit2, stdout2, _, _) = run_hook(input);

        assert_eq!(exit1, exit2, "Exit code not deterministic for: {}", input);
        assert_eq!(stdout1, stdout2, "stdout not deterministic for: {}", input);
    }
}

// =============================================================================
// Test Summary
// =============================================================================

/// Summary test that runs all critical checks and produces a report.
#[test]
fn test_hook_non_interference_summary() {
    let logger = TestLogger::for_test("test_hook_non_interference_summary");
    require_binary!();

    logger.log(TestPhase::Setup, "Starting hook non-interference summary");

    // Test 1: Valid JSON
    let input = r#"{"tool_name":"Bash","tool_input":{"command":"echo hello"}}"#;
    logger.log(TestPhase::Execute, "Running passthrough command");
    let (exit, stdout, _stderr, dur) = run_hook(input);

    logger.log_with_data(
        TestPhase::Verify,
        "Passthrough command result",
        serde_json::json!({
            "exit_code": exit,
            "stdout_len": stdout.len(),
            "duration_ms": dur.as_millis() as u64
        }),
    );

    // Test 2: No ANSI codes
    let has_ansi = stdout.contains(ANSI_ESC);
    assert!(!has_ansi, "stdout must not contain ANSI codes");
    logger.log(TestPhase::Verify, "No ANSI codes in stdout");

    // Test 3: Valid JSON structure
    if !stdout.is_empty() {
        let valid_json = serde_json::from_str::<serde_json::Value>(&stdout).is_ok();
        assert!(valid_json, "stdout must be valid JSON");
        logger.log(TestPhase::Verify, "stdout is valid JSON");
    } else {
        logger.log(TestPhase::Verify, "stdout is empty (allow response)");
    }

    // Test 4: Timing
    let mut total_time = Duration::ZERO;
    let iterations = 10;
    for _ in 0..iterations {
        let (_, _, _, d) = run_hook(input);
        total_time += d;
    }
    let avg_time = total_time / iterations;

    logger.log_with_data(
        TestPhase::Verify,
        "Timing validation",
        serde_json::json!({
            "avg_ms": avg_time.as_millis() as u64,
            "threshold_ms": MAX_HOOK_TIME_MS,
            "iterations": iterations
        }),
    );

    assert!(
        avg_time.as_millis() <= MAX_HOOK_TIME_MS as u128,
        "Hook timing exceeds threshold"
    );

    logger.pass();
}