bamboo-hooks 2026.10.13

Lifecycle hook dispatch and external script orchestration for Bamboo
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
950
951
952
953
954
955
956
957
958
959
960
961
962
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;

use bamboo_agent_core::{AgentHook, Session};
use bamboo_config::LifecycleHooksConfig;
use bamboo_domain::{AgentHookPoint, HookPayload, HookResult};

/// Aggregate control and context output from one lifecycle seam.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HookRunOutcome {
    pub decision: HookResult,
    pub injected_contexts: Vec<String>,
    pub plugin_contexts: Vec<crate::portable::PluginContext>,
}

impl Default for HookRunOutcome {
    fn default() -> Self {
        Self {
            decision: HookResult::Continue,
            injected_contexts: Vec::new(),
            plugin_contexts: Vec::new(),
        }
    }
}

/// One completed handler execution, returned to the engine for observability.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HookExecution {
    pub hook_name: String,
    pub point: AgentHookPoint,
    pub duration_ms: u64,
    pub result: HookResult,
}

/// Complete dispatch result for one lifecycle seam.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct HookDispatchReport {
    pub outcome: HookRunOutcome,
    pub executions: Vec<HookExecution>,
}

/// Registry and deterministic dispatcher for programmatic and configured hooks.
#[derive(Clone)]
struct RegisteredHook {
    hook: Arc<dyn AgentHook>,
    may_suspend: bool,
}

#[derive(Clone, Default)]
pub struct HookDispatcher {
    hooks: Vec<RegisteredHook>,
}

impl HookDispatcher {
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a hook. Lower numeric priorities run first.
    pub fn register(&mut self, hook: Arc<dyn AgentHook>) {
        let may_suspend = hook.may_suspend();
        self.hooks.push(RegisteredHook { hook, may_suspend });
        self.hooks
            .sort_by_key(|registered| registered.hook.priority());
    }

    /// Clone this registry and append configured handlers from a frozen config
    /// snapshot. The original registry remains reusable by future runs.
    pub fn with_lifecycle_config(
        &self,
        config: &LifecycleHooksConfig,
        fallback_cwd: Option<PathBuf>,
    ) -> Self {
        let mut dispatcher = self.clone();
        crate::configured::register_configured_hooks(&mut dispatcher, config, fallback_cwd);
        dispatcher
    }

    pub async fn run_hooks(
        &self,
        point: AgentHookPoint,
        payload: &HookPayload,
        session: &Session,
    ) -> HookDispatchReport {
        self.run_hooks_with_control(point, payload, session, true, None, None)
            .await
    }

    /// Preserve the original native hook payload while allowing a resolved
    /// builtin shell identity and its legacy projection as fallback matches.
    /// Foreign identities are never normalized by this compatibility seam.
    pub async fn run_hooks_with_resolved_shell(
        &self,
        point: AgentHookPoint,
        payload: &HookPayload,
        session: &Session,
        resolved_tool_name: Option<&str>,
        original_tool_input: Option<&serde_json::Value>,
    ) -> HookDispatchReport {
        self.run_hooks_with_control(
            point,
            payload,
            session,
            true,
            resolved_tool_name,
            original_tool_input,
        )
        .await
    }

    /// Run all matching handlers without allowing a control decision to
    /// suppress later observer handlers.
    pub async fn run_observer_hooks(
        &self,
        point: AgentHookPoint,
        payload: &HookPayload,
        session: &Session,
    ) -> HookDispatchReport {
        self.run_hooks_with_control(point, payload, session, false, None, None)
            .await
    }

    async fn run_hooks_with_control(
        &self,
        point: AgentHookPoint,
        payload: &HookPayload,
        session: &Session,
        honor_control_decisions: bool,
        resolved_tool_name: Option<&str>,
        original_tool_input: Option<&serde_json::Value>,
    ) -> HookDispatchReport {
        let mut report = HookDispatchReport::default();
        let resolved_shell = resolved_tool_name
            .filter(|name| matches!(*name, "exec_command" | "write_stdin"))
            .and_then(|name| {
                let mut resolved = payload.clone();
                match &mut resolved {
                    HookPayload::ToolExecution { tool_name, .. }
                    | HookPayload::ToolResult { tool_name, .. } => {
                        *tool_name = name.to_owned();
                        Some(resolved)
                    }
                    _ => None,
                }
            });
        let legacy = crate::shell_compatibility::legacy_shell_projections(
            resolved_shell.as_ref().unwrap_or(payload),
            session,
            original_tool_input,
        );

        let complete_operation_checks = crate::requires_complete_shell_policy_checks(
            point,
            resolved_shell.as_ref().unwrap_or(payload),
            session,
            original_tool_input,
        );
        let mut pending_composite_ask = false;

        for registered in &self.hooks {
            let hook = &registered.hook;
            if hook.point() != point {
                continue;
            }
            // Original/resolved-name and wildcard handlers run once. A legacy-
            // only matcher must evaluate each distinct operation it covers:
            // stdin/kill also reads output, whose policy can independently deny.
            let selected: Vec<_> = if hook.matches(payload) {
                vec![(payload, false)]
            } else if let Some(resolved) = resolved_shell
                .as_ref()
                .filter(|resolved| hook.matches(resolved))
            {
                vec![(resolved, false)]
            } else {
                legacy
                    .iter()
                    .filter(|projected| hook.matches(&projected.payload))
                    .map(|projected| (&projected.payload, projected.restriction_only))
                    .collect()
            };

            let mut deferred_ask = false;
            for (selected_payload, restriction_only) in selected {
                let started = Instant::now();
                let result = hook.run(point, selected_payload, session).await;
                let result = if registered.may_suspend {
                    result
                } else {
                    enforce_non_suspending_hook(result)
                };
                report.executions.push(HookExecution {
                    hook_name: hook.name().to_string(),
                    point,
                    duration_ms: started.elapsed().as_millis() as u64,
                    result: result.clone(),
                });

                let (result, mut contexts) = unwrap_context_result(result);
                report.outcome.injected_contexts.append(&mut contexts);
                let result = if restriction_only && matches!(result, HookResult::Allow) {
                    // An output-only legacy policy may restrict the read side of a
                    // combined continuation, but cannot grant stdin/kill authority.
                    HookResult::Continue
                } else {
                    result
                };

                match &result {
                    HookResult::Abort { .. }
                    | HookResult::Suspend { .. }
                    | HookResult::Deny { .. } => {
                        if honor_control_decisions {
                            report.outcome.decision = result;
                            return report;
                        }
                    }
                    HookResult::Ask => {
                        // Approval of the input side cannot override an unchecked
                        // output denial from this same legacy-only handler.
                        deferred_ask |= honor_control_decisions;
                    }
                    HookResult::InjectContext { text } => {
                        report.outcome.injected_contexts.push(text.clone());
                    }
                    HookResult::Mutated => {
                        if matches!(report.outcome.decision, HookResult::Continue) {
                            report.outcome.decision = HookResult::Mutated;
                        }
                    }
                    HookResult::Allow => report.outcome.decision = HookResult::Allow,
                    HookResult::Continue => {}
                    HookResult::WithContext { .. } => unreachable!("context results are unwrapped"),
                }
            }
            if deferred_ask {
                if complete_operation_checks {
                    // A separate handler may still restrict the output side.
                    pending_composite_ask = true;
                } else {
                    report.outcome.decision = HookResult::Ask;
                    return report;
                }
            }
        }

        if pending_composite_ask {
            report.outcome.decision = HookResult::Ask;
        }
        report
    }

    /// Native control hooks are conservatively resumable unless their host
    /// implementation explicitly declares the non-suspending contract.
    pub fn has_suspending_hooks_for(&self, point: AgentHookPoint) -> bool {
        self.hooks
            .iter()
            .any(|registered| registered.hook.point() == point && registered.may_suspend)
    }

    pub fn has_hooks_for(&self, point: AgentHookPoint) -> bool {
        self.hooks
            .iter()
            .any(|registered| registered.hook.point() == point)
    }

    pub fn len(&self) -> usize {
        self.hooks.len()
    }

    pub fn is_empty(&self) -> bool {
        self.hooks.is_empty()
    }
}

fn unwrap_context_result(mut result: HookResult) -> (HookResult, Vec<String>) {
    let mut contexts = Vec::new();
    while let HookResult::WithContext {
        result: inner,
        text,
    } = result
    {
        if !text.trim().is_empty() {
            contexts.push(text);
        }
        result = *inner;
    }
    (result, contexts)
}

fn enforce_non_suspending_hook(result: HookResult) -> HookResult {
    match result {
        HookResult::Suspend { reason } => HookResult::Abort {
            reason: format!("non-suspending native hook requested suspension: {reason}"),
        },
        HookResult::WithContext { result, text } => HookResult::WithContext {
            result: Box::new(enforce_non_suspending_hook(*result)),
            text,
        },
        result => result,
    }
}

#[cfg(test)]
mod tests {

    #[tokio::test]
    async fn combined_shell_ask_checks_later_native_output_policy() {
        for name in ["write_stdin", "default::write_stdin"] {
            for args in [
                json!({"session_id":"exec", "chars":"yes"}),
                json!({"session_id":"exec", "eof":true}),
                json!({"session_id":"exec", "chars":"\u{3}"}),
            ] {
                for output in [
                    HookResult::Deny {
                        reason: "output denied".into(),
                    },
                    HookResult::Abort {
                        reason: "output aborted".into(),
                    },
                    HookResult::Suspend {
                        reason: "output suspended".into(),
                    },
                    HookResult::Continue,
                    HookResult::Allow,
                    HookResult::Mutated,
                ] {
                    for primary_matcher in ["^(BashInput|KillShell)$", "^write_stdin$", ".*"] {
                        let mut dispatcher = HookDispatcher::new();
                        let seen = Arc::new(Mutex::new(Vec::new()));
                        for (matcher, result) in [
                            (primary_matcher, HookResult::Ask),
                            ("^BashOutput$", output.clone()),
                        ] {
                            dispatcher.register(Arc::new(RecordingHook {
                                point: AgentHookPoint::BeforeToolExecution,
                                matcher: regex::Regex::new(matcher).unwrap(),
                                seen: seen.clone(),
                                result,
                            }));
                        }
                        let expected = if matches!(
                            output,
                            HookResult::Deny { .. }
                                | HookResult::Abort { .. }
                                | HookResult::Suspend { .. }
                        ) {
                            output.clone()
                        } else {
                            HookResult::Ask
                        };
                        let report = dispatcher
                            .run_hooks_with_resolved_shell(
                                AgentHookPoint::BeforeToolExecution,
                                &HookPayload::ToolExecution {
                                    tool_name: name.into(),
                                    tool_call_id: "combined-ask".into(),
                                    parsed_args: args.clone(),
                                },
                                &Session::new("combined-ask", "model"),
                                Some("write_stdin"),
                                Some(&args),
                            )
                            .await;
                        assert_eq!(report.outcome.decision, expected, "{name}: {args}");
                        assert_eq!(
                            report.executions.len(),
                            2,
                            "distinct handlers must both run"
                        );
                        assert!(
                            matches!(&seen.lock().unwrap()[1], HookPayload::ToolExecution { tool_name, .. } if tool_name == "BashOutput")
                        );
                    }
                }
            }
        }
        // These single-operation calls retain the existing Ask short circuit.
        for (name, args) in [
            ("BashInput", json!({"bash_id":"legacy", "input":"yes"})),
            ("KillShell", json!({"bash_id":"legacy"})),
            ("exec_command", json!({"cmd":"echo yes"})),
            ("write_stdin", json!({"session_id":"exec", "chars":""})),
            (
                "vendor::write_stdin",
                json!({"session_id":"foreign", "chars":"yes"}),
            ),
        ] {
            let mut dispatcher = HookDispatcher::new();
            let seen = Arc::new(Mutex::new(Vec::new()));
            for result in [
                HookResult::Ask,
                HookResult::Deny {
                    reason: "must remain skipped".into(),
                },
            ] {
                dispatcher.register(Arc::new(RecordingHook {
                    point: AgentHookPoint::BeforeToolExecution,
                    matcher: regex::Regex::new(".*").unwrap(),
                    seen: seen.clone(),
                    result,
                }));
            }
            let report = dispatcher
                .run_hooks_with_resolved_shell(
                    AgentHookPoint::BeforeToolExecution,
                    &HookPayload::ToolExecution {
                        tool_name: name.into(),
                        tool_call_id: "ordinary-ask".into(),
                        parsed_args: args.clone(),
                    },
                    &Session::new("ordinary-ask", "model"),
                    Some(name),
                    Some(&args),
                )
                .await;
            assert_eq!(report.outcome.decision, HookResult::Ask, "{name}");
            assert_eq!(report.executions.len(), 1, "{name}");
            assert_eq!(seen.lock().unwrap().len(), 1);
        }
        // A combined post-tool payload keeps its prior Ask short circuit.
        for name in ["write_stdin", "default::write_stdin"] {
            let mut dispatcher = HookDispatcher::new();
            let seen = Arc::new(Mutex::new(Vec::new()));
            for result in [
                HookResult::Ask,
                HookResult::Deny {
                    reason: "post-tool skipped".into(),
                },
            ] {
                dispatcher.register(Arc::new(RecordingHook {
                    point: AgentHookPoint::AfterToolExecution,
                    matcher: regex::Regex::new(".*").unwrap(),
                    seen: seen.clone(),
                    result,
                }));
            }
            let report = dispatcher
                .run_hooks_with_resolved_shell(
                    AgentHookPoint::AfterToolExecution,
                    &HookPayload::ToolResult {
                        tool_name: name.into(),
                        tool_call_id: "post-ask".into(),
                        outcome: bamboo_domain::HookToolOutcome {
                            success: true,
                            result: Some("done".into()),
                            error: None,
                            needs_human: false,
                            duration_ms: 1,
                        },
                    },
                    &Session::new("post-ask", "model"),
                    Some("write_stdin"),
                    Some(&json!({"session_id":"exec", "chars":"yes"})),
                )
                .await;
            assert_eq!(report.outcome.decision, HookResult::Ask);
            assert_eq!(report.executions.len(), 1);
        }
    }
    use super::*;
    use async_trait::async_trait;
    use serde_json::json;
    use std::sync::Mutex;

    struct RecordingHook {
        point: AgentHookPoint,
        matcher: regex::Regex,
        seen: Arc<Mutex<Vec<HookPayload>>>,
        result: HookResult,
    }

    #[async_trait]
    impl AgentHook for RecordingHook {
        fn point(&self) -> AgentHookPoint {
            self.point
        }
        fn matches(&self, payload: &HookPayload) -> bool {
            match payload {
                HookPayload::ToolExecution { tool_name, .. }
                | HookPayload::ToolResult { tool_name, .. } => self.matcher.is_match(tool_name),
                _ => false,
            }
        }
        async fn run(
            &self,
            _point: AgentHookPoint,
            payload: &HookPayload,
            _session: &Session,
        ) -> HookResult {
            self.seen.lock().unwrap().push(payload.clone());
            self.result.clone()
        }
    }

    struct LegacyOperationPolicyHook {
        matcher: regex::Regex,
        seen: Arc<Mutex<Vec<HookPayload>>>,
        output_result: HookResult,
        primary_result: HookResult,
    }

    #[async_trait]
    impl AgentHook for LegacyOperationPolicyHook {
        fn point(&self) -> AgentHookPoint {
            AgentHookPoint::BeforeToolExecution
        }

        fn matches(&self, payload: &HookPayload) -> bool {
            matches!(payload, HookPayload::ToolExecution { tool_name, .. }
                if self.matcher.is_match(tool_name))
        }

        async fn run(&self, _: AgentHookPoint, payload: &HookPayload, _: &Session) -> HookResult {
            self.seen.lock().unwrap().push(payload.clone());
            if matches!(payload, HookPayload::ToolExecution { tool_name, .. } if tool_name == "BashOutput")
            {
                self.output_result.clone()
            } else {
                self.primary_result.clone()
            }
        }
    }

    #[tokio::test]
    async fn legacy_only_overlap_output_deny_precedes_input_ask() {
        for args in [
            json!({"session_id":"exec", "chars":"yes"}),
            json!({"session_id":"exec", "eof":true}),
            json!({"session_id":"exec", "chars":char::from(3).to_string()}),
        ] {
            for output_result in [
                HookResult::Deny {
                    reason: "output remains denied".into(),
                },
                HookResult::Continue,
                HookResult::Allow,
            ] {
                let mut dispatcher = HookDispatcher::new();
                let seen = Arc::new(Mutex::new(Vec::new()));
                let expected = if matches!(output_result, HookResult::Deny { .. }) {
                    output_result.clone()
                } else {
                    HookResult::Ask
                };
                dispatcher.register(Arc::new(LegacyOperationPolicyHook {
                    matcher: regex::Regex::new("^(BashInput|KillShell|BashOutput)$").unwrap(),
                    seen: seen.clone(),
                    primary_result: HookResult::Ask,
                    output_result,
                }));
                let report = dispatcher
                    .run_hooks_with_resolved_shell(
                        AgentHookPoint::BeforeToolExecution,
                        &HookPayload::ToolExecution {
                            tool_name: "write_stdin".into(),
                            tool_call_id: "ask-before-output".into(),
                            parsed_args: args.clone(),
                        },
                        &Session::new("ask-before-output", "model"),
                        Some("write_stdin"),
                        Some(&args),
                    )
                    .await;
                assert_eq!(report.outcome.decision, expected);
                assert_eq!(report.executions.len(), 2);
                assert!(
                    matches!(&seen.lock().unwrap()[1], HookPayload::ToolExecution {tool_name,..} if tool_name == "BashOutput")
                );
            }
        }
    }

    #[tokio::test]
    async fn legacy_only_overlap_evaluates_output_policy_after_input_eof_or_kill() {
        for (input, primary_name) in [
            (json!({"session_id":"exec", "chars":"yes\n"}), "BashInput"),
            (json!({"session_id":"exec", "eof":true}), "BashInput"),
            (json!({"session_id":"exec", "chars":"\u{3}"}), "KillShell"),
        ] {
            for output_result in [
                HookResult::Deny {
                    reason: "legacy output policy".into(),
                },
                HookResult::Ask,
                HookResult::Allow,
            ] {
                let mut dispatcher = HookDispatcher::new();
                let seen = Arc::new(Mutex::new(Vec::new()));
                for matcher in [
                    "^default::write_stdin$",
                    "^write_stdin$",
                    ".*",
                    "^(write_stdin|BashInput|KillShell|BashOutput)$",
                ] {
                    dispatcher.register(Arc::new(RecordingHook {
                        point: AgentHookPoint::BeforeToolExecution,
                        matcher: regex::Regex::new(matcher).unwrap(),
                        seen: seen.clone(),
                        result: HookResult::Continue,
                    }));
                }
                dispatcher.register(Arc::new(LegacyOperationPolicyHook {
                    matcher: regex::Regex::new("^(BashInput|KillShell|BashOutput)$").unwrap(),
                    seen: seen.clone(),
                    output_result: output_result.clone(),
                    primary_result: HookResult::Continue,
                }));
                let payload = HookPayload::ToolExecution {
                    tool_name: "default::write_stdin".into(),
                    tool_call_id: "overlap-call".into(),
                    parsed_args: input.clone(),
                };
                let report = dispatcher
                    .run_hooks_with_resolved_shell(
                        AgentHookPoint::BeforeToolExecution,
                        &payload,
                        &Session::new("overlap", "model"),
                        Some("write_stdin"),
                        Some(&input),
                    )
                    .await;
                let expected = if matches!(output_result, HookResult::Allow) {
                    HookResult::Continue
                } else {
                    output_result
                };
                assert_eq!(report.outcome.decision, expected);
                assert_eq!(report.executions.len(), 6);
                let seen = seen.lock().unwrap();
                let names: Vec<_> = seen
                    .iter()
                    .map(|payload| match payload {
                        HookPayload::ToolExecution { tool_name, .. } => tool_name.as_str(),
                        _ => unreachable!(),
                    })
                    .collect();
                assert_eq!(
                    names,
                    [
                        "default::write_stdin",
                        "write_stdin",
                        "default::write_stdin",
                        "write_stdin",
                        primary_name,
                        "BashOutput"
                    ]
                );
                assert!(
                    matches!(&seen[5], HookPayload::ToolExecution { parsed_args, .. }
                    if parsed_args == &json!({"bash_id":"exec"}))
                );
            }
        }
    }

    #[tokio::test]
    async fn legacy_shell_policy_applies_without_duplicate_primary_or_wildcard_hooks() {
        for control in [
            HookResult::Deny {
                reason: "legacy policy".into(),
            },
            HookResult::Ask,
        ] {
            let mut dispatcher = HookDispatcher::new();
            let seen = Arc::new(Mutex::new(Vec::new()));
            for (matcher, result) in [
                ("^exec_command$", HookResult::Allow),
                (".*", HookResult::Continue),
                ("^(exec_command|Bash)$", HookResult::Continue),
                ("^Bash$", control.clone()),
            ] {
                dispatcher.register(Arc::new(RecordingHook {
                    point: AgentHookPoint::BeforeToolExecution,
                    matcher: regex::Regex::new(matcher).unwrap(),
                    seen: seen.clone(),
                    result,
                }));
            }
            let payload = HookPayload::ToolExecution {
                tool_name: "exec_command".into(),
                tool_call_id: "exec-call".into(),
                parsed_args: json!({"cmd":"cargo test"}),
            };
            let report = dispatcher
                .run_hooks(
                    AgentHookPoint::BeforeToolExecution,
                    &payload,
                    &Session::new("test", "model"),
                )
                .await;
            assert_eq!(report.outcome.decision, control);
            assert_eq!(report.executions.len(), 4);
            let seen = seen.lock().unwrap();
            assert_eq!(&seen[..3], &[payload.clone(), payload.clone(), payload]);
            assert!(
                matches!(&seen[3], HookPayload::ToolExecution { tool_name, parsed_args, .. }
                if tool_name == "Bash" && parsed_args["command"] == "cargo test" && parsed_args.get("cmd").is_none())
            );
        }
    }

    #[tokio::test]
    async fn resolved_shell_aliases_preserve_primary_spelling_and_run_fallbacks_once() {
        for (alias, resolved, legacy, input) in [
            (
                "default::exec_command",
                "exec_command",
                "Bash",
                json!({"cmd":"cargo test"}),
            ),
            (
                "default::write_stdin",
                "write_stdin",
                "BashInput",
                json!({"session_id":"exec", "chars":"yes\n"}),
            ),
        ] {
            for (point, payload) in [
                (
                    AgentHookPoint::BeforeToolExecution,
                    HookPayload::ToolExecution {
                        tool_name: alias.into(),
                        tool_call_id: "alias-call".into(),
                        parsed_args: input.clone(),
                    },
                ),
                (
                    AgentHookPoint::AfterToolExecution,
                    HookPayload::ToolResult {
                        tool_name: alias.into(),
                        tool_call_id: "alias-call".into(),
                        outcome: bamboo_domain::HookToolOutcome {
                            success: true,
                            result: Some("done".into()),
                            error: None,
                            needs_human: false,
                            duration_ms: 1,
                        },
                    },
                ),
            ] {
                let mut dispatcher = HookDispatcher::new();
                let seen = Arc::new(Mutex::new(Vec::new()));
                for matcher in [
                    format!("^{alias}$"),
                    ".*".into(),
                    format!("^({alias}|{resolved}|{legacy})$"),
                    format!("^{resolved}$"),
                    format!("^{legacy}$"),
                ] {
                    dispatcher.register(Arc::new(RecordingHook {
                        point,
                        matcher: regex::Regex::new(&matcher).unwrap(),
                        seen: seen.clone(),
                        result: HookResult::Continue,
                    }));
                }
                let report = dispatcher
                    .run_hooks_with_resolved_shell(
                        point,
                        &payload,
                        &Session::new("test", "model"),
                        Some(resolved),
                        Some(&input),
                    )
                    .await;
                assert_eq!(report.executions.len(), 5);
                let seen = seen.lock().unwrap();
                assert_eq!(&seen[..3], &[payload.clone(), payload.clone(), payload]);
                let name = |payload: &HookPayload| match payload {
                    HookPayload::ToolExecution { tool_name, .. }
                    | HookPayload::ToolResult { tool_name, .. } => tool_name.clone(),
                    _ => panic!("expected tool payload"),
                };
                assert_eq!(name(&seen[3]), resolved);
                assert_eq!(name(&seen[4]), legacy);
            }
        }
    }

    #[tokio::test]
    async fn combined_continuations_apply_output_restrictions_without_duplicate_primary_hooks() {
        for (input, primary) in [
            (
                json!({"session_id":"exec", "chars":"yes\n", "eof":false}),
                "BashInput",
            ),
            (json!({"session_id":"exec", "eof":true}), "BashInput"),
            (json!({"session_id":"exec", "chars":"\u{3}"}), "KillShell"),
        ] {
            for control in [
                HookResult::Deny {
                    reason: "output policy".into(),
                },
                HookResult::Ask,
            ] {
                for (point, payload) in [
                    (
                        AgentHookPoint::BeforeToolExecution,
                        HookPayload::ToolExecution {
                            tool_name: "default::write_stdin".into(),
                            tool_call_id: "combined-call".into(),
                            parsed_args: input.clone(),
                        },
                    ),
                    (
                        AgentHookPoint::AfterToolExecution,
                        HookPayload::ToolResult {
                            tool_name: "default::write_stdin".into(),
                            tool_call_id: "combined-call".into(),
                            outcome: bamboo_domain::HookToolOutcome {
                                success: true,
                                result: Some("output".into()),
                                error: None,
                                needs_human: false,
                                duration_ms: 1,
                            },
                        },
                    ),
                ] {
                    let mut dispatcher = HookDispatcher::new();
                    let seen = Arc::new(Mutex::new(Vec::new()));
                    for (matcher, result) in [
                        ("^default::write_stdin$", HookResult::Continue),
                        (".*", HookResult::Continue),
                        ("^(BashInput|KillShell|BashOutput)$", HookResult::Continue),
                        ("^write_stdin$", HookResult::Continue),
                        ("^BashOutput$", control.clone()),
                    ] {
                        dispatcher.register(Arc::new(RecordingHook {
                            point,
                            matcher: regex::Regex::new(matcher).unwrap(),
                            seen: seen.clone(),
                            result,
                        }));
                    }
                    let report = dispatcher
                        .run_hooks_with_resolved_shell(
                            point,
                            &payload,
                            &Session::new("test", "model"),
                            Some("write_stdin"),
                            Some(&input),
                        )
                        .await;
                    assert_eq!(report.outcome.decision, control);
                    assert_eq!(report.executions.len(), 6);
                    let seen = seen.lock().unwrap();
                    assert_eq!(&seen[..2], &[payload.clone(), payload]);
                    let name = |payload: &HookPayload| match payload {
                        HookPayload::ToolExecution { tool_name, .. }
                        | HookPayload::ToolResult { tool_name, .. } => tool_name.clone(),
                        _ => unreachable!(),
                    };
                    assert_eq!(name(&seen[2]), primary);
                    assert_eq!(name(&seen[3]), "BashOutput");
                    assert_eq!(name(&seen[4]), "write_stdin");
                    assert_eq!(name(&seen[5]), "BashOutput");
                    if let HookPayload::ToolExecution { parsed_args, .. } = &seen[5] {
                        assert_eq!(parsed_args, &json!({"bash_id":"exec"}));
                    }
                }
            }
        }
    }

    #[tokio::test]
    async fn secondary_output_allow_preserves_context_without_granting_input_authority() {
        for (matcher, chars, expected) in [
            ("^BashOutput$", "yes\n", HookResult::Continue),
            ("^BashInput$", "yes\n", HookResult::Allow),
            ("^write_stdin$", "yes\n", HookResult::Allow),
            ("^BashOutput$", "", HookResult::Allow),
        ] {
            let mut dispatcher = HookDispatcher::new();
            let seen = Arc::new(Mutex::new(Vec::new()));
            dispatcher.register(Arc::new(RecordingHook {
                point: AgentHookPoint::BeforeToolExecution,
                matcher: regex::Regex::new(matcher).unwrap(),
                seen: seen.clone(),
                result: HookResult::WithContext {
                    result: Box::new(HookResult::Allow),
                    text: "policy context".into(),
                },
            }));
            let payload = HookPayload::ToolExecution {
                tool_name: "write_stdin".into(),
                tool_call_id: "allow-call".into(),
                parsed_args: json!({"session_id":"exec", "chars":chars}),
            };
            let report = dispatcher
                .run_hooks(
                    AgentHookPoint::BeforeToolExecution,
                    &payload,
                    &Session::new("test", "model"),
                )
                .await;
            assert_eq!(report.outcome.decision, expected);
            assert_eq!(report.outcome.injected_contexts, ["policy context"]);
            assert_eq!(seen.lock().unwrap().len(), 1);
        }
    }

    #[tokio::test]
    async fn legacy_post_tool_hooks_resolve_stdin_operation_from_original_call() {
        use bamboo_agent_core::{FunctionCall, Message, ToolCall};
        for (input, legacy_name) in [
            (json!({"session_id":"exec", "chars":""}), "BashOutput"),
            (json!({"session_id":"exec", "chars":"yes\n"}), "BashInput"),
            (
                json!({"session_id":"exec", "chars":"", "eof":true}),
                "BashInput",
            ),
            (json!({"session_id":"exec", "chars":"\u{3}"}), "KillShell"),
        ] {
            let mut session = Session::new("test", "model");
            session.messages.push(Message::assistant(
                "",
                Some(vec![ToolCall {
                    id: "stdin-call".into(),
                    tool_type: "function".into(),
                    function: FunctionCall {
                        name: "write_stdin".into(),
                        arguments: input.to_string(),
                    },
                }]),
            ));
            let payload = HookPayload::ToolResult {
                tool_name: "write_stdin".into(),
                tool_call_id: "stdin-call".into(),
                outcome: bamboo_domain::HookToolOutcome {
                    success: true,
                    result: Some("done".into()),
                    error: None,
                    needs_human: false,
                    duration_ms: 1,
                },
            };
            let seen = Arc::new(Mutex::new(Vec::new()));
            let mut dispatcher = HookDispatcher::new();
            dispatcher.register(Arc::new(RecordingHook {
                point: AgentHookPoint::AfterToolExecution,
                matcher: regex::Regex::new(&format!("^{legacy_name}$")).unwrap(),
                seen: seen.clone(),
                result: HookResult::Ask,
            }));
            let report = dispatcher
                .run_hooks(AgentHookPoint::AfterToolExecution, &payload, &session)
                .await;
            assert_eq!(report.outcome.decision, HookResult::Ask);
            assert!(
                matches!(&seen.lock().unwrap()[0], HookPayload::ToolResult { tool_name, .. } if tool_name == legacy_name)
            );
        }
    }
}

#[cfg(test)]
mod non_suspending_tests;