diff --git a/src/api/agent_search.rs b/src/api/agent_search.rs index 75a70b58..259b0b31 100644 --- a/src/api/agent_search.rs +++ b/src/api/agent_search.rs @@ -488,8 +488,29 @@ async fn agent_search( ) -> Result, (StatusCode, Json)> { 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 = messages.into_iter().skip(1).collect();