claude-agent 0.2.25

Rust SDK for building AI agents with Anthropic's Claude - Direct API, no CLI dependency
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
//! Hook manager for registering and executing hooks.

use super::{Hook, HookContext, HookEvent, HookInput, HookOutput};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::time::{Duration, timeout};

#[derive(Clone)]
pub struct HookManager {
    hooks: Vec<Arc<dyn Hook>>,
    cache: HashMap<HookEvent, Vec<usize>>,
    default_timeout_secs: u64,
}

impl Default for HookManager {
    fn default() -> Self {
        Self::new()
    }
}

impl HookManager {
    pub fn new() -> Self {
        Self {
            hooks: Vec::new(),
            cache: HashMap::new(),
            default_timeout_secs: 60,
        }
    }

    pub fn timeout(timeout_secs: u64) -> Self {
        Self {
            hooks: Vec::new(),
            cache: HashMap::new(),
            default_timeout_secs: timeout_secs,
        }
    }

    fn rebuild_cache(&mut self) {
        self.cache.clear();
        for event in HookEvent::all() {
            let mut indices: Vec<usize> = self
                .hooks
                .iter()
                .enumerate()
                .filter(|(_, h)| h.events().contains(event))
                .map(|(i, _)| i)
                .collect();
            indices.sort_by_key(|&i| std::cmp::Reverse(self.hooks[i].priority()));
            self.cache.insert(*event, indices);
        }
    }

    pub fn register<H: Hook + 'static>(&mut self, hook: H) {
        self.hooks.push(Arc::new(hook));
        self.rebuild_cache();
    }

    pub fn register_arc(&mut self, hook: Arc<dyn Hook>) {
        self.hooks.push(hook);
        self.rebuild_cache();
    }

    pub fn unregister(&mut self, name: &str) {
        self.hooks.retain(|h| h.name() != name);
        self.rebuild_cache();
    }

    pub fn hook_names(&self) -> Vec<&str> {
        self.hooks.iter().map(|h| h.name()).collect()
    }

    pub fn has_hook(&self, name: &str) -> bool {
        self.hooks.iter().any(|h| h.name() == name)
    }

    #[inline]
    pub fn hooks_for_event(&self, event: HookEvent) -> Vec<&Arc<dyn Hook>> {
        self.cache
            .get(&event)
            .map(|indices| indices.iter().map(|&i| &self.hooks[i]).collect())
            .unwrap_or_default()
    }

    pub async fn execute(
        &self,
        event: HookEvent,
        input: HookInput,
        hook_context: &HookContext,
    ) -> Result<HookOutput, crate::Error> {
        self.execute_hooks::<fn(&str, &HookOutput)>(event, input, hook_context, None)
            .await
    }

    pub async fn execute_with_handler<F>(
        &self,
        event: HookEvent,
        input: HookInput,
        hook_context: &HookContext,
        handler: F,
    ) -> Result<HookOutput, crate::Error>
    where
        F: FnMut(&str, &HookOutput),
    {
        self.execute_hooks(event, input, hook_context, Some(handler))
            .await
    }

    async fn execute_hooks<F>(
        &self,
        event: HookEvent,
        input: HookInput,
        hook_context: &HookContext,
        mut handler: Option<F>,
    ) -> Result<HookOutput, crate::Error>
    where
        F: FnMut(&str, &HookOutput),
    {
        let hooks = self.hooks_for_event(event);

        if hooks.is_empty() {
            return Ok(HookOutput::allow());
        }

        let mut merged_output = HookOutput::allow();

        for hook in hooks {
            if let (Some(matcher), Some(tool_name)) = (hook.tool_matcher(), input.tool_name())
                && !matcher.is_match(tool_name)
            {
                continue;
            }

            let hook_timeout = hook.timeout_secs().min(self.default_timeout_secs);
            let result = timeout(
                Duration::from_secs(hook_timeout),
                hook.execute(input.clone(), hook_context),
            )
            .await;

            let output = match result {
                Ok(Ok(output)) => output,
                Ok(Err(e)) => {
                    if event.can_block() {
                        // Blockable hooks use fail-closed: errors propagate
                        return Err(crate::Error::HookFailed {
                            hook: hook.name().to_string(),
                            reason: e.to_string(),
                        });
                    }
                    // Non-blockable hooks use fail-open: log and continue
                    tracing::warn!(hook = hook.name(), error = %e, "Hook execution failed");
                    continue;
                }
                Err(_) => {
                    if event.can_block() {
                        // Blockable hooks use fail-closed: timeouts propagate
                        return Err(crate::Error::HookTimeout {
                            hook: hook.name().to_string(),
                            duration_secs: hook_timeout,
                        });
                    }
                    // Non-blockable hooks use fail-open: log and continue
                    tracing::warn!(
                        hook = hook.name(),
                        timeout_secs = hook_timeout,
                        "Hook timed out"
                    );
                    continue;
                }
            };

            if let Some(ref mut h) = handler {
                h(hook.name(), &output);
            }
            merged_output = Self::merge_outputs(merged_output, output);

            if !merged_output.continue_execution {
                break;
            }
        }

        Ok(merged_output)
    }

    fn merge_outputs(base: HookOutput, new: HookOutput) -> HookOutput {
        HookOutput {
            continue_execution: base.continue_execution && new.continue_execution,
            stop_reason: new.stop_reason.or(base.stop_reason),
            suppress_logging: base.suppress_logging || new.suppress_logging,
            system_message: new.system_message.or(base.system_message),
            updated_input: new.updated_input.or(base.updated_input),
            additional_context: match (base.additional_context, new.additional_context) {
                (Some(a), Some(b)) => Some(format!("{}\n{}", a, b)),
                (a, b) => a.or(b),
            },
        }
    }
}

impl std::fmt::Debug for HookManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HookManager")
            .field("hook_count", &self.hooks.len())
            .field("hook_names", &self.hook_names())
            .field("default_timeout_secs", &self.default_timeout_secs)
            .finish()
    }
}

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

    struct TestHook {
        name: String,
        events: Vec<HookEvent>,
        priority: i32,
        block: bool,
    }

    impl TestHook {
        fn new(name: impl Into<String>, events: Vec<HookEvent>, priority: i32) -> Self {
            Self {
                name: name.into(),
                events,
                priority,
                block: false,
            }
        }

        fn blocking(name: impl Into<String>, events: Vec<HookEvent>, priority: i32) -> Self {
            Self {
                name: name.into(),
                events,
                priority,
                block: true,
            }
        }
    }

    #[async_trait]
    impl Hook for TestHook {
        fn name(&self) -> &str {
            &self.name
        }

        fn events(&self) -> &[HookEvent] {
            &self.events
        }

        fn priority(&self) -> i32 {
            self.priority
        }

        async fn execute(
            &self,
            _input: HookInput,
            _hook_context: &HookContext,
        ) -> Result<HookOutput, crate::Error> {
            if self.block {
                Ok(HookOutput::block(format!("Blocked by {}", self.name)))
            } else {
                Ok(HookOutput::allow())
            }
        }
    }

    #[tokio::test]
    async fn test_hook_registration() {
        let mut manager = HookManager::new();
        manager.register(TestHook::new("hook1", vec![HookEvent::PreToolUse], 0));
        manager.register(TestHook::new("hook2", vec![HookEvent::PostToolUse], 0));

        assert!(manager.has_hook("hook1"));
        assert!(manager.has_hook("hook2"));
        assert!(!manager.has_hook("hook3"));
        assert_eq!(manager.hook_names().len(), 2);
    }

    #[tokio::test]
    async fn test_hook_unregistration() {
        let mut manager = HookManager::new();
        manager.register(TestHook::new("hook1", vec![HookEvent::PreToolUse], 0));
        manager.register(TestHook::new("hook2", vec![HookEvent::PreToolUse], 0));

        manager.unregister("hook1");

        assert!(!manager.has_hook("hook1"));
        assert!(manager.has_hook("hook2"));
    }

    #[tokio::test]
    async fn test_hooks_for_event() {
        let mut manager = HookManager::new();
        manager.register(TestHook::new("hook1", vec![HookEvent::PreToolUse], 10));
        manager.register(TestHook::new(
            "hook2",
            vec![HookEvent::PreToolUse, HookEvent::PostToolUse],
            5,
        ));
        manager.register(TestHook::new("hook3", vec![HookEvent::SessionStart], 0));

        let pre_hooks = manager.hooks_for_event(HookEvent::PreToolUse);
        assert_eq!(pre_hooks.len(), 2);
        // Check priority order (hook1 has higher priority)
        assert_eq!(pre_hooks[0].name(), "hook1");
        assert_eq!(pre_hooks[1].name(), "hook2");

        let session_hooks = manager.hooks_for_event(HookEvent::SessionStart);
        assert_eq!(session_hooks.len(), 1);
        assert_eq!(session_hooks[0].name(), "hook3");
    }

    #[tokio::test]
    async fn test_execute_allows() {
        let mut manager = HookManager::new();
        manager.register(TestHook::new("hook1", vec![HookEvent::PreToolUse], 0));
        manager.register(TestHook::new("hook2", vec![HookEvent::PreToolUse], 0));

        let input = HookInput::pre_tool_use("session-1", "Read", serde_json::json!({}));
        let hook_context = HookContext::new("session-1");
        let output = manager
            .execute(HookEvent::PreToolUse, input, &hook_context)
            .await
            .unwrap();

        assert!(output.continue_execution);
    }

    #[tokio::test]
    async fn test_execute_blocks() {
        let mut manager = HookManager::new();
        manager.register(TestHook::new("hook1", vec![HookEvent::PreToolUse], 0));
        manager.register(TestHook::blocking(
            "hook2",
            vec![HookEvent::PreToolUse],
            10, // Higher priority, runs first
        ));

        let input = HookInput::pre_tool_use("session-1", "Read", serde_json::json!({}));
        let hook_context = HookContext::new("session-1");
        let output = manager
            .execute(HookEvent::PreToolUse, input, &hook_context)
            .await
            .unwrap();

        assert!(!output.continue_execution);
        assert_eq!(output.stop_reason, Some("Blocked by hook2".to_string()));
    }

    #[tokio::test]
    async fn test_no_hooks_allows() {
        let manager = HookManager::new();

        let input = HookInput::pre_tool_use("session-1", "Read", serde_json::json!({}));
        let hook_context = HookContext::new("session-1");
        let output = manager
            .execute(HookEvent::PreToolUse, input, &hook_context)
            .await
            .unwrap();

        assert!(output.continue_execution);
    }

    // Hook that always fails
    struct FailingHook {
        name: String,
        events: Vec<HookEvent>,
    }

    impl FailingHook {
        fn new(name: impl Into<String>, events: Vec<HookEvent>) -> Self {
            Self {
                name: name.into(),
                events,
            }
        }
    }

    #[async_trait]
    impl Hook for FailingHook {
        fn name(&self) -> &str {
            &self.name
        }

        fn events(&self) -> &[HookEvent] {
            &self.events
        }

        async fn execute(
            &self,
            _input: HookInput,
            _hook_context: &HookContext,
        ) -> Result<HookOutput, crate::Error> {
            Err(crate::Error::Config("Hook failed intentionally".into()))
        }
    }

    // Hook that times out
    struct SlowHook {
        name: String,
        events: Vec<HookEvent>,
    }

    impl SlowHook {
        fn new(name: impl Into<String>, events: Vec<HookEvent>) -> Self {
            Self {
                name: name.into(),
                events,
            }
        }
    }

    #[async_trait]
    impl Hook for SlowHook {
        fn name(&self) -> &str {
            &self.name
        }

        fn events(&self) -> &[HookEvent] {
            &self.events
        }

        fn timeout_secs(&self) -> u64 {
            1 // Short timeout for testing
        }

        async fn execute(
            &self,
            _input: HookInput,
            _hook_context: &HookContext,
        ) -> Result<HookOutput, crate::Error> {
            // Sleep longer than timeout
            tokio::time::sleep(Duration::from_secs(5)).await;
            Ok(HookOutput::allow())
        }
    }

    #[tokio::test]
    async fn test_blockable_hook_failure_returns_error() {
        let mut manager = HookManager::new();
        manager.register(FailingHook::new("failing", vec![HookEvent::PreToolUse]));

        let input = HookInput::pre_tool_use("session-1", "Read", serde_json::json!({}));
        let hook_context = HookContext::new("session-1");
        let result = manager
            .execute(HookEvent::PreToolUse, input, &hook_context)
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, crate::Error::HookFailed { .. }));
    }

    #[tokio::test]
    async fn test_blockable_hook_timeout_returns_error() {
        let mut manager = HookManager::timeout(1);
        manager.register(SlowHook::new("slow", vec![HookEvent::UserPromptSubmit]));

        let input = HookInput::user_prompt_submit("session-1", "test prompt");
        let hook_context = HookContext::new("session-1");
        let result = manager
            .execute(HookEvent::UserPromptSubmit, input, &hook_context)
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, crate::Error::HookTimeout { .. }));
    }

    #[tokio::test]
    async fn test_non_blockable_hook_failure_continues() {
        let mut manager = HookManager::new();
        // SessionEnd is non-blockable
        manager.register(FailingHook::new("failing", vec![HookEvent::SessionEnd]));
        manager.register(TestHook::new("success", vec![HookEvent::SessionEnd], 0));

        let input = HookInput::session_end("session-1");
        let hook_context = HookContext::new("session-1");
        let result = manager
            .execute(HookEvent::SessionEnd, input, &hook_context)
            .await;

        // Should succeed despite the failing hook
        assert!(result.is_ok());
        assert!(result.unwrap().continue_execution);
    }

    #[tokio::test]
    async fn test_non_blockable_hook_timeout_continues() {
        let mut manager = HookManager::timeout(1);
        // PostToolUse is non-blockable
        manager.register(SlowHook::new("slow", vec![HookEvent::PostToolUse]));

        let input = HookInput::post_tool_use(
            "session-1",
            "Read",
            crate::types::ToolOutput::success("result"),
        );
        let hook_context = HookContext::new("session-1");
        let result = manager
            .execute(HookEvent::PostToolUse, input, &hook_context)
            .await;

        // Should succeed despite the timeout
        assert!(result.is_ok());
        assert!(result.unwrap().continue_execution);
    }
}