Skip to content

Commit 2674dd6

Browse files
committed
feat(rig): add automatic Reflexion fallback to RigExecutor
1 parent a231760 commit 2674dd6

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

crates/codegraph-mcp-rig/src/agent/executor.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// ABOUTME: Rig agent executor with conversation memory
22
// ABOUTME: Maintains conversation history for analysis task duration
33

4-
use super::api::AgentEvent;
4+
use super::api::{AgentEvent, RigAgentTrait};
55
use super::builder::RigAgentBuilder;
66
use super::RigAgentOutput;
7-
use crate::adapter::RigLLMAdapter;
87
use anyhow::Result;
98
use codegraph_mcp_core::analysis::AnalysisType;
109
use codegraph_mcp_core::context_aware_limits::ContextTier;
@@ -61,7 +60,7 @@ impl RigExecutor {
6160
);
6261

6362
// --- Dynamic Context Throttling ---
64-
let max_tokens = RigLLMAdapter::context_window();
63+
let max_tokens = crate::adapter::RigLLMAdapter::context_window();
6564
let estimated_history_tokens: usize = self
6665
.history
6766
.iter()
@@ -96,12 +95,40 @@ impl RigExecutor {
9695
self.build_contextualized_query(query)
9796
};
9897

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+
};
101130

102131
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();
105132

106133
// Record in history
107134
let turn = ConversationTurn {

0 commit comments

Comments
 (0)