loopctl 0.1.0

A trait-based framework for building agent loops with pluggable LLM clients, tools, and memory
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
//! Loop runtime — capability traits and runtime infrastructure for agent loops.
//!
//! Two categories of types govern how agent loops
//! interact with their infrastructure:
//!
//! # Capability Traits
//!
//! Capability traits are composable interfaces that represent distinct
//! infrastructure concerns. Each trait describes a single ability —
//! observing lifecycle events, detecting loops, falling back to alternate
//! models, etc. The [`LoopRuntime`] struct implements all capability traits,
//! providing a concrete, all-in-one infrastructure bundle.
//!
//! | Trait                    | Purpose                                          |
//! |--------------------------|--------------------------------------------------|
//! | [`Observable`]           | Emit lifecycle events to registered observers    |
//! | [`Detectable`]           | Detect repetitive loops and convergence          |
//! | [`FallbackCapable`]      | Circuit-breaker fallback to alternate models     |
//! | [`Compactable`]          | Automatic context compaction when tokens exceed  |
//! | [`StreamCapable`]        | Resilient streaming with retries and timeouts    |
//! | [`Hookable`]             | Bidirectional hooks that can block actions       |
//! | [`PipelineAware`]        | Dispatch tools through a middleware pipeline     |
//! | [`HealthTrackable`]      | Per-tool health tracking with circuit breakers   |
//!
//! # `LoopRuntime`
//!
//! [`LoopRuntime`] is the framework's default infrastructure bundle.
//! It bundles all managers, observers, hooks, and middleware into a single
//! struct that can be passed to any agent loop implementation:
//!
//! ```text
//! LoopRuntime
//!   ├── ObserverHost           → Observable
//!   ├── DetectionManager       → Detectable
//!   ├── FallbackManager        → FallbackCapable
//!   ├── Option<ContextManager> → Compactable
//!   ├── Option<StreamHandler>  → StreamCapable
//!   ├── Option<HookExecutor>   → Hookable
//!   ├── Option<ToolPipeline>   → PipelineAware
//!   └── Option<ToolHealthReg>  → HealthTrackable
//! ```
//!
//! # Design Philosophy
//!
//! The trait hierarchy separates **what the runtime can do** (capability
//! traits) from **how it's composed** (the `LoopRuntime` struct). This
//! allows:
//!
//! - **Generic programming** — agent loops can be written against
//!   `impl Observable + Detectable` rather than a concrete type.
//! - **Testing** — swap `LoopRuntime` for a stub that implements only the
//!   traits under test.
//! ```rust,ignore
//! let runtime = LoopRuntime::builder()
//!     .with_fallback(FallbackManager::for_model("llm-70b"))
//!     .with_detection(DetectionManager::default())
//!     .with_observer(Arc::new(logging_observer))
//!     .build();
//!
//! let agent = BareLoop::new_with_managers(client, tools, runtime, config);
//! ```
//!
//! Every capability is optional. A runtime with no `.with_*()` calls still
//! works — it just has no observers, no hooks, no pipeline, etc.  This means
//! you only pay for (and configure) the infrastructure you actually use.

use std::sync::Arc;

use crate::compact::ContextManager;
use crate::detection::DetectionManager;
use crate::fallback::FallbackManager;
#[cfg(feature = "hooks")]
use crate::hooks::HookExecutor;
use crate::middleware::ToolPipeline;
use crate::observer::{LoopObserver, ObserverHost};
use crate::stream::handler::StreamHandler;
#[cfg(feature = "tool_health")]
use crate::tool::health::ToolHealthRegistry;

pub use crate::capabilities::*;

// ==================================================
// LoopRuntime
// ==================================================

/// The framework's default infrastructure bundle for agent loops.
///
/// `LoopRuntime` bundles all the cross-cutting infrastructure an agent
/// loop needs: observers, detection, fallback, hooks, middleware pipeline,
/// and health tracking. It implements all capability traits so that agent
/// loops can be written against trait bounds rather than a concrete type.
///
/// # Construction
///
/// Use [`LoopRuntime::builder()`] to compose only the capabilities you need,
/// or [`LoopRuntime::new()`] for a runtime with default managers and no
/// optional components:
///
/// ```
/// # use loopctl::runtime::LoopRuntime;
/// # use loopctl::runtime::FallbackCapable;
/// use loopctl::fallback::FallbackManager;
///
/// // Builder — explicit capability composition:
/// let runtime = LoopRuntime::builder()
///     .with_fallback(FallbackManager::for_model("llm-70b"))
///     .build();
/// assert_eq!(runtime.fallback().active_model().as_deref(), Some("llm-70b"));
/// ```
///
/// # Capability Traits
///
/// `LoopRuntime` implements all capability traits defined in this module:
///
/// - [`Observable`] — via the internal [`ObserverHost`]
/// - [`Detectable`] — via the internal [`DetectionManager`]
/// - [`FallbackCapable`] — via the internal [`FallbackManager`]
/// - [`Compactable`] — via an optional [`ContextManager`]
/// - [`StreamCapable`] — via an optional [`StreamHandler`]
/// - [`Hookable`] — via an optional [`HookExecutor`] **
/// - [`PipelineAware`] — via an optional [`ToolPipeline`]
/// - [`HealthTrackable`] — via an optional [`ToolHealthRegistry`] *(requires `tool_health` feature)*
///
/// # Reset
///
/// Call [`reset_all`](LoopRuntime::reset_all) at the start of a new
/// session to reinitialise every manager and observer to its default state.
pub struct LoopRuntime {
    /// Circuit breaker for API model fallback.
    pub fallback: FallbackManager,
    /// Loop and convergence detection orchestrator.
    pub detection: DetectionManager,
    /// Observer host for lifecycle event fan-out.
    observer_host: ObserverHost,
    /// Optional middleware pipeline wrapping tool dispatch.
    tool_pipeline: Option<ToolPipeline>,
    /// Optional context manager for automatic compaction.
    context_manager: Option<Arc<ContextManager>>,
    /// Optional stream handler for resilient streaming with retries.
    stream_handler: Option<StreamHandler>,
    /// Optional hook executor for bidirectional lifecycle interception.
    #[cfg(feature = "hooks")]
    hook_executor: Option<Arc<HookExecutor>>,
    /// Optional per-tool health tracker with circuit breakers. *Requires `tool_health` feature.*
    #[cfg(feature = "tool_health")]
    health_registry: Option<Arc<ToolHealthRegistry>>,
}

/// Builder for [`LoopRuntime`] — compose only the capabilities you need.
///
/// Created via [`LoopRuntime::builder()`]. Each `.with_*()` method adds a
/// capability and returns `self` for chaining. Call `.build()` to finalize.
///
/// # Example
///
/// ```
/// # use loopctl::runtime::LoopRuntime;
/// # use loopctl::runtime::{Detectable, FallbackCapable};
/// let runtime = LoopRuntime::builder()
///     .with_fallback(Default::default())
///     .with_detection(Default::default())
///     .build();
///
/// // Only the capabilities you configured are present:
/// assert!(runtime.fallback().active_model().is_none());
/// ```
///
/// All capabilities are optional — a bare `.build()` with no `.with_*()`
/// calls produces an empty runtime that still works but has no observers,
/// no hooks, no pipeline, etc.
#[must_use]
pub struct LoopRuntimeBuilder {
    inner: LoopRuntime,
}

impl LoopRuntimeBuilder {
    /// Replace the fallback manager.
    pub fn with_fallback(mut self, fallback: FallbackManager) -> Self {
        self.inner.fallback = fallback;
        self
    }

    /// Replace the detection manager.
    pub fn with_detection(mut self, detection: DetectionManager) -> Self {
        self.inner.detection = detection;
        self
    }

    /// Register an observer.
    pub fn with_observer(mut self, observer: Arc<dyn LoopObserver>) -> Self {
        self.inner.observer_host.register(observer);
        self
    }

    /// Set the middleware pipeline for tool dispatch.
    pub fn with_pipeline(mut self, pipeline: ToolPipeline) -> Self {
        self.inner.tool_pipeline = Some(pipeline);
        self
    }

    /// Set the context manager for automatic compaction.
    pub fn with_context_manager(mut self, manager: Arc<ContextManager>) -> Self {
        self.inner.context_manager = Some(manager);
        self
    }

    /// Set the stream handler for resilient streaming.
    pub fn with_stream_handler(mut self, handler: StreamHandler) -> Self {
        self.inner.stream_handler = Some(handler);
        self
    }

    /// Set the hook executor for bidirectional lifecycle interception.
    #[cfg(feature = "hooks")]
    pub fn with_hook_executor(mut self, executor: Arc<HookExecutor>) -> Self {
        self.inner.hook_executor = Some(executor);
        self
    }

    /// Set the tool health registry for per-tool health tracking.
    #[cfg(feature = "tool_health")]
    pub fn with_health_registry(mut self, registry: Arc<ToolHealthRegistry>) -> Self {
        self.inner.health_registry = Some(registry);
        self
    }

    /// Finalize the builder and return the configured [`LoopRuntime`].
    pub fn build(self) -> LoopRuntime {
        self.inner
    }
}

impl LoopRuntime {
    /// Create a new runtime with default managers and no optional components.
    ///
    /// # Example
    ///
    /// ```
    /// # use loopctl::runtime::LoopRuntime;
    /// # use loopctl::runtime::FallbackCapable;
    /// let runtime = LoopRuntime::new();
    /// assert!(runtime.fallback().active_model().is_none());
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            fallback: FallbackManager::default(),
            detection: DetectionManager::default(),
            observer_host: ObserverHost::new(),
            tool_pipeline: None,
            context_manager: None,
            stream_handler: None,
            #[cfg(feature = "hooks")]
            hook_executor: None,
            #[cfg(feature = "tool_health")]
            health_registry: None,
        }
    }

    // ==================================================
    // Builder methods — compose only the capabilities you need
    // ==================================================

    /// Create a new runtime builder.
    ///
    /// Starts with no capabilities enabled. Use the `.with_*()` methods
    /// to add only the infrastructure you need, then pass the result to
    /// [`BareLoop::new_with_managers`](crate::engine::BareLoop::new_with_managers).
    ///
    /// # Example
    ///
    /// ```
    /// # use loopctl::runtime::LoopRuntime;
    /// # use loopctl::runtime::{Detectable, FallbackCapable};
    /// let runtime = LoopRuntime::builder()
    ///     .with_fallback(Default::default())
    ///     .with_detection(Default::default())
    ///     .build();
    /// ```
    ///
    /// This is equivalent to [`LoopRuntime::new`] — both start from an
    /// empty runtime. Use `builder()` when you want the intent to be
    /// explicit that you're composing capabilities.
    pub fn builder() -> LoopRuntimeBuilder {
        LoopRuntimeBuilder { inner: Self::new() }
    }

    /// Replace the fallback manager with a custom instance.
    ///
    /// # Example
    ///
    /// ```
    /// # use loopctl::runtime::LoopRuntime;
    /// # use loopctl::runtime::FallbackCapable;
    /// # use loopctl::fallback::FallbackManager;
    /// let runtime = LoopRuntime::new()
    ///     .with_fallback(FallbackManager::for_model("llm-70b"));
    /// assert_eq!(runtime.fallback().active_model().as_deref(), Some("llm-70b"));
    /// ```
    #[must_use]
    pub fn with_fallback(mut self, fallback: FallbackManager) -> Self {
        self.fallback = fallback;
        self
    }

    /// Replace the detection manager with a custom instance.
    ///
    /// # Example
    ///
    /// ```
    /// # use loopctl::runtime::LoopRuntime;
    /// # use loopctl::runtime::Detectable;
    /// # use loopctl::detection::{DetectionManager, DetectionConfig};
    /// let config = DetectionConfig {
    ///     loop_threshold: 5,
    ///     ..Default::default()
    /// };
    /// let runtime = LoopRuntime::new()
    ///     .with_detection(DetectionManager::new_with_config(config).unwrap());
    /// assert_eq!(runtime.detection().config().loop_threshold, 5);
    /// ```
    #[must_use]
    pub fn with_detection(mut self, detection: DetectionManager) -> Self {
        self.detection = detection;
        self
    }

    // ==================================================
    // Setters for optional components
    // ==================================================

    /// Register an observer with the observer host.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use loopctl::observer::LoopObserver;
    /// use std::sync::Arc;
    ///
    /// let mut runtime = LoopRuntime::new();
    /// runtime.register_observer(Arc::new(MyObserver));
    /// ```
    pub fn register_observer(&mut self, observer: Arc<dyn LoopObserver>) {
        self.observer_host.register(observer);
    }

    /// Register an observer and return `self` for chaining.
    ///
    /// Builder-style alias for [`register_observer`](Self::register_observer).
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let runtime = LoopRuntime::builder()
    ///     .with_observer(Arc::new(logging_observer))
    ///     .with_observer(Arc::new(metrics_observer))
    ///     .build();
    /// ```
    #[must_use]
    pub fn with_observer(mut self, observer: Arc<dyn LoopObserver>) -> Self {
        self.observer_host.register(observer);
        self
    }

    /// Access the observer host directly.
    pub fn observers(&self) -> &ObserverHost {
        &self.observer_host
    }

    /// Set the middleware pipeline for tool dispatch (builder-style).
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let runtime = LoopRuntime::builder()
    ///     .with_pipeline(builder.build()?)
    ///     .build();
    /// ```
    #[must_use]
    pub fn with_pipeline(mut self, pipeline: ToolPipeline) -> Self {
        self.tool_pipeline = Some(pipeline);
        self
    }

    /// Set the middleware pipeline for tool dispatch (`&mut self` variant).
    pub fn set_pipeline(&mut self, pipeline: ToolPipeline) {
        self.tool_pipeline = Some(pipeline);
    }

    /// Set the context manager for automatic compaction (builder-style).
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let runtime = LoopRuntime::builder()
    ///     .with_context_manager(Arc::new(manager))
    ///     .build();
    /// ```
    #[must_use]
    pub fn with_context_manager(mut self, manager: Arc<ContextManager>) -> Self {
        self.context_manager = Some(manager);
        self
    }

    /// Set the context manager for automatic compaction (`&mut self` variant).
    pub fn set_context_manager(&mut self, manager: Arc<ContextManager>) {
        self.context_manager = Some(manager);
    }

    /// Set the stream handler for resilient streaming (builder-style).
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let runtime = LoopRuntime::builder()
    ///     .with_stream_handler(handler)
    ///     .build();
    /// ```
    #[must_use]
    pub fn with_stream_handler(mut self, handler: StreamHandler) -> Self {
        self.stream_handler = Some(handler);
        self
    }

    /// Set the stream handler for resilient streaming (`&mut self` variant).
    pub fn set_stream_handler(&mut self, handler: StreamHandler) {
        self.stream_handler = Some(handler);
    }

    /// Set the hook executor for bidirectional lifecycle interception (builder-style).
    ///
    /// *Requires `hooks` feature.*
    #[must_use]
    #[cfg(feature = "hooks")]
    pub fn with_hook_executor(mut self, executor: Arc<HookExecutor>) -> Self {
        self.hook_executor = Some(executor);
        self
    }

    /// Set the hook executor (`&mut self` variant).
    ///
    /// *Requires `hooks` feature.*
    #[cfg(feature = "hooks")]
    pub fn set_hook_executor(&mut self, executor: Arc<HookExecutor>) {
        self.hook_executor = Some(executor);
    }

    /// Set the tool health registry for per-tool health tracking (builder-style).
    ///
    /// When set, records success/failure counts and latency for every
    /// tool dispatch. Tools that exceed the failure threshold have their
    /// circuit breaker opened, blocking subsequent calls until recovery.
    ///
    /// *Requires `tool_health` feature.*
    #[must_use]
    #[cfg(feature = "tool_health")]
    pub fn with_health_registry(mut self, registry: Arc<ToolHealthRegistry>) -> Self {
        self.health_registry = Some(registry);
        self
    }

    /// Set the tool health registry (`&mut self` variant).
    ///
    /// *Requires `tool_health` feature.*
    #[cfg(feature = "tool_health")]
    pub fn set_health_registry(&mut self, registry: Arc<ToolHealthRegistry>) {
        self.health_registry = Some(registry);
    }

    // ==================================================
    // Lifecycle
    // ==================================================

    /// Reset all managers and observers to their initial state.
    ///
    /// Delegates to each manager's `reset()` method and calls
    /// [`ObserverHost::reset_all`]. Typically called at the start of
    /// a new agent task or session.
    ///
    /// # Example
    ///
    /// ```
    /// # use loopctl::runtime::LoopRuntime;
    /// let runtime = LoopRuntime::new();
    /// // ... after a session ...
    /// runtime.reset_all();
    /// // All managers are back to their initial state
    /// ```
    pub fn reset_all(&self) {
        self.fallback.reset();
        self.detection.reset();
        self.observer_host.reset_all();
    }

    // ==================================================
    // Detection interpretation
    // ==================================================

    /// Interpret a [`DetectedPattern`](crate::detection::DetectedPattern) and decide whether to abort.
    ///
    /// Generic framework logic: checks loop-detection thresholds,
    /// maps convergence actions, and notifies observers. Returns
    /// `None` to continue, or `Some(Err)` to abort the session.
    ///
    /// Called by the agent loop after each response is recorded with the
    /// [`DetectionManager`].
    pub fn handle_detected_pattern(
        &self,
        pattern: &crate::detection::DetectedPattern,
        turn: usize,
    ) -> Option<Result<crate::engine::loop_core::SessionResult, crate::error::LoopError>> {
        use crate::detection::{ConvergenceAction, DetectedPattern};
        use crate::error::LoopError;
        use crate::observer::{ConvergenceDetectedContext, LoopDetectedContext};

        match pattern {
            DetectedPattern::NoPattern => None,

            DetectedPattern::LoopDetected {
                repetitions,
                pattern_description,
            } => {
                tracing::warn!(
                    repetitions,
                    pattern = %pattern_description,
                    turn,
                    "loop detected"
                );

                self.observer_host.on_loop_detected(&LoopDetectedContext {
                    pattern: pattern_description.clone(),
                    repetitions: *repetitions,
                });

                if *repetitions >= self.detection.config().stop_threshold {
                    tracing::error!(
                        repetitions,
                        pattern = %pattern_description,
                        turn,
                        "stopping agent: loop threshold exceeded"
                    );
                    Some(Err(LoopError::LoopDetected {
                        message: format!("{pattern_description} repeated {repetitions} times"),
                    }))
                } else {
                    None
                }
            }

            DetectedPattern::ConvergenceDetected {
                similarity,
                consecutive_count,
            } => {
                tracing::warn!(similarity, consecutive_count, turn, "convergence detected");
                let action = self.detection.config().on_converge;
                let action_str = match action {
                    ConvergenceAction::Stop => "stop",
                    ConvergenceAction::Warn => "warn",
                    ConvergenceAction::Compact => "compact",
                    ConvergenceAction::AskUser => "ask_user",
                    ConvergenceAction::SwitchPhase => "switch_phase",
                };

                self.observer_host
                    .on_convergence_detected(&ConvergenceDetectedContext {
                        action: action_str.to_string(),
                    });

                match action {
                    ConvergenceAction::Stop => Some(Err(LoopError::LoopDetected {
                        message: "agent stopped: convergence detected".into(),
                    })),
                    ConvergenceAction::AskUser => Some(Err(LoopError::LoopDetected {
                        message: "agent stopped: convergence detected, user input needed".into(),
                    })),
                    ConvergenceAction::Warn
                    | ConvergenceAction::Compact
                    | ConvergenceAction::SwitchPhase => None,
                }
            }
        }
    }

    // ==================================================
    // Session lifecycle notifications
    // ==================================================

    /// Notify observers and hooks that a session has started.
    ///
    /// Fan-out to the [`ObserverHost`] and (if configured) the hook
    /// executor. Generic for any loop implementation.
    pub fn notify_session_start(
        &self,
        session_id: uuid::Uuid,
        #[allow(unused_variables)] model: &str,
    ) {
        use crate::observer::SessionStartContext;

        self.observer_host
            .on_session_start(&SessionStartContext { session_id });

        #[cfg(feature = "hooks")]
        if let Some(executor) = self.hook_executor() {
            use crate::hooks::context::SessionStartContext as HookSessionStartContext;

            let ctx = HookSessionStartContext {
                session_id,
                model: model.to_string(),
                working_directory: std::env::current_dir()
                    .map(|p| p.to_string_lossy().into_owned())
                    .unwrap_or_default(),
            };
            executor.notify_session_start(&ctx);
        }
    }

    /// Notify observers and hooks that a session has ended.
    ///
    /// Takes the final [`SessionResult`](crate::engine::loop_core::SessionResult) and duration. Fan-out to
    /// the [`ObserverHost`] and (if configured) the hook executor.
    pub fn notify_session_end(
        &self,
        result: &crate::engine::loop_core::SessionResult,
        duration: std::time::Duration,
    ) {
        use crate::observer::SessionEndContext;

        self.observer_host.on_session_end(&SessionEndContext {
            success: result.success,
            error: result.error.clone(),
            total_turns: result.total_turns,
            duration_ms: u64::try_from(duration.as_millis()).unwrap_or(u64::MAX),
        });

        #[cfg(feature = "hooks")]
        if let Some(executor) = self.hook_executor() {
            use crate::hooks::context::{
                SessionEndContext as HookSessionEndContext, SessionEndReason,
            };

            let reason = if result.success {
                SessionEndReason::Complete
            } else {
                SessionEndReason::Error
            };
            let ctx = HookSessionEndContext {
                session_id: result.session_id,
                reason,
                total_turns: result.total_turns,
                total_tokens: result.input_tokens.saturating_add(result.output_tokens),
                duration_secs: duration.as_secs(),
            };
            executor.notify_session_end(&ctx);
        }
    }
}

impl Default for LoopRuntime {
    /// Produce a [`LoopRuntime`] with default managers and no optional components.
    ///
    /// Equivalent to [`LoopRuntime::new`].
    fn default() -> Self {
        Self::new()
    }
}

// ==================================================
// Capability trait implementations
// ==================================================

impl crate::capabilities::Observable for LoopRuntime {
    fn observers(&self) -> &ObserverHost {
        &self.observer_host
    }
}

impl crate::capabilities::Detectable for LoopRuntime {
    fn detection(&self) -> &DetectionManager {
        &self.detection
    }
}

impl crate::capabilities::FallbackCapable for LoopRuntime {
    fn fallback(&self) -> &FallbackManager {
        &self.fallback
    }
}

impl crate::capabilities::Compactable for LoopRuntime {
    fn context_manager(&self) -> Option<&Arc<ContextManager>> {
        self.context_manager.as_ref()
    }
}

impl crate::capabilities::StreamCapable for LoopRuntime {
    fn stream_handler(&self) -> Option<&StreamHandler> {
        self.stream_handler.as_ref()
    }
}

#[cfg(feature = "hooks")]
impl crate::capabilities::Hookable for LoopRuntime {
    fn hook_executor(&self) -> Option<&HookExecutor> {
        self.hook_executor.as_deref()
    }
}

impl crate::capabilities::PipelineAware for LoopRuntime {
    fn pipeline(&self) -> Option<&ToolPipeline> {
        self.tool_pipeline.as_ref()
    }
}

#[cfg(feature = "tool_health")]
impl crate::capabilities::HealthTrackable for LoopRuntime {
    fn health_registry(&self) -> Option<&ToolHealthRegistry> {
        self.health_registry.as_deref()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::detection::{DetectedPattern, DetectionManager};

    #[test]
    fn test_runtime_default() {
        let runtime = LoopRuntime::default();
        assert!(runtime.fallback().active_model().is_none());
    }

    #[test]
    fn test_runtime_with_custom_fallback() {
        let fallback = FallbackManager::for_model("my-model");
        let runtime = LoopRuntime::new().with_fallback(fallback);
        assert_eq!(
            runtime.fallback().active_model().as_deref(),
            Some("my-model")
        );
    }

    #[test]
    fn test_reset_all() {
        let runtime = LoopRuntime::new();
        runtime.reset_all();
    }

    #[test]
    fn test_runtime_contains_detection_manager() {
        let runtime = LoopRuntime::new();
        assert_eq!(runtime.detection().config().loop_threshold, 3);
        assert_eq!(runtime.detection().config().stop_threshold, 10);
    }

    #[test]
    fn test_runtime_with_custom_detection() {
        use crate::detection::DetectionConfig;

        let config = DetectionConfig {
            loop_threshold: 7,
            stop_threshold: 20,
            ..Default::default()
        };
        let detection = DetectionManager::new_with_config(config).unwrap();
        let runtime = LoopRuntime::new().with_detection(detection);
        assert_eq!(runtime.detection().config().loop_threshold, 7);
        assert_eq!(runtime.detection().config().stop_threshold, 20);
        assert!(runtime.fallback().active_model().is_none());
    }

    #[test]
    fn test_reset_all_clears_detection() {
        let runtime = LoopRuntime::new();
        let _ = runtime.detection().record_tool_call("Read", 12345);
        runtime.reset_all();
        let pattern = runtime.detection().record_tool_call("Read", 12345);
        assert!(matches!(pattern, DetectedPattern::NoPattern));
    }

    #[test]
    fn test_capability_traits_are_object_safe() {
        // Verify that trait objects can be created
        fn _assert_observable(_: &dyn Observable) {}
        fn _assert_detectable(_: &dyn Detectable) {}
        fn _assert_fallback(_: &dyn FallbackCapable) {}
        fn _assert_pipeline(_: &dyn PipelineAware) {}
        fn _assert_compactable(_: &dyn Compactable) {}
        fn _assert_stream_capable(_: &dyn StreamCapable) {}

        let runtime = LoopRuntime::new();
        _assert_observable(&runtime);
        _assert_detectable(&runtime);
        _assert_fallback(&runtime);
        _assert_pipeline(&runtime);
        _assert_compactable(&runtime);
        _assert_stream_capable(&runtime);
    }

    #[test]
    fn test_pipeline_defaults_to_none() {
        let runtime = LoopRuntime::new();
        assert!(runtime.pipeline().is_none());
    }

    #[test]
    fn test_observers_accessible_via_trait() {
        let runtime = LoopRuntime::new();
        let _: &ObserverHost = runtime.observers();
    }

    #[test]
    fn test_context_manager_defaults_to_none() {
        let runtime = LoopRuntime::new();
        assert!(runtime.context_manager().is_none());
    }

    #[test]
    fn test_stream_handler_defaults_to_none() {
        let runtime = LoopRuntime::new();
        assert!(runtime.stream_handler().is_none());
    }

    #[test]
    fn test_set_pipeline_returns_some() {
        use crate::middleware::ToolPipeline;
        use crate::tool::ToolRegistry;
        use std::sync::Arc;

        let registry = Arc::new(ToolRegistry::new());
        let pipeline = ToolPipeline::new(registry);
        let mut runtime = LoopRuntime::new();
        assert!(runtime.pipeline().is_none());
        runtime.set_pipeline(pipeline);
        assert!(runtime.pipeline().is_some());
    }

    #[test]
    fn test_set_context_manager_returns_some() {
        use crate::compact::{ContextManager, TruncatingCompactor};

        let compactor = TruncatingCompactor::new();
        let manager = ContextManager::new(Arc::new(compactor));
        let mut runtime = LoopRuntime::new();
        assert!(runtime.context_manager().is_none());
        runtime.set_context_manager(Arc::new(manager));
        assert!(runtime.context_manager().is_some());
    }

    #[test]
    fn test_set_stream_handler_returns_some() {
        use crate::stream::handler::StreamHandler;

        let handler = StreamHandler::new();
        let mut runtime = LoopRuntime::new();
        assert!(runtime.stream_handler().is_none());
        runtime.set_stream_handler(handler);
        assert!(runtime.stream_handler().is_some());
    }

    #[test]
    fn test_register_observer_increments_count() {
        use crate::observer::LoopObserver;
        use std::sync::Arc;

        struct NopObserver;
        impl LoopObserver for NopObserver {
            fn name(&self) -> &'static str {
                "NopObserver"
            }
        }

        let mut runtime = LoopRuntime::new();
        assert!(runtime.observers().is_empty());
        runtime.register_observer(Arc::new(NopObserver));
        assert_eq!(runtime.observers().len(), 1);
        runtime.register_observer(Arc::new(NopObserver));
        assert_eq!(runtime.observers().len(), 2);
    }

    #[test]
    fn test_reset_all_clears_fallback() {
        let runtime = LoopRuntime::new();
        let _ = runtime.fallback().record_api_failure();
        runtime.reset_all();
        assert!(runtime.fallback().active_model().is_none());
    }

    #[test]
    fn test_reset_all_clears_observers() {
        use crate::observer::LoopObserver;
        use std::sync::Arc;

        struct NopObserver;
        impl LoopObserver for NopObserver {
            fn name(&self) -> &'static str {
                "NopObserver"
            }
        }

        let mut runtime = LoopRuntime::new();
        runtime.register_observer(Arc::new(NopObserver));
        assert_eq!(runtime.observers().len(), 1);
        runtime.reset_all();
        // Observer count persists across reset — reset_all calls
        // reset_all on each observer, it doesn't remove them.
        assert_eq!(runtime.observers().len(), 1);
    }

    #[test]
    fn test_generic_bounds_accept_runtime() {
        fn accepts_observable(_: &impl Observable) {}
        fn accepts_detectable(_: &impl Detectable) {}
        fn accepts_fallback(_: &impl FallbackCapable) {}
        fn accepts_compactable(_: &impl Compactable) {}
        fn accepts_stream_capable(_: &impl StreamCapable) {}
        fn accepts_pipeline(_: &impl PipelineAware) {}
        fn accepts_multi_bound(_: &(impl Observable + Detectable + FallbackCapable)) {}

        let runtime = LoopRuntime::new();
        accepts_observable(&runtime);
        accepts_detectable(&runtime);
        accepts_fallback(&runtime);
        accepts_compactable(&runtime);
        accepts_stream_capable(&runtime);
        accepts_pipeline(&runtime);
        accepts_multi_bound(&runtime);
    }
}