feat: refactor agent step limits and enhance workflow orchestration with new findings tool

This commit is contained in:
asepharyana
2026-07-13 14:39:39 +07:00
parent 0d6f558b2b
commit 3b660e09a8
9 changed files with 229 additions and 100 deletions
-26
View File
@@ -957,16 +957,6 @@ fn archive_message(db: Option<&std::sync::Arc<std::sync::Mutex<rusqlite::Connect
}
}
/// Maximum number of LLM call + tool-execution iterations per single
/// agent turn before bailing. Prevents runaway token consumption when
/// the agent gets stuck in a loop (e.g. an unachievable todo item).
const MAX_TURN_STEPS: usize = 10000;
/// Hard wall-clock timeout per agent turn (5 minutes). Prevents a single
/// user turn from running indefinitely even if the step budget isn't
/// exhausted (e.g. slow LLM responses, stuck tool calls).
const MAX_TURN_TIMEOUT_MS: u64 = 300_000;
/// Maximum number of auto inline reviews spawned per single agent turn.
/// After N edits, the inline review is skipped to keep the turn fast;
/// background subagents still fire at the end of the turn.
@@ -1006,7 +996,6 @@ fn run_agent_turn(
let mut edited_paths: Vec<String> = Vec::new();
let mut inline_reviews_count: usize = 0;
let mut prev_shaped = false;
let turn_start_ms = std::time::Instant::now();
// Build system prompt components once and cache them for the entire turn
// instead of regenerating on every loop iteration (which walks the full
@@ -1141,24 +1130,9 @@ fn run_agent_turn(
return Ok(());
}
let mut turn_step = 0usize;
let mut todo_retry_count = 0usize;
loop {
turn_step += 1;
if turn_step > MAX_TURN_STEPS {
anyhow::bail!(
"turn exceeded maximum steps ({MAX_TURN_STEPS}) — possible runaway loop. \
aborting to prevent excessive token usage",
);
}
if turn_start_ms.elapsed().as_millis() as u64 > MAX_TURN_TIMEOUT_MS {
anyhow::bail!(
"turn exceeded maximum duration ({}s) — aborting. \
Use /compact or shorter prompts if the model needs more time.",
MAX_TURN_TIMEOUT_MS / 1000,
);
}
let total_chars: usize = msgs.iter()
.filter_map(|m| m.content.as_deref())
.map(str::len)