fix(growth): Evolution Engine 审计修复 — 7项全部完成
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
HIGH-1: 提取共享 json_utils.rs,skill_generator/workflow_composer 去重
HIGH-2: FeedbackCollector Vec→HashMap,消除 unwrap() panic 风险
HIGH-3: ProfileUpdater 改为 collect_updates() 返回字段列表,
growth.rs 直接 async 调用 update_field(),不再用 no-op 闭包
MEDIUM-1: EvolutionMiddleware 注入后自动 drain,防止重复注入
MEDIUM-2: PatternAggregator tools 提取改为直接收集 context 值
MEDIUM-3: evolution_engine.rs 移除 4 个未使用 imports
MEDIUM-4: workflow_composer parse_response pattern 参数加下划线
MEDIUM-7: SkillCandidate 添加 version 字段(默认=1)
测试: zclaw-growth 128 tests, zclaw-runtime 86 tests, workspace 0 failures
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
//! 收集用户对进化产物(技能/Pipeline)的显式/隐式反馈
|
||||
//! 管理信任度衰减和优化循环
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -58,21 +60,32 @@ pub struct TrustRecord {
|
||||
|
||||
/// 反馈收集器
|
||||
/// 管理反馈记录和信任度评分
|
||||
/// 内存存储,可持久化到 SQLite(后续版本)
|
||||
pub struct FeedbackCollector {
|
||||
/// 信任度记录表(内存,可持久化到 SQLite)
|
||||
trust_records: Vec<TrustRecord>,
|
||||
trust_records: HashMap<String, TrustRecord>,
|
||||
}
|
||||
|
||||
impl FeedbackCollector {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
trust_records: Vec::new(),
|
||||
trust_records: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 提交一条反馈
|
||||
pub fn submit_feedback(&mut self, entry: FeedbackEntry) -> TrustUpdate {
|
||||
let record = self.get_or_create_record(&entry.artifact_id, &entry.artifact_type);
|
||||
let record = self
|
||||
.trust_records
|
||||
.entry(entry.artifact_id.clone())
|
||||
.or_insert_with(|| TrustRecord {
|
||||
artifact_id: entry.artifact_id.clone(),
|
||||
artifact_type: entry.artifact_type.clone(),
|
||||
trust_score: 0.5,
|
||||
total_feedback: 0,
|
||||
positive_count: 0,
|
||||
negative_count: 0,
|
||||
last_updated: Utc::now(),
|
||||
});
|
||||
|
||||
// 更新计数
|
||||
record.total_feedback += 1;
|
||||
@@ -106,13 +119,13 @@ impl FeedbackCollector {
|
||||
|
||||
/// 获取信任度记录
|
||||
pub fn get_trust(&self, artifact_id: &str) -> Option<&TrustRecord> {
|
||||
self.trust_records.iter().find(|r| r.artifact_id == artifact_id)
|
||||
self.trust_records.get(artifact_id)
|
||||
}
|
||||
|
||||
/// 获取所有需要优化的产物(信任度 < 0.4)
|
||||
pub fn get_artifacts_needing_optimization(&self) -> Vec<&TrustRecord> {
|
||||
self.trust_records
|
||||
.iter()
|
||||
.values()
|
||||
.filter(|r| r.trust_score < 0.4 && r.total_feedback >= 2)
|
||||
.collect()
|
||||
}
|
||||
@@ -120,7 +133,7 @@ impl FeedbackCollector {
|
||||
/// 获取所有应该归档的产物(信任度 < 0.2 且反馈 >= 5)
|
||||
pub fn get_artifacts_to_archive(&self) -> Vec<&TrustRecord> {
|
||||
self.trust_records
|
||||
.iter()
|
||||
.values()
|
||||
.filter(|r| r.trust_score < 0.2 && r.total_feedback >= 5)
|
||||
.collect()
|
||||
}
|
||||
@@ -128,37 +141,11 @@ impl FeedbackCollector {
|
||||
/// 获取所有高信任产物(信任度 >= 0.8)
|
||||
pub fn get_recommended_artifacts(&self) -> Vec<&TrustRecord> {
|
||||
self.trust_records
|
||||
.iter()
|
||||
.values()
|
||||
.filter(|r| r.trust_score >= 0.8)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_or_create_record(
|
||||
&mut self,
|
||||
artifact_id: &str,
|
||||
artifact_type: &EvolutionArtifact,
|
||||
) -> &mut TrustRecord {
|
||||
let exists = self
|
||||
.trust_records
|
||||
.iter()
|
||||
.any(|r| r.artifact_id == artifact_id);
|
||||
if !exists {
|
||||
self.trust_records.push(TrustRecord {
|
||||
artifact_id: artifact_id.to_string(),
|
||||
artifact_type: artifact_type.clone(),
|
||||
trust_score: 0.5, // 初始信任度
|
||||
total_feedback: 0,
|
||||
positive_count: 0,
|
||||
negative_count: 0,
|
||||
last_updated: Utc::now(),
|
||||
});
|
||||
}
|
||||
self.trust_records
|
||||
.iter_mut()
|
||||
.find(|r| r.artifact_id == artifact_id)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn calculate_trust_internal(
|
||||
positive: u32,
|
||||
negative: u32,
|
||||
@@ -268,7 +255,6 @@ mod tests {
|
||||
#[test]
|
||||
fn test_recommend_optimize() {
|
||||
let mut collector = FeedbackCollector::new();
|
||||
// 2 negative → trust < 0.4
|
||||
collector.submit_feedback(make_feedback("skill-1", Sentiment::Negative));
|
||||
let update = collector.submit_feedback(make_feedback("skill-1", Sentiment::Negative));
|
||||
assert_eq!(update.action, RecommendedAction::Optimize);
|
||||
|
||||
Reference in New Issue
Block a user