sombrax_agentic_core 0.1.1

SombraX Agentic Core (SAC) — a provider-agnostic Rust library for building LLM agents: content-modifying hooks, tool/MCP integration, and context optimization.
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//! Built-in hook implementations
//!
//! Provides commonly used hooks out of the box.

use crate::context::HookContext;
use crate::error::{HookError, HookResult, HookStage};
use crate::hook::{Hook, ToolCallDecision};
use crate::message::{validate_tool_result_ids, Message};
#[allow(unused_imports)]
use crate::provider::CompletionResponse;
use std::collections::HashSet;
use std::path::PathBuf;

/// A simple logging hook that logs messages and responses
#[derive(Clone, Debug, Default)]
pub struct LoggingHook {
    /// Log level for messages
    log_level: LogLevel,
}

/// Log level for the logging hook
#[derive(Clone, Debug, Default)]
pub enum LogLevel {
    /// Debug level
    Debug,
    /// Info level (default)
    #[default]
    Info,
    /// Warn level
    Warn,
}

impl LoggingHook {
    /// Create a new logging hook
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the log level
    pub fn with_level(mut self, level: LogLevel) -> Self {
        self.log_level = level;
        self
    }
}

impl Hook for LoggingHook {
    async fn pre_completion(
        &self,
        message: Message,
        _history: &[Message],
        ctx: &mut HookContext,
    ) -> HookResult<Message> {
        match self.log_level {
            LogLevel::Debug => {
                tracing::debug!(
                    request_id = %ctx.request_id,
                    message = ?message.text(),
                    "pre_completion: processing message"
                );
            }
            LogLevel::Info => {
                tracing::info!(
                    request_id = %ctx.request_id,
                    message_len = %message.content_length(),
                    "pre_completion: processing message"
                );
            }
            LogLevel::Warn => {
                tracing::warn!(
                    request_id = %ctx.request_id,
                    "pre_completion: processing message"
                );
            }
        }
        Ok(message)
    }

    async fn post_completion_message(
        &self,
        message: Message,
        ctx: &mut HookContext,
    ) -> HookResult<Message> {
        match self.log_level {
            LogLevel::Debug => {
                tracing::debug!(
                    request_id = %ctx.request_id,
                    elapsed_ms = %ctx.elapsed().as_millis(),
                    response = ?message.text(),
                    "post_completion: received response"
                );
            }
            LogLevel::Info => {
                tracing::info!(
                    request_id = %ctx.request_id,
                    elapsed_ms = %ctx.elapsed().as_millis(),
                    response_len = %message.content_length(),
                    "post_completion: received response"
                );
            }
            LogLevel::Warn => {
                tracing::warn!(
                    request_id = %ctx.request_id,
                    elapsed_ms = %ctx.elapsed().as_millis(),
                    "post_completion: received response"
                );
            }
        }
        Ok(message)
    }

    async fn on_assistant_message(
        &self,
        message: &Message,
        ctx: &mut HookContext,
    ) -> HookResult<()> {
        let text = message.text();
        let has_tool_calls = message.has_tool_calls();

        match self.log_level {
            LogLevel::Debug => {
                tracing::debug!(
                    request_id = %ctx.request_id,
                    text = %text,
                    has_tool_calls = %has_tool_calls,
                    "assistant message"
                );
            }
            LogLevel::Info => {
                if !text.is_empty() {
                    tracing::info!(
                        request_id = %ctx.request_id,
                        text_len = %text.len(),
                        has_tool_calls = %has_tool_calls,
                        "assistant message"
                    );
                }
            }
            LogLevel::Warn => {
                if !text.is_empty() {
                    tracing::warn!(
                        request_id = %ctx.request_id,
                        has_tool_calls = %has_tool_calls,
                        "assistant message"
                    );
                }
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "LoggingHook"
    }
}

/// A hook that adds a prefix to all user messages
#[derive(Clone, Debug)]
pub struct PrefixHook {
    prefix: String,
}

impl PrefixHook {
    /// Create a new prefix hook
    pub fn new(prefix: impl Into<String>) -> Self {
        Self {
            prefix: prefix.into(),
        }
    }
}

impl Hook for PrefixHook {
    async fn pre_completion(
        &self,
        mut message: Message,
        _history: &[Message],
        _ctx: &mut HookContext,
    ) -> HookResult<Message> {
        message.prepend_text(&self.prefix);
        Ok(message)
    }

    fn name(&self) -> &str {
        "PrefixHook"
    }
}

/// A hook that adds a suffix to all user messages
#[derive(Clone, Debug)]
pub struct SuffixHook {
    suffix: String,
}

impl SuffixHook {
    /// Create a new suffix hook
    pub fn new(suffix: impl Into<String>) -> Self {
        Self {
            suffix: suffix.into(),
        }
    }
}

impl Hook for SuffixHook {
    async fn pre_completion(
        &self,
        mut message: Message,
        _history: &[Message],
        _ctx: &mut HookContext,
    ) -> HookResult<Message> {
        message.append_text(&self.suffix);
        Ok(message)
    }

    fn name(&self) -> &str {
        "SuffixHook"
    }
}

/// A hook that validates messages according to message validation rules
///
/// Validation rules enforced:
/// - User messages must have non-empty content
/// - Assistant messages must have non-empty content
/// - Text content must not be empty or whitespace-only
/// - Tool result IDs must match previous tool call IDs in history
/// - Tool calls must reference registered tools (when tool names are configured)
///
/// # Example
///
/// ```ignore
/// use rig_agent::hook::builtin::ValidationHook;
///
/// let hook = ValidationHook::new();
///
/// // Or with registered tool names for tool call validation
/// let hook = ValidationHook::with_tools(vec!["get_weather", "search"]);
/// ```
#[derive(Clone, Debug, Default)]
pub struct ValidationHook {
    /// Registered tool names for tool call validation
    registered_tools: HashSet<String>,
}

impl ValidationHook {
    /// Create a new validation hook
    ///
    /// This will validate:
    /// - Non-empty message content
    /// - Non-empty text
    /// - Tool result ID matching against history
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a validation hook with registered tool names
    ///
    /// This additionally validates that tool calls reference known tools.
    pub fn with_tools<I, S>(tools: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            registered_tools: tools.into_iter().map(|s| s.into()).collect(),
        }
    }

    /// Add a tool name to the set of registered tools
    pub fn add_tool(&mut self, tool_name: impl Into<String>) {
        self.registered_tools.insert(tool_name.into());
    }
}

impl Hook for ValidationHook {
    async fn pre_completion(
        &self,
        message: Message,
        history: &[Message],
        _ctx: &mut HookContext,
    ) -> HookResult<Message> {
        // Validate message content (non-empty, non-whitespace text)
        if let Err(errors) = message.validate() {
            let error_msgs: Vec<String> = errors.iter().map(|e| e.to_string()).collect();
            return Err(HookError::hook_failed(
                "ValidationHook",
                HookStage::PreCompletion,
                error_msgs.join("; "),
            ));
        }

        // Validate tool result IDs match previous tool calls
        if let Err(errors) = validate_tool_result_ids(&message, history) {
            let error_msgs: Vec<String> = errors.iter().map(|e| e.to_string()).collect();
            return Err(HookError::hook_failed(
                "ValidationHook",
                HookStage::PreCompletion,
                error_msgs.join("; "),
            ));
        }

        Ok(message)
    }

    async fn post_completion_message(
        &self,
        message: Message,
        _ctx: &mut HookContext,
    ) -> HookResult<Message> {
        // Validate assistant response content
        if let Err(errors) = message.validate() {
            let error_msgs: Vec<String> = errors.iter().map(|e| e.to_string()).collect();
            return Err(HookError::hook_failed(
                "ValidationHook",
                HookStage::PostCompletion,
                error_msgs.join("; "),
            ));
        }

        // Validate tool calls reference registered tools (if configured)
        if !self.registered_tools.is_empty() {
            let tools_ref: HashSet<&str> =
                self.registered_tools.iter().map(|s| s.as_str()).collect();
            if let Err(errors) = message.validate_tool_calls(&tools_ref) {
                let error_msgs: Vec<String> = errors.iter().map(|e| e.to_string()).collect();
                return Err(HookError::hook_failed(
                    "ValidationHook",
                    HookStage::PostCompletion,
                    error_msgs.join("; "),
                ));
            }
        }

        Ok(message)
    }

    fn name(&self) -> &str {
        "ValidationHook"
    }
}

/// A hook that enforces workspace boundary for file operations.
///
/// This hook intercepts file-related tool calls (read, write, edit, grep, glob)
/// and ensures that all file paths are within the configured workspace directory.
/// This prevents agents from accessing files outside the allowed directory tree.
///
/// # Security
///
/// This hook provides a defense-in-depth measure. The tools themselves also
/// validate paths, but this hook adds an additional layer at the agent level.
///
/// # Example
///
/// ```ignore
/// use sombrax_agentic_core::hook::builtin::WorkspaceBoundaryHook;
/// use std::path::PathBuf;
///
/// // Restrict to current working directory
/// let hook = WorkspaceBoundaryHook::new(PathBuf::from("."));
///
/// // Or allow specific additional directories
/// let hook = WorkspaceBoundaryHook::new(PathBuf::from("."))
///     .allow_path("/usr/share/dict");
///
/// let agent = Agent::builder(model)
///     .hook(hook)
///     .build();
/// ```
#[derive(Clone, Debug)]
pub struct WorkspaceBoundaryHook {
    /// The workspace root directory (canonicalized)
    workspace: PathBuf,
    /// Additional allowed paths outside workspace
    allowed_paths: Vec<PathBuf>,
    /// Tools to check (defaults to file operation tools)
    file_tools: HashSet<String>,
}

impl WorkspaceBoundaryHook {
    /// Create a new workspace boundary hook.
    ///
    /// The workspace path will be canonicalized to resolve symlinks and relative paths.
    pub fn new(workspace: PathBuf) -> Self {
        let canonical = workspace.canonicalize().unwrap_or(workspace);

        // Default file operation tools
        let file_tools: HashSet<String> = ["read", "write", "edit", "grep", "glob"]
            .iter()
            .map(|s| s.to_string())
            .collect();

        Self {
            workspace: canonical,
            allowed_paths: Vec::new(),
            file_tools,
        }
    }

    /// Allow access to an additional path outside the workspace.
    ///
    /// The path will be canonicalized. If canonicalization fails,
    /// the original path is used.
    pub fn allow_path(mut self, path: impl Into<PathBuf>) -> Self {
        let path = path.into();
        let canonical = path.canonicalize().unwrap_or(path);
        self.allowed_paths.push(canonical);
        self
    }

    /// Add a custom tool name to be checked for path validation.
    pub fn add_tool(mut self, tool_name: impl Into<String>) -> Self {
        self.file_tools.insert(tool_name.into());
        self
    }

    /// Check if a path is within the allowed boundaries.
    fn is_path_allowed(&self, path: &str) -> bool {
        let path = PathBuf::from(path);

        // Make path absolute if relative
        let absolute_path = if path.is_absolute() {
            path
        } else {
            self.workspace.join(&path)
        };

        // Try to canonicalize (resolves symlinks and ..)
        let canonical = match absolute_path.canonicalize() {
            Ok(p) => p,
            Err(_) => {
                // For new files, check if parent is allowed
                if let Some(parent) = absolute_path.parent() {
                    if let Ok(parent_canonical) = parent.canonicalize() {
                        // Check parent against workspace and allowed paths
                        if parent_canonical.starts_with(&self.workspace) {
                            return true;
                        }
                        for allowed in &self.allowed_paths {
                            if parent_canonical.starts_with(allowed) {
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
        };

        // Check if within workspace
        if canonical.starts_with(&self.workspace) {
            return true;
        }

        // Check if within any allowed path
        for allowed in &self.allowed_paths {
            if canonical.starts_with(allowed) {
                return true;
            }
        }

        false
    }

    /// Extract path from tool arguments based on tool name.
    fn extract_path(&self, tool_name: &str, args: &serde_json::Value) -> Option<String> {
        match tool_name {
            "read" | "write" | "edit" => args
                .get("file_path")
                .and_then(|v| v.as_str())
                .map(String::from),
            "grep" | "glob" => {
                // These tools have optional "path" parameter
                args.get("path").and_then(|v| v.as_str()).map(String::from)
            }
            _ => None,
        }
    }
}

impl Hook for WorkspaceBoundaryHook {
    async fn pre_tool_call(
        &self,
        tool_name: &str,
        args: serde_json::Value,
        _ctx: &mut HookContext,
    ) -> HookResult<ToolCallDecision> {
        // Only check file operation tools
        if !self.file_tools.contains(tool_name) {
            return Ok(ToolCallDecision::Proceed(args));
        }

        // Extract path from arguments
        if let Some(path) = self.extract_path(tool_name, &args) {
            if !self.is_path_allowed(&path) {
                tracing::warn!(
                    tool = %tool_name,
                    path = %path,
                    workspace = %self.workspace.display(),
                    "Blocked tool call: path outside workspace boundary"
                );
                return Ok(ToolCallDecision::Block(format!(
                    "Access denied: path '{}' is outside the allowed workspace. \
                     Only paths within '{}' are permitted.",
                    path,
                    self.workspace.display()
                )));
            }
        }

        Ok(ToolCallDecision::Proceed(args))
    }

    fn name(&self) -> &str {
        "WorkspaceBoundaryHook"
    }
}

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

    #[tokio::test]
    async fn test_prefix_hook() {
        let hook = PrefixHook::new("[IMPORTANT] ");
        let message = Message::user("Hello");
        let mut ctx = HookContext::new("test-123");

        let result = hook.pre_completion(message, &[], &mut ctx).await.unwrap();
        assert_eq!(result.text(), "[IMPORTANT] Hello");
    }

    #[tokio::test]
    async fn test_suffix_hook() {
        let hook = SuffixHook::new(" [END]");
        let message = Message::user("Hello");
        let mut ctx = HookContext::new("test-123");

        let result = hook.pre_completion(message, &[], &mut ctx).await.unwrap();
        assert_eq!(result.text(), "Hello [END]");
    }

    #[tokio::test]
    async fn test_logging_hook() {
        let hook = LoggingHook::new().with_level(LogLevel::Debug);
        let message = Message::user("Hello");
        let mut ctx = HookContext::new("test-123");

        // Should not modify the message
        let result = hook
            .pre_completion(message.clone(), &[], &mut ctx)
            .await
            .unwrap();
        assert_eq!(result.text(), message.text());
    }

    #[tokio::test]
    async fn test_validation_hook_valid_message() {
        let hook = ValidationHook::new();
        let message = Message::user("Hello, world!");
        let mut ctx = HookContext::new("test-123");

        let result = hook.pre_completion(message.clone(), &[], &mut ctx).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().text(), "Hello, world!");
    }

    #[tokio::test]
    async fn test_validation_hook_empty_content() {
        let hook = ValidationHook::new();
        // Create a message with empty content directly
        let message = Message::User {
            content: vec![],
            id: None,
        };
        let mut ctx = HookContext::new("test-123");

        let result = hook.pre_completion(message, &[], &mut ctx).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("empty content"));
    }

    #[tokio::test]
    async fn test_validation_hook_empty_text() {
        use crate::message::UserContent;

        let hook = ValidationHook::new();
        let message = Message::User {
            content: vec![UserContent::Text {
                text: "   ".to_string(),
            }],
            id: None,
        };
        let mut ctx = HookContext::new("test-123");

        let result = hook.pre_completion(message, &[], &mut ctx).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("empty or whitespace"));
    }

    #[tokio::test]
    async fn test_validation_hook_tool_result_matching() {
        use crate::message::{AssistantContent, ToolCall};

        let hook = ValidationHook::new();

        // Create history with a tool call
        let assistant_msg = Message::Assistant {
            content: vec![AssistantContent::ToolCall(ToolCall::new(
                "call-123",
                "get_weather",
                r#"{"city": "NYC"}"#,
            ))],
            id: None,
            reasoning: None,
        };

        // Create a tool result that matches
        let tool_result = Message::tool_result("call-123", "Sunny, 72°F");
        let mut ctx = HookContext::new("test-123");

        let result = hook
            .pre_completion(tool_result, &[assistant_msg], &mut ctx)
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_validation_hook_unmatched_tool_result() {
        let hook = ValidationHook::new();

        // Create a tool result without matching tool call in history
        let tool_result = Message::tool_result("call-unknown", "Some result");
        let mut ctx = HookContext::new("test-123");

        let result = hook.pre_completion(tool_result, &[], &mut ctx).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("call-unknown"));
        assert!(err.to_string().contains("does not match"));
    }

    #[tokio::test]
    async fn test_validation_hook_unknown_tool_call() {
        use crate::message::{AssistantContent, ToolCall};

        let hook = ValidationHook::with_tools(vec!["get_weather", "search"]);

        // Create an assistant message with an unknown tool call
        let assistant_msg = Message::Assistant {
            content: vec![AssistantContent::ToolCall(ToolCall::new(
                "call-456",
                "unknown_tool",
                "{}",
            ))],
            id: None,
            reasoning: None,
        };
        let mut ctx = HookContext::new("test-123");

        let result = hook.post_completion_message(assistant_msg, &mut ctx).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("unknown_tool"));
    }

    #[tokio::test]
    async fn test_validation_hook_valid_tool_call() {
        use crate::message::{AssistantContent, ToolCall};

        let hook = ValidationHook::with_tools(vec!["get_weather", "search"]);

        // Create an assistant message with a known tool call
        let assistant_msg = Message::Assistant {
            content: vec![AssistantContent::ToolCall(ToolCall::new(
                "call-789",
                "get_weather",
                r#"{"city": "NYC"}"#,
            ))],
            id: None,
            reasoning: None,
        };
        let mut ctx = HookContext::new("test-123");

        let result = hook.post_completion_message(assistant_msg, &mut ctx).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_workspace_boundary_hook_allows_cwd() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let hook = WorkspaceBoundaryHook::new(temp_dir.path().to_path_buf());
        let mut ctx = HookContext::new("test-123");

        // Create a test file path within workspace
        let file_path = temp_dir.path().join("test.txt");
        std::fs::write(&file_path, "test").unwrap();

        let args = serde_json::json!({
            "file_path": file_path.to_string_lossy()
        });

        let result = hook.pre_tool_call("read", args, &mut ctx).await.unwrap();
        assert!(result.should_proceed());
    }

    #[tokio::test]
    async fn test_workspace_boundary_hook_blocks_outside() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let hook = WorkspaceBoundaryHook::new(temp_dir.path().to_path_buf());
        let mut ctx = HookContext::new("test-123");

        // Try to read a file outside workspace
        let args = serde_json::json!({
            "file_path": "/etc/passwd"
        });

        let result = hook.pre_tool_call("read", args, &mut ctx).await.unwrap();
        assert!(!result.should_proceed());
        assert!(result.block_reason().unwrap().contains("outside"));
    }

    #[tokio::test]
    async fn test_workspace_boundary_hook_allows_relative_paths() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let subdir = temp_dir.path().join("subdir");
        std::fs::create_dir(&subdir).unwrap();
        let file_path = subdir.join("test.txt");
        std::fs::write(&file_path, "test").unwrap();

        let hook = WorkspaceBoundaryHook::new(temp_dir.path().to_path_buf());
        let mut ctx = HookContext::new("test-123");

        // Relative path within workspace
        let args = serde_json::json!({
            "file_path": "subdir/test.txt"
        });

        let result = hook.pre_tool_call("read", args, &mut ctx).await.unwrap();
        assert!(result.should_proceed());
    }

    #[tokio::test]
    async fn test_workspace_boundary_hook_blocks_path_traversal() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let hook = WorkspaceBoundaryHook::new(temp_dir.path().to_path_buf());
        let mut ctx = HookContext::new("test-123");

        // Try path traversal attack
        let args = serde_json::json!({
            "file_path": "../../../etc/passwd"
        });

        let result = hook.pre_tool_call("read", args, &mut ctx).await.unwrap();
        assert!(!result.should_proceed());
    }

    #[tokio::test]
    async fn test_workspace_boundary_hook_allows_extra_path() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let extra_dir = tempfile::TempDir::new().unwrap();
        let extra_file = extra_dir.path().join("allowed.txt");
        std::fs::write(&extra_file, "allowed").unwrap();

        let hook =
            WorkspaceBoundaryHook::new(temp_dir.path().to_path_buf()).allow_path(extra_dir.path());
        let mut ctx = HookContext::new("test-123");

        // Access file in allowed extra path
        let args = serde_json::json!({
            "file_path": extra_file.to_string_lossy()
        });

        let result = hook.pre_tool_call("read", args, &mut ctx).await.unwrap();
        assert!(result.should_proceed());
    }

    #[tokio::test]
    async fn test_workspace_boundary_hook_grep_glob_path() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let hook = WorkspaceBoundaryHook::new(temp_dir.path().to_path_buf());
        let mut ctx = HookContext::new("test-123");

        // grep/glob use "path" parameter
        let args = serde_json::json!({
            "pattern": "*.rs",
            "path": "/etc"
        });

        let result = hook.pre_tool_call("grep", args, &mut ctx).await.unwrap();
        assert!(!result.should_proceed());

        let args2 = serde_json::json!({
            "pattern": "*.rs",
            "path": "/etc"
        });

        let result2 = hook.pre_tool_call("glob", args2, &mut ctx).await.unwrap();
        assert!(!result2.should_proceed());
    }

    #[tokio::test]
    async fn test_workspace_boundary_hook_ignores_other_tools() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let hook = WorkspaceBoundaryHook::new(temp_dir.path().to_path_buf());
        let mut ctx = HookContext::new("test-123");

        // Other tools should pass through
        let args = serde_json::json!({
            "command": "ls /etc"
        });

        let result = hook.pre_tool_call("bash", args, &mut ctx).await.unwrap();
        assert!(result.should_proceed());
    }
}