loopctl 0.2.1

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
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
//! Reflection and recovery for failed agent turns.
//!
//! When a tool call fails or a turn produces an unexpected result, the
//! framework needs to decide what to do. A pluggable
//! two-layer system:
//!
//! 1. **[`Reflector`]** — Analyses the failure and produces a
//!    [`FailureAnalysis`] describing what went wrong and whether it's
//!    recoverable.
//!
//! 2. **[`RecoveryStrategy`]** — Takes the analysis and decides on a
//!    [`RecoveryAction`] (retry, skip, ask user, or fail).
//!
//! Both layers are trait-based so agents can plug in their own strategies.
//! The framework provides reference implementations:
//!
//! - [`NoopReflector`] — marks everything as non-recoverable (safe default).
//! - [`ExponentialBackoffRecovery`] — retries with exponential backoff up to
//!   a configurable limit.
//!
//! # Quick Start
//!
//! ```rust
//! use loopctl::reflection::{
//!     NoopReflector, ExponentialBackoffRecovery, ReflectionContext,
//! };
//! use std::sync::Arc;
//!
//! let reflector = NoopReflector;
//! let strategy = ExponentialBackoffRecovery::new(3);
//!
//! // Usage (in BareLoop):
//! // let analysis = reflector.analyze(error, tool_name, input, None, &context).await?;
//! // let action = strategy.decide(&analysis, attempt, max_attempts).await;
//! ```

pub mod backoff;
pub use backoff::ExponentialBackoffRecovery;

pub mod llm;
pub use llm::LlmReflector;

use serde::{Deserialize, Serialize};
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;

/// How severe a failure is.
///
/// Ordered from least to most severe. The [`RecoveryStrategy`] may use
/// severity to decide whether retrying is worthwhile — a `Low` severity
/// issue (e.g., a transient network blip) is more retryable than a
/// `Critical` one (e.g., invalid API key).
///
/// # Ordering and comparison
///
/// Derives [`Ord`], so variants can be compared directly: `Low < Medium <
/// High < Critical`. Strategies commonly write thresholds like
/// `analysis.severity >= FailureSeverity::High` to gate retries.
///
/// # Serialization
///
/// Serializes to and from the lowercase snake-case form of the variant
/// name (`"low"`, `"medium"`, `"high"`, `"critical"`) via
/// `#[serde(rename_all = "snake_case")]`. The same four strings are the
/// `enum` values in the [`FailureAnalysis`] JSON Schema, so model output
/// round-trips through deserialization without renaming.
///
/// # Example
///
/// ```rust
/// use loopctl::reflection::FailureSeverity;
///
/// assert!(FailureSeverity::Low < FailureSeverity::Critical);
/// ```
///
/// [`FailureAnalysis`]: crate::reflection::FailureAnalysis
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
#[serde(rename_all = "snake_case")]
pub enum FailureSeverity {
    /// Minor issue — a retry will likely fix it.
    ///
    /// Typical causes: a transient network blip, a momentary rate-limit,
    /// or a tool that succeeded on a second attempt without any input
    /// changes. Strategies usually retry immediately on `Low` severity.
    Low,

    /// Moderate issue — may need a correction before retrying.
    ///
    /// The failure looks recoverable, but a bare retry is less likely to
    /// succeed than at `Low`: the input may have a small mistake (a typo
    /// in a path, a slightly wrong argument), or the tool's
    /// preconditions may need a step first. Strategies commonly consult
    /// [`FailureAnalysis::correction`] before retrying at this severity.
    ///
    /// [`FailureAnalysis::correction`]: crate::reflection::FailureAnalysis::correction
    Medium,

    /// Serious issue — retrying without changes is unlikely to help.
    ///
    /// The call is fundamentally off: the wrong tool was chosen, the
    /// argument type is incorrect, or the task itself is misframed. A
    /// retry that doesn't first apply a [`Correction`] is probably
    /// wasted. Strategies may still retry when a correction is supplied
    /// and the attempt budget allows, but should treat `High` as a
    /// signal to slow down rather than retry blindly.
    ///
    /// [`Correction`]: crate::reflection::Correction
    High,

    /// Unrecoverable — the agent should stop or escalate.
    ///
    /// No correction can rescue this turn. Typical causes: an invalid
    /// API key, a permissions failure the agent can't self-resolve, or a
    /// bug in the tool itself. Strategies usually map `Critical` to
    /// [`RecoveryAction::Fail`] (or
    /// [`RecoveryAction::AskUser`] when interactive recovery is an
    /// option) rather than retry.
    ///
    /// [`RecoveryAction::Fail`]: crate::reflection::RecoveryAction::Fail
    /// [`RecoveryAction::AskUser`]: crate::reflection::RecoveryAction::AskUser
    Critical,
}

impl fmt::Display for FailureSeverity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Low => write!(f, "low"),
            Self::Medium => write!(f, "medium"),
            Self::High => write!(f, "high"),
            Self::Critical => write!(f, "critical"),
        }
    }
}

/// Context provided to [`Reflector::analyze()`] describing the retry state.
///
/// Built by the framework before invoking the reflector. Carries what the
/// agent was trying to do and where it is in the retry budget, so a
/// reflector can factor both into its analysis — e.g., be more conservative
/// with suggested corrections on the last permitted attempt.
///
/// # Lifecycle
///
/// The engine constructs a fresh `ReflectionContext` for each failure
/// (see `recover_tool_error` in `engine/bare/dispatch.rs`) and passes it by
/// reference to [`Reflector::analyze`]. It is not stored across calls;
/// reflectors that want to track cross-failure history must keep their own
/// state.
///
/// # Attempt indexing
///
/// `attempt` is 0-indexed: the first try of a tool is `attempt = 0`. A
/// reflector rendering the value for a model prompt should add 1 (the
/// framework's built-in `LlmReflector` does this — "Attempt: 1 of N").
///
/// # Example
///
/// ```rust
/// use loopctl::reflection::ReflectionContext;
///
/// let context = ReflectionContext {
///     task: "Fix the bug in main.rs".to_string(),
///     attempt: 2,
///     max_attempts: 5,
/// };
/// assert_eq!(context.attempt, 2);
/// assert_eq!(context.max_attempts, 5);
/// ```
#[derive(Debug, Clone, Default)]
pub struct ReflectionContext {
    /// What the agent was trying to accomplish when the failure occurred.
    ///
    /// Free-form text — typically the original user message, a summary of
    /// the current step, or an empty string when the engine has no task
    /// description to share. A reflector may include this in its prompt so
    /// the model can reason about whether the failure is relevant to the
    /// stated goal.
    pub task: String,

    /// Current attempt number for this tool call, 0-indexed.
    ///
    /// `0` is the first attempt; the framework increments this on each
    /// retry within the recovery loop. Compare against
    /// [`max_attempts`](Self::max_attempts) to know how much budget
    /// remains. Render as `attempt + 1` when showing the value to a user
    /// or model.
    pub attempt: u32,

    /// Maximum attempts allowed before the framework gives up on this
    /// tool call.
    ///
    /// Set by the engine to its configured recovery ceiling
    /// (`BareLoop::MAX_RECOVERY_ATTEMPTS`). When `attempt >= max_attempts`,
    /// a [`RecoveryStrategy`] should typically return
    /// [`RecoveryAction::Fail`] rather than schedule another retry.
    ///
    /// [`RecoveryAction::Fail`]: crate::reflection::RecoveryAction::Fail
    pub max_attempts: u32,
}

/// Type of correction to apply.
///
/// Categorizes the fix strategy that the reflection system has determined
/// is most appropriate for the observed failure. Each variant maps to a
/// different retry approach.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CorrectionType {
    /// Fix the input to the tool.
    ///
    /// The tool was correct but its input parameters were wrong (e.g., a
    /// typo in a file path). The correction provides a fixed input via
    /// [`Correction::modified_input`].
    InputFix,

    /// Use a different tool.
    ///
    /// The chosen tool was inappropriate for the task. The correction
    /// specifies an alternative via [`Correction::alternative_tool`].
    ToolChange,

    /// Fix a dependency or prerequisite.
    ///
    /// The tool call failed because a prerequisite was not met (e.g.,
    /// a directory doesn't exist). The correction describes what needs
    /// to be done first.
    PrerequisiteFix,

    /// Change the approach entirely.
    ///
    /// The current strategy is fundamentally flawed. The correction
    /// provides high-level guidance for a different approach via
    /// [`Correction::guidance`].
    ApproachChange,

    /// No fix possible, escalate.
    ///
    /// The reflection system cannot determine a correction. The
    /// framework should propagate the error to the user or higher-level
    /// handler.
    Escalate,
}

/// A correction produced by the reflection system.
///
/// When a tool call fails and reflection is enabled (via configuration),
/// the agent analyzes the error and produces a `Correction` that describes
/// how to fix the problem. The framework applies the correction and
/// retries — `recover_tool_error` in `engine/bare/dispatch.rs` clones the
/// `Correction` out of the [`FailureAnalysis`] and feeds it back into the
/// retry loop.
///
/// # Which fields apply when
///
/// The fields are deliberately permissive (four `Option`s + one enum) so a
/// single shape covers every [`CorrectionType`] strategy. The convention is
/// that only the fields named by the variant's doc are meaningful for a
/// given `correction_type`; consumers should consult `correction_type`
/// first and read the relevant fields accordingly rather than treating
/// every `Some` as load-bearing. There is no runtime enforcement of this
/// pairing — a reflector that fills `modified_input` while declaring
/// `correction_type: Escalate` will not be rejected.
///
/// # Serialization
///
/// Implements `Serialize` and `Deserialize` for persistence (e.g., writing
/// analyses to a session log) and so an [`LlmReflector`] can request it
/// back as a nested object inside a [`FailureAnalysis`] via the
/// [`StructuredOutput`] trait.
///
/// [`LlmReflector`]: crate::reflection::LlmReflector
/// [`StructuredOutput`]: crate::structured::StructuredOutput
/// [`FailureAnalysis`]: crate::reflection::FailureAnalysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Correction {
    /// Which fix strategy this correction represents.
    ///
    /// Drives which of the remaining fields the framework will actually
    /// consume on retry. See [`CorrectionType`] for the five strategies
    /// and the field each one pairs with.
    pub correction_type: CorrectionType,

    /// Human-readable explanation of *what* went wrong and *how* this
    /// correction addresses it.
    ///
    /// Always populated — even an [`CorrectionType::Escalate`] correction
    /// carries a description so the framework can surface it to the user
    /// or a higher-level handler. The string is free-form; some
    /// reflectors include a short root-cause summary here in addition to
    /// [`FailureAnalysis::root_cause`].
    ///
    /// [`FailureAnalysis::root_cause`]: crate::reflection::FailureAnalysis::root_cause
    pub description: String,

    /// Corrected JSON input to pass on retry, when the fix is to change
    /// the arguments rather than the tool.
    ///
    /// Set only for [`CorrectionType::InputFix`] corrections, where the
    /// retry should substitute this value for the original
    /// [`MessagePart::ToolCall::input`]. The shape must conform to the
    /// tool's `input_schema`; an `LlmReflector` with the
    /// `schema_validation` feature enabled will reject a non-conforming
    /// suggestion before it reaches the retry.
    ///
    /// [`MessagePart::ToolCall::input`]: crate::message::MessagePart::ToolCall
    pub modified_input: Option<serde_json::Value>,

    /// Name of a different tool to call instead, when the fix is to swap
    /// tools rather than rewrite arguments.
    ///
    /// Set only for [`CorrectionType::ToolChange`] corrections. Must
    /// match a tool name the registry knows; the retry loop will fail
    /// normally if it does not. Leave `None` for all other correction
    /// types.
    pub alternative_tool: Option<String>,

    /// Free-form instructions to help avoid the same failure on a future
    /// turn.
    ///
    /// Used by [`CorrectionType::ApproachChange`] (high-level
    /// re-strategizing) and optionally by other variants as a sidecar
    /// note. Not consumed mechanically by the retry loop — it is
    /// advisory, typically surfaced to a user or appended to context for
    /// the next model turn.
    pub guidance: Option<String>,
}

/// Result of applying a correction.
///
/// Indicates whether the reflection system's correction was successfully
/// applied, failed, or was skipped. Produced after attempting to retry
/// with the corrected parameters.
#[derive(Debug, Clone)]
pub enum CorrectionResult {
    /// Correction was applied successfully.
    ///
    /// The retry with corrected parameters succeeded and the agent can
    /// continue processing normally.
    Applied,

    /// Correction failed.
    ///
    /// The retry also failed. Contains a human-readable error message
    /// describing what went wrong with the corrected attempt.
    Failed(String),

    /// No correction was needed or possible.
    ///
    /// The reflection system decided not to apply a correction (e.g.,
    /// the error is transient or the correction type was
    /// [`Escalate`](CorrectionType::Escalate)).
    Skipped,
}

/// Result of analyzing a failure via [`Reflector::analyze()`].
///
/// Describes what went wrong, how severe it is, whether it's worth
/// retrying, and optionally provides a [`Correction`] the agent can apply
/// before retrying. Produced by a [`Reflector`] and consumed by a
/// [`RecoveryStrategy`] to decide the next action.
///
/// # How the engine consumes it
///
/// `recover_tool_error` in `engine/bare/dispatch.rs` calls
/// [`Reflector::analyze`] to get a `FailureAnalysis`, hands it to
/// [`RecoveryStrategy::decide`] for the action, and clones `correction`
/// out separately so the retry loop can apply it. If the reflector
/// itself errors, the engine conservatively fails the turn rather than
/// guessing — see [`ReflectionError`].
///
/// # Structured output
///
/// Implements [`StructuredOutput`] so an [`LlmReflector`] can request it
/// back from a model via [`request_structured`] with a guaranteed-schema
/// response. The hand-written schema enumerates the five fields below,
/// pins [`FailureSeverity`] as a four-value string enum, and embeds the
/// [`Correction`] shape under `correction`.
///
/// # Example
///
/// ```rust
/// use loopctl::reflection::{FailureAnalysis, FailureSeverity};
///
/// let analysis = FailureAnalysis {
///     is_recoverable: true,
///     root_cause: "file not found".to_string(),
///     severity: FailureSeverity::Medium,
///     correction: None,
///     context: "Attempted to read config.yaml".to_string(),
/// };
/// assert!(analysis.is_recoverable);
/// ```
///
/// [`StructuredOutput`]: crate::structured::StructuredOutput
/// [`LlmReflector`]: crate::reflection::LlmReflector
/// [`request_structured`]: crate::structured::request_structured
/// [`RecoveryStrategy::decide`]: crate::reflection::RecoveryStrategy::decide
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FailureAnalysis {
    /// Whether the failure can be recovered from.
    ///
    /// The headline signal. A [`RecoveryStrategy`] typically maps `false`
    /// to [`RecoveryAction::Fail`] (or [`RecoveryAction::Skip`] when the
    /// step is optional) and `true` to [`RecoveryAction::Retry`] when the
    /// attempt budget allows. This is independent of [`severity`](Self::severity):
    /// a `Low`-severity failure may still be non-recoverable if the
    /// reflector can't suggest a fix, and a `Critical`-severity failure
    /// may technically be recoverable if a correction is supplied.
    ///
    /// [`RecoveryAction::Fail`]: crate::reflection::RecoveryAction::Fail
    /// [`RecoveryAction::Skip`]: crate::reflection::RecoveryAction::Skip
    /// [`RecoveryAction::Retry`]: crate::reflection::RecoveryAction::Retry
    pub is_recoverable: bool,

    /// Description of what went wrong.
    ///
    /// Free-form text identifying the root cause — e.g., `"file not
    /// found"`, `"401 Unauthorized"`, `"tool input did not match schema"`.
    /// Often echoes the tool's error message but may be rephrased or
    /// sharpened by the reflector. Surfaced to users in failure output
    /// and included by an [`LlmReflector`] in its analysis of subsequent
    /// failures.
    ///
    /// [`LlmReflector`]: crate::reflection::LlmReflector
    pub root_cause: String,

    /// How severe the failure is.
    ///
    /// See [`FailureSeverity`] for the four levels and their typical
    /// recovery implications. Strategies commonly use this to gate
    /// retries — e.g., refusing to retry `Critical` even when
    /// [`is_recoverable`](Self::is_recoverable) is `true`.
    pub severity: FailureSeverity,

    /// Suggested correction for the agent to apply before retrying.
    ///
    /// `None` when the reflector has no concrete fix to suggest (the
    /// failure is either non-recoverable, or recoverable by a bare retry
    /// with no input changes). When `Some`, the engine clones the
    /// [`Correction`] out and feeds it into the retry loop, which
    /// substitutes `modified_input` / `alternative_tool` as the
    /// correction directs. Reflectors that populate this field should
    /// keep [`Correction::correction_type`] consistent with the fields
    /// they fill.
    pub correction: Option<Correction>,

    /// Additional context the reflector wants the framework or a future
    /// turn to see.
    ///
    /// Free-form; common uses are environment state at the time of
    /// failure (cwd, available tools), the original task description, or
    /// a short note about what the reflector considered. The framework
    /// does not parse this field — it is advisory, typically logged
    /// alongside the analysis or surfaced to a user when the turn fails.
    pub context: String,
}

impl crate::structured::StructuredOutput for FailureAnalysis {
    fn name() -> &'static str {
        "failure_analysis"
    }

    fn schema() -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "is_recoverable": {"type": "boolean"},
                "root_cause": {"type": "string"},
                "severity": {
                    "type": "string",
                    "enum": ["low", "medium", "high", "critical"]
                },
                "correction": {
                    "anyOf": [
                        {
                            "type": "object",
                            "properties": {
                                "correction_type": {
                                    "type": "string",
                                    "enum": [
                                        "input_fix",
                                        "tool_change",
                                        "prerequisite_fix",
                                        "approach_change",
                                        "escalate"
                                    ]
                                },
                                "description": {"type": "string"},
                                "modified_input": {"type": ["object", "null"]},
                                "alternative_tool": {"type": ["string", "null"]},
                                "guidance": {"type": ["string", "null"]}
                            },
                            "required": [
                                "correction_type",
                                "description",
                                "modified_input",
                                "alternative_tool",
                                "guidance"
                            ],
                            "additionalProperties": false
                        },
                        {"type": "null"}
                    ]
                },
                "context": {"type": "string"}
            },
            "required": [
                "is_recoverable",
                "root_cause",
                "severity",
                "correction",
                "context"
            ],
            "additionalProperties": false
        })
    }
}

/// Errors produced by [`Reflector::analyze()`].
///
/// The reflector can either produce a valid analysis, skip analysis
/// (letting the framework use its default behaviour), or fail
/// during analysis.
#[derive(Debug, thiserror::Error)]
pub enum ReflectionError {
    /// The reflector opted out of analyzing this failure.
    ///
    /// The framework should fall back to its default error handling.
    #[error("reflection skipped: {0}")]
    Skipped(String),

    /// The reflector itself encountered an error.
    ///
    /// Distinct from the tool failure being analyzed — it means
    /// the reflector's own logic broke (e.g., an LLM call for
    /// summarisation failed).
    #[error("reflection internal error: {0}")]
    Internal(String),
}

/// What the framework should do after a failure.
///
/// Produced by [`RecoveryStrategy::decide()`] after the
/// [`Reflector::analyze()`] step. Each variant maps to a different action
/// in the agent loop — the strategy decides which one; the engine
/// executes it.
///
/// # Variants in rough order of severity
///
/// [`Retry`] is the most permissive (try again, optionally with a
/// correction); [`Skip`] continues past the failed step; [`AskUser`]
/// yields control for human input; [`Fail`] terminates the operation and
/// propagates the error. A typical strategy progresses through these as
/// the attempt budget drains: early attempts → `Retry`, late attempts →
/// `Fail` or `AskUser`.
///
/// # Equality and ordering
///
/// Derives [`Eq`] so two actions compare equal when their payloads
/// match (same `delay`, same string). Useful in tests that assert a
/// strategy picked a specific action; not meaningful for runtime
/// prioritization — there is no `Ord` impl, by design.
///
/// # Example
///
/// ```rust
/// use loopctl::reflection::RecoveryAction;
/// use std::time::Duration;
///
/// let action = RecoveryAction::Retry {
///     delay: Duration::from_millis(500),
/// };
/// assert!(action.is_retry());
/// assert_eq!(action.delay(), Some(Duration::from_millis(500)));
///
/// let fail = RecoveryAction::Fail("unrecoverable".to_string());
/// assert!(fail.is_fail());
/// ```
///
/// [`Retry`]: Self::Retry
/// [`Skip`]: Self::Skip
/// [`AskUser`]: Self::AskUser
/// [`Fail`]: Self::Fail
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecoveryAction {
    /// Retry the failed operation.
    ///
    /// Wait for `delay` before retrying. If the
    /// [`FailureAnalysis::correction`] that produced this action was
    /// `Some`, the engine applies it (substituting
    /// [`Correction::modified_input`] or swapping to
    /// [`Correction::alternative_tool`]) before re-dispatching the tool
    /// call. The strategy is responsible for choosing a sensible `delay`
    /// — typically backoff that grows with the attempt number.
    ///
    /// [`FailureAnalysis::correction`]: crate::reflection::FailureAnalysis::correction
    /// [`Correction::modified_input`]: crate::reflection::Correction::modified_input
    /// [`Correction::alternative_tool`]: crate::reflection::Correction::alternative_tool
    Retry {
        /// Duration to wait before retrying, chosen by the strategy.
        ///
        /// Strategies commonly use exponential backoff here. A `delay`
        /// of zero is permitted (immediate retry) but should be reserved
        /// for cases where the failure is known to be transient and the
        /// retry is cheap.
        delay: Duration,
    },

    /// Skip the failed operation and continue with the rest of the turn.
    ///
    /// The framework logs the reason and moves on rather than
    /// retrying. Use when the step is optional or the failure is
    /// non-fatal — e.g., a metric-emitting tool that the agent can
    /// safely proceed without. The carried string is the human-readable
    /// reason to log.
    Skip(String),

    /// Ask the user for input before continuing.
    ///
    /// Yields control to the caller with a prompt string. The framework
    /// surfaces this in whatever interaction model it runs under
    /// (headless: prints the prompt and waits on stdin; TUI: renders a
    /// prompt and waits for input). Use when the strategy cannot decide
    /// autonomously — e.g., a permission-style failure that a human
    /// should adjudicate, or a `ToolChange` correction that requires a
    /// choice between plausible alternatives.
    AskUser(String),

    /// Fail the operation and propagate the error.
    ///
    /// No further retries — the framework should report this failure and
    /// stop the recovery loop. The carried string is the error message
    /// to surface. Use when the failure is unrecoverable, when the
    /// attempt budget is exhausted, or when a [`Reflector`] returned
    /// [`ReflectionError::Internal`] (the engine conservatively maps
    /// reflector failure to `Fail`).
    ///
    /// [`ReflectionError::Internal`]: crate::reflection::ReflectionError::Internal
    Fail(String),
}

impl RecoveryAction {
    /// Returns the retry delay, if this is a [`Retry`](Self::Retry) action.
    ///
    /// Lets callers branch on the wait without a full `match`. Returns
    /// `None` for the other three variants, so a strategy can write
    /// `action.delay().unwrap_or(Duration::ZERO)` to default a non-retry
    /// action to immediate handling.
    #[must_use]
    pub fn delay(&self) -> Option<Duration> {
        match self {
            Self::Retry { delay } => Some(*delay),
            _ => None,
        }
    }

    /// Returns `true` if this is a [`Retry`](Self::Retry) action.
    ///
    /// Convenience predicate; equivalent to
    /// `matches!(action, RecoveryAction::Retry { .. })`. Useful in
    /// engine code that gates on retry vs. non-retry without caring
    /// about the delay.
    #[must_use]
    pub fn is_retry(&self) -> bool {
        matches!(self, Self::Retry { .. })
    }

    /// Returns `true` if this is a [`Fail`](Self::Fail) action.
    ///
    /// Convenience predicate. Engine code commonly checks this to decide
    /// whether to terminate the recovery loop and propagate the error.
    #[must_use]
    pub fn is_fail(&self) -> bool {
        matches!(self, Self::Fail(_))
    }

    /// Returns `true` if this is a [`Skip`](Self::Skip) action.
    ///
    /// Convenience predicate. Use to distinguish "move on silently" from
    /// the harder-failure variants when logging.
    #[must_use]
    pub fn is_skip(&self) -> bool {
        matches!(self, Self::Skip(_))
    }

    /// Returns `true` if this is an [`AskUser`](Self::AskUser) action.
    ///
    /// Convenience predicate. Engine code checks this to know it must
    /// yield control to the caller (headless: stdin; TUI: prompt) rather
    /// than continue autonomously.
    #[must_use]
    pub fn is_ask_user(&self) -> bool {
        matches!(self, Self::AskUser(_))
    }
}

impl fmt::Display for RecoveryAction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Retry { delay } => {
                write!(f, "retry after {delay:?}")
            }
            Self::Skip(reason) => write!(f, "skip: {reason}"),
            Self::AskUser(prompt) => write!(f, "ask user: {prompt}"),
            Self::Fail(reason) => write!(f, "fail: {reason}"),
        }
    }
}

/// Analyses failed tool calls to determine recoverability and corrections.
///
/// Implement this trait to provide custom failure analysis — for example,
/// an LLM-based reflector that reads error messages and suggests fixes,
/// or a rule-based reflector that matches known error patterns.
///
/// The trait is [`Send + Sync`] so it can be stored in `Arc<dyn Reflector>`
/// and shared across threads.
///
/// # Example
///
/// ```rust
/// use loopctl::reflection::{
///     Reflector, ReflectionContext, FailureAnalysis, FailureSeverity, ReflectionError,
/// };
/// use std::future::Future;
/// use std::pin::Pin;
///
/// struct PatternReflector;
///
/// impl Reflector for PatternReflector {
///     fn analyze(
///         &self,
///         error: &str,
///         tool_name: &str,
///         _tool_input: &serde_json::Value,
///         _tool_schema: Option<&loopctl::tool::ToolSchema>,
///         _context: &ReflectionContext,
///     ) -> Pin<Box<dyn Future<Output = Result<FailureAnalysis, ReflectionError>> + Send + '_>> {
///         let error = error.to_string();
///         let tool_name = tool_name.to_string();
///         Box::pin(async move {
///             if error.contains("not found") {
///                 Ok(FailureAnalysis {
///                     is_recoverable: true,
///                     root_cause: error,
///                     severity: FailureSeverity::Medium,
///                     correction: None,
///                     context: format!("tool: {tool_name}"),
///                 })
///             } else {
///                 Ok(FailureAnalysis {
///                     is_recoverable: false,
///                     root_cause: error,
///                     severity: FailureSeverity::High,
///                     correction: None,
///                     context: String::new(),
///                 })
///             }
///         })
///     }
/// }
/// ```
pub trait Reflector: Send + Sync {
    /// Analyse a failed tool call.
    ///
    /// # Arguments
    ///
    /// - `error` — The error message from the failed call.
    /// - `tool_name` — Which tool was called.
    /// - `tool_input` — The JSON input that was passed.
    /// - `tool_schema` — The schema of the tool that failed, when the
    ///   engine can resolve it. `None` if the tool isn't in the registry
    ///   or the schema is otherwise unavailable. Reflectors that want to
    ///   validate a suggested `modified_input` should skip validation
    ///   when this is `None`.
    /// - `context` — Retry state and task description.
    ///
    /// # Errors
    ///
    /// Returns [`ReflectionError::Skipped`] if the reflector opts out,
    /// or [`ReflectionError::Internal`] if the reflector itself fails.
    fn analyze(
        &self,
        error: &str,
        tool_name: &str,
        tool_input: &serde_json::Value,
        tool_schema: Option<&crate::tool::ToolSchema>,
        context: &ReflectionContext,
    ) -> Pin<Box<dyn Future<Output = Result<FailureAnalysis, ReflectionError>> + Send + '_>>;
}

/// Decides what to do after a failure has been analyzed.
///
/// Takes a [`FailureAnalysis`] and the current retry state, returns a
/// [`RecoveryAction`]. Implement this trait to provide custom recovery
/// policies — for example, circuit-breaker patterns, rate-limit-aware
/// backoff, or user-prompting strategies.
///
/// The trait is [`Send + Sync`] so it can be stored in
/// `Arc<dyn RecoveryStrategy>` and shared across threads.
///
/// # Example
///
/// ```rust
/// use loopctl::reflection::{
///     RecoveryStrategy, FailureAnalysis, FailureSeverity, RecoveryAction,
/// };
/// use std::future::Future;
/// use std::pin::Pin;
///
/// struct AlwaysRetryStrategy;
///
/// impl RecoveryStrategy for AlwaysRetryStrategy {
///     fn decide(
///         &self,
///         _analysis: &FailureAnalysis,
///         attempt: u32,
///         max_attempts: u32,
///     ) -> Pin<Box<dyn Future<Output = RecoveryAction> + Send + '_>> {
///         let action = if attempt >= max_attempts {
///             RecoveryAction::Fail("max retries exceeded".to_string())
///         } else {
///             RecoveryAction::Retry {
///                 delay: std::time::Duration::from_secs(1),
///             }
///         };
///         Box::pin(async move { action })
///     }
/// }
/// ```
pub trait RecoveryStrategy: Send + Sync {
    /// Decide what to do after a failure.
    ///
    /// # Arguments
    ///
    /// - `analysis` — The reflector's analysis of the failure.
    /// - `attempt` — Current attempt number (0-indexed).
    /// - `max_attempts` — Maximum attempts before giving up.
    ///
    /// # Returns
    ///
    /// A [`RecoveryAction`] telling the framework what to do next.
    fn decide(
        &self,
        analysis: &FailureAnalysis,
        attempt: u32,
        max_attempts: u32,
    ) -> Pin<Box<dyn Future<Output = RecoveryAction> + Send + '_>>;
}

/// A no-op reflector that marks everything as non-recoverable.
///
/// Useful as a safe default when reflection is disabled, or as a base
/// for building composite reflectors via the decorator pattern.
///
/// # Example
///
/// ```rust,ignore
/// # tokio_test::block_on(async {
/// let reflector = NoopReflector;
/// let context = ReflectionContext {
///     task: "test".to_string(),
///     attempt: 0,
///     max_attempts: 3,
/// };
///
/// let analysis = reflector
///     .analyze("error", "tool", &json!({}), None, &context)
///     .await
///     .unwrap();
/// assert!(!analysis.is_recoverable);
/// # });
/// ```
pub struct NoopReflector;

impl Reflector for NoopReflector {
    fn analyze(
        &self,
        error: &str,
        _tool_name: &str,
        _tool_input: &serde_json::Value,
        _tool_schema: Option<&crate::tool::ToolSchema>,
        _context: &ReflectionContext,
    ) -> Pin<Box<dyn Future<Output = Result<FailureAnalysis, ReflectionError>> + Send + '_>> {
        let root_cause = error.to_string();
        Box::pin(async move {
            Ok(FailureAnalysis {
                is_recoverable: false,
                root_cause,
                severity: FailureSeverity::Medium,
                correction: None,
                context: String::new(),
            })
        })
    }
}

impl fmt::Debug for NoopReflector {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("NoopReflector").finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn severity_ordering() {
        assert!(FailureSeverity::Low < FailureSeverity::Medium);
        assert!(FailureSeverity::Medium < FailureSeverity::High);
        assert!(FailureSeverity::High < FailureSeverity::Critical);
    }

    #[test]
    fn severity_display() {
        assert_eq!(FailureSeverity::Low.to_string(), "low");
        assert_eq!(FailureSeverity::Medium.to_string(), "medium");
        assert_eq!(FailureSeverity::High.to_string(), "high");
        assert_eq!(FailureSeverity::Critical.to_string(), "critical");
    }

    #[test]
    fn severity_serde_round_trip() {
        let severity = FailureSeverity::High;
        let json = serde_json::to_string(&severity).unwrap();
        assert_eq!(json, "\"high\"");
        let deserialized: FailureSeverity = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, severity);
    }

    #[test]
    fn reflection_context_fields() {
        let ctx = ReflectionContext {
            task: "fix bug".to_string(),
            attempt: 2,
            max_attempts: 5,
        };
        assert_eq!(ctx.task, "fix bug");
        assert_eq!(ctx.attempt, 2);
        assert_eq!(ctx.max_attempts, 5);
    }

    #[test]
    fn failure_analysis_recoverable() {
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "timeout".to_string(),
            severity: FailureSeverity::Low,
            correction: None,
            context: "network".to_string(),
        };
        assert!(analysis.is_recoverable);
        assert_eq!(analysis.root_cause, "timeout");
    }

    #[test]
    fn failure_analysis_with_correction() {
        let correction = Correction {
            correction_type: CorrectionType::InputFix,
            description: "fix path".to_string(),
            modified_input: Some(serde_json::json!({"path": "/correct/path"})),
            alternative_tool: None,
            guidance: Some("check paths".to_string()),
        };
        let analysis = FailureAnalysis {
            is_recoverable: true,
            root_cause: "file not found".to_string(),
            severity: FailureSeverity::Medium,
            correction: Some(correction),
            context: String::new(),
        };
        assert!(analysis.correction.is_some());
        let c = analysis.correction.unwrap();
        assert_eq!(c.description, "fix path");
    }

    #[test]
    fn reflection_error_skipped_display() {
        let err = ReflectionError::Skipped("not applicable".to_string());
        let s = err.to_string();
        assert!(s.contains("skipped"));
        assert!(s.contains("not applicable"));
    }

    #[test]
    fn reflection_error_internal_display() {
        let err = ReflectionError::Internal("llm timeout".to_string());
        let s = err.to_string();
        assert!(s.contains("internal"));
        assert!(s.contains("llm timeout"));
    }

    #[test]
    fn action_retry_accessors() {
        let action = RecoveryAction::Retry {
            delay: Duration::from_secs(2),
        };
        assert!(action.is_retry());
        assert!(!action.is_fail());
        assert_eq!(action.delay(), Some(Duration::from_secs(2)));
    }

    #[test]
    fn action_skip_accessors() {
        let action = RecoveryAction::Skip("not important".to_string());
        assert!(!action.is_retry());
        assert!(!action.is_fail());
        assert_eq!(action.delay(), None);
    }

    #[test]
    fn action_ask_user() {
        let action = RecoveryAction::AskUser("choose option".to_string());
        assert!(!action.is_retry());
        assert_eq!(action.delay(), None);
    }

    #[test]
    fn action_fail_accessors() {
        let action = RecoveryAction::Fail("unrecoverable".to_string());
        assert!(action.is_fail());
        assert!(!action.is_retry());
        assert_eq!(action.delay(), None);
    }

    #[test]
    fn action_display() {
        let retry = RecoveryAction::Retry {
            delay: Duration::from_secs(1),
        };
        assert!(retry.to_string().contains("retry"));

        let skip = RecoveryAction::Skip("reason".to_string());
        assert!(skip.to_string().contains("skip: reason"));

        let ask = RecoveryAction::AskUser("prompt".to_string());
        assert!(ask.to_string().contains("ask user: prompt"));

        let fail = RecoveryAction::Fail("bad".to_string());
        assert!(fail.to_string().contains("fail: bad"));
    }

    #[tokio::test]
    async fn noop_reflector_marks_non_recoverable() {
        let reflector = NoopReflector;
        let ctx = ReflectionContext {
            task: "test".to_string(),
            attempt: 0,
            max_attempts: 3,
        };
        let analysis = reflector
            .analyze("some error", "tool", &serde_json::json!({}), None, &ctx)
            .await
            .unwrap();
        assert!(!analysis.is_recoverable);
        assert_eq!(analysis.root_cause, "some error");
        assert_eq!(analysis.severity, FailureSeverity::Medium);
        assert!(analysis.correction.is_none());
    }

    #[test]
    fn noop_reflector_debug() {
        let reflector = NoopReflector;
        let debug = format!("{reflector:?}");
        assert!(debug.contains("NoopReflector"));
    }

    #[test]
    fn failure_analysis_structured_round_trip() {
        use crate::structured::StructuredOutput;
        let v = serde_json::json!({
            "is_recoverable": true,
            "root_cause": "timeout",
            "severity": "low",
            "correction": {
                "correction_type": "input_fix",
                "description": "fix the path",
                "modified_input": {"path": "/x"},
                "alternative_tool": null,
                "guidance": null
            },
            "context": "open call"
        });
        let analysis = FailureAnalysis::from_value(v).expect("should deserialize");
        assert!(analysis.is_recoverable);
        assert_eq!(analysis.root_cause, "timeout");
        assert_eq!(analysis.severity, FailureSeverity::Low);
        let correction = analysis.correction.expect("correction");
        assert_eq!(correction.description, "fix the path");
        assert_eq!(
            correction.modified_input,
            Some(serde_json::json!({"path": "/x"}))
        );
    }

    #[test]
    fn failure_analysis_schema_is_valid_json() {
        let schema = <FailureAnalysis as crate::structured::StructuredOutput>::schema();
        let obj = schema.as_object().expect("schema must be a JSON object");
        // Five top-level properties.
        assert_eq!(obj["type"], "object");
        let required = obj["required"]
            .as_array()
            .expect("required must be an array");
        assert_eq!(required.len(), 5);
    }

    #[test]
    fn failure_analysis_schema_correction_type_enum() {
        let schema = <FailureAnalysis as crate::structured::StructuredOutput>::schema();
        let enum_values = schema
            .pointer("/properties/correction/anyOf/0/properties/correction_type/enum")
            .expect("correction_type enum must be present")
            .as_array()
            .expect("enum must be an array");
        let values: Vec<&str> = enum_values.iter().map(|v| v.as_str().unwrap()).collect();
        // Pin the 5 snake_case variants matching CorrectionType's serde rename.
        assert_eq!(
            values,
            vec![
                "input_fix",
                "tool_change",
                "prerequisite_fix",
                "approach_change",
                "escalate"
            ]
        );
    }

    #[test]
    fn failure_analysis_name_is_stable() {
        use crate::structured::StructuredOutput;
        let name = FailureAnalysis::name();
        assert_eq!(name, "failure_analysis");
        assert!(
            name.chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'),
            "name must match ^[a-zA-Z0-9_-]+$: {name}"
        );
    }

    #[tokio::test]
    async fn noop_reflector_accepts_new_signature() {
        // Pins the breaking trait change: NoopReflector implements the
        // 5-arg analyze and its semantics are unchanged.
        let reflector = NoopReflector;
        let ctx = ReflectionContext {
            task: "t".to_string(),
            attempt: 0,
            max_attempts: 1,
        };
        let analysis = reflector
            .analyze("err", "tool", &serde_json::json!({}), None, &ctx)
            .await
            .unwrap();
        assert!(!analysis.is_recoverable);
        assert_eq!(analysis.severity, FailureSeverity::Medium);
    }
}