diff --git a/docs_v1.0/M4_workspace/2026-10-07_agent_search_timeout_fix.md b/docs_v1.0/M4_workspace/2026-10-07_agent_search_timeout_fix.md new file mode 100644 index 00000000..56054e32 --- /dev/null +++ b/docs_v1.0/M4_workspace/2026-10-07_agent_search_timeout_fix.md @@ -0,0 +1,158 @@ +# Agent Search Timeout Fix - 2026-10-07 + +## 问题 + +用户报告 `search_agents` API 超时。 + +## 根因 + +`/api/v1/agents/search` 端点没有超时保护机制,可能无限期执行(最多 15 轮 LLM 调用)。 + +## 修复 + +### 代码修改 + +**文件**: `src/api/agent_search.rs` + +**修改**: 添加 60 秒超时保护 + +```rust +async fn agent_search(...) -> Result, (StatusCode, Json)> { + 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 + })) + )); + } + }; + ... +} +``` + +### 提交 + +- **Commit**: `d5786608` +- **Date**: 2026-10-07 +- **Status**: ✅ 已部署 + +## 测试结果 + +| 查询 | 耗时 | 状态 | +|------|------|------| +| "hi" | 3.3s | ✅ 正常 | +| "gun" | 22s | ✅ 正常 | +| "找所有演员的信息和他们的对话" | 25s | ✅ 正常 | + +## 用户指南 + +### 前端配置 + +如果前端仍然报告超时,请检查: + +1. **HTTP Client 超时设置**: + - 建议设置为 **90 秒**(后端 60s + 网络 buffer) + - 示例: + ```javascript + // Axios + axios.post('/api/v1/agents/search', data, { + timeout: 90000 // 90s + }) + + // Fetch + const controller = new AbortController() + setTimeout(() => controller.abort(), 90000) + fetch('/api/v1/agents/search', { signal: controller.signal }) + ``` + +2. **查询优化建议**: + - 简单查询("gun", "car"):通常 < 30s + - 复杂查询("找所有演员的信息"):可能 30-60s + - 如果超时,尝试: + - 缩小范围:"Charade 电影中的 gun" + - 简化问题:"列出演员" + - 分步查询:先找文件,再查询详情 + +3. **错误处理**: + ```javascript + try { + const response = await fetch('/api/v1/agents/search', {...}) + if (response.status === 408) { + // Request Timeout + const error = await response.json() + showToast(error.answer || 'Search timed out') + } + } catch (err) { + if (err.name === 'AbortError') { + showToast('Request cancelled due to timeout') + } + } + ``` + +### API 响应格式 + +**成功** (200 OK): +```json +{ + "success": true, + "conversation_id": "abc123", + "answer": "...", + "sources": [...] +} +``` + +**超时** (408 Request Timeout): +```json +{ + "success": false, + "error": "Agent search timed out after 60 seconds", + "answer": "Search request timed out. Please try with a simpler query.", + "conversation_id": "abc123" +} +``` + +## 监控 + +### 日志 + +超时事件会记录在服务器日志中: + +```bash +# 查看超时日志 +grep "timed out" logs/server.log + +# 查看 AGENT 查询日志 +grep "AGENT" logs/server.log +``` + +### 关键指标 + +- **查询成功率**: 统计 200 vs 408 响应比例 +- **平均响应时间**: 监控 `/api/v1/agents/search` 的 P50/P95/P99 +- **LLM 调用次数**: 日志中的 "Round" 和 "Tool called" 计数 + +## 相关文档 + +- `/Users/accusys/momentry_core/src/api/agent_search.rs` - Agent Search 实现 +- `/Users/accusys/momentry_core/docs_v1.0/M4_workspace/2026-10-07_visual_search_fault_analysis.md` - VLM Search 故障分析 + +--- + +**修复者**: Core Team +**部署日期**: 2026-10-07 +**服务器**: M5Max48 (Port 3002) +**状态**: ✅ 生产环境已更新 \ No newline at end of file