fix: add 60s timeout to agent search

- Wrap run_tool_loop in tokio::time::timeout
- Return 408 Request Timeout with JSON error
- Prevent infinite loops from blocking server
- Log timeout events for monitoring

Fixes: search_agents timeout issue
This commit is contained in:
Accusys
2026-10-07 18:19:13 +08:00
parent 9596cec7ba
commit d5786608f0
+23 -2
View File
@@ -488,8 +488,29 @@ async fn agent_search(
) -> Result<Json<AgentSearchResponse>, (StatusCode, Json<serde_json::Value>)> {
let (conv_id, history) = get_or_create_conv(req.conversation_id.as_deref());
let (answer, messages, sources) =
run_tool_loop(state.db.pool(), SYSTEM_PROMPT, &req.query, history).await;
// Add 60s timeout for the entire agent search
let timeout_duration = std::time::Duration::from_secs(60);
let result = tokio::time::timeout(
timeout_duration,
run_tool_loop(state.db.pool(), SYSTEM_PROMPT, &req.query, history)
).await;
let (answer, messages, sources) = match result {
Ok((answer, messages, sources)) => (answer, messages, sources),
Err(_) => {
tracing::error!("[AGENT] Search timed out after {}s", timeout_duration.as_secs());
return Err((
StatusCode::REQUEST_TIMEOUT,
Json(serde_json::json!({
"success": false,
"error": format!("Agent search timed out after {} seconds", timeout_duration.as_secs()),
"answer": "Search request timed out. Please try with a simpler query.",
"conversation_id": conv_id
}))
));
}
};
// Save messages (skip system prompt — build_conversation re-adds it)
let history: Vec<ChatMessage> = messages.into_iter().skip(1).collect();