zeptoclaw 0.9.0

Ultra-lightweight personal AI assistant
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
817
818
819
820
821
822
//! Hook system for ZeptoClaw agent loop.
//!
//! Config-driven hooks that fire at specific points in the agent loop:
//!
//! - `before_tool` — before tool execution (can log or block)
//! - `after_tool` — after tool execution (can log)
//! - `on_error` — when a tool fails (can log)
//!
//! # Configuration
//!
//! ```json
//! {
//!     "hooks": {
//!         "enabled": true,
//!         "before_tool": [
//!             { "action": "log", "tools": ["shell"], "level": "warn" },
//!             { "action": "block", "tools": ["shell"], "channels": ["telegram"], "message": "Shell disabled on Telegram" }
//!         ],
//!         "after_tool": [
//!             { "action": "log", "tools": ["*"], "level": "info" }
//!         ],
//!         "on_error": [
//!             { "action": "log", "level": "error" }
//!         ]
//!     }
//! }
//! ```
//!
//! # Example
//!
//! ```rust
//! use zeptoclaw::hooks::{HooksConfig, HookEngine, HookResult, HookAction, HookRule};
//!
//! let config = HooksConfig {
//!     enabled: true,
//!     before_tool: vec![HookRule {
//!         action: HookAction::Block,
//!         tools: vec!["shell".to_string()],
//!         channels: vec!["telegram".to_string()],
//!         message: Some("Shell disabled on Telegram".to_string()),
//!         ..Default::default()
//!     }],
//!     ..Default::default()
//! };
//! let engine = HookEngine::new(config);
//! let result = engine.before_tool("shell", &serde_json::json!({}), "telegram", "chat-1");
//! assert!(matches!(result, HookResult::Block(_)));
//! ```

use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::bus::{MessageBus, OutboundMessage};

// ---------------------------------------------------------------------------
// Hook action enum
// ---------------------------------------------------------------------------

/// What a hook rule does when triggered.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HookAction {
    /// Log the event via tracing.
    Log,
    /// Block the tool from executing (before_tool only).
    Block,
    /// Send a notification message via the message bus.
    Notify,
}

// ---------------------------------------------------------------------------
// Hook rule
// ---------------------------------------------------------------------------

/// A single hook rule that matches tool calls and performs an action.
///
/// Rules are evaluated in order. For `before_tool`, the first `Block` rule
/// that matches wins. `Log` rules always execute (no short-circuit).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct HookRule {
    /// Action to perform.
    pub action: HookAction,
    /// Tool names to match. `["*"]` matches all tools. Empty = match none.
    pub tools: Vec<String>,
    /// Channel names to match. Empty = match all channels.
    pub channels: Vec<String>,
    /// Log level for `Log` action (trace/debug/info/warn/error).
    pub level: Option<String>,
    /// Custom message for `Block` action.
    pub message: Option<String>,
    /// Optional target channel name for `Notify` action.
    /// Falls back to current tool call channel when unset.
    pub channel: Option<String>,
    /// Optional target chat ID for `Notify` action.
    /// Falls back to current tool call chat_id when unset.
    pub chat_id: Option<String>,
}

impl Default for HookRule {
    fn default() -> Self {
        Self {
            action: HookAction::Log,
            tools: vec![],
            channels: vec![],
            level: None,
            message: None,
            channel: None,
            chat_id: None,
        }
    }
}

impl HookRule {
    /// Check if this rule matches the given tool name.
    pub fn matches_tool(&self, tool_name: &str) -> bool {
        self.tools.iter().any(|t| t == "*" || t == tool_name)
    }

    /// Check if this rule matches the given channel name.
    /// Empty channels list means match all.
    pub fn matches_channel(&self, channel_name: &str) -> bool {
        self.channels.is_empty() || self.channels.iter().any(|c| c == "*" || c == channel_name)
    }
}

// ---------------------------------------------------------------------------
// Hooks config
// ---------------------------------------------------------------------------

/// Hooks configuration for `config.json`.
///
/// Controls the hook system that fires at specific points in the agent loop.
///
/// # Defaults
///
/// - `enabled`: `false`
/// - All rule lists: empty
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct HooksConfig {
    /// Master switch for hooks.
    pub enabled: bool,
    /// Rules evaluated before each tool execution.
    pub before_tool: Vec<HookRule>,
    /// Rules evaluated after each tool execution.
    pub after_tool: Vec<HookRule>,
    /// Rules evaluated when a tool returns an error.
    pub on_error: Vec<HookRule>,
}

// ---------------------------------------------------------------------------
// Hook result
// ---------------------------------------------------------------------------

/// Result of evaluating before_tool hooks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HookResult {
    /// Allow the tool to execute.
    Continue,
    /// Block the tool with the given message.
    Block(String),
}

// ---------------------------------------------------------------------------
// Hook engine
// ---------------------------------------------------------------------------

/// Runtime hook engine that evaluates rules from HooksConfig.
///
/// Created once per agent loop iteration and called at 3 points:
/// 1. `before_tool` — before approval gate + tool execution
/// 2. `after_tool` — after successful tool execution
/// 3. `on_error` — after failed tool execution
pub struct HookEngine {
    config: HooksConfig,
    bus: Option<Arc<MessageBus>>,
}

impl HookEngine {
    /// Create a new HookEngine from configuration.
    pub fn new(config: HooksConfig) -> Self {
        Self { config, bus: None }
    }

    /// Attach a message bus for `notify` actions.
    pub fn with_bus(mut self, bus: Arc<MessageBus>) -> Self {
        self.bus = Some(bus);
        self
    }

    fn resolve_notify_target(
        rule: &HookRule,
        current_channel: &str,
        current_chat_id: &str,
    ) -> Option<(String, String)> {
        let target_channel = rule
            .channel
            .as_deref()
            .unwrap_or(current_channel)
            .trim()
            .to_string();
        let target_chat_id = rule
            .chat_id
            .as_deref()
            .unwrap_or(current_chat_id)
            .trim()
            .to_string();

        if target_channel.is_empty() || target_chat_id.is_empty() {
            return None;
        }

        Some((target_channel, target_chat_id))
    }

    fn emit_notify(
        &self,
        hook: &str,
        tool_name: &str,
        rule: &HookRule,
        current_channel: &str,
        current_chat_id: &str,
        message: String,
    ) {
        let Some(bus) = self.bus.as_ref() else {
            tracing::debug!(
                hook = hook,
                tool = tool_name,
                "Hook notify skipped: message bus not configured"
            );
            return;
        };

        let Some((target_channel, target_chat_id)) =
            Self::resolve_notify_target(rule, current_channel, current_chat_id)
        else {
            tracing::warn!(
                hook = hook,
                tool = tool_name,
                channel = current_channel,
                chat_id = current_chat_id,
                "Hook notify skipped: missing channel/chat_id target"
            );
            return;
        };

        let outbound = OutboundMessage::new(&target_channel, &target_chat_id, &message);
        match bus.try_publish_outbound(outbound) {
            Ok(()) => tracing::info!(
                hook = hook,
                tool = tool_name,
                target_channel = %target_channel,
                target_chat_id = %target_chat_id,
                "Hook notify dispatched"
            ),
            Err(error) => tracing::warn!(
                hook = hook,
                tool = tool_name,
                target_channel = %target_channel,
                target_chat_id = %target_chat_id,
                error = %error,
                "Hook notify failed to publish"
            ),
        }
    }

    /// Evaluate before_tool hooks. Returns Block if any matching rule blocks.
    ///
    /// Rules are evaluated in order. `Log` rules execute without stopping.
    /// The first `Block` rule that matches returns immediately.
    pub fn before_tool(
        &self,
        tool_name: &str,
        _args: &serde_json::Value,
        channel: &str,
        chat_id: &str,
    ) -> HookResult {
        if !self.config.enabled {
            return HookResult::Continue;
        }

        for rule in &self.config.before_tool {
            if !rule.matches_tool(tool_name) || !rule.matches_channel(channel) {
                continue;
            }

            match rule.action {
                HookAction::Log => {
                    let level = rule.level.as_deref().unwrap_or("info");
                    match level {
                        "error" => tracing::error!(
                            hook = "before_tool",
                            tool = tool_name,
                            channel = channel,
                            "Hook: tool call"
                        ),
                        "warn" => tracing::warn!(
                            hook = "before_tool",
                            tool = tool_name,
                            channel = channel,
                            "Hook: tool call"
                        ),
                        "debug" => tracing::debug!(
                            hook = "before_tool",
                            tool = tool_name,
                            channel = channel,
                            "Hook: tool call"
                        ),
                        "trace" => tracing::trace!(
                            hook = "before_tool",
                            tool = tool_name,
                            channel = channel,
                            "Hook: tool call"
                        ),
                        _ => tracing::info!(
                            hook = "before_tool",
                            tool = tool_name,
                            channel = channel,
                            "Hook: tool call"
                        ),
                    }
                }
                HookAction::Block => {
                    let msg = rule
                        .message
                        .clone()
                        .unwrap_or_else(|| format!("Tool '{}' blocked by hook", tool_name));
                    tracing::info!(
                        hook = "before_tool",
                        tool = tool_name,
                        channel = channel,
                        "Hook: blocking tool"
                    );
                    return HookResult::Block(msg);
                }
                HookAction::Notify => {
                    let message = rule.message.clone().unwrap_or_else(|| {
                        format!(
                            "Hook notify (before_tool): tool '{}' called in {}:{}",
                            tool_name, channel, chat_id
                        )
                    });
                    self.emit_notify("before_tool", tool_name, rule, channel, chat_id, message);
                }
            }
        }

        HookResult::Continue
    }

    /// Evaluate after_tool hooks (logging only, no blocking).
    pub fn after_tool(
        &self,
        tool_name: &str,
        _result: &str,
        elapsed: std::time::Duration,
        channel: &str,
        chat_id: &str,
    ) {
        if !self.config.enabled {
            return;
        }

        for rule in &self.config.after_tool {
            if !rule.matches_tool(tool_name) || !rule.matches_channel(channel) {
                continue;
            }

            match rule.action {
                HookAction::Log => {
                    let ms = elapsed.as_millis();
                    let level = rule.level.as_deref().unwrap_or("info");
                    match level {
                        "error" => {
                            tracing::error!(hook = "after_tool", tool = tool_name, latency_ms = %ms, "Hook: tool completed")
                        }
                        "warn" => {
                            tracing::warn!(hook = "after_tool", tool = tool_name, latency_ms = %ms, "Hook: tool completed")
                        }
                        "debug" => {
                            tracing::debug!(hook = "after_tool", tool = tool_name, latency_ms = %ms, "Hook: tool completed")
                        }
                        _ => {
                            tracing::info!(hook = "after_tool", tool = tool_name, latency_ms = %ms, "Hook: tool completed")
                        }
                    }
                }
                HookAction::Block => {} // Block is a no-op in after_tool
                HookAction::Notify => {
                    let ms = elapsed.as_millis();
                    let message = rule.message.clone().unwrap_or_else(|| {
                        format!(
                            "Hook notify (after_tool): tool '{}' succeeded in {}ms ({}:{})",
                            tool_name, ms, channel, chat_id
                        )
                    });
                    self.emit_notify("after_tool", tool_name, rule, channel, chat_id, message);
                }
            }
        }
    }

    /// Evaluate on_error hooks (logging only, no blocking).
    pub fn on_error(&self, tool_name: &str, error: &str, channel: &str, chat_id: &str) {
        if !self.config.enabled {
            return;
        }

        for rule in &self.config.on_error {
            if !rule.matches_tool(tool_name) || !rule.matches_channel(channel) {
                continue;
            }

            match rule.action {
                HookAction::Log => {
                    let level = rule.level.as_deref().unwrap_or("error");
                    match level {
                        "warn" => tracing::warn!(
                            hook = "on_error",
                            tool = tool_name,
                            error = error,
                            "Hook: tool error"
                        ),
                        "debug" => tracing::debug!(
                            hook = "on_error",
                            tool = tool_name,
                            error = error,
                            "Hook: tool error"
                        ),
                        _ => tracing::error!(
                            hook = "on_error",
                            tool = tool_name,
                            error = error,
                            "Hook: tool error"
                        ),
                    }
                }
                HookAction::Block => {} // Block is a no-op in on_error
                HookAction::Notify => {
                    let message = rule.message.clone().unwrap_or_else(|| {
                        format!(
                            "Hook notify (on_error): tool '{}' failed: {} ({}:{})",
                            tool_name, error, channel, chat_id
                        )
                    });
                    self.emit_notify("on_error", tool_name, rule, channel, chat_id, message);
                }
            }
        }
    }

    /// Whether hooks are enabled.
    pub fn is_enabled(&self) -> bool {
        self.config.enabled
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // ---- HooksConfig defaults ----

    #[test]
    fn test_hooks_config_default() {
        let config = HooksConfig::default();
        assert!(!config.enabled);
        assert!(config.before_tool.is_empty());
        assert!(config.after_tool.is_empty());
        assert!(config.on_error.is_empty());
    }

    #[test]
    fn test_hooks_config_deserialize() {
        let json = r#"{
            "enabled": true,
            "before_tool": [
                { "action": "log", "tools": ["shell"], "level": "warn" }
            ]
        }"#;
        let config: HooksConfig = serde_json::from_str(json).unwrap();
        assert!(config.enabled);
        assert_eq!(config.before_tool.len(), 1);
        assert_eq!(config.before_tool[0].action, HookAction::Log);
    }

    #[test]
    fn test_hooks_config_serialization_roundtrip() {
        let config = HooksConfig {
            enabled: true,
            before_tool: vec![HookRule {
                action: HookAction::Block,
                tools: vec!["shell".to_string()],
                channels: vec!["telegram".to_string()],
                message: Some("blocked".to_string()),
                ..Default::default()
            }],
            after_tool: vec![HookRule {
                action: HookAction::Log,
                tools: vec!["*".to_string()],
                ..Default::default()
            }],
            on_error: vec![],
        };
        let json = serde_json::to_string(&config).unwrap();
        let deserialized: HooksConfig = serde_json::from_str(&json).unwrap();
        assert!(deserialized.enabled);
        assert_eq!(deserialized.before_tool.len(), 1);
        assert_eq!(deserialized.after_tool.len(), 1);
    }

    // ---- HookRule matching ----

    #[test]
    fn test_hook_rule_matches_tool() {
        let rule = HookRule {
            tools: vec!["shell".to_string()],
            ..Default::default()
        };
        assert!(rule.matches_tool("shell"));
        assert!(!rule.matches_tool("echo"));
    }

    #[test]
    fn test_hook_rule_wildcard_matches_all() {
        let rule = HookRule {
            tools: vec!["*".to_string()],
            ..Default::default()
        };
        assert!(rule.matches_tool("shell"));
        assert!(rule.matches_tool("echo"));
        assert!(rule.matches_tool("anything"));
    }

    #[test]
    fn test_hook_rule_empty_tools_matches_none() {
        let rule = HookRule::default();
        assert!(!rule.matches_tool("shell"));
        assert!(!rule.matches_tool("anything"));
    }

    #[test]
    fn test_hook_rule_matches_channel() {
        let rule = HookRule {
            channels: vec!["telegram".to_string()],
            ..Default::default()
        };
        assert!(rule.matches_channel("telegram"));
        assert!(!rule.matches_channel("discord"));
    }

    #[test]
    fn test_hook_rule_empty_channels_matches_all() {
        let rule = HookRule::default();
        assert!(rule.matches_channel("telegram"));
        assert!(rule.matches_channel("discord"));
        assert!(rule.matches_channel("cli"));
    }

    #[test]
    fn test_hook_rule_channel_wildcard() {
        let rule = HookRule {
            channels: vec!["*".to_string()],
            ..Default::default()
        };
        assert!(rule.matches_channel("telegram"));
        assert!(rule.matches_channel("cli"));
    }

    // ---- HookEngine ----

    #[test]
    fn test_hook_engine_disabled_does_nothing() {
        let config = HooksConfig::default();
        let engine = HookEngine::new(config);
        let result = engine.before_tool("shell", &serde_json::json!({}), "telegram", "chat1");
        assert_eq!(result, HookResult::Continue);
    }

    #[test]
    fn test_hook_engine_before_tool_log() {
        let config = HooksConfig {
            enabled: true,
            before_tool: vec![HookRule {
                action: HookAction::Log,
                tools: vec!["shell".to_string()],
                level: Some("warn".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        };
        let engine = HookEngine::new(config);
        let result = engine.before_tool("shell", &serde_json::json!({"cmd": "ls"}), "cli", "cli");
        assert_eq!(result, HookResult::Continue);
    }

    #[test]
    fn test_hook_engine_before_tool_block() {
        let config = HooksConfig {
            enabled: true,
            before_tool: vec![HookRule {
                action: HookAction::Block,
                tools: vec!["shell".to_string()],
                channels: vec!["telegram".to_string()],
                message: Some("Shell disabled on Telegram".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        };
        let engine = HookEngine::new(config);

        // Should block shell on telegram
        let result = engine.before_tool("shell", &serde_json::json!({}), "telegram", "chat1");
        assert!(matches!(result, HookResult::Block(_)));
        if let HookResult::Block(msg) = result {
            assert_eq!(msg, "Shell disabled on Telegram");
        }

        // Should NOT block shell on CLI
        let result = engine.before_tool("shell", &serde_json::json!({}), "cli", "chat1");
        assert_eq!(result, HookResult::Continue);

        // Should NOT block echo on telegram
        let result = engine.before_tool("echo", &serde_json::json!({}), "telegram", "chat1");
        assert_eq!(result, HookResult::Continue);
    }

    #[test]
    fn test_hook_engine_before_tool_block_default_message() {
        let config = HooksConfig {
            enabled: true,
            before_tool: vec![HookRule {
                action: HookAction::Block,
                tools: vec!["shell".to_string()],
                ..Default::default()
            }],
            ..Default::default()
        };
        let engine = HookEngine::new(config);
        let result = engine.before_tool("shell", &serde_json::json!({}), "cli", "chat1");
        if let HookResult::Block(msg) = result {
            assert!(msg.contains("shell"));
            assert!(msg.contains("blocked by hook"));
        } else {
            panic!("Expected Block");
        }
    }

    #[test]
    fn test_hook_engine_multiple_rules_first_block_wins() {
        let config = HooksConfig {
            enabled: true,
            before_tool: vec![
                HookRule {
                    action: HookAction::Log,
                    tools: vec!["*".to_string()],
                    level: Some("info".to_string()),
                    ..Default::default()
                },
                HookRule {
                    action: HookAction::Block,
                    tools: vec!["shell".to_string()],
                    message: Some("blocked".to_string()),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        let engine = HookEngine::new(config);
        let result = engine.before_tool("shell", &serde_json::json!({}), "cli", "chat1");
        assert!(matches!(result, HookResult::Block(_)));
    }

    #[test]
    fn test_hook_engine_after_tool() {
        let config = HooksConfig {
            enabled: true,
            after_tool: vec![HookRule {
                action: HookAction::Log,
                tools: vec!["*".to_string()],
                level: Some("info".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        };
        let engine = HookEngine::new(config);
        engine.after_tool(
            "shell",
            "result text",
            std::time::Duration::from_millis(50),
            "cli",
            "chat1",
        );
    }

    #[test]
    fn test_hook_engine_on_error() {
        let config = HooksConfig {
            enabled: true,
            on_error: vec![HookRule {
                action: HookAction::Log,
                tools: vec!["*".to_string()],
                level: Some("error".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        };
        let engine = HookEngine::new(config);
        engine.on_error("shell", "command not found", "cli", "chat1");
    }

    #[test]
    fn test_hook_engine_is_enabled() {
        let enabled = HookEngine::new(HooksConfig {
            enabled: true,
            ..Default::default()
        });
        assert!(enabled.is_enabled());

        let disabled = HookEngine::new(HooksConfig::default());
        assert!(!disabled.is_enabled());
    }

    #[tokio::test]
    async fn test_hook_engine_notify_before_tool_publishes_message() {
        use tokio::time::{timeout, Duration};

        let bus = Arc::new(MessageBus::new());
        let config = HooksConfig {
            enabled: true,
            before_tool: vec![HookRule {
                action: HookAction::Notify,
                tools: vec!["shell".to_string()],
                message: Some("manual approval required".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        };
        let engine = HookEngine::new(config).with_bus(Arc::clone(&bus));

        let result = engine.before_tool("shell", &serde_json::json!({}), "telegram", "chat77");
        assert_eq!(result, HookResult::Continue);

        let outbound = timeout(Duration::from_millis(300), bus.consume_outbound())
            .await
            .expect("timed out waiting for outbound message")
            .expect("expected outbound message");
        assert_eq!(outbound.channel, "telegram");
        assert_eq!(outbound.chat_id, "chat77");
        assert_eq!(outbound.content, "manual approval required");
    }

    #[tokio::test]
    async fn test_hook_engine_notify_after_tool_uses_override_target() {
        use tokio::time::{timeout, Duration};

        let bus = Arc::new(MessageBus::new());
        let config = HooksConfig {
            enabled: true,
            after_tool: vec![HookRule {
                action: HookAction::Notify,
                tools: vec!["*".to_string()],
                channel: Some("slack".to_string()),
                chat_id: Some("ops-room".to_string()),
                message: Some("tool completed".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        };
        let engine = HookEngine::new(config).with_bus(Arc::clone(&bus));

        engine.after_tool(
            "echo",
            "ok",
            std::time::Duration::from_millis(15),
            "telegram",
            "chat77",
        );

        let outbound = timeout(Duration::from_millis(300), bus.consume_outbound())
            .await
            .expect("timed out waiting for outbound message")
            .expect("expected outbound message");
        assert_eq!(outbound.channel, "slack");
        assert_eq!(outbound.chat_id, "ops-room");
        assert_eq!(outbound.content, "tool completed");
    }

    #[tokio::test]
    async fn test_hook_engine_notify_on_error_default_message_contains_error() {
        use tokio::time::{timeout, Duration};

        let bus = Arc::new(MessageBus::new());
        let config = HooksConfig {
            enabled: true,
            on_error: vec![HookRule {
                action: HookAction::Notify,
                tools: vec!["shell".to_string()],
                ..Default::default()
            }],
            ..Default::default()
        };
        let engine = HookEngine::new(config).with_bus(Arc::clone(&bus));

        engine.on_error("shell", "permission denied", "telegram", "chat77");

        let outbound = timeout(Duration::from_millis(300), bus.consume_outbound())
            .await
            .expect("timed out waiting for outbound message")
            .expect("expected outbound message");
        assert_eq!(outbound.channel, "telegram");
        assert_eq!(outbound.chat_id, "chat77");
        assert!(outbound.content.contains("permission denied"));
        assert!(outbound.content.contains("shell"));
    }
}