|
1 | 1 | // ABOUTME: Rig agent executor with conversation memory |
2 | 2 | // ABOUTME: Maintains conversation history for analysis task duration |
3 | 3 |
|
4 | | -use super::api::AgentEvent; |
| 4 | +use super::api::{AgentEvent, RigAgentTrait}; |
5 | 5 | use super::builder::RigAgentBuilder; |
6 | 6 | use super::RigAgentOutput; |
7 | | -use crate::adapter::RigLLMAdapter; |
8 | 7 | use anyhow::Result; |
9 | 8 | use codegraph_mcp_core::analysis::AnalysisType; |
10 | 9 | use codegraph_mcp_core::context_aware_limits::ContextTier; |
@@ -61,7 +60,7 @@ impl RigExecutor { |
61 | 60 | ); |
62 | 61 |
|
63 | 62 | // --- Dynamic Context Throttling --- |
64 | | - let max_tokens = RigLLMAdapter::context_window(); |
| 63 | + let max_tokens = crate::adapter::RigLLMAdapter::context_window(); |
65 | 64 | let estimated_history_tokens: usize = self |
66 | 65 | .history |
67 | 66 | .iter() |
@@ -96,12 +95,40 @@ impl RigExecutor { |
96 | 95 | self.build_contextualized_query(query) |
97 | 96 | }; |
98 | 97 |
|
99 | | - // Execute the agent |
100 | | - let response = agent.execute(&contextualized_query).await?; |
| 98 | + // Execute with automatic Reflexion fallback |
| 99 | + let (response, tool_calls, tool_traces) = match agent.execute(&contextualized_query).await { |
| 100 | + Ok(resp) => { |
| 101 | + let calls = agent.take_tool_call_count(); |
| 102 | + let traces = agent.take_tool_traces(); |
| 103 | + (resp, calls, traces) |
| 104 | + }, |
| 105 | + Err(e) => { |
| 106 | + info!( |
| 107 | + error = %e, |
| 108 | + "Primary agent execution failed. Initiating Reflexion auto-recovery..." |
| 109 | + ); |
| 110 | + |
| 111 | + // Wrap the primary agent in ReflexionAgent for retry |
| 112 | + let reflexion_agent = crate::agent::reflexion::ReflexionAgent { |
| 113 | + inner: agent, |
| 114 | + max_retries: 2, |
| 115 | + }; |
| 116 | + |
| 117 | + // Retry execution with self-correction |
| 118 | + match reflexion_agent.execute(&contextualized_query).await { |
| 119 | + Ok(resp) => { |
| 120 | + let calls = reflexion_agent.take_tool_call_count(); |
| 121 | + let traces = reflexion_agent.take_tool_traces(); |
| 122 | + (resp, calls, traces) |
| 123 | + }, |
| 124 | + Err(final_err) => { |
| 125 | + return Err(anyhow::anyhow!("Agent failed after Reflexion recovery: {}", final_err)); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + }; |
101 | 130 |
|
102 | 131 | let duration_ms = start.elapsed().as_millis() as u64; |
103 | | - let tool_calls = agent.take_tool_call_count(); |
104 | | - let tool_traces = agent.take_tool_traces(); |
105 | 132 |
|
106 | 133 | // Record in history |
107 | 134 | let turn = ConversationTurn { |
|
0 commit comments