aither-agent 0.27.0

AI agent framework with planning, memory, and tool execution for aither
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! Lifecycle hooks driven by real hook scripts through a scripted model.

use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use aither_core::llm::{
    Event, LLMRequest, Tool, ToolContext, ToolResult, model::Profile as ModelProfile,
};
use aither_sandbox::{ScriptLanguage, ToolRegistryBuilder};
use futures_core::Stream;
use schemars::JsonSchema;
use serde::Deserialize;

use super::*;
use crate::ask_user::{self, AnswerValue, AskUserArgs, AskUserTool, QuestionAnswer};
use crate::{Agent, HCons};

const HANDOFF_JSON: &str = r#"{"current_task":"keep going","key_decisions":[],"context_files":[],"errors_fixes":[],"pending_work":["continue"],"recovery_hints":[],"skills":[]}"#;

/// Longest any wait in these tests lasts.
const BOUND: Duration = Duration::from_secs(60);

#[derive(Debug)]
struct MockError;

impl std::fmt::Display for MockError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("mock error")
    }
}

impl std::error::Error for MockError {}

impl aither_core::llm::Classify for MockError {
    fn failure(&self) -> aither_core::llm::Failure {
        aither_core::llm::Failure::permanent()
    }
}

/// Answers each run with the next queued text and each compaction with a
/// handoff document.
#[derive(Clone)]
struct ScriptedLlm {
    answers: Arc<Mutex<VecDeque<String>>>,
}

impl ScriptedLlm {
    fn new(answers: &[&str]) -> Self {
        Self {
            answers: Arc::new(Mutex::new(
                answers.iter().map(ToString::to_string).collect(),
            )),
        }
    }
}

impl aither_core::LanguageModel for ScriptedLlm {
    type Error = MockError;

    fn respond(
        &self,
        request: LLMRequest,
    ) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
        let text = if request.parameters().structured_outputs {
            HANDOFF_JSON.to_string()
        } else {
            self.answers
                .lock()
                .unwrap()
                .pop_front()
                .unwrap_or_else(|| "done".to_string())
        };
        futures_lite::stream::iter([Ok(Event::Text(text))])
    }

    fn profile(
        &self,
    ) -> impl std::future::Future<Output = Result<ModelProfile, Self::Error>> + Send {
        std::future::ready(Ok(ModelProfile::new(
            "mock",
            "test",
            "mock-model",
            "mock model",
            1_000_000,
        )))
    }
}

/// Writes an executable hook script.
pub fn script(dir: &Path, name: &str, body: &str) -> PathBuf {
    use std::os::unix::fs::PermissionsExt;
    let path = dir.join(name);
    std::fs::write(&path, body).unwrap();
    std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
    path
}

/// Appends the input to the log named by the first argument and injects
/// `saw <event>` as context.
pub const RECORDER: &str = r#"#!/bin/sh
input=$(cat)
printf '%s\n' "$input" >> "$1"
event=$(printf '%s' "$input" | sed -n 's/^{"event":"\([a-z_]*\)".*/\1/p')
printf '{"context":"saw %s"}' "$event"
"#;

pub fn recorder(dir: &Path, log: &Path, events: &[LifecycleKind]) -> LifecycleHookConfig {
    LifecycleHookConfig {
        name: "recorder".into(),
        command: HookCommand {
            command: vec![
                "sh".into(),
                script(dir, "recorder.sh", RECORDER)
                    .to_string_lossy()
                    .into_owned(),
                log.to_string_lossy().into_owned(),
            ],
            deadline_ms: 20_000,
        },
        events: events.to_vec(),
    }
}

/// The inputs the recorder logged, in order.
pub fn logged(log: &Path) -> Vec<serde_json::Value> {
    std::fs::read_to_string(log)
        .unwrap_or_default()
        .lines()
        .map(|line| serde_json::from_str(line).expect("the input is JSON"))
        .collect()
}

fn events_of(log: &Path) -> Vec<String> {
    logged(log)
        .iter()
        .map(|input| input["event"].as_str().unwrap().to_string())
        .collect()
}

/// How many conversation messages carry `needle`.
fn mentions<H: crate::Hook>(
    agent: &Agent<ScriptedLlm, ScriptedLlm, ScriptedLlm, H>,
    needle: &str,
) -> usize {
    agent
        .history()
        .iter()
        .filter(|message| message.content().contains(needle))
        .count()
}

fn agent_with(
    llm: ScriptedLlm,
    hooks: &ExternalHooks,
) -> Agent<ScriptedLlm, ScriptedLlm, ScriptedLlm, HCons<ExternalHooks, ()>> {
    Agent::builder(llm).hook(hooks.clone()).build()
}

pub fn executor() -> executor_core::AnyExecutor {
    executor_core::AnyExecutor::new(executor_core::tokio::TokioGlobal)
}

/// Starts `config` with the fixture's prepared tools.
pub async fn live(config: HooksConfig, tools: &crate::test_support::PreparedFixture) -> LiveHooks {
    LiveHooks::start(config, &tools.tools, executor())
        .await
        .expect("the hooks start")
}

/// The main agent's scope over lifecycle `hooks`.
pub async fn lifecycle_hooks(
    hooks: Vec<LifecycleHookConfig>,
    tools: &crate::test_support::PreparedFixture,
) -> ExternalHooks {
    let config = HooksConfig {
        lifecycle: hooks,
        ..HooksConfig::default()
    };
    ExternalHooks::new(&live(config, tools).await)
}

/// The main agent's scope over `reviewers`.
pub async fn reviewer_hooks(
    reviewers: Vec<ReviewerConfig>,
    tools: &crate::test_support::PreparedFixture,
) -> ExternalHooks {
    let config = HooksConfig {
        reviewers,
        ..HooksConfig::default()
    };
    ExternalHooks::new(&live(config, tools).await)
}

async fn bounded<T>(work: impl std::future::Future<Output = T>) -> T {
    tokio::time::timeout(BOUND, work)
        .await
        .expect("the work finishes within its bound")
}

#[tokio::test]
async fn session_start_user_message_and_stop_fire_with_typed_input_and_inject_once() {
    let tools = crate::test_support::prepared_fixture().await;
    let dir = tempfile::tempdir().unwrap();
    let log = dir.path().join("log");
    let hooks = lifecycle_hooks(
        vec![recorder(
            dir.path(),
            &log,
            &[
                LifecycleKind::SessionStart,
                LifecycleKind::UserMessage,
                LifecycleKind::Stop,
            ],
        )],
        &tools,
    )
    .await;
    let mut agent = agent_with(ScriptedLlm::new(&["first answer", "second answer"]), &hooks);

    bounded(agent.query("hello")).await.unwrap();
    assert_eq!(events_of(&log), ["session_start", "user_message", "stop"]);
    let inputs = logged(&log);
    assert_eq!(inputs[0]["role"], "main");
    assert_eq!(
        inputs[1],
        serde_json::json!({
            "event": "user_message", "role": "main", "message": "hello", "attachments": 0
        })
    );
    assert_eq!(
        inputs[2],
        serde_json::json!({
            "event": "stop", "role": "main", "final_text": "first answer",
            "turns": 1, "reason": "no_tool_calls"
        })
    );
    // Context injected before the request reached it; the stop's waits for
    // the next request.
    assert_eq!(mentions(&agent, "saw session_start"), 1);
    assert_eq!(mentions(&agent, "saw user_message"), 1);
    assert_eq!(mentions(&agent, "saw stop"), 0);

    bounded(agent.query("again")).await.unwrap();
    assert_eq!(
        events_of(&log),
        [
            "session_start",
            "user_message",
            "stop",
            "user_message",
            "stop"
        ],
        "the session starts once"
    );
    assert_eq!(mentions(&agent, "saw session_start"), 1);
    assert_eq!(mentions(&agent, "saw user_message"), 2);
    assert_eq!(
        mentions(&agent, "saw stop"),
        1,
        "each stop is delivered once"
    );
    let alert = agent
        .history()
        .iter()
        .find(|message| message.content().contains("saw session_start"))
        .unwrap()
        .content()
        .to_string();
    assert!(
        alert.contains("The recorder hook added context when the session started:"),
        "{alert}"
    );
}

#[tokio::test]
async fn compaction_fires_before_and_after() {
    let tools = crate::test_support::prepared_fixture().await;
    let dir = tempfile::tempdir().unwrap();
    let log = dir.path().join("log");
    let hooks = lifecycle_hooks(
        vec![recorder(
            dir.path(),
            &log,
            &[
                LifecycleKind::BeforeCompaction,
                LifecycleKind::AfterCompaction,
            ],
        )],
        &tools,
    )
    .await;
    let mut agent = agent_with(ScriptedLlm::new(&["answer", "next"]), &hooks);

    bounded(agent.query("hello")).await.unwrap();
    assert_eq!(events_of(&log), Vec::<String>::new());
    let compacted = bounded(agent.compact(Some("the parser")))
        .await
        .unwrap()
        .expect("there was something to compact");

    assert_eq!(events_of(&log), ["before_compaction", "after_compaction"]);
    let inputs = logged(&log);
    assert_eq!(inputs[0]["messages"], compacted.messages_compacted);
    assert_eq!(inputs[0]["focus"], "the parser");
    assert_eq!(
        inputs[1]["messages_remaining"],
        compacted.messages_remaining
    );

    bounded(agent.query("go on")).await.unwrap();
    assert_eq!(mentions(&agent, "saw after_compaction"), 1);
    bounded(agent.query("and on")).await.unwrap();
    assert_eq!(
        mentions(&agent, "saw after_compaction"),
        1,
        "delivered once"
    );
}

/// A command that echoes its text argument.
#[derive(Debug, Deserialize, JsonSchema)]
struct EchoArgs {
    text: String,
}

struct EchoTool;

impl Tool for EchoTool {
    fn name(&self) -> std::borrow::Cow<'static, str> {
        "echo".into()
    }
    type Arguments = EchoArgs;
    type Res = ToolResult;

    fn call(
        &self,
        args: Self::Arguments,
        _cx: ToolContext,
    ) -> impl std::future::Future<Output = aither_core::Result<Self::Res>> + Send {
        std::future::ready(Ok(ToolResult::text(format!("echoed {}", args.text))))
    }
}

#[tokio::test]
async fn an_ipc_call_event_carries_the_resolved_name_arguments_and_result() {
    let tools = crate::test_support::prepared_fixture().await;
    let dir = tempfile::tempdir().unwrap();
    let log = dir.path().join("log");
    let hooks = lifecycle_hooks(
        vec![recorder(dir.path(), &log, &[LifecycleKind::CommandCall])],
        &tools,
    )
    .await;
    let mut builder = ToolRegistryBuilder::new();
    builder.configure_tool(EchoTool);
    builder.observe_commands(hooks.command_observer());
    let registry =
        builder.build(crate::test_support::output_store(dir.path().join("outputs")).await);

    let args = ["--text".to_string(), "hi there".to_string()];
    let envelope = bounded(registry.query_tool_handler("echo", &args)).await;
    assert!(envelope.ok, "{envelope:?}");
    let refused = bounded(registry.query_tool_handler("no-such-command", &[])).await;
    assert!(!refused.ok);

    // The hook runs in the background; wait for it to log its input.
    let deadline = Instant::now() + Duration::from_secs(20);
    while logged(&log).is_empty() && Instant::now() < deadline {
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    let inputs = logged(&log);
    assert_eq!(inputs.len(), 1, "only a resolved command is reported");
    assert_eq!(
        inputs[0],
        serde_json::json!({
            "event": "command_call", "role": "main", "name": "echo",
            "args": ["--text", "hi there"], "ok": true,
            "output": "echoed hi there", "error": null
        })
    );

    wait_for_context(&hooks, 1).await;
    let mut agent = agent_with(ScriptedLlm::new(&["a", "b"]), &hooks);
    bounded(agent.query("go")).await.unwrap();
    assert_eq!(mentions(&agent, "saw command_call"), 1);
    bounded(agent.query("go again")).await.unwrap();
    assert_eq!(mentions(&agent, "saw command_call"), 1, "delivered once");
}

/// Waits until `count` injected pieces are queued in `hooks`' inbox.
async fn wait_for_context(hooks: &ExternalHooks, count: usize) {
    let deadline = Instant::now() + Duration::from_secs(20);
    while hooks.queued() < count && Instant::now() < deadline {
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    assert_eq!(hooks.queued(), count);
}

#[tokio::test]
async fn an_ipc_call_answers_before_a_slow_hook_finishes_and_its_context_arrives_once() {
    let tools = crate::test_support::prepared_fixture().await;
    let dir = tempfile::tempdir().unwrap();
    let slow = script(
        dir.path(),
        "slow.sh",
        "#!/bin/sh\ncat >/dev/null\nsleep 3\nprintf '{\"context\":\"slow finished\"}'\n",
    );
    let hooks = lifecycle_hooks(
        vec![LifecycleHookConfig {
            name: "slow".into(),
            command: HookCommand {
                command: vec!["sh".into(), slow.to_string_lossy().into_owned()],
                deadline_ms: 20_000,
            },
            events: vec![LifecycleKind::CommandCall],
        }],
        &tools,
    )
    .await;
    let mut builder = ToolRegistryBuilder::new();
    builder.configure_tool(EchoTool);
    builder.observe_commands(hooks.command_observer());
    let registry =
        builder.build(crate::test_support::output_store(dir.path().join("outputs")).await);

    let started = Instant::now();
    let args = ["--text".to_string(), "hi".to_string()];
    let envelope = bounded(registry.query_tool_handler("echo", &args)).await;

    assert!(envelope.ok, "{envelope:?}");
    assert!(
        started.elapsed() < Duration::from_secs(2),
        "the call waited for its hook: {:?}",
        started.elapsed()
    );
    assert_eq!(hooks.queued(), 0, "the hook is still running");

    wait_for_context(&hooks, 1).await;
    let mut agent = agent_with(ScriptedLlm::new(&["a", "b"]), &hooks);
    bounded(agent.query("go")).await.unwrap();
    assert_eq!(mentions(&agent, "slow finished"), 1);
    bounded(agent.query("go again")).await.unwrap();
    assert_eq!(mentions(&agent, "slow finished"), 1, "delivered once");
}

#[tokio::test]
async fn an_ask_user_event_carries_the_questions_and_the_answers() {
    let tools = crate::test_support::prepared_fixture().await;
    let dir = tempfile::tempdir().unwrap();
    let log = dir.path().join("log");
    let hooks = lifecycle_hooks(
        vec![recorder(dir.path(), &log, &[LifecycleKind::AskUser])],
        &tools,
    )
    .await;
    let (broker, queue) = ask_user::channel();
    let tool = AskUserTool::new("session", broker).with_hooks(hooks.clone());
    let user = tokio::spawn(async move {
        queue
            .next()
            .await
            .expect("a question arrives")
            .respond(vec![QuestionAnswer {
                section: String::new(),
                answer: AnswerValue::Selected(vec!["postgres".into()]),
            }])
            .unwrap();
    });
    let args = AskUserArgs {
        question: Some("Which database?".into()),
        option: vec!["postgres".into(), "sqlite".into()],
        multi_select: false,
        questions: Vec::new(),
    };

    bounded(Tool::call(&tool, args, ToolContext::new()))
        .await
        .unwrap();
    bounded(user).await.unwrap();

    let inputs = logged(&log);
    assert_eq!(inputs.len(), 1);
    assert_eq!(inputs[0]["event"], "ask_user");
    assert_eq!(inputs[0]["questions"][0]["question"], "Which database?");
    assert_eq!(inputs[0]["questions"][0]["option"][1], "sqlite");
    assert_eq!(inputs[0]["answers"][0]["answer"][0], "postgres");
    assert_eq!(crate::Hook::take_injected_context(&hooks).len(), 1);
}

#[tokio::test]
async fn a_lifecycle_hook_cannot_block() {
    let tools = crate::test_support::prepared_fixture().await;
    let dir = tempfile::tempdir().unwrap();
    let hook = |name: &str, body: &str| LifecycleHookConfig {
        name: name.into(),
        command: HookCommand {
            command: vec![
                "sh".into(),
                script(dir.path(), name, body)
                    .to_string_lossy()
                    .into_owned(),
            ],
            deadline_ms: 20_000,
        },
        events: vec![LifecycleKind::UserMessage, LifecycleKind::Stop],
    };
    let hooks = lifecycle_hooks(vec![
            // Tries to block, with context attached.
            hook(
                "decider",
                "#!/bin/sh\ncat >/dev/null\nprintf '{\"decision\":\"block\",\"context\":\"smuggled\"}'\n",
            ),
            // Fails outright.
            hook(
                "crasher",
                "#!/bin/sh\ncat >/dev/null\necho broken >&2\nexit 3\n",
            ),
            // Prints something that is not an answer.
            hook("rambler", "#!/bin/sh\ncat >/dev/null\necho not json\n"),
            // Says nothing.
            hook("silent", "#!/bin/sh\ncat >/dev/null\n"),
        ], &tools).await;
    let mut agent = agent_with(ScriptedLlm::new(&["the run went through"]), &hooks);

    let answer = bounded(agent.query("hello")).await.unwrap();

    assert_eq!(answer, "the run went through");
    assert_eq!(mentions(&agent, "smuggled"), 0);
    assert_eq!(crate::Hook::take_injected_context(&hooks).len(), 0);
    assert!(serde_json::from_str::<LifecycleOutput>(r#"{"approve":true}"#).is_err());
    assert_eq!(
        serde_json::from_str::<LifecycleOutput>("{}").unwrap(),
        LifecycleOutput::default()
    );
}

fn pid_is_alive(pid: &str) -> bool {
    std::process::Command::new("kill")
        .args(["-0", pid])
        .stderr(std::process::Stdio::null())
        .status()
        .unwrap()
        .success()
}

#[tokio::test]
async fn a_hook_past_its_deadline_is_killed_with_its_process_group() {
    let tools = crate::test_support::prepared_fixture().await;
    let dir = tempfile::tempdir().unwrap();
    let pids = dir.path().join("pids");
    let hung = script(
        dir.path(),
        "hung.sh",
        "#!/bin/sh\ncat >/dev/null\nsleep 300 &\necho $$ $! > \"$1\"\nwait\n",
    );
    let command = HookCommand {
        command: vec![
            "sh".into(),
            hung.to_string_lossy().into_owned(),
            pids.to_string_lossy().into_owned(),
        ],
        deadline_ms: 1_000,
    };

    let started = Instant::now();
    let error = bounded(
        HookRunner::new(&tools.tools)
            .run::<_, LifecycleOutput>(&command, &serde_json::json!({"event": "stop"})),
    )
    .await
    .expect_err("the hook outlives its deadline");

    assert!(matches!(error, HookRunError::Timeout { .. }), "{error}");
    assert!(started.elapsed() < Duration::from_secs(30));
    let recorded = std::fs::read_to_string(&pids).unwrap();
    let pids: Vec<&str> = recorded.split_whitespace().collect();
    assert_eq!(
        pids.len(),
        2,
        "the hook and its background child: {recorded}"
    );
    for pid in pids {
        let deadline = Instant::now() + Duration::from_secs(20);
        while pid_is_alive(pid) && Instant::now() < deadline {
            tokio::time::sleep(Duration::from_millis(50)).await;
        }
        assert!(
            !pid_is_alive(pid),
            "process {pid} outlived its group's kill"
        );
    }
}

#[tokio::test]
async fn a_hook_that_fails_in_every_way_is_reported_and_ignored() {
    let tools = crate::test_support::prepared_fixture().await;
    let dir = tempfile::tempdir().unwrap();
    let runner = HookRunner::new(&tools.tools);
    let runner = &runner;
    let run = |command: Vec<String>| {
        let command = HookCommand {
            command,
            deadline_ms: 20_000,
        };
        async move { bounded(runner.run::<_, LifecycleOutput>(&command, &serde_json::json!({}))).await }
    };
    assert!(matches!(run(vec![]).await, Err(HookRunError::EmptyCommand)));
    assert!(matches!(
        run(vec![
            dir.path().join("missing").to_string_lossy().into_owned()
        ])
        .await,
        Err(HookRunError::Spawn { .. })
    ));
    assert!(matches!(
        run(vec!["sh".into(), "-c".into(), "exit 2".into()]).await,
        Err(HookRunError::Exit { .. })
    ));
    assert!(matches!(
        run(vec![
            "sh".into(),
            "-c".into(),
            "echo '{\"context\":1}'".into()
        ])
        .await,
        Err(HookRunError::Output(_))
    ));
    assert_eq!(
        run(vec!["sh".into(), "-c".into(), "true".into()])
            .await
            .unwrap(),
        None
    );
}

pub mod reviewers {
    use aither_sandbox::permission::{
        PermissionError, PermissionHandler, PermissionRequest, Review, TerminalMode,
    };

    use super::*;

    /// Logs the input to the file named by the first argument and prints the
    /// answer given as the second.
    pub const REVIEWER: &str = r#"#!/bin/sh
cat >> "$1"
printf '\n' >> "$1"
printf '%s' "$2"
"#;

    /// The existing route: records the requests that reach it and answers
    /// `allow`.
    #[derive(Clone, Default)]
    pub struct Route {
        pub asked: Arc<Mutex<Vec<(TerminalMode, String)>>>,
        pub allow: bool,
        /// The domains `check_domain` was asked about.
        pub domains_asked: Arc<Mutex<Vec<String>>>,
        /// Whether `check_domain` approves the domains it is asked about.
        pub allow_domain: bool,
        /// Domains that need no approval: the default allow-list and the
        /// domains already approved, like a user-facing handler keeps.
        pub allowed_domains: Arc<Mutex<Vec<String>>>,
    }

    pub const COMMAND: PermissionRequest<'static> = PermissionRequest::Command {
        mode: TerminalMode::Unsafe,
        script: "make install",
        language: ScriptLanguage::Shell,
    };

    pub const DOMAIN: PermissionRequest<'static> = PermissionRequest::Domain {
        domain: "registry.example",
        port: 443,
    };

    impl PermissionHandler for Route {
        fn check(
            &self,
            mode: TerminalMode,
            script: &str,
            _language: ScriptLanguage,
        ) -> impl Future<Output = Result<bool, PermissionError>> + Send {
            self.asked.lock().unwrap().push((mode, script.to_string()));
            std::future::ready(Ok(self.allow))
        }

        fn check_domain(&self, domain: &str, _port: u16) -> impl Future<Output = bool> + Send {
            self.domains_asked.lock().unwrap().push(domain.to_string());
            let approved = {
                let mut allowed = self.allowed_domains.lock().unwrap();
                let approved = allowed.iter().any(|d| d == domain);
                if self.allow_domain && !approved {
                    allowed.push(domain.to_string());
                }
                approved
            };
            std::future::ready(self.allow_domain || approved)
        }

        fn domain_needs_approval(
            &self,
            domain: &str,
            _port: u16,
        ) -> impl Future<Output = bool> + Send {
            std::future::ready(
                !self
                    .allowed_domains
                    .lock()
                    .unwrap()
                    .iter()
                    .any(|d| d == domain),
            )
        }

        fn remember_domain(&self, domain: &str, _port: u16) -> impl Future<Output = ()> + Send {
            self.allowed_domains
                .lock()
                .unwrap()
                .push(domain.to_string());
            std::future::ready(())
        }
    }

    pub fn reviewer(dir: &Path, name: &str, log: &Path, answer: &str) -> ReviewerConfig {
        ReviewerConfig {
            name: name.into(),
            command: HookCommand {
                command: vec![
                    "sh".into(),
                    script(dir, "reviewer.sh", REVIEWER)
                        .to_string_lossy()
                        .into_owned(),
                    log.to_string_lossy().into_owned(),
                    answer.into(),
                ],
                deadline_ms: 20_000,
            },
        }
    }

    async fn handler(chain: Vec<ReviewerConfig>) -> (ReviewedPermission<Route>, Route) {
        let tools = crate::test_support::prepared_fixture().await;
        let route = Route::default();
        (
            ReviewedPermission::new(route.clone(), reviewer_hooks(chain, &tools).await),
            route,
        )
    }

    #[tokio::test]
    async fn approve_grants_without_reaching_the_route_and_ends_the_chain() {
        let dir = tempfile::tempdir().unwrap();
        let (first, second) = (dir.path().join("first"), dir.path().join("second"));
        let (handler, route) = handler(vec![
            reviewer(dir.path(), "first", &first, r#"{"decision":"approve"}"#),
            reviewer(
                dir.path(),
                "second",
                &second,
                r#"{"decision":"deny","reason":"no"}"#,
            ),
        ])
        .await;
        assert_eq!(bounded(handler.review(&COMMAND)).await, Review::Approved);
        assert!(route.asked.lock().unwrap().is_empty());
        assert_eq!(logged(&first).len(), 1);
        assert!(!second.exists(), "the first decision ends the chain");
        assert_eq!(
            logged(&first)[0],
            serde_json::json!({"role": "main", "kind": "command", "mode": "unsafe", "script": "make install", "language": "shell"})
        );
    }

    #[tokio::test]
    async fn deny_refuses_with_a_reason_the_model_reads() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("log");
        let (handler, route) = handler(vec![
            reviewer(dir.path(), "policy", &log, r#"{"decision":"pass"}"#),
            reviewer(
                dir.path(),
                "guard",
                &log,
                r#"{"decision":"deny","reason":"touches /etc"}"#,
            ),
        ])
        .await;
        let Review::Denied(reason) = bounded(handler.review(&COMMAND)).await else {
            panic!("the guard denies");
        };
        assert_eq!(
            reason.trim(),
            "The permission reviewer guard denied this request: touches /etc"
        );
        assert!(route.asked.lock().unwrap().is_empty());
        assert_eq!(logged(&log).len(), 2, "the passing reviewer ran first");
    }

    #[tokio::test]
    async fn a_request_every_reviewer_passes_reaches_the_existing_route() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("log");
        let route = Route {
            allow: true,
            ..Route::default()
        };
        let tools = crate::test_support::prepared_fixture().await;
        let handler = ReviewedPermission::new(
            route.clone(),
            reviewer_hooks(
                vec![
                    reviewer(dir.path(), "a", &log, r#"{"decision":"pass"}"#),
                    reviewer(dir.path(), "b", &log, ""),
                ],
                &tools,
            )
            .await,
        );
        assert_eq!(bounded(handler.review(&COMMAND)).await, Review::Passed);
        assert!(
            bounded(handler.check(TerminalMode::Unsafe, "make install", ScriptLanguage::Shell))
                .await
                .unwrap()
        );
        assert_eq!(
            *route.asked.lock().unwrap(),
            [(TerminalMode::Unsafe, "make install".to_string())]
        );
        assert_eq!(logged(&log).len(), 2);
    }

    #[tokio::test]
    async fn a_subagents_request_is_labelled_and_reaches_the_same_route() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("log");
        let (handler, route) = handler(vec![reviewer(
            dir.path(),
            "a",
            &log,
            r#"{"decision":"pass"}"#,
        )])
        .await;
        let subagent = handler
            .for_subagent()
            .expect("a reviewed handler labels subagents");
        assert_eq!(bounded(subagent.review(&COMMAND)).await, Review::Passed);
        bounded(subagent.check(TerminalMode::Unsafe, "make install", ScriptLanguage::Shell))
            .await
            .unwrap();
        assert_eq!(logged(&log)[0]["role"], "subagent");
        assert_eq!(route.asked.lock().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn a_reviewer_that_fails_counts_as_a_pass_and_grants_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("log");
        let failing = |body: &str, name: &str| {
            let path = script(dir.path(), name, body);
            ReviewerConfig {
                name: name.into(),
                command: HookCommand {
                    command: vec![path.to_string_lossy().into_owned()],
                    deadline_ms: 500,
                },
            }
        };
        let (handler, route) = handler(vec![
            failing("#!/bin/sh\nexit 3\n", "crashes"),
            failing("#!/bin/sh\nsleep 30\n", "hangs"),
            failing("#!/bin/sh\nprintf 'approve please'\n", "garbles"),
            failing(
                "#!/bin/sh\nprintf '{\"decision\":\"approve\",\"extra\":1}'\n",
                "extra_field",
            ),
            ReviewerConfig {
                name: "missing".into(),
                command: HookCommand {
                    command: vec![dir.path().join("absent").to_string_lossy().into_owned()],
                    deadline_ms: 500,
                },
            },
        ])
        .await;
        let started = Instant::now();
        assert_eq!(
            bounded(handler.review(&COMMAND)).await,
            Review::Passed,
            "a failure grants nothing"
        );
        assert!(route.asked.lock().unwrap().is_empty());
        assert!(
            started.elapsed() < Duration::from_secs(20),
            "the hang was killed"
        );
        assert!(!log.exists());
    }

    #[tokio::test]
    async fn a_reviewer_approves_a_domain_and_it_is_not_reviewed_again() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("log");
        let (handler, route) = handler(vec![reviewer(
            dir.path(),
            "net",
            &log,
            r#"{"decision":"approve"}"#,
        )])
        .await;
        assert!(bounded(handler.authorize_domain("registry.example", 443)).await);
        assert!(
            route.domains_asked.lock().unwrap().is_empty(),
            "the user's route was not asked"
        );
        assert_eq!(
            logged(&log),
            [serde_json::json!({
                "role": "main", "kind": "domain", "domain": "registry.example", "port": 443
            })]
        );
        // The next connection to it is remembered: no reviewer runs.
        assert!(bounded(handler.authorize_domain("registry.example", 443)).await);
        assert_eq!(logged(&log).len(), 1);
        // Another domain is reviewed.
        assert!(bounded(handler.authorize_domain("other.example", 443)).await);
        assert_eq!(logged(&log).len(), 2);
    }

    #[tokio::test]
    async fn a_reviewer_denies_a_domain_and_it_is_reviewed_again() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("log");
        let (handler, route) = handler(vec![reviewer(
            dir.path(),
            "net",
            &log,
            r#"{"decision":"deny","reason":"unknown host"}"#,
        )])
        .await;
        let Review::Denied(reason) = bounded(handler.review(&DOMAIN)).await else {
            panic!("the reviewer denies");
        };
        assert!(reason.contains("unknown host"));
        assert!(!bounded(handler.authorize_domain("registry.example", 443)).await);
        assert!(!bounded(handler.authorize_domain("registry.example", 443)).await);
        assert!(route.domains_asked.lock().unwrap().is_empty());
        assert_eq!(logged(&log).len(), 3, "a denial is not remembered");
    }

    #[tokio::test]
    async fn an_allowed_domain_and_a_passed_domain_follow_the_existing_route() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("log");
        let route = Route {
            allow_domain: true,
            allowed_domains: Arc::new(Mutex::new(vec!["allowed.example".into()])),
            ..Route::default()
        };
        let tools = crate::test_support::prepared_fixture().await;
        let handler = ReviewedPermission::new(
            route.clone(),
            reviewer_hooks(
                vec![reviewer(dir.path(), "net", &log, r#"{"decision":"pass"}"#)],
                &tools,
            )
            .await,
        );
        // Allowed without asking anyone: no reviewer, and the route decides.
        assert!(bounded(handler.authorize_domain("allowed.example", 443)).await);
        assert!(!log.exists());
        // Passed by every reviewer: the existing route is asked, once.
        assert!(bounded(handler.authorize_domain("registry.example", 443)).await);
        assert_eq!(logged(&log).len(), 1);
        assert_eq!(
            *route.domains_asked.lock().unwrap(),
            ["allowed.example", "registry.example"]
        );
        assert!(bounded(handler.authorize_domain("registry.example", 443)).await);
        assert_eq!(logged(&log).len(), 1, "the user's approval is remembered");
    }
}