pub use crate::github::types::ReviewIssue;
use crate::store;
use crate::store::TaskStore;
use std::collections::HashSet;
use std::collections::hash_map::DefaultHasher;
use std::fs;
use std::fs::File;
use std::hash::Hash;
use std::hash::Hasher;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::path::{Path, PathBuf};
use std::result;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use std::time::SystemTime;
use tokio::fs as tokio_fs;
use tokio::task;
use tokio::time;
use crate::config as app_config;
pub use crate::cooldown::{
clear_expired_cooldowns, is_agent_in_cooldown, is_model_in_cooldown,
record_agent_failure_with_message, record_model_failure, set_agent_cooldown,
};
use crate::engine;
use crate::engine::agents::parser::AgentResponse;
use crate::engine::router::config;
use crate::engine::runner::ndjson;
use crate::store::MemoryEntry;
use crate::store::Task;
use crate::sys::cmd_cache;
use crate::sys::home;
const JITTER_FACTOR: f64 = 0.3;
const MAX_OUTPUT_SIZE_BYTES: u64 = 10 * 1024 * 1024;
fn read_file_with_limit<P: AsRef<Path>>(path: P, max_bytes: u64) -> anyhow::Result<String> {
let path = path.as_ref();
let metadata = fs::metadata(path)?;
if !metadata.is_file() {
anyhow::bail!("not a file: {}", path.display());
}
let file_size = metadata.len();
if file_size <= max_bytes {
return fs::read_to_string(path).map_err(Into::into);
}
let mut file = File::open(path)?;
let start_pos = file_size.saturating_sub(max_bytes);
file.seek(SeekFrom::Start(start_pos))?;
let mut buffer = Vec::with_capacity(usize::try_from(max_bytes).unwrap_or(0));
file.read_to_end(&mut buffer)?;
let content = String::from_utf8_lossy(&buffer);
tracing::warn!(
path = %path.display(),
file_size = file_size,
max_bytes = max_bytes,
"output file truncated: exceeded size limit"
);
Ok(content.into_owned())
}
pub(crate) fn calculate_backoff_delay(retry_count: usize) -> u64 {
let base_delay = config::retry_base_delay_ms();
let max_delay = config::retry_max_delay_ms();
let exponential =
base_delay * (2_u64.saturating_pow(u32::try_from(retry_count).unwrap_or(u32::MAX)));
let capped = exponential.min(max_delay);
#[expect(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a jitter range in milliseconds, far below 2^52 and never negative"
)]
let jitter_range = (capped as f64 * JITTER_FACTOR) as u64;
let jitter = {
let mut h = DefaultHasher::new();
SystemTime::now().hash(&mut h);
thread::current().id().hash(&mut h);
h.finish() % (jitter_range * 2 + 1)
};
capped.saturating_sub(jitter_range) + jitter
}
fn fallback_retry_count(task: Option<&Task>) -> usize {
task.map_or(0, |t| {
let chain_retry_count = t
.limit_reroute_chain
.split(',')
.filter(|entry| !entry.is_empty())
.count();
let network_retry_count = usize::try_from(t.network_retries).unwrap_or(0);
chain_retry_count.max(network_retry_count)
})
}
pub async fn wait_for_fallback_backoff(
task_id: &str,
store: &Option<Arc<TaskStore>>,
repo: &str,
) -> u64 {
let task = store::opt_store_get_task(store, repo, task_id).await;
let retry_count = fallback_retry_count(task.as_ref());
let delay = calculate_backoff_delay(retry_count);
if delay > 0 {
tracing::debug!(
task_id,
retry_count = retry_count,
delay_ms = delay,
"fallback backoff: waiting before retry"
);
time::sleep(Duration::from_millis(delay)).await;
}
delay
}
#[derive(Debug, Clone)]
pub enum WeightSignal {
Success { agent: String },
RateLimited { agent: String },
Failed { agent: String },
Blocked,
Rerouted,
None,
}
pub async fn read_output_file(task_id: &str, primary_path: &Path, repo: &str) -> String {
if let Some(content) = task::spawn_blocking({
let p = primary_path.to_path_buf();
move || read_file_with_limit(p, MAX_OUTPUT_SIZE_BYTES)
})
.await
.ok()
.and_then(result::Result::ok)
&& !content.is_empty()
{
return content;
}
if let Ok(task_dir) = home::task_dir_async(repo, task_id).await {
let attempts_dir = task_dir.join("attempts");
if tokio_fs::metadata(&attempts_dir)
.await
.is_ok_and(|m| m.is_dir())
{
let mut attempt_nums: Vec<u32> = task::spawn_blocking({
let a = attempts_dir.clone();
move || {
match fs::read_dir(&a) {
Ok(entries) => {
let mut nums = Vec::new();
for entry in entries {
match entry {
Ok(e) => {
if let Some(n) =
e.file_name().to_str().and_then(|n| n.parse().ok())
{
nums.push(n);
}
}
Err(e) => tracing::warn!(
path = %a.display(),
err = %e,
"error reading attempt dir entry in output fallback"
),
}
}
nums
}
Err(e) => {
tracing::warn!(path = %a.display(), err = %e, "failed to read attempts dir in output fallback");
Vec::new()
}
}
}
})
.await
.unwrap_or_default();
attempt_nums.sort_unstable_by(|a, b| b.cmp(a)); for n in attempt_nums {
let p = attempts_dir.join(n.to_string()).join("output.json");
if let Some(content) = task::spawn_blocking({
let p = p.clone();
move || read_file_with_limit(p, MAX_OUTPUT_SIZE_BYTES)
})
.await
.ok()
.and_then(result::Result::ok)
&& !content.is_empty()
{
tracing::info!(task_id, path = %p.display(), "read output from attempt dir");
return content;
}
}
}
}
let state_dir = home::state_dir().unwrap_or_else(|_| PathBuf::from("/tmp"));
let mut fallbacks = vec![
PathBuf::from(format!("/tmp/output-{task_id}.json")),
state_dir.join(format!("output-{task_id}.json")),
];
if let Ok(legacy_path) = home::state_file(&format!("output-{task_id}.json"))
&& !fallbacks.contains(&legacy_path)
{
fallbacks.push(legacy_path);
}
for path in &fallbacks {
if let Some(content) = task::spawn_blocking({
let p = path.clone();
move || read_file_with_limit(p, MAX_OUTPUT_SIZE_BYTES)
})
.await
.ok()
.and_then(result::Result::ok)
&& !content.is_empty()
{
tracing::info!(task_id, path = %path.display(), "read output from legacy fallback");
return content;
}
}
String::new()
}
pub fn pick_fallback_agent(
current_agent: &str,
chain: &str,
available_agents: &[String],
) -> Option<String> {
clear_expired_cooldowns();
let chain_set: HashSet<&str> = if chain.is_empty() {
HashSet::new()
} else {
chain.split(',').collect()
};
for agent in available_agents {
if agent == current_agent || chain_set.contains(agent.as_str()) {
continue;
}
if is_agent_in_cooldown(agent) {
continue;
}
return Some(agent.clone());
}
None
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RetryableError {
Timeout,
Interrupted,
UsageLimit,
AuthError,
Failed,
MissingTooling,
ModelUnavailable,
}
impl RetryableError {
pub fn type_str(self) -> &'static str {
match self {
RetryableError::Timeout => "timeout",
RetryableError::Interrupted => "interrupted",
RetryableError::UsageLimit => "rate_limit",
RetryableError::AuthError => "auth_error",
RetryableError::ModelUnavailable => "model_unavailable",
RetryableError::Failed | RetryableError::MissingTooling => "failed",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FailoverOutcome {
pub status: String,
pub next_agent: Option<String>,
}
impl FailoverOutcome {
fn with_status(status: impl Into<String>) -> Self {
Self {
status: status.into(),
next_agent: None,
}
}
fn switched(next_agent: String) -> Self {
Self {
status: "new".to_string(),
next_agent: Some(next_agent),
}
}
}
pub async fn handle_failover(
task_id: &str,
agent_name: &str,
error_type: RetryableError,
error_message: &str,
credit_recorded: bool,
store: &Option<Arc<TaskStore>>,
repo: &str,
) -> FailoverOutcome {
let chain = get_reroute_chain(task_id, store, repo).await;
let chain = update_reroute_chain(task_id, agent_name, &chain, store, repo).await;
if error_type == RetryableError::Timeout
&& let Some(status) =
block_after_repeated_timeouts(task_id, error_message, store, repo).await
{
return FailoverOutcome::with_status(status);
}
let available: Vec<String> = engine::configured_agents()
.into_iter()
.filter(|a| cmd_cache::command_exists(a))
.collect();
let chain_set: HashSet<&str> = if chain.is_empty() {
HashSet::new()
} else {
chain.split(',').collect()
};
let is_exhausted = available.iter().all(|a| chain_set.contains(a.as_str()));
if is_exhausted {
let is_retryable = matches!(
error_type,
RetryableError::UsageLimit
| RetryableError::AuthError
| RetryableError::ModelUnavailable
| RetryableError::Timeout
| RetryableError::Interrupted
);
let next_status = if is_retryable { "new" } else { "blocked" };
tracing::warn!(
task_id,
agent = agent_name,
error_type = ?error_type,
next_status,
"all agents exhausted"
);
let mut store_fields: Vec<(&str, serde_json::Value)> = Vec::new();
if is_retryable {
let msg = format!("{error_message} (all agents exhausted, will retry when available)");
store_fields.push(("last_error", serde_json::json!(msg)));
store_fields.push(("limit_reroute_chain", serde_json::json!("")));
} else {
let msg = format!("{error_message} (all agents exhausted)");
let block_reason = format!(
"all agents exhausted: {} — {}",
error_type.type_str(),
error_message
);
store_fields.push(("last_error", serde_json::json!(msg)));
store_fields.push(("block_reason", serde_json::json!(block_reason)));
}
if let Err(e) = store::store_set_result(store, repo, task_id, &store_fields).await {
tracing::warn!(task_id, err = %e, "failed to write agents_exhausted fields to store");
}
let details = serde_json::json!({
"failure_reason": error_type.type_str(),
"error_message": error_message,
"chain": chain,
"agents_exhausted": true,
"is_retryable": is_retryable,
});
store::store_log_activity(
store,
repo,
task_id,
"rerouted",
Some("in_progress"),
Some(next_status),
None::<&str>,
None::<&str>,
Some(&details),
)
.await;
return FailoverOutcome::with_status(next_status);
}
if let Some(next) = pick_fallback_agent(agent_name, &chain, &available) {
tracing::info!(
task_id,
from = agent_name,
to = next,
error_type = ?error_type,
"failover: switching to fallback agent"
);
if !matches!(
error_type,
RetryableError::MissingTooling
| RetryableError::ModelUnavailable
| RetryableError::Timeout
| RetryableError::Interrupted
) {
if !credit_recorded {
record_agent_failure_with_message(agent_name, error_message).await;
}
set_agent_cooldown(agent_name, 120).await;
}
let msg = format!("{error_message}, switching to {next} for re-route");
if let Err(e) = store::store_set_result(
store,
repo,
task_id,
&[
("agent", serde_json::json!(next)),
("model", serde_json::json!("")),
("last_error", serde_json::json!(msg)),
],
)
.await
{
tracing::warn!(task_id, err = %e, "failed to write failover agent to store");
}
wait_for_fallback_backoff(task_id, store, repo).await;
let details = serde_json::json!({
"failure_reason": error_type.type_str(),
"error_message": error_message,
"from_agent": agent_name,
"to_agent": next,
"chain": chain,
});
store::store_log_activity(
store,
repo,
task_id,
"rerouted",
Some("in_progress"),
Some("new"),
Some(&next),
None::<&str>,
Some(&details),
)
.await;
return FailoverOutcome::switched(next);
}
tracing::warn!(task_id, agent = agent_name, "no fallback agents available");
let is_retryable = matches!(
error_type,
RetryableError::UsageLimit
| RetryableError::AuthError
| RetryableError::ModelUnavailable
| RetryableError::Timeout
| RetryableError::Interrupted
);
if !matches!(
error_type,
RetryableError::MissingTooling
| RetryableError::ModelUnavailable
| RetryableError::Timeout
| RetryableError::Interrupted
) && !credit_recorded
{
record_agent_failure_with_message(agent_name, error_message).await;
}
if is_retryable {
wait_for_fallback_backoff(task_id, store, repo).await;
let msg = format!("{error_message} (no fallback agents, will retry)");
if let Err(e) = store::store_set_result(
store,
repo,
task_id,
&[
("last_error", serde_json::json!(msg)),
("limit_reroute_chain", serde_json::json!("")),
],
)
.await
{
tracing::warn!(task_id, err = %e, "failed to write no_fallback retry fields to store");
}
let details = serde_json::json!({
"failure_reason": error_type.type_str(),
"error_message": error_message,
"chain": chain,
"no_fallback_available": true,
"is_retryable": true,
});
store::store_log_activity(
store,
repo,
task_id,
"rerouted",
Some("in_progress"),
Some("new"),
None::<&str>,
None::<&str>,
Some(&details),
)
.await;
return FailoverOutcome::with_status("new");
}
let msg = format!("{error_message}, no fallback agents");
if let Err(e) = store::store_set_result(
store,
repo,
task_id,
&[("last_error", serde_json::json!(msg))],
)
.await
{
tracing::warn!(task_id, err = %e, "failed to write no_fallback last_error to store");
}
let details = serde_json::json!({
"failure_reason": error_type.type_str(),
"error_message": error_message,
"chain": chain,
"no_fallback_available": true,
});
store::store_log_activity(
store,
repo,
task_id,
"rerouted",
Some("in_progress"),
Some("needs_review"),
None::<&str>,
None::<&str>,
Some(&details),
)
.await;
FailoverOutcome::with_status("needs_review")
}
pub async fn block_after_repeated_timeouts(
task_id: &str,
error_message: &str,
store: &Option<Arc<TaskStore>>,
repo: &str,
) -> Option<String> {
let max: i64 = app_config::get("workflow.max_timeout_retries")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(3);
let count = match store::store_increment(store, repo, task_id, "timeout_retries").await {
Ok(count) => i64::try_from(count).unwrap_or(i64::MAX),
Err(e) => {
tracing::warn!(task_id, err = %e, "failed to increment timeout_retries");
return None;
}
};
if count < max {
return None;
}
let block_reason = format!("timed out {count} times");
tracing::warn!(
task_id,
count,
max,
"timeout retries exhausted, blocking task"
);
if let Err(e) = store::store_set_result(
store,
repo,
task_id,
&[
(
"last_error",
serde_json::json!(format!("{error_message} ({block_reason})")),
),
("block_reason", serde_json::json!(block_reason)),
("limit_reroute_chain", serde_json::json!("")),
],
)
.await
{
tracing::warn!(task_id, err = %e, "failed to write timeout block fields to store");
}
let details = serde_json::json!({
"failure_reason": "timeout",
"error_message": error_message,
"timeout_retries": count,
});
store::store_log_activity(
store,
repo,
task_id,
"rerouted",
Some("in_progress"),
Some("blocked"),
None::<&str>,
None::<&str>,
Some(&details),
)
.await;
Some("blocked".to_string())
}
pub async fn get_reroute_chain(
task_id: &str,
store: &Option<Arc<TaskStore>>,
repo: &str,
) -> String {
store::opt_store_get_task(store, repo, task_id)
.await
.map(|t| t.limit_reroute_chain)
.unwrap_or_default()
.trim()
.to_string()
}
pub async fn update_reroute_chain(
task_id: &str,
current_agent: &str,
existing_chain: &str,
store: &Option<Arc<TaskStore>>,
repo: &str,
) -> String {
let mut chain = existing_chain.to_string();
if chain.is_empty() {
chain = current_agent.to_string();
} else if !chain.split(',').any(|a| a == current_agent) {
chain = format!("{chain},{current_agent}");
}
if let Err(e) = store::store_set_result(
store,
repo,
task_id,
&[("limit_reroute_chain", serde_json::json!(chain))],
)
.await
{
tracing::warn!(task_id, err = %e, "failed to write limit_reroute_chain to store");
}
chain
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct ReviewResponse {
pub decision: String,
pub notes: String,
pub test_results: Option<String>,
pub issues: Vec<ReviewIssue>,
}
pub fn parse_review_response(text: &str) -> anyhow::Result<ReviewResponse> {
let resp = parse_review_json(text)?;
if matches!(resp.decision.as_str(), "approve" | "request_changes") {
return Ok(resp);
}
let stripped = text.replace("approve|request_changes", "");
infer_review_response_from_text(&stripped)
.ok_or_else(|| anyhow::anyhow!("invalid review decision: {}", resp.decision))
}
fn parse_review_json(text: &str) -> anyhow::Result<ReviewResponse> {
if let Ok(resp) = serde_json::from_str::<ReviewResponse>(text) {
return Ok(resp);
}
if let Some(json_str) = extract_json_block(text)
&& let Ok(resp) = serde_json::from_str::<ReviewResponse>(&json_str)
{
return Ok(resp);
}
if let Some(resp) = extract_review_response_object(text) {
return Ok(resp);
}
anyhow::bail!("failed to parse review response")
}
fn infer_review_response_from_text(text: &str) -> Option<ReviewResponse> {
let lower = text.to_ascii_lowercase();
let has_changes_requested =
lower.contains("changes requested") || lower.contains("request_changes");
if has_changes_requested {
return Some(ReviewResponse {
decision: "request_changes".to_string(),
notes: "Inferred decision from plain-text review output.".to_string(),
test_results: None,
issues: Vec::new(),
});
}
let positive_approval = lower
.split(|c: char| !c.is_ascii_alphabetic())
.any(|word| word == "lgtm")
|| lower.contains("decision is `approve`")
|| lower.contains("decision: `approve`")
|| lower.contains("decision is approve")
|| lower.contains("decision: approve");
if positive_approval {
return Some(ReviewResponse {
decision: "approve".to_string(),
notes: "Inferred decision from plain-text review output.".to_string(),
test_results: None,
issues: Vec::new(),
});
}
None
}
pub fn infer_review_response(text: &str) -> Option<ReviewResponse> {
infer_review_response_from_text(text)
}
fn extract_json_block(text: &str) -> Option<String> {
let mut search_from = 0;
while let Some(start) = text[search_from..].find("```json") {
let abs_start = search_from + start;
let after_tag = abs_start + "```json".len();
let Some(rest) = text[after_tag..].strip_prefix('\n') else {
search_from = after_tag;
continue;
};
let content_start = text.len() - rest.len();
let Some(end_offset) = rest.find("```") else {
search_from = content_start;
continue;
};
let end = content_start + end_offset;
let content = text[content_start..end].trim();
if content.starts_with('{') {
return Some(content.to_string());
}
search_from = end + 3;
}
None
}
fn extract_review_response_object(text: &str) -> Option<ReviewResponse> {
let mut start: Option<usize> = None;
let mut depth: i32 = 0;
let mut in_string = false;
let mut escape = false;
for (idx, ch) in text.char_indices() {
if start.is_some() {
if in_string {
if escape {
escape = false;
} else if ch == '\\' {
escape = true;
} else if ch == '"' {
in_string = false;
}
continue;
}
match ch {
'"' => in_string = true,
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
debug_assert!(
start.is_some(),
"start must be Some when depth returns to 0"
);
let start_idx = start.unwrap_or(0);
let candidate = &text[start_idx..=idx];
if let Ok(resp) = serde_json::from_str::<ReviewResponse>(candidate) {
return Some(resp);
}
start = None;
}
}
_ => {}
}
continue;
}
if ch == '{' {
start = Some(idx);
depth = 1;
in_string = false;
escape = false;
}
}
None
}
#[expect(
clippy::too_many_arguments,
reason = "a memory entry records the task, attempt, agent, model and outcome"
)]
pub async fn store_learnings_from_response(
task_id: &str,
attempt: u32,
agent: &str,
model: Option<&str>,
response: &AgentResponse,
error: Option<&str>,
store: &Option<Arc<TaskStore>>,
repo: &str,
raw_stdout: &str,
) {
let files_modified = if response.files.is_empty() {
let extracted = ndjson::extract_files_changed(raw_stdout);
if !extracted.is_empty() {
tracing::debug!(
task_id,
count = extracted.len(),
"files_modified: fell back to NDJSON extraction (agent reported none)"
);
}
extracted
} else {
response.files.clone()
};
let entry = MemoryEntry {
attempt,
agent: agent.to_string(),
model: model.map(String::from),
learnings: response.learnings.clone(),
error: error.map(String::from),
files_modified,
approach: response.summary.clone(),
timestamp: chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string(),
};
if let Some(st) = store
&& let Ok(Some(store_id)) = st.resolve_task_id(repo, task_id).await
{
match st.append_memory(store_id, &entry).await {
Err(e) => {
tracing::warn!(task_id, error = ?e, "failed to store memory");
}
_ => {
tracing::debug!(task_id, attempt, "stored memory for attempt");
}
}
}
}
pub async fn store_failure_memory(
task_id: &str,
attempt: u32,
agent: &str,
model: Option<&str>,
error: &str,
store: &Option<Arc<TaskStore>>,
repo: &str,
) {
let entry = MemoryEntry {
attempt,
agent: agent.to_string(),
model: model.map(String::from),
learnings: vec![],
error: Some(error.to_string()),
files_modified: vec![],
approach: String::new(),
timestamp: chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string(),
};
if let Some(st) = store
&& let Ok(Some(store_id)) = st.resolve_task_id(repo, task_id).await
{
match st.append_memory(store_id, &entry).await {
Err(e) => {
tracing::warn!(task_id, error = ?e, "failed to store failure memory");
}
_ => {
tracing::debug!(task_id, attempt, "stored failure memory");
}
}
}
}
#[cfg(test)]
mod tests;