fix(core): 修复 v0.3.0 审查发现的 6 项警告问题
- InMemoryGraph 的 10 处 lock().unwrap() 改为错误传播,避免 Mutex poison 时 panic - SubTaskResult 添加 Serialize/Deserialize,支持结果序列化 - dispatch_all 移除多余的 task.clone() - truncate_total_chars 补充字节切片安全性注释 - SessionManager::children 添加 ponytail 注释标记 O(N) 扫描现状 - SessionMemoryEntry.created_at 添加 i64 类型说明注释
This commit is contained in:
+30
-10
@@ -253,7 +253,9 @@ impl KnowledgeGraph for InMemoryGraph {
|
||||
if entity.id.is_empty() {
|
||||
return Err(MemoryError::InvalidInput("entity id is empty".into()));
|
||||
}
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
let mut inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
// upsert:若已存在,先收集旧标签用于清理反向引用(避免同时借用 entities 和 tag_index)
|
||||
let old_tags: Vec<String> = inner
|
||||
.entities
|
||||
@@ -281,12 +283,16 @@ impl KnowledgeGraph for InMemoryGraph {
|
||||
}
|
||||
|
||||
async fn get_entity(&self, id: &str) -> Result<Option<GraphEntity>, MemoryError> {
|
||||
let inner = self.inner.lock().unwrap();
|
||||
let inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
Ok(inner.entities.get(id).cloned())
|
||||
}
|
||||
|
||||
async fn remove_entity(&self, id: &str) -> Result<(), MemoryError> {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
let mut inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
// 移除实体并清理其标签反向引用
|
||||
if let Some(entity) = inner.entities.remove(id) {
|
||||
for tag in &entity.tags {
|
||||
@@ -306,7 +312,9 @@ impl KnowledgeGraph for InMemoryGraph {
|
||||
}
|
||||
|
||||
async fn add_relation(&self, relation: GraphRelation) -> Result<(), MemoryError> {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
let mut inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
// 校验两端实体存在
|
||||
if !inner.entities.contains_key(&relation.source_id) {
|
||||
return Err(MemoryError::InvalidInput(format!(
|
||||
@@ -340,7 +348,9 @@ impl KnowledgeGraph for InMemoryGraph {
|
||||
target_id: &str,
|
||||
relation_type: &str,
|
||||
) -> Result<(), MemoryError> {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
let mut inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
inner.relations.retain(|r| {
|
||||
!(r.source_id == source_id && r.target_id == target_id && r.relation_type == relation_type)
|
||||
});
|
||||
@@ -354,7 +364,9 @@ impl KnowledgeGraph for InMemoryGraph {
|
||||
direction: RelationDirection,
|
||||
relation_types: Option<&[&str]>,
|
||||
) -> Result<Vec<ScoredEntity>, MemoryError> {
|
||||
let inner = self.inner.lock().unwrap();
|
||||
let inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
|
||||
// 1. 验证起点存在
|
||||
if !inner.entities.contains_key(entity_id) {
|
||||
@@ -458,7 +470,9 @@ impl KnowledgeGraph for InMemoryGraph {
|
||||
}
|
||||
|
||||
async fn find_by_keywords(&self, keywords: &[String]) -> Result<Vec<GraphEntity>, MemoryError> {
|
||||
let inner = self.inner.lock().unwrap();
|
||||
let inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
if keywords.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
@@ -481,7 +495,9 @@ impl KnowledgeGraph for InMemoryGraph {
|
||||
}
|
||||
|
||||
async fn find_tags(&self, prefix: &str) -> Result<Vec<String>, MemoryError> {
|
||||
let inner = self.inner.lock().unwrap();
|
||||
let inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
let prefix_l = prefix.to_lowercase();
|
||||
let mut tags: Vec<String> = inner
|
||||
.tag_index
|
||||
@@ -498,7 +514,9 @@ impl KnowledgeGraph for InMemoryGraph {
|
||||
entity_id: &str,
|
||||
tags: Vec<String>,
|
||||
) -> Result<usize, MemoryError> {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
let mut inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
|
||||
// 先收集旧标签(避免同时借用 entities 和 tag_index)
|
||||
let old_tags: Vec<String> = inner
|
||||
@@ -536,7 +554,9 @@ impl KnowledgeGraph for InMemoryGraph {
|
||||
}
|
||||
|
||||
async fn entity_count_by_tag(&self, tag: &str) -> Result<usize, MemoryError> {
|
||||
let inner = self.inner.lock().unwrap();
|
||||
let inner = self.inner.lock().map_err(|e| {
|
||||
MemoryError::RetrievalError(format!("lock poisoned: {e}"))
|
||||
})?;
|
||||
Ok(inner
|
||||
.tag_index
|
||||
.get(tag)
|
||||
|
||||
Reference in New Issue
Block a user