everruns-core 0.9.0

Core agent abstractions for Everruns - agent loop, events, tools, LLM providers
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
// User-defined hooks: adapters that bridge `UserHookSpec` to the runtime's
// per-event hook traits.
//
// `HookAdapterBuilder` is the single place specs become `Arc<dyn …Hook>`.
// Capability authors return data (`Vec<UserHookSpec>`); this module owns the
// translation to executable adapters so timeout/output/sandbox limits stay
// centrally enforced.
//
// Wired adapters: `PreToolUseHookAdapter` (with `build_pre_tool_use_hooks`)
// and `PostToolUseHookAdapter` (with `build_post_tool_use_hooks`). Adapters
// for `session_*`, `user_prompt_submit`, `turn_end` land alongside their
// respective runtime wire-in PRs.

use std::sync::Arc;

use async_trait::async_trait;
use serde_json::json;

use crate::atoms::{PostToolExecHook, PreToolUseDecision, PreToolUseHook};
use crate::hook_executor::{
    BashHookDispatcher, BashHookExecutor, ExecutorOpts, HookExecutor, HookPayload,
};
use crate::tool_types::{ToolCall, ToolDefinition, ToolResult};
use crate::traits::ToolContext;
use crate::user_hook_types::{
    ExecutorSpec, HookEvent, HookOutcome, HookSource, OnError, UserHookSpec,
};

// ============================================================================
// PostToolUseHookAdapter
// ============================================================================

/// Adapter that implements `PostToolExecHook` for a single `UserHookSpec`
/// whose event is `post_tool_use`.
///
/// Lifecycle per `after_exec` call:
///
/// 1. Skip when the matcher rejects the (tool_name, arguments) pair.
/// 2. Build a `HookPayload` from the tool call + result.
/// 3. Run the executor with the spec's `timeout_ms`.
/// 4. On `Allow`, do nothing.
/// 5. On `Mutate { patch }`, apply the patch to the `ToolResult`:
///    - `patch.result` overwrites `result.result`
///    - `patch.error` overwrites `result.error`
///    - `patch.additional_context` is appended as a `hook_context` field on
///      `result.result` so the model sees the hook's commentary.
/// 6. On `Block`, log a warning — `post_tool_use` cannot block by spec.
/// 7. On `Error`, apply the spec's `on_error` policy. `Block` policy
///    overwrites `result.error`; `Warn`/`Allow` just log.
pub struct PostToolUseHookAdapter {
    spec: UserHookSpec,
    executor: Arc<dyn HookExecutor>,
    opts: ExecutorOpts,
    /// Resolved once at construction so every payload/log line carries a
    /// stable, unique id even for specs that omit an explicit `id` (the
    /// chain position disambiguates them). See `finalize_hook_specs`.
    hook_id: crate::user_hook_types::HookId,
}

impl PostToolUseHookAdapter {
    pub fn new(spec: UserHookSpec, executor: Arc<dyn HookExecutor>) -> Self {
        Self::with_index(spec, executor, 0)
    }

    pub fn with_index(spec: UserHookSpec, executor: Arc<dyn HookExecutor>, index: usize) -> Self {
        let opts = ExecutorOpts {
            timeout_ms: spec.timeout_ms,
            max_output_bytes: 64 * 1024,
        };
        let hook_id = spec.resolve_id(index);
        Self {
            spec,
            executor,
            opts,
            hook_id,
        }
    }

    fn build_payload(
        &self,
        tool_call: &ToolCall,
        result: &ToolResult,
        context: &ToolContext,
    ) -> HookPayload {
        let success = result.error.is_none();
        HookPayload {
            event: HookEvent::PostToolUse,
            hook_id: self.hook_id.clone(),
            session_id: context.session_id,
            turn_id: None,
            org_id: context.org_id,
            agent_id: None,
            ts: chrono::Utc::now().to_rfc3339(),
            data: json!({
                "tool_name": tool_call.name,
                "tool_call_id": tool_call.id,
                "arguments": tool_call.arguments,
                "result": result.result,
                "error": result.error,
                "success": success,
            }),
        }
    }
}

#[async_trait]
impl PostToolExecHook for PostToolUseHookAdapter {
    async fn after_exec(
        &self,
        tool_call: &ToolCall,
        _tool_def: &ToolDefinition,
        result: &mut ToolResult,
        context: &ToolContext,
    ) {
        if !self
            .spec
            .matcher
            .matches(&tool_call.name, &tool_call.arguments)
        {
            return;
        }

        let payload = self.build_payload(tool_call, result, context);
        let outcome = self.executor.run(payload, &self.opts).await;
        let hook_id = &self.hook_id;

        match outcome {
            HookOutcome::Allow => {}
            HookOutcome::Mutate { patch, .. } => apply_post_tool_use_patch(result, &patch),
            HookOutcome::Block { reason, .. } => {
                tracing::warn!(
                    hook_id = %hook_id.as_str(),
                    tool_call_id = %tool_call.id,
                    reason = %reason,
                    "post_tool_use hook returned Block, which is not allowed for this event; ignoring"
                );
            }
            HookOutcome::Error { message } => match self.spec.on_error {
                OnError::Block => {
                    // post_tool_use cannot block execution that already
                    // happened, but we can replace the result with an
                    // error so the model sees the failure.
                    result.error = Some(format!("hook {}: {}", hook_id.as_str(), message));
                    tracing::warn!(
                        hook_id = %hook_id.as_str(),
                        tool_call_id = %tool_call.id,
                        message = %message,
                        "post_tool_use hook errored with on_error=block; replacing tool result with error"
                    );
                }
                OnError::Warn => {
                    tracing::warn!(
                        hook_id = %hook_id.as_str(),
                        tool_call_id = %tool_call.id,
                        message = %message,
                        "post_tool_use hook errored"
                    );
                }
                OnError::Allow => {}
            },
        }
    }
}

/// Apply a `post_tool_use` `Mutate` patch to a `ToolResult` in place.
///
/// Patch shape (any subset, see `specs/user-hooks.md`):
///   { "result": …, "error": …, "additional_context": "..." }
fn apply_post_tool_use_patch(result: &mut ToolResult, patch: &serde_json::Value) {
    if let Some(new_result) = patch.get("result") {
        result.result = Some(new_result.clone());
    }
    if let Some(new_error) = patch.get("error").and_then(|v| v.as_str()) {
        result.error = Some(new_error.to_string());
    }
    if let Some(ctx) = patch.get("additional_context").and_then(|v| v.as_str()) {
        // Append as `hook_context` so the model sees the hook's narration.
        match result.result.as_mut() {
            Some(serde_json::Value::Object(map)) => {
                map.insert("hook_context".to_string(), json!(ctx));
            }
            Some(other) => {
                let prior = other.clone();
                *other = json!({ "value": prior, "hook_context": ctx });
            }
            None => {
                result.result = Some(json!({ "hook_context": ctx }));
            }
        }
    }
}

// ============================================================================
// Factory
// ============================================================================

/// Build `PostToolExecHook` adapters from every `UserHookSpec` in `specs`
/// whose event is `PostToolUse`. `dispatcher` is the bash backend used for
/// every spec whose executor is `Bash`; other backends will land alongside
/// their own dispatchers.
///
/// Specs that fail `validate()` are silently dropped with a warning log so
/// one bad spec doesn't take down the entire chain.
pub fn build_post_tool_use_hooks(
    specs: &[UserHookSpec],
    dispatcher: Arc<dyn BashHookDispatcher>,
) -> Vec<Arc<dyn PostToolExecHook>> {
    let mut out: Vec<Arc<dyn PostToolExecHook>> = Vec::new();
    for (index, spec) in specs.iter().enumerate() {
        if spec.event != HookEvent::PostToolUse {
            continue;
        }
        if let Err(e) = spec.validate() {
            let hook_id_for_log = spec.resolve_id(index);
            tracing::warn!(
                hook_id = %hook_id_for_log.as_str(),
                error = %e,
                "skipping invalid post_tool_use hook spec"
            );
            continue;
        }
        let executor: Arc<dyn HookExecutor> = match &spec.executor {
            ExecutorSpec::Bash { command, env } => Arc::new(BashHookExecutor::with_dispatcher(
                command.clone(),
                env.clone(),
                dispatcher.clone(),
            )),
        };
        out.push(Arc::new(PostToolUseHookAdapter::with_index(
            spec.clone(),
            executor,
            index,
        )));
    }
    out
}

/// Finalize contributed hook specs before they become adapters.
///
/// Each entry in `contributions` is `(capability_id, specs)` as returned by a
/// single capability's `user_hooks_with_config`. This function:
///
/// 1. **Stamps the source namespace.** Specs from any capability other than
///    the user-facing `user_hooks` capability are stamped
///    `HookSource::Capability { capability_id }` so their resolved `HookId`
///    lands in the `{capability_id}:` namespace. (The `user_hooks` capability
///    already stamps its own entries `UserConfig`.) This guarantees correct
///    namespacing regardless of whether the capability author set `source` —
///    per the `HookSource` doc contract, the runtime owns this stamp.
/// 2. **Assigns a stable default id** (`{event}_{idx}`, indexed within the
///    contributing capability) to any spec that omits an explicit `id`, so
///    every hook is individually addressable and mutable.
/// 3. **Applies `disabled_contributions`.** Any spec whose resolved `HookId`
///    appears in `disabled` is dropped, so operators can mute
///    capability-contributed hooks (TM-HOOK-004).
pub fn finalize_hook_specs(
    contributions: Vec<(String, Vec<UserHookSpec>)>,
    disabled: &[String],
) -> Vec<UserHookSpec> {
    let disabled: std::collections::HashSet<&str> = disabled.iter().map(String::as_str).collect();
    let mut out: Vec<UserHookSpec> = Vec::new();
    for (capability_id, specs) in contributions {
        for (idx, mut spec) in specs.into_iter().enumerate() {
            // The `user_hooks` capability stamps its own entries `UserConfig`
            // (and assigns ids) in `parse_config`. Every other capability is a
            // capability contribution; stamp it here so authors can't forget.
            if capability_id != "user_hooks" {
                spec.source = HookSource::Capability {
                    capability_id: capability_id.clone(),
                };
                if spec.id.is_none() {
                    spec.id = Some(format!("{}_{}", spec.event.as_str(), idx));
                }
            }
            let resolved = spec.resolve_id(idx);
            if disabled.contains(resolved.as_str()) {
                tracing::info!(
                    hook_id = %resolved.as_str(),
                    "muting hook via disabled_contributions"
                );
                continue;
            }
            out.push(spec);
        }
    }
    out
}

/// Convenience: classify a stable hook id from a spec source. Useful for
/// audit logs that need the `{capability_id}:{name}` namespace.
pub fn hook_id_namespace(spec: &UserHookSpec) -> &'static str {
    match spec.source {
        HookSource::UserConfig => "user",
        HookSource::Capability { .. } => "capability",
    }
}

// ============================================================================
// PreToolUseHookAdapter
// ============================================================================

/// Adapter that implements `PreToolUseHook` for a single `UserHookSpec`
/// whose event is `pre_tool_use`.
///
/// Lifecycle per `before_exec` call:
///
/// 1. Skip when the matcher rejects the (tool_name, arguments) pair —
///    `Continue(tool_call)` with no mutation.
/// 2. Build a `HookPayload` from the tool call.
/// 3. Run the executor with the spec's `timeout_ms`.
/// 4. On `Allow`, return `Continue(tool_call)` unchanged.
/// 5. On `Mutate { patch }`, merge `patch.arguments` into
///    `tool_call.arguments` and return `Continue`.
/// 6. On `Block { reason, user_message }`, return `Block` — `ActAtom`
///    short-circuits the tool call.
/// 7. On `Error`, apply the spec's `on_error` policy:
///    - `Block` → `Block { reason = "hook errored: …" }`
///    - `Warn` / `Allow` → log + `Continue(tool_call)` unchanged.
pub struct PreToolUseHookAdapter {
    spec: UserHookSpec,
    executor: Arc<dyn HookExecutor>,
    opts: ExecutorOpts,
    /// Resolved once at construction; see `PostToolUseHookAdapter::hook_id`.
    hook_id: crate::user_hook_types::HookId,
}

impl PreToolUseHookAdapter {
    pub fn new(spec: UserHookSpec, executor: Arc<dyn HookExecutor>) -> Self {
        Self::with_index(spec, executor, 0)
    }

    pub fn with_index(spec: UserHookSpec, executor: Arc<dyn HookExecutor>, index: usize) -> Self {
        let opts = ExecutorOpts {
            timeout_ms: spec.timeout_ms,
            max_output_bytes: 64 * 1024,
        };
        let hook_id = spec.resolve_id(index);
        Self {
            spec,
            executor,
            opts,
            hook_id,
        }
    }

    fn build_payload(&self, tool_call: &ToolCall, context: &ToolContext) -> HookPayload {
        HookPayload {
            event: HookEvent::PreToolUse,
            hook_id: self.hook_id.clone(),
            session_id: context.session_id,
            turn_id: None,
            org_id: context.org_id,
            agent_id: None,
            ts: chrono::Utc::now().to_rfc3339(),
            data: json!({
                "tool_name": tool_call.name,
                "tool_call_id": tool_call.id,
                "arguments": tool_call.arguments,
            }),
        }
    }
}

#[async_trait]
impl PreToolUseHook for PreToolUseHookAdapter {
    async fn before_exec(
        &self,
        tool_call: ToolCall,
        _tool_def: &ToolDefinition,
        context: &ToolContext,
    ) -> PreToolUseDecision {
        if !self
            .spec
            .matcher
            .matches(&tool_call.name, &tool_call.arguments)
        {
            return PreToolUseDecision::Continue(tool_call);
        }

        let payload = self.build_payload(&tool_call, context);
        let outcome = self.executor.run(payload, &self.opts).await;
        let hook_id = &self.hook_id;

        match outcome {
            HookOutcome::Allow => PreToolUseDecision::Continue(tool_call),
            HookOutcome::Mutate { patch, .. } => {
                let mutated = apply_pre_tool_use_patch(tool_call, &patch);
                PreToolUseDecision::Continue(mutated)
            }
            HookOutcome::Block {
                reason,
                user_message,
            } => PreToolUseDecision::Block {
                tool_call,
                reason,
                user_message,
            },
            HookOutcome::Error { message } => match self.spec.on_error {
                OnError::Block => PreToolUseDecision::Block {
                    tool_call,
                    reason: format!("hook {} errored: {}", hook_id.as_str(), message),
                    user_message: None,
                },
                OnError::Warn => {
                    tracing::warn!(
                        hook_id = %hook_id.as_str(),
                        tool_call_id = %tool_call.id,
                        message = %message,
                        "pre_tool_use hook errored"
                    );
                    PreToolUseDecision::Continue(tool_call)
                }
                OnError::Allow => PreToolUseDecision::Continue(tool_call),
            },
        }
    }
}

/// Apply a `pre_tool_use` `Mutate` patch to a `ToolCall`. Only the
/// `patch.arguments` object is honored; it is merged into the existing
/// arguments, with patch keys winning on conflict. Non-object patches
/// and missing fields are silently ignored.
fn apply_pre_tool_use_patch(mut tool_call: ToolCall, patch: &serde_json::Value) -> ToolCall {
    if let Some(new_args) = patch.get("arguments")
        && let Some(new_obj) = new_args.as_object()
    {
        match tool_call.arguments.as_object_mut() {
            Some(existing) => {
                for (k, v) in new_obj {
                    existing.insert(k.clone(), v.clone());
                }
            }
            None => {
                tool_call.arguments = serde_json::Value::Object(new_obj.clone());
            }
        }
    }
    tool_call
}

/// Build `PreToolUseHook` adapters from every `UserHookSpec` in `specs`
/// whose event is `PreToolUse`. Specs that fail `validate()` are dropped
/// with a warning log so one bad spec doesn't take down the chain.
pub fn build_pre_tool_use_hooks(
    specs: &[UserHookSpec],
    dispatcher: Arc<dyn BashHookDispatcher>,
) -> Vec<Arc<dyn PreToolUseHook>> {
    let mut out: Vec<Arc<dyn PreToolUseHook>> = Vec::new();
    for (index, spec) in specs.iter().enumerate() {
        if spec.event != HookEvent::PreToolUse {
            continue;
        }
        if let Err(e) = spec.validate() {
            let hook_id_for_log = spec.resolve_id(index);
            tracing::warn!(
                hook_id = %hook_id_for_log.as_str(),
                error = %e,
                "skipping invalid pre_tool_use hook spec"
            );
            continue;
        }
        let executor: Arc<dyn HookExecutor> = match &spec.executor {
            ExecutorSpec::Bash { command, env } => Arc::new(BashHookExecutor::with_dispatcher(
                command.clone(),
                env.clone(),
                dispatcher.clone(),
            )),
        };
        out.push(Arc::new(PreToolUseHookAdapter::with_index(
            spec.clone(),
            executor,
            index,
        )));
    }
    out
}

#[cfg(test)]
mod pre_tool_use_tests {
    use super::*;
    use crate::tool_types::{BuiltinTool, DeferrablePolicy, ToolHints, ToolPolicy};
    use serde_json::json;
    use std::sync::Mutex;

    fn make_spec(matcher: crate::user_hook_types::HookMatcher) -> UserHookSpec {
        UserHookSpec {
            id: Some("pre".into()),
            event: HookEvent::PreToolUse,
            matcher,
            executor: ExecutorSpec::Bash {
                command: "true".into(),
                env: Default::default(),
            },
            timeout_ms: 5000,
            on_error: OnError::Warn,
            description: None,
            source: HookSource::UserConfig,
        }
    }

    fn make_tool_call() -> ToolCall {
        ToolCall {
            id: "call_x".into(),
            name: "bash".into(),
            arguments: json!({"command": "rm -rf /"}),
        }
    }

    fn make_tool_def() -> ToolDefinition {
        ToolDefinition::Builtin(BuiltinTool {
            name: "bash".into(),
            display_name: None,
            description: "".into(),
            parameters: json!({}),
            policy: ToolPolicy::Auto,
            category: None,
            deferrable: DeferrablePolicy::Never,
            hints: ToolHints::default(),
            full_parameters: None,
        })
    }

    struct ProgrammedExecutor {
        outcome: HookOutcome,
        calls: Mutex<Vec<HookPayload>>,
    }

    #[async_trait]
    impl HookExecutor for ProgrammedExecutor {
        fn kind(&self) -> &'static str {
            "test"
        }
        async fn run(&self, payload: HookPayload, _opts: &ExecutorOpts) -> HookOutcome {
            self.calls.lock().unwrap().push(payload);
            self.outcome.clone()
        }
    }

    fn programmed(outcome: HookOutcome) -> Arc<ProgrammedExecutor> {
        Arc::new(ProgrammedExecutor {
            outcome,
            calls: Mutex::new(Vec::new()),
        })
    }

    #[tokio::test]
    async fn matcher_miss_skips_executor() {
        let exec = programmed(HookOutcome::Allow);
        let exec_arc: Arc<dyn HookExecutor> = exec.clone();
        let matcher = crate::user_hook_types::HookMatcher {
            tool_name: Some("edit_file".into()),
            ..Default::default()
        };
        let adapter = PreToolUseHookAdapter::new(make_spec(matcher), exec_arc);
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));

        let decision = adapter
            .before_exec(make_tool_call(), &make_tool_def(), &ctx)
            .await;
        assert!(matches!(decision, PreToolUseDecision::Continue(_)));
        assert_eq!(exec.calls.lock().unwrap().len(), 0);
    }

    #[tokio::test]
    async fn allow_outcome_returns_continue_unchanged() {
        let exec_arc: Arc<dyn HookExecutor> = programmed(HookOutcome::Allow);
        let adapter = PreToolUseHookAdapter::new(make_spec(Default::default()), exec_arc);
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));
        let original = make_tool_call();

        let decision = adapter
            .before_exec(original.clone(), &make_tool_def(), &ctx)
            .await;
        match decision {
            PreToolUseDecision::Continue(tc) => assert_eq!(tc.arguments, original.arguments),
            other => panic!("expected Continue, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn block_outcome_propagates() {
        let exec_arc: Arc<dyn HookExecutor> = programmed(HookOutcome::Block {
            reason: "denied".into(),
            user_message: Some("nope".into()),
        });
        let adapter = PreToolUseHookAdapter::new(make_spec(Default::default()), exec_arc);
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));

        let decision = adapter
            .before_exec(make_tool_call(), &make_tool_def(), &ctx)
            .await;
        match decision {
            PreToolUseDecision::Block {
                reason,
                user_message,
                ..
            } => {
                assert_eq!(reason, "denied");
                assert_eq!(user_message.as_deref(), Some("nope"));
            }
            other => panic!("expected Block, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn mutate_patch_merges_arguments() {
        let exec_arc: Arc<dyn HookExecutor> = programmed(HookOutcome::Mutate {
            patch: json!({ "arguments": { "command": "ls" } }),
            reason: None,
        });
        let adapter = PreToolUseHookAdapter::new(make_spec(Default::default()), exec_arc);
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));

        let decision = adapter
            .before_exec(make_tool_call(), &make_tool_def(), &ctx)
            .await;
        match decision {
            PreToolUseDecision::Continue(tc) => {
                assert_eq!(tc.arguments["command"], "ls");
            }
            other => panic!("expected Continue, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn error_with_on_error_block_returns_block() {
        let exec_arc: Arc<dyn HookExecutor> = programmed(HookOutcome::Error {
            message: "boom".into(),
        });
        let mut spec = make_spec(Default::default());
        spec.on_error = OnError::Block;
        let adapter = PreToolUseHookAdapter::new(spec, exec_arc);
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));

        let decision = adapter
            .before_exec(make_tool_call(), &make_tool_def(), &ctx)
            .await;
        match decision {
            PreToolUseDecision::Block { reason, .. } => {
                assert!(reason.contains("boom"), "{reason}");
            }
            other => panic!("expected Block, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn error_with_on_error_warn_returns_continue() {
        let exec_arc: Arc<dyn HookExecutor> = programmed(HookOutcome::Error {
            message: "boom".into(),
        });
        let adapter = PreToolUseHookAdapter::new(make_spec(Default::default()), exec_arc);
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));

        let decision = adapter
            .before_exec(make_tool_call(), &make_tool_def(), &ctx)
            .await;
        assert!(matches!(decision, PreToolUseDecision::Continue(_)));
    }

    #[test]
    fn factory_filters_to_pre_tool_use_event() {
        let specs = vec![
            make_spec(Default::default()),
            UserHookSpec {
                event: HookEvent::PostToolUse,
                ..make_spec(Default::default())
            },
        ];
        struct NoopDispatcher;
        #[async_trait]
        impl BashHookDispatcher for NoopDispatcher {
            async fn dispatch(
                &self,
                _payload: &HookPayload,
                _command: &str,
                _extra_env: &std::collections::BTreeMap<String, String>,
                _opts: &ExecutorOpts,
            ) -> Result<crate::hook_executor::BashExecOutput, String> {
                Ok(crate::hook_executor::BashExecOutput {
                    exit_code: 0,
                    stdout: String::new(),
                    stderr: String::new(),
                })
            }
        }
        let dispatcher: Arc<dyn BashHookDispatcher> = Arc::new(NoopDispatcher);
        let hooks = build_pre_tool_use_hooks(&specs, dispatcher);
        assert_eq!(hooks.len(), 1);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tool_types::{BuiltinTool, DeferrablePolicy, ToolHints, ToolPolicy};
    use serde_json::json;
    use std::sync::Mutex;

    fn make_spec(event: HookEvent, command: &str) -> UserHookSpec {
        UserHookSpec {
            id: Some("t".into()),
            event,
            matcher: Default::default(),
            executor: ExecutorSpec::Bash {
                command: command.into(),
                env: Default::default(),
            },
            timeout_ms: 5000,
            on_error: OnError::Warn,
            description: None,
            source: HookSource::UserConfig,
        }
    }

    fn make_tool_call(name: &str) -> ToolCall {
        ToolCall {
            id: "call_1".into(),
            name: name.into(),
            arguments: json!({}),
        }
    }

    fn make_tool_def(name: &str) -> ToolDefinition {
        ToolDefinition::Builtin(BuiltinTool {
            name: name.into(),
            display_name: None,
            description: "x".into(),
            parameters: json!({}),
            policy: ToolPolicy::Auto,
            category: None,
            deferrable: DeferrablePolicy::Never,
            hints: ToolHints::default(),
            full_parameters: None,
        })
    }

    fn empty_result() -> ToolResult {
        ToolResult {
            tool_call_id: "call_1".into(),
            result: Some(json!({"out": "stuff"})),
            images: None,
            error: None,
            connection_required: None,
            raw_output: None,
        }
    }

    /// Test executor that records calls and returns a programmed outcome.
    struct ProgrammedExecutor {
        outcome: HookOutcome,
        calls: Mutex<Vec<HookPayload>>,
    }

    #[async_trait]
    impl HookExecutor for ProgrammedExecutor {
        fn kind(&self) -> &'static str {
            "test"
        }
        async fn run(&self, payload: HookPayload, _opts: &ExecutorOpts) -> HookOutcome {
            self.calls.lock().unwrap().push(payload);
            self.outcome.clone()
        }
    }

    fn programmed(outcome: HookOutcome) -> Arc<ProgrammedExecutor> {
        Arc::new(ProgrammedExecutor {
            outcome,
            calls: Mutex::new(Vec::new()),
        })
    }

    #[tokio::test]
    async fn adapter_runs_executor_when_matcher_passes() {
        let exec = programmed(HookOutcome::Allow);
        let exec_arc: Arc<dyn HookExecutor> = exec.clone();
        let adapter =
            PostToolUseHookAdapter::new(make_spec(HookEvent::PostToolUse, "true"), exec_arc);

        let tc = make_tool_call("edit_file");
        let td = make_tool_def("edit_file");
        let mut result = empty_result();
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));
        adapter.after_exec(&tc, &td, &mut result, &ctx).await;

        assert_eq!(exec.calls.lock().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn adapter_skips_when_matcher_rejects() {
        let exec = programmed(HookOutcome::Allow);
        let mut spec = make_spec(HookEvent::PostToolUse, "true");
        spec.matcher.tool_name = Some("read_file".into()); // won't match edit_file
        let exec_arc: Arc<dyn HookExecutor> = exec.clone();
        let adapter = PostToolUseHookAdapter::new(spec, exec_arc);

        let tc = make_tool_call("edit_file");
        let td = make_tool_def("edit_file");
        let mut result = empty_result();
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));
        adapter.after_exec(&tc, &td, &mut result, &ctx).await;

        assert_eq!(exec.calls.lock().unwrap().len(), 0);
    }

    #[tokio::test]
    async fn mutate_patch_replaces_result_and_error() {
        let exec_arc: Arc<dyn HookExecutor> = Arc::new(ProgrammedExecutor {
            outcome: HookOutcome::Mutate {
                patch: json!({
                    "result": {"replaced": true},
                    "error": "redacted",
                }),
                reason: None,
            },
            calls: Mutex::new(Vec::new()),
        });
        let adapter =
            PostToolUseHookAdapter::new(make_spec(HookEvent::PostToolUse, "true"), exec_arc);

        let tc = make_tool_call("edit_file");
        let td = make_tool_def("edit_file");
        let mut result = empty_result();
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));
        adapter.after_exec(&tc, &td, &mut result, &ctx).await;

        assert_eq!(result.result, Some(json!({"replaced": true})));
        assert_eq!(result.error.as_deref(), Some("redacted"));
    }

    #[tokio::test]
    async fn mutate_additional_context_appends_hook_context() {
        let exec_arc: Arc<dyn HookExecutor> = Arc::new(ProgrammedExecutor {
            outcome: HookOutcome::Mutate {
                patch: json!({"additional_context": "fmt clean"}),
                reason: None,
            },
            calls: Mutex::new(Vec::new()),
        });
        let adapter =
            PostToolUseHookAdapter::new(make_spec(HookEvent::PostToolUse, "true"), exec_arc);

        let tc = make_tool_call("edit_file");
        let td = make_tool_def("edit_file");
        let mut result = empty_result();
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));
        adapter.after_exec(&tc, &td, &mut result, &ctx).await;

        // Should append a hook_context key to the existing object result.
        let r = result.result.as_ref().unwrap();
        assert_eq!(r["hook_context"], "fmt clean");
        assert_eq!(r["out"], "stuff"); // original keys preserved
    }

    #[tokio::test]
    async fn block_outcome_is_ignored_for_post_tool_use() {
        let exec_arc: Arc<dyn HookExecutor> = Arc::new(ProgrammedExecutor {
            outcome: HookOutcome::Block {
                reason: "bogus".into(),
                user_message: None,
            },
            calls: Mutex::new(Vec::new()),
        });
        let adapter =
            PostToolUseHookAdapter::new(make_spec(HookEvent::PostToolUse, "true"), exec_arc);

        let tc = make_tool_call("edit_file");
        let td = make_tool_def("edit_file");
        let mut result = empty_result();
        let original = result.result.clone();
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));
        adapter.after_exec(&tc, &td, &mut result, &ctx).await;

        assert_eq!(result.result, original);
        assert!(result.error.is_none());
    }

    #[tokio::test]
    async fn error_with_on_error_block_replaces_result_with_error() {
        let exec_arc: Arc<dyn HookExecutor> = Arc::new(ProgrammedExecutor {
            outcome: HookOutcome::Error {
                message: "boom".into(),
            },
            calls: Mutex::new(Vec::new()),
        });
        let mut spec = make_spec(HookEvent::PostToolUse, "true");
        spec.on_error = OnError::Block;
        let adapter = PostToolUseHookAdapter::new(spec, exec_arc);

        let tc = make_tool_call("edit_file");
        let td = make_tool_def("edit_file");
        let mut result = empty_result();
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));
        adapter.after_exec(&tc, &td, &mut result, &ctx).await;

        let err = result.error.unwrap();
        assert!(err.contains("boom"), "{err}");
    }

    #[tokio::test]
    async fn error_with_on_error_warn_keeps_result_intact() {
        let exec_arc: Arc<dyn HookExecutor> = Arc::new(ProgrammedExecutor {
            outcome: HookOutcome::Error {
                message: "boom".into(),
            },
            calls: Mutex::new(Vec::new()),
        });
        let adapter =
            PostToolUseHookAdapter::new(make_spec(HookEvent::PostToolUse, "true"), exec_arc);

        let tc = make_tool_call("edit_file");
        let td = make_tool_def("edit_file");
        let mut result = empty_result();
        let original = result.result.clone();
        let ctx = ToolContext::new(crate::typed_id::SessionId::from_uuid(uuid::Uuid::nil()));
        adapter.after_exec(&tc, &td, &mut result, &ctx).await;

        assert_eq!(result.result, original);
        assert!(result.error.is_none());
    }

    #[test]
    fn factory_filters_to_post_tool_use_event() {
        let specs = vec![
            make_spec(HookEvent::PostToolUse, "true"),
            make_spec(HookEvent::PreToolUse, "true"),
            make_spec(HookEvent::SessionStart, "true"),
        ];
        struct NoopDispatcher;
        #[async_trait]
        impl BashHookDispatcher for NoopDispatcher {
            async fn dispatch(
                &self,
                _payload: &HookPayload,
                _command: &str,
                _extra_env: &std::collections::BTreeMap<String, String>,
                _opts: &ExecutorOpts,
            ) -> Result<crate::hook_executor::BashExecOutput, String> {
                Ok(crate::hook_executor::BashExecOutput {
                    exit_code: 0,
                    stdout: String::new(),
                    stderr: String::new(),
                })
            }
        }
        let dispatcher: Arc<dyn BashHookDispatcher> = Arc::new(NoopDispatcher);
        let hooks = build_post_tool_use_hooks(&specs, dispatcher);
        assert_eq!(hooks.len(), 1, "only the PostToolUse spec should be built");
    }
}