## 锁迁移:std::sync → parking_lot 消除锁中毒(poison)导致的级联崩溃风险。parking_lot 锁不会中毒, 且性能更优。迁移覆盖全部生产代码: - experts/mod.rs: 4 RwLock + 13 expect - skills/mod.rs: 1 RwLock + 11 expect - tools/registry.rs: 1 RwLock + 2 expect - gateway/model_selection.rs: 1 RwLock + 2 expect - tools/task/repository.rs: 1 RwLock + 4 unwrap - tools/task/runtime.rs: 2 RwLock + 17 expect - gateway/session.rs + task/runtime.rs: stream_message_id Mutex - gateway/processor.rs: description_generation_in_flight Mutex - command/handler.rs + help.rs: metadata Mutex(公开 API) - mcp/client.rs: stderr_lines Mutex 测试代码中的 std::sync::Mutex(串行化锁 + TestObserver)有意保留, 已通过 unwrap_or_else(|err| err.into_inner()) 做中毒恢复。 ## P1: 阻塞 IO 迁移到 spawn_blocking 将 3 处阻塞 async worker 的操作迁移到 blocking 线程池: - file_read.rs: read_to_string + 行处理 + base64 编码整体包入 spawn_blocking - agent_loop.rs: 新增 preencode_images_for_request 两阶段预编码 (顺序分配预算 → 并行 spawn_blocking 编码),build_llm_request 改为 async - wechat.rs: media_to_send_content 改为 async,std::fs::read 用 spawn_blocking 包裹 ## P2: session_history topic_histories 内存上限 新增 MAX_CACHED_TOPICS=32 上限和 evict_inactive_if_needed 方法。 超限时驱逐非活跃 topic(不在 chat_topic_ids、不在 compression_in_flight、 serial_lock 未被持有)。活跃 topic 永不误驱逐。 remove_history 同步清理 topic_serial_locks,防止无限增长。 ## P3: 减少 panic 面 agent_loop.rs retry 循环的 response.expect(...) 改为 ok_or_else(...)? 返回 AgentError::Other,逻辑 bug 不再导致整个 agent 崩溃。 ## 对抗性审查修复 - preencode_images_for_request: 用 seen HashSet 去重,防止同 path 重复 编码导致 HashMap entry 覆盖(NoBudget 覆盖 Encoded 等) - evict_inactive_if_needed: 检查 topic_serial_lock.try_lock(),防止驱逐 正在 agent 处理中的 topic(original_topic_id 不在 chat_topic_ids 但 agent 仍持锁) - remove_history: 清理 topic_serial_locks ## 验证 - cargo check: 通过(仅既有 lifetime 警告) - cargo test: 559 passed / 3 failed(均为环境/sandbox 权限问题,与本次改动无关)
75 lines
2.2 KiB
Rust
75 lines
2.2 KiB
Rust
use std::collections::HashMap;
|
|
use parking_lot::RwLock;
|
|
|
|
/// per-session 的用户模型覆盖选择存储。
|
|
///
|
|
/// 与 ExpertRuntime 的 session_experts 平级独立,职责单一:
|
|
/// 只负责存储 session_id -> (provider, model) 的映射,不依赖任何业务模块。
|
|
#[derive(Debug, Default)]
|
|
pub struct ModelSelectionStore {
|
|
selections: RwLock<HashMap<String, (Option<String>, Option<String>)>>,
|
|
}
|
|
|
|
impl ModelSelectionStore {
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
/// 设置 session 的用户模型覆盖。provider 和 model 均为 None 时清除该 session 的选择。
|
|
pub fn set(&self, session_id: &str, provider: Option<String>, model: Option<String>) {
|
|
let mut selections = self
|
|
.selections
|
|
.write();
|
|
if provider.is_none() && model.is_none() {
|
|
selections.remove(session_id);
|
|
} else {
|
|
selections.insert(session_id.to_string(), (provider, model));
|
|
}
|
|
}
|
|
|
|
/// 读取 session 的用户模型覆盖。
|
|
pub fn get(&self, session_id: &str) -> Option<(Option<String>, Option<String>)> {
|
|
self.selections
|
|
.read()
|
|
.get(session_id)
|
|
.cloned()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn set_and_get() {
|
|
let store = ModelSelectionStore::new();
|
|
store.set("s1", Some("p1".to_string()), Some("m1".to_string()));
|
|
assert_eq!(
|
|
store.get("s1"),
|
|
Some((Some("p1".to_string()), Some("m1".to_string())))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn get_missing_returns_none() {
|
|
let store = ModelSelectionStore::new();
|
|
assert_eq!(store.get("missing"), None);
|
|
}
|
|
|
|
#[test]
|
|
fn set_none_none_removes_entry() {
|
|
let store = ModelSelectionStore::new();
|
|
store.set("s1", Some("p1".to_string()), Some("m1".to_string()));
|
|
assert!(store.get("s1").is_some());
|
|
store.set("s1", None, None);
|
|
assert!(store.get("s1").is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn set_only_provider_keeps_entry() {
|
|
let store = ModelSelectionStore::new();
|
|
store.set("s1", Some("p1".to_string()), None);
|
|
assert_eq!(store.get("s1"), Some((Some("p1".to_string()), None)));
|
|
}
|
|
}
|