rch-common 1.0.26

Shared types and utilities for Remote Compilation Helper
Documentation
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Claude Code hook protocol definitions.
//!
//! Defines the JSON structures for PreToolUse hook input/output.

use serde::{Deserialize, Serialize};

/// Input received from Claude Code PreToolUse hook.
#[derive(Debug, Clone, Deserialize)]
pub struct HookInput {
    /// The tool being invoked (e.g., "Bash", "Read", "Write").
    pub tool_name: String,
    /// Tool-specific input.
    pub tool_input: ToolInput,
    /// Optional session ID.
    #[serde(default)]
    pub session_id: Option<String>,
}

/// Tool-specific input for Bash commands.
#[derive(Debug, Clone, Deserialize)]
pub struct ToolInput {
    /// The command to execute.
    pub command: String,
    /// Optional description of what the command does.
    #[serde(default)]
    pub description: Option<String>,
}

/// Output sent back to Claude Code from PreToolUse hook.
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum HookOutput {
    /// Allow the command to proceed (empty object or no output).
    Allow(AllowOutput),
    /// Allow with a modified/replaced command (for transparent interception).
    /// Used when RCH has already executed the command remotely and wants to
    /// replace the original command with a no-op for transparency.
    AllowWithModifiedCommand(AllowWithModifiedCommandOutput),
    /// Deny the command with a reason.
    Deny(DenyOutput),
}

/// Empty output to allow command execution.
#[derive(Debug, Clone, Default, Serialize)]
pub struct AllowOutput {}

/// Output to allow with a modified command (for transparent interception).
/// This tells Claude Code "allow this tool, but replace the command with this one".
/// Used when RCH has already executed remotely and wants to substitute a no-op.
#[derive(Debug, Clone, Serialize)]
pub struct AllowWithModifiedCommandOutput {
    #[serde(rename = "hookSpecificOutput")]
    pub hook_specific_output: AllowWithModifiedHookSpecificOutput,
}

/// Hook-specific output for allow-with-modification responses.
#[derive(Debug, Clone, Serialize)]
pub struct AllowWithModifiedHookSpecificOutput {
    #[serde(rename = "hookEventName")]
    pub hook_event_name: String,
    #[serde(rename = "permissionDecision")]
    pub permission_decision: String,
    #[serde(rename = "updatedInput")]
    pub updated_input: UpdatedInput,
}

/// The modified input to substitute.
#[derive(Debug, Clone, Serialize)]
pub struct UpdatedInput {
    /// The replacement command (typically a no-op like "true" or "exit 0").
    pub command: String,
}

/// Output to deny/block command execution.
#[derive(Debug, Clone, Serialize)]
pub struct DenyOutput {
    #[serde(rename = "hookSpecificOutput")]
    pub hook_specific_output: HookSpecificOutput,
}

#[derive(Debug, Clone, Serialize)]
pub struct HookSpecificOutput {
    #[serde(rename = "hookEventName")]
    pub hook_event_name: String,
    #[serde(rename = "permissionDecision")]
    pub permission_decision: String,
    #[serde(rename = "permissionDecisionReason")]
    pub permission_decision_reason: String,
}

impl HookOutput {
    /// Create an allow output (command proceeds normally).
    pub fn allow() -> Self {
        Self::Allow(AllowOutput {})
    }

    /// Create an allow output with a modified/replaced command.
    ///
    /// This is used for transparent interception: RCH has already executed the
    /// command remotely, so we replace it with a no-op to prevent double execution.
    /// The agent sees the remote output but thinks the command ran locally.
    ///
    /// # Arguments
    /// * `replacement_command` - The command to substitute (typically "true" for a no-op)
    pub fn allow_with_modified_command(replacement_command: impl Into<String>) -> Self {
        Self::AllowWithModifiedCommand(AllowWithModifiedCommandOutput {
            hook_specific_output: AllowWithModifiedHookSpecificOutput {
                hook_event_name: "PreToolUse".to_string(),
                permission_decision: "allow".to_string(),
                updated_input: UpdatedInput {
                    command: replacement_command.into(),
                },
            },
        })
    }

    /// Create a deny output with a reason.
    pub fn deny(reason: impl Into<String>) -> Self {
        Self::Deny(DenyOutput {
            hook_specific_output: HookSpecificOutput {
                hook_event_name: "PreToolUse".to_string(),
                permission_decision: "deny".to_string(),
                permission_decision_reason: reason.into(),
            },
        })
    }

    /// Check if this output allows the command.
    pub fn is_allow(&self) -> bool {
        matches!(self, Self::Allow(_) | Self::AllowWithModifiedCommand(_))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_hook_input() {
        let json = r#"{
            "tool_name": "Bash",
            "tool_input": {
                "command": "cargo build --release",
                "description": "Build the project"
            },
            "session_id": "abc123"
        }"#;

        let input: HookInput = serde_json::from_str(json).unwrap();
        assert_eq!(input.tool_name, "Bash");
        assert_eq!(input.tool_input.command, "cargo build --release");
        assert_eq!(input.session_id, Some("abc123".to_string()));
    }

    #[test]
    fn test_allow_output() {
        let output = HookOutput::allow();
        let json = serde_json::to_string(&output).unwrap();
        assert_eq!(json, "{}");
    }

    #[test]
    fn test_deny_output() {
        let output = HookOutput::deny("Remote execution failed");
        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("permissionDecision"));
        assert!(json.contains("deny"));
    }

    #[test]
    fn test_parse_hook_input_minimal() {
        // Parse without optional fields
        let json = r#"{
            "tool_name": "Bash",
            "tool_input": {
                "command": "ls -la"
            }
        }"#;

        let input: HookInput = serde_json::from_str(json).unwrap();
        assert_eq!(input.tool_name, "Bash");
        assert_eq!(input.tool_input.command, "ls -la");
        assert!(input.tool_input.description.is_none());
        assert!(input.session_id.is_none());
    }

    #[test]
    fn test_parse_hook_input_with_empty_description() {
        let json = r#"{
            "tool_name": "Read",
            "tool_input": {
                "command": "cat file.txt",
                "description": ""
            }
        }"#;

        let input: HookInput = serde_json::from_str(json).unwrap();
        assert_eq!(input.tool_name, "Read");
        assert_eq!(input.tool_input.description, Some("".to_string()));
    }

    #[test]
    fn test_hook_output_is_allow_true() {
        let output = HookOutput::allow();
        assert!(output.is_allow());
    }

    #[test]
    fn test_hook_output_is_allow_false_for_deny() {
        let output = HookOutput::deny("blocked");
        assert!(!output.is_allow());
    }

    #[test]
    fn test_deny_output_preserves_reason() {
        let reason = "Command not allowed: security violation";
        let output = HookOutput::deny(reason);

        if let HookOutput::Deny(deny) = output {
            assert_eq!(deny.hook_specific_output.permission_decision_reason, reason);
            assert_eq!(deny.hook_specific_output.permission_decision, "deny");
            assert_eq!(deny.hook_specific_output.hook_event_name, "PreToolUse");
        } else {
            panic!("Expected Deny variant");
        }
    }

    #[test]
    fn test_deny_output_with_empty_reason() {
        let output = HookOutput::deny("");
        if let HookOutput::Deny(deny) = output {
            assert_eq!(deny.hook_specific_output.permission_decision_reason, "");
        } else {
            panic!("Expected Deny variant");
        }
    }

    #[test]
    fn test_allow_output_default() {
        let output = AllowOutput::default();
        let json = serde_json::to_string(&output).unwrap();
        assert_eq!(json, "{}");
    }

    #[test]
    fn test_deny_output_json_structure() {
        let output = HookOutput::deny("test reason");
        let json = serde_json::to_string(&output).unwrap();

        // Verify the exact JSON structure expected by Claude Code
        assert!(json.contains("hookSpecificOutput"));
        assert!(json.contains("hookEventName"));
        assert!(json.contains("PreToolUse"));
        assert!(json.contains("permissionDecision"));
        assert!(json.contains("\"deny\""));
        assert!(json.contains("permissionDecisionReason"));
        assert!(json.contains("test reason"));
    }

    #[test]
    fn test_hook_input_clone() {
        let original = HookInput {
            tool_name: "Bash".to_string(),
            tool_input: ToolInput {
                command: "cargo test".to_string(),
                description: Some("Run tests".to_string()),
            },
            session_id: Some("session-123".to_string()),
        };

        let cloned = original.clone();
        assert_eq!(original.tool_name, cloned.tool_name);
        assert_eq!(original.tool_input.command, cloned.tool_input.command);
        assert_eq!(original.session_id, cloned.session_id);
    }

    #[test]
    fn test_tool_input_clone() {
        let original = ToolInput {
            command: "make build".to_string(),
            description: None,
        };

        let cloned = original.clone();
        assert_eq!(original.command, cloned.command);
        assert_eq!(original.description, cloned.description);
    }

    #[test]
    fn test_hook_output_clone_allow() {
        let original = HookOutput::allow();
        let cloned = original.clone();
        assert!(cloned.is_allow());
    }

    #[test]
    fn test_hook_output_clone_deny() {
        let original = HookOutput::deny("cloned reason");
        let cloned = original.clone();
        assert!(!cloned.is_allow());
    }

    #[test]
    fn test_deny_output_from_string() {
        // Test the Into<String> conversion
        let output = HookOutput::deny(String::from("owned reason"));
        if let HookOutput::Deny(deny) = output {
            assert_eq!(
                deny.hook_specific_output.permission_decision_reason,
                "owned reason"
            );
        } else {
            panic!("Expected Deny variant");
        }
    }

    #[test]
    fn test_parse_hook_input_different_tools() {
        let tools = ["Bash", "Read", "Write", "Edit", "Glob", "Grep"];

        for tool in tools {
            let json = format!(
                r#"{{"tool_name": "{}", "tool_input": {{"command": "test"}}}}"#,
                tool
            );
            let input: HookInput = serde_json::from_str(&json).unwrap();
            assert_eq!(input.tool_name, tool);
        }
    }

    #[test]
    fn test_parse_hook_input_unicode_command() {
        let json = r#"{
            "tool_name": "Bash",
            "tool_input": {
                "command": "echo '日本語 测试 émojis 🦀'"
            }
        }"#;

        let input: HookInput = serde_json::from_str(json).unwrap();
        assert!(input.tool_input.command.contains("日本語"));
        assert!(input.tool_input.command.contains("🦀"));
    }

    #[test]
    fn test_parse_hook_input_special_characters() {
        let json = r#"{
            "tool_name": "Bash",
            "tool_input": {
                "command": "echo \"hello\\nworld\" | grep 'pattern'"
            }
        }"#;

        let input: HookInput = serde_json::from_str(json).unwrap();
        assert!(input.tool_input.command.contains("echo"));
        assert!(input.tool_input.command.contains("grep"));
    }

    #[test]
    fn test_hook_specific_output_debug() {
        let output = HookSpecificOutput {
            hook_event_name: "PreToolUse".to_string(),
            permission_decision: "deny".to_string(),
            permission_decision_reason: "test".to_string(),
        };

        // Verify Debug trait works
        let debug_str = format!("{:?}", output);
        assert!(debug_str.contains("HookSpecificOutput"));
        assert!(debug_str.contains("PreToolUse"));
    }

    #[test]
    fn test_deny_output_debug() {
        let output = DenyOutput {
            hook_specific_output: HookSpecificOutput {
                hook_event_name: "PreToolUse".to_string(),
                permission_decision: "deny".to_string(),
                permission_decision_reason: "test".to_string(),
            },
        };

        let debug_str = format!("{:?}", output);
        assert!(debug_str.contains("DenyOutput"));
    }

    #[test]
    fn test_allow_output_debug() {
        let output = AllowOutput {};
        let debug_str = format!("{:?}", output);
        assert!(debug_str.contains("AllowOutput"));
    }

    // Tests for AllowWithModifiedCommand (transparent interception)

    #[test]
    fn test_allow_with_modified_command_serializes() {
        let output = HookOutput::allow_with_modified_command("true");
        let json = serde_json::to_string(&output).unwrap();

        // Verify the JSON structure expected by Claude Code
        assert!(json.contains("hookSpecificOutput"));
        assert!(json.contains("hookEventName"));
        assert!(json.contains("PreToolUse"));
        assert!(json.contains("permissionDecision"));
        assert!(json.contains("\"allow\""));
        assert!(json.contains("updatedInput"));
        assert!(json.contains("\"command\""));
        assert!(json.contains("\"true\""));
    }

    #[test]
    fn test_allow_with_modified_command_is_allow() {
        let output = HookOutput::allow_with_modified_command("true");
        assert!(output.is_allow());
    }

    #[test]
    fn test_allow_with_modified_command_preserves_replacement() {
        let output = HookOutput::allow_with_modified_command("exit 101");
        if let HookOutput::AllowWithModifiedCommand(allow_mod) = output {
            assert_eq!(
                allow_mod.hook_specific_output.updated_input.command,
                "exit 101"
            );
            assert_eq!(allow_mod.hook_specific_output.permission_decision, "allow");
        } else {
            panic!("Expected AllowWithModifiedCommand variant");
        }
    }

    #[test]
    fn test_allow_with_modified_command_from_string() {
        let output = HookOutput::allow_with_modified_command(String::from("echo done"));
        if let HookOutput::AllowWithModifiedCommand(allow_mod) = output {
            assert_eq!(
                allow_mod.hook_specific_output.updated_input.command,
                "echo done"
            );
        } else {
            panic!("Expected AllowWithModifiedCommand variant");
        }
    }

    #[test]
    fn test_allow_with_modified_command_clone() {
        let original = HookOutput::allow_with_modified_command("true");
        let cloned = original.clone();
        assert!(cloned.is_allow());
        if let HookOutput::AllowWithModifiedCommand(allow_mod) = cloned {
            assert_eq!(allow_mod.hook_specific_output.updated_input.command, "true");
        } else {
            panic!("Expected AllowWithModifiedCommand variant");
        }
    }

    #[test]
    fn test_allow_with_modified_command_json_structure() {
        // Verify exact JSON format expected by Claude Code
        let output = HookOutput::allow_with_modified_command("true");
        let json = serde_json::to_string(&output).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

        assert!(parsed.get("hookSpecificOutput").is_some());
        let hook_output = parsed.get("hookSpecificOutput").unwrap();
        assert_eq!(hook_output.get("hookEventName").unwrap(), "PreToolUse");
        assert_eq!(hook_output.get("permissionDecision").unwrap(), "allow");
        assert!(hook_output.get("updatedInput").is_some());
        let updated_input = hook_output.get("updatedInput").unwrap();
        assert_eq!(updated_input.get("command").unwrap(), "true");
    }
}