Merge pull request #765 from felix307253927/pr-main-0320

fix: Fix the issue of duplicate tool calls with identical arguments i…
This commit is contained in:
Jaber Jaber
2026-03-27 05:34:59 +03:00
committed by GitHub
2 changed files with 17 additions and 3 deletions
+16 -2
View File
@@ -628,7 +628,7 @@ pub async fn run_agent_loop(
// Execute each tool call with loop guard, timeout, and truncation
let mut tool_result_blocks = Vec::new();
for tool_call in &response.tool_calls {
for tool_call in deduplicate_tool_calls(&response) {
// Loop guard check
let verdict = loop_guard.check(&tool_call.name, &tool_call.input);
match &verdict {
@@ -1767,7 +1767,7 @@ pub async fn run_agent_loop_streaming(
// Execute each tool call with loop guard, timeout, and truncation
let mut tool_result_blocks = Vec::new();
for tool_call in &response.tool_calls {
for tool_call in deduplicate_tool_calls(&response) {
// Loop guard check
let verdict = loop_guard.check(&tool_call.name, &tool_call.input);
match &verdict {
@@ -2872,6 +2872,20 @@ fn try_parse_bare_json_tool_call(
parse_json_tool_call_object(&text[..end], tool_names)
}
/// Deduplicate tool calls from the response.
/// Returns a reference to the deduplicated tool calls.
pub fn deduplicate_tool_calls(response: &crate::llm_driver::CompletionResponse) -> Vec<&ToolCall> {
let mut hash_set = std::collections::HashSet::new();
let mut deduplicated = Vec::new();
for tool_call in &response.tool_calls {
let hash = LoopGuard::compute_hash(&tool_call.name, &tool_call.input);
if hash_set.insert(hash) {
deduplicated.push(tool_call);
}
}
deduplicated
}
#[cfg(test)]
mod tests {
use super::*;
+1 -1
View File
@@ -498,7 +498,7 @@ impl LoopGuard {
}
/// Compute a SHA-256 hash of the tool name and parameters.
fn compute_hash(tool_name: &str, params: &serde_json::Value) -> String {
pub fn compute_hash(tool_name: &str, params: &serde_json::Value) -> String {
let mut hasher = Sha256::new();
hasher.update(tool_name.as_bytes());
hasher.update(b"|");