aither-agent 0.31.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
//! What a run emits: the events that observe it.
//!
//! Events observe; [`Hooks`](crate::Hooks) decide. Every enum, every variant
//! and every struct here is `#[non_exhaustive]`, and none has a public
//! constructor, because only aither emits them: a consumer matches with a
//! wildcard arm and keeps working when a release adds an event.
//!
//! Every event reaches the consumer as a [`TimedEvent`], which names the agent
//! that emitted it by its [`AgentPath`]: a subagent's events are forwarded
//! on its parent's stream, tagged with the child's path.

use core::time::Duration;
use std::fmt;

use aither_core::llm::tool::{ApprovalKind, Progress};
use aither_core::llm::{ToolCallId, ToolResult, Usage};
use jiff::Timestamp;

use crate::context_window::ContextWindowPhase;
use crate::error::AgentError;

pub use aither_core::llm::AgentPath;

macro_rules! text_id {
    ($(#[$meta:meta])* $name:ident) => {
        $(#[$meta])*
        #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
        #[cfg_attr(feature = "serde", serde(transparent))]
        pub struct $name(String);

        impl $name {
            /// Names an id, for an agent that lives outside this crate and
            /// mints its own.
            #[must_use]
            pub fn new(id: impl Into<String>) -> Self {
                Self(id.into())
            }

            /// The identifier as text.
            #[must_use]
            pub fn as_str(&self) -> &str {
                &self.0
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(&self.0)
            }
        }
    };
}

macro_rules! counter_id {
    ($(#[$meta:meta])* $name:ident($inner:ty)) => {
        $(#[$meta])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
        #[cfg_attr(feature = "serde", serde(transparent))]
        pub struct $name($inner);

        impl $name {
            /// Names an id, for an agent that lives outside this crate and
            /// mints its own.
            #[must_use]
            pub const fn new(id: $inner) -> Self {
                Self(id)
            }

            /// The identifier's number.
            #[must_use]
            pub const fn get(self) -> $inner {
                self.0
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                self.0.fmt(f)
            }
        }
    };
}

text_id! {
    /// Identifies one run of an agent: from [`Agent::run`](crate::Agent::run)
    /// or [`Agent::resume`](crate::Agent::resume) to its
    /// [`RunEnded`](AgentEvent::RunEnded).
    RunId
}

/// Identifies a [context source](crate::ContextSource) in
/// [`ContextInjected`](AgentEvent::ContextInjected).
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct SourceId(String);

impl SourceId {
    /// Names a source.
    #[must_use]
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// The name as text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for SourceId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

counter_id! {
    /// Identifies one request sent to the model, unique within an agent. A
    /// request sent again after its response stream broke has a new id.
    RequestId(u64)
}

counter_id! {
    /// Identifies one block of a response within its request.
    BlockId(u32)
}

counter_id! {
    /// Identifies one pause, unique within an agent.
    PauseId(u64)
}

/// What a streamed block of the model's response is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum OutputKind {
    /// Text for the reader.
    Text,
    /// The model's reasoning.
    Reasoning,
}

/// Where a tool call runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum ToolOrigin {
    /// A tool registered on the agent, run by the agent.
    Local,
    /// A tool the provider runs itself; nothing runs locally.
    Hosted,
}

/// What the agent settled on for a tool call before running it.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum ToolVerdict {
    /// The call runs as the model made it.
    Allowed,
    /// The call does not run; the model is told `reason`.
    Denied {
        /// Why the call was refused.
        reason: String,
    },
    /// The call runs with arguments a hook replaced.
    Rewritten,
    /// The call waits on the approver; a [`Paused`](AgentEvent::Paused)
    /// follows when the answer is not immediate, and the verdict is
    /// [`Allowed`](Self::Allowed) or [`Denied`](Self::Denied) once it comes.
    AwaitingApproval,
}

/// Why a run stopped short of an answer without failing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum RunPause {
    /// The context window is full and the run cannot continue.
    ///
    /// Emitted when usage reaches the
    /// [pause point](crate::ContextPolicy::pause_point) and no compaction can
    /// relieve it: there was nothing to compact, or the compaction landed and
    /// the window is still full. Sending the request anyway would have the
    /// provider reject it for length. A compaction that fails twice is a
    /// failed run, not this pause.
    ContextExhausted,
}

/// How a run ended.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum RunOutcome {
    /// The model answered and nothing held the run.
    Completed {
        /// The model's final reply.
        final_text: String,
    },
    /// The run stopped for a reason that needs the embedder.
    Paused {
        /// Why.
        pause: RunPause,
    },
    /// [`Run::cancel`](crate::Run::cancel) was called, or the run was dropped.
    Cancelled,
    /// The run failed; [`Run::drive`](crate::Run::drive) returns the error.
    Failed {
        /// What went wrong.
        error: AgentError,
    },
    /// The model kept calling tools past
    /// [`max_iterations`](crate::AgentConfig::max_iterations).
    IterationLimit,
}

/// How a request ended.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum RequestOutcome {
    /// The response stream ended.
    Completed,
    /// The response stream broke after it opened, and the same request is
    /// sent again after `delay` as a new request.
    ///
    /// Everything streamed since the request started — text, reasoning,
    /// tool-call deltas and hosted tool calls — belonged to the broken
    /// attempt: none of it enters the conversation and none of its tool calls
    /// run. A client drops what it rendered of that attempt.
    Retrying {
        /// Why the stream broke.
        error: AgentError,
        /// Which retry this is, counting from 1.
        retry: u32,
        /// The most retries the model's
        /// [`RetryPolicy`](aither_core::llm::RetryPolicy) allows.
        max_retries: u32,
        /// How long the agent waits before sending the request again.
        delay: Duration,
    },
    /// The request failed and the run fails with the error.
    Failed {
        /// What went wrong.
        error: AgentError,
    },
}

/// Why a compaction started.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum CompactionTrigger {
    /// [`Agent::compact`](crate::Agent::compact) was called.
    Requested,
    /// The window crossed the policy's
    /// [start point](crate::ContextPolicy::start_point); the compaction is
    /// forked alongside the run.
    WindowFilling,
    /// The window reached the pause point and the run made room.
    PausePoint,
    /// [`Agent::compact_when_idle`](crate::Agent::compact_when_idle) ran
    /// shortly before the cache expired.
    Idle,
    /// A send found the cache cold and a compaction model set.
    ColdCache,
}

/// How a compaction announced by [`AgentEvent::CompactionStarted`] ended.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum CompactionOutcome {
    /// The handoff replaced the conversation below its fork point.
    Applied(crate::CompactResult),
    /// There was no conversation to compact; the window is as it was.
    NothingToCompact,
    /// The compaction failed; the window is as it was.
    Failed(AgentError),
    /// The handoff was dropped unapplied: the window it was forked from was
    /// replaced first, by an explicit compaction,
    /// [`clear_history`](crate::Agent::clear_history),
    /// [`restore_checkpoint`](crate::Agent::restore_checkpoint), or by
    /// dropping the run that carried it.
    Discarded,
}

/// What an injection into the conversation was.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum InjectionKind {
    /// A durable block appended to the conversation, which stays in the
    /// cached prefix from the next request on.
    Stable,
    /// A block rendered into one request's volatile tail.
    Tail,
    /// The diff alert of a live source whose content changed.
    LiveChange,
}

/// Why a checkpoint was emitted.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum CheckpointReason {
    /// A turn that called tools ended.
    TurnBoundary,
    /// The run is stopping.
    Stop,
}

/// Events emitted during agent execution.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum AgentEvent {
    /// A run began.
    #[non_exhaustive]
    RunStarted {
        /// The run.
        run: RunId,
        /// The agent that runs it.
        agent: AgentPath,
    },

    /// A run ended; the last event of the run.
    #[non_exhaustive]
    RunEnded {
        /// The run.
        run: RunId,
        /// How it ended.
        outcome: RunOutcome,
    },

    /// A turn began: one request to the model and the tool calls it makes.
    #[non_exhaustive]
    TurnStarted {
        /// The run.
        run: RunId,
        /// The turn, counting from 1.
        turn: usize,
    },

    /// A turn ended.
    #[non_exhaustive]
    TurnEnded {
        /// The run.
        run: RunId,
        /// The turn.
        turn: usize,
        /// How many tool calls the turn made.
        tool_calls: usize,
    },

    /// A request to the model was sent.
    #[non_exhaustive]
    RequestStarted {
        /// The request.
        request: RequestId,
        /// The hash chain of its prompt prefix and what rewrote that prefix
        /// since the previous request, so a host can tell where a cache miss
        /// began.
        prefix: crate::context::PromptPrefix,
    },

    /// A request ended.
    #[non_exhaustive]
    RequestEnded {
        /// The request.
        request: RequestId,
        /// The usage the provider reported for it, if it did.
        usage: Option<Usage>,
        /// How it ended.
        outcome: RequestOutcome,
    },

    /// A chunk of a block of the response.
    #[non_exhaustive]
    BlockDelta {
        /// The request.
        request: RequestId,
        /// The block within the request.
        block: BlockId,
        /// What the block is.
        kind: OutputKind,
        /// The chunk.
        text: String,
    },

    /// A block of the response is complete.
    #[non_exhaustive]
    BlockEnded {
        /// The request.
        request: RequestId,
        /// The block.
        block: BlockId,
        /// What the block is.
        kind: OutputKind,
    },

    /// The model is streaming a tool call's name and arguments, before the
    /// call is assembled.
    #[non_exhaustive]
    ToolCallDelta {
        /// The call.
        call: ToolCallId,
        /// The tool; empty in the earliest deltas of some providers.
        name: String,
        /// The next fragment of the JSON arguments.
        arguments_fragment: String,
    },

    /// A tool call was made.
    #[non_exhaustive]
    ToolCallStarted {
        /// The call.
        call: ToolCallId,
        /// The tool.
        name: String,
        /// The arguments as the model made them; the detail of a hosted
        /// call as a string.
        arguments: serde_json::Value,
        /// Where the call runs.
        origin: ToolOrigin,
    },

    /// The agent settled whether the call runs, and how.
    #[non_exhaustive]
    ToolCallDecided {
        /// The call.
        call: ToolCallId,
        /// What was decided.
        decision: ToolVerdict,
    },

    /// A running tool reported progress.
    #[non_exhaustive]
    ToolProgress {
        /// The call.
        call: ToolCallId,
        /// The tool's typed report.
        progress: Progress,
    },

    /// A tool call ended.
    #[non_exhaustive]
    ToolCallEnded {
        /// The call.
        call: ToolCallId,
        /// What the model reads of it.
        result: ToolResult,
    },

    /// Work is waiting on something outside the agent: an approval, or a
    /// question a tool put through the approver under a kind of its own.
    #[non_exhaustive]
    Paused {
        /// The pause.
        pause: PauseId,
        /// The tool call that waits, if a tool call does.
        call: Option<ToolCallId>,
        /// What is waited for, as the embedder named it.
        kind: ApprovalKind,
        /// What the approval is about.
        subject: String,
    },

    /// The pause ended.
    #[non_exhaustive]
    Resumed {
        /// The pause.
        pause: PauseId,
    },

    /// A compaction started: one forked alongside the run, or one run by
    /// [`Agent::compact`](crate::Agent::compact),
    /// [`Agent::compact_when_idle`](crate::Agent::compact_when_idle) or the
    /// run itself at the pause point or on a cold cache.
    ///
    /// Exactly one [`CompactionEnded`](Self::CompactionEnded) follows, and no
    /// other compaction starts before it. It is emitted on the stream of
    /// whatever ends the compaction, which for one forked alongside a run may
    /// be a later run or explicit compaction; one ended where no stream is
    /// open finishes as [`CompactionOutcome::Discarded`] first thing on the
    /// next stream the agent opens.
    #[non_exhaustive]
    CompactionStarted {
        /// Why it started.
        trigger: CompactionTrigger,
    },

    /// The compaction last [started](Self::CompactionStarted) ended.
    #[non_exhaustive]
    CompactionEnded {
        /// How it ended.
        outcome: CompactionOutcome,
    },

    /// A compaction failed with the window at the
    /// [pause point](crate::ContextPolicy::pause_point), so the context was
    /// cleaned and the compaction reruns on it now.
    ///
    /// The cleanup removes system alerts and reminders and moves long tool
    /// outputs to files. When the rerun fails too the run fails; otherwise
    /// the run continues on the compacted window.
    #[non_exhaustive]
    CompactionRecovery {
        /// Why the compaction failed.
        error: AgentError,
        /// System alerts and reminders the cleanup removed.
        alerts_removed: usize,
        /// Long tool outputs the cleanup moved to files.
        outputs_spilled: usize,
    },

    /// A context source or a hook put a block into the conversation.
    #[non_exhaustive]
    ContextInjected {
        /// Who injected it.
        source: SourceId,
        /// What kind of injection it was.
        kind: InjectionKind,
    },

    /// A subagent began. Its own events follow, tagged with its path.
    #[non_exhaustive]
    SubagentStarted {
        /// The child's path.
        child: AgentPath,
        /// The child's name.
        name: String,
        /// What it was asked to do.
        prompt: String,
    },

    /// A subagent ended.
    #[non_exhaustive]
    SubagentEnded {
        /// The child's path.
        child: AgentPath,
        /// How its run ended.
        outcome: RunOutcome,
    },

    /// A snapshot of the agent's state was taken after it changed.
    #[non_exhaustive]
    Checkpoint {
        /// The run.
        run: RunId,
        /// Why the checkpoint was emitted.
        reason: CheckpointReason,
        /// The turn.
        turn: usize,
        /// The lifecycle phase of the assembled context window.
        phase: ContextWindowPhase,
        /// Number of recent conversation messages currently in memory.
        message_count: usize,
    },
}

/// An [`AgentEvent`] with the UTC time it was emitted and the agent that
/// emitted it.
///
/// The time is taken at the emission point inside the run, not when the
/// consumer reads the event.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct TimedEvent {
    /// When the agent emitted the event.
    pub at: Timestamp,
    /// Which agent emitted it: the main agent, or a subagent whose events
    /// are forwarded on its parent's stream.
    pub agent: AgentPath,
    /// The event itself.
    pub event: AgentEvent,
}

impl TimedEvent {
    /// An event emitted now by `agent`, for a [`ChildAgent`](crate::ChildAgent)
    /// that lives outside this crate.
    #[must_use]
    pub fn now(agent: AgentPath, event: AgentEvent) -> Self {
        Self {
            at: Timestamp::now(),
            agent,
            event,
        }
    }
}

/// Constructors for the events an agent that lives outside this crate, such
/// as an external session run as a [`ChildAgent`](crate::ChildAgent), can
/// report.
impl AgentEvent {
    /// A run began.
    #[must_use]
    pub const fn run_started(run: RunId, agent: AgentPath) -> Self {
        Self::RunStarted { run, agent }
    }

    /// A run ended.
    #[must_use]
    pub const fn run_ended(run: RunId, outcome: RunOutcome) -> Self {
        Self::RunEnded { run, outcome }
    }

    /// A chunk of a block of a response.
    #[must_use]
    pub const fn block_delta(
        request: RequestId,
        block: BlockId,
        kind: OutputKind,
        text: String,
    ) -> Self {
        Self::BlockDelta {
            request,
            block,
            kind,
            text,
        }
    }

    /// A block of a response is complete.
    #[must_use]
    pub const fn block_ended(request: RequestId, block: BlockId, kind: OutputKind) -> Self {
        Self::BlockEnded {
            request,
            block,
            kind,
        }
    }

    /// A tool call was made.
    #[must_use]
    pub const fn tool_call_started(
        call: ToolCallId,
        name: String,
        arguments: serde_json::Value,
        origin: ToolOrigin,
    ) -> Self {
        Self::ToolCallStarted {
            call,
            name,
            arguments,
            origin,
        }
    }

    /// The call's fate was settled.
    #[must_use]
    pub const fn tool_call_decided(call: ToolCallId, decision: ToolVerdict) -> Self {
        Self::ToolCallDecided { call, decision }
    }

    /// A running tool reported progress.
    #[must_use]
    pub const fn tool_progress(call: ToolCallId, progress: Progress) -> Self {
        Self::ToolProgress { call, progress }
    }

    /// A tool call ended.
    #[must_use]
    pub const fn tool_call_ended(call: ToolCallId, result: ToolResult) -> Self {
        Self::ToolCallEnded { call, result }
    }
}

impl AgentEvent {
    /// The run this event belongs to, for the events that carry one.
    #[must_use]
    pub const fn run(&self) -> Option<&RunId> {
        match self {
            Self::RunStarted { run, .. }
            | Self::RunEnded { run, .. }
            | Self::TurnStarted { run, .. }
            | Self::TurnEnded { run, .. }
            | Self::Checkpoint { run, .. } => Some(run),
            _ => None,
        }
    }
}

#[cfg(all(test, feature = "serde"))]
mod serde_tests {
    use aither_core::llm::tool::{ApprovalError, ApproverFailure};
    use aither_core::llm::{Failure, Message};
    use std::sync::Arc;

    use super::*;
    use crate::context::{PrefixChain, PrefixRewrites, PromptPrefix};

    fn call() -> ToolCallId {
        ToolCallId::new("call-1")
    }

    fn errors() -> Vec<AgentError> {
        vec![
            AgentError::Llm {
                message: "overloaded".into(),
                failure: Failure::transient().with_retry_after(Some(Duration::from_secs(5))),
            },
            AgentError::ToolExecution {
                name: "terminal".into(),
                error: "exit 1".into(),
            },
            AgentError::MaxIterations { limit: 8 },
            AgentError::HookRejected {
                hook: "before_tool".into(),
                reason: "no".into(),
            },
            AgentError::ToolNotFound { name: "x".into() },
            AgentError::Config("bad".into()),
            AgentError::Approval(Arc::new(ApprovalError::NoApprover {
                kind: ApprovalKind::new("run"),
            })),
            AgentError::Approval(Arc::new(ApprovalError::Failed {
                kind: ApprovalKind::new("run"),
                failure: ApproverFailure::new(std::io::Error::other("closed")),
            })),
            AgentError::Approval(Arc::new(ApprovalError::Cancelled)),
            AgentError::Cancelled,
            AgentError::Paused(RunPause::ContextExhausted),
        ]
    }

    fn outcomes() -> Vec<RunOutcome> {
        let mut all = vec![
            RunOutcome::Completed {
                final_text: "done".into(),
            },
            RunOutcome::Paused {
                pause: RunPause::ContextExhausted,
            },
            RunOutcome::Cancelled,
            RunOutcome::IterationLimit,
        ];
        all.extend(
            errors()
                .into_iter()
                .map(|error| RunOutcome::Failed { error }),
        );
        all
    }

    #[allow(clippy::too_many_lines)]
    fn events() -> Vec<AgentEvent> {
        let run = || RunId::new("run-1");
        let request = || RequestId::new(3);
        let child = || AgentPath::main().child("reviewer");
        let mut all = vec![
            AgentEvent::RunStarted {
                run: run(),
                agent: child(),
            },
            AgentEvent::TurnStarted {
                run: run(),
                turn: 1,
            },
            AgentEvent::TurnEnded {
                run: run(),
                turn: 1,
                tool_calls: 2,
            },
            AgentEvent::RequestStarted {
                request: request(),
                prefix: PromptPrefix {
                    chain: PrefixChain::of(&[], &[Message::user("hello")]),
                    rewrites: PrefixRewrites {
                        compacted: true,
                        reassembled: false,
                    },
                    first_request: true,
                },
            },
            AgentEvent::RequestEnded {
                request: request(),
                usage: Some(Usage {
                    prompt_tokens: Some(10),
                    ..Usage::default()
                }),
                outcome: RequestOutcome::Completed,
            },
            AgentEvent::BlockDelta {
                request: request(),
                block: BlockId::new(0),
                kind: OutputKind::Reasoning,
                text: "hm".into(),
            },
            AgentEvent::BlockEnded {
                request: request(),
                block: BlockId::new(0),
                kind: OutputKind::Text,
            },
            AgentEvent::ToolCallDelta {
                call: call(),
                name: "terminal".into(),
                arguments_fragment: "{\"cmd".into(),
            },
            AgentEvent::ToolCallStarted {
                call: call(),
                name: "terminal".into(),
                arguments: serde_json::json!({"cmd": "ls"}),
                origin: ToolOrigin::Hosted,
            },
            AgentEvent::ToolProgress {
                call: call(),
                progress: Progress::new(1.0),
            },
            AgentEvent::ToolCallEnded {
                call: call(),
                result: ToolResult::text("ok"),
            },
            AgentEvent::Paused {
                pause: PauseId::new(1),
                call: Some(call()),
                kind: ApprovalKind::new("run"),
                subject: "rm -rf".into(),
            },
            AgentEvent::Resumed {
                pause: PauseId::new(1),
            },
            AgentEvent::CompactionStarted {
                trigger: CompactionTrigger::ColdCache,
            },
            AgentEvent::CompactionRecovery {
                error: AgentError::Cancelled,
                alerts_removed: 1,
                outputs_spilled: 2,
            },
            AgentEvent::ContextInjected {
                source: SourceId::new("memory"),
                kind: InjectionKind::LiveChange,
            },
            AgentEvent::SubagentStarted {
                child: child(),
                name: "reviewer".into(),
                prompt: "look".into(),
            },
            AgentEvent::Checkpoint {
                run: run(),
                reason: CheckpointReason::Stop,
                turn: 2,
                phase: ContextWindowPhase::Stable,
                message_count: 4,
            },
        ];
        for decision in [
            ToolVerdict::Allowed,
            ToolVerdict::Denied {
                reason: "nope".into(),
            },
            ToolVerdict::Rewritten,
            ToolVerdict::AwaitingApproval,
        ] {
            all.push(AgentEvent::ToolCallDecided {
                call: call(),
                decision,
            });
        }
        for outcome in outcomes() {
            all.push(AgentEvent::RunEnded {
                run: run(),
                outcome: outcome.clone(),
            });
            all.push(AgentEvent::SubagentEnded {
                child: child(),
                outcome,
            });
        }
        for error in errors() {
            all.push(AgentEvent::RequestEnded {
                request: request(),
                usage: None,
                outcome: RequestOutcome::Failed {
                    error: error.clone(),
                },
            });
            all.push(AgentEvent::CompactionEnded {
                outcome: CompactionOutcome::Failed(error),
            });
        }
        all.push(AgentEvent::RequestEnded {
            request: request(),
            usage: None,
            outcome: RequestOutcome::Retrying {
                error: AgentError::llm_local("x"),
                retry: 1,
                max_retries: 3,
                delay: Duration::from_millis(1500),
            },
        });
        all.push(AgentEvent::CompactionEnded {
            outcome: CompactionOutcome::Applied(crate::CompactResult {
                messages_compacted: 5,
                messages_remaining: 2,
                summary: "s".into(),
            }),
        });
        all.push(AgentEvent::CompactionEnded {
            outcome: CompactionOutcome::NothingToCompact,
        });
        all.push(AgentEvent::CompactionEnded {
            outcome: CompactionOutcome::Discarded,
        });
        all
    }

    #[test]
    fn every_event_survives_a_round_trip() {
        let at = Timestamp::from_second(1_700_000_000).expect("a valid timestamp");
        for event in events() {
            let timed = TimedEvent {
                at,
                agent: AgentPath::main().child("reviewer"),
                event,
            };
            let text = serde_json::to_string(&timed).expect("serializes");
            let back: TimedEvent = serde_json::from_str(&text).expect("deserializes");
            assert_eq!(back.at, timed.at, "{text}");
            assert_eq!(back.agent, timed.agent, "{text}");
            assert_eq!(
                serde_json::to_string(&back).expect("serializes again"),
                text,
                "a decoded event encodes to the same text"
            );
            assert_eq!(format!("{:?}", back.event), format!("{:?}", timed.event));
        }
    }
}