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
//! Self-Reflection Architecture for RuvLLM
//!
//! This module provides a comprehensive self-reflection system enabling error recovery
//! and self-correction capabilities for LLM-based agents. The architecture supports
//! multiple reflection strategies and learns from past errors to improve future performance.
//!
//! ## Key Components
//!
//! - [`ReflectiveAgent`]: Wrapper that adds reflection capabilities to any base agent
//! - [`ConfidenceChecker`]: Implements If-or-Else (IoE) pattern for targeted revision
//! - [`ErrorPatternLearner`]: Learns recovery strategies from historical errors
//! - [`Perspective`]: Multi-perspective critique system for comprehensive reflection
//!
//! ## Reflection Strategies
//!
//! The module supports four reflection strategies (see [`ReflectionStrategy`]):
//!
//! 1. **Retry**: Simple retry with reflection context on failure
//! 2. **IfOrElse (IoE)**: Confidence-based revision - only revise when confidence is LOW
//! 3. **MultiPerspective**: Critique from multiple angles (correctness, completeness, consistency)
//! 4. **TrajectoryReflection**: Reflect on entire execution trajectory for learning
//!
//! ## Architecture
//!
//! ```text
//! +-------------------+ +----------------------+ +------------------+
//! | ReflectiveAgent |---->| ReflectionStrategy |---->| Reflection |
//! | - base_agent | | - Retry | | - context |
//! | - strategy | | - IfOrElse | | - insights |
//! | - error_learner | | - MultiPerspective | | - suggestions |
//! +-------------------+ | - TrajectoryReflect | +------------------+
//! +----------------------+
//! |
//! +--------------------------|---------------------------+
//! | | |
//! v v v
//! +----------------+ +---------------------+ +--------------------+
//! | ConfidenceChk | | ErrorPatternLearner | | Perspectives |
//! | - threshold | | - clusters | | - Correctness |
//! | - budget | | - strategies | | - Completeness |
//! +----------------+ +---------------------+ | - Consistency |
//! +--------------------+
//! ```
//!
//! ## Example
//!
//! ```rust,ignore
//! use ruvllm::reflection::{
//! ReflectiveAgent, ReflectionStrategy, ConfidenceChecker,
//! ErrorPatternLearner, CorrectnessChecker, CompletenessChecker,
//! };
//! use ruvllm::claude_flow::AgentType;
//!
//! // Create a reflective agent with multi-perspective strategy
//! let mut agent = ReflectiveAgent::new(
//! base_agent,
//! ReflectionStrategy::MultiPerspective {
//! perspectives: vec![
//! Box::new(CorrectnessChecker::new()),
//! Box::new(CompletenessChecker::new()),
//! ],
//! },
//! );
//!
//! // Execute with automatic reflection on failure
//! let result = agent.execute_with_reflection("implement a REST API", &context).await?;
//!
//! // The result includes reflection insights if recovery occurred
//! if result.recovered_via_reflection {
//! println!("Recovered via: {}", result.reflection.unwrap().strategy);
//! }
//! ```
//!
//! ## Integration with ReasoningBank
//!
//! This module integrates with the existing `Verdict` enum by adding a
//! `RecoveredViaReflection` variant to track successful error recovery:
//!
//! ```rust,ignore
//! use ruvllm::claude_flow::Verdict;
//!
//! let verdict = Verdict::RecoveredViaReflection {
//! original_error: "Type mismatch in function call".to_string(),
//! recovery_strategy: "Added explicit type annotation".to_string(),
//! attempts: 2,
//! };
//! ```
// Re-export all public types
pub use ;
pub use ;
pub use ;
pub use ;