feat: 自优化4大链路补齐

1. GapDetector 自动接入 recall 路径
   - MissRecorder callback → RecallPipeline
   - 0 结果时自动 RecordMiss(query)
   - server.go 中 gapDetector 初始化后挂载

2. Dashboard 持久化到 Redis
   - EnableRedisPersistence() 启动时加载
   - persist() 每次 mutate 后同步 13 个计数器到 Redis HASH
   - 重启不丢指标

3. PassiveValidator 接入 commit 路径
   - 新记忆提交后自动 P1/P2/P3 匹配验证
   - 复用 Validator.Validate 逻辑

4. VPropagator 接入 feedback/correct
   - 用户修正时记录决策 V 值
   - 使用 user_correct 来源 + success 结果

Closes: 10% 缺失代码 + 40% 缺失链路
This commit is contained in:
xiaowei
2026-05-30 11:27:27 +08:00
parent 064046f6da
commit bceb5fb059
5 changed files with 126 additions and 2 deletions
+80 -2
View File
@@ -3,12 +3,79 @@ package selfoptimize
import (
"math"
"strconv"
"sync"
"time"
"github.com/xiaoxue/memoryweave/internal/storage"
)
// Redis persistence key
const dashboardRedisKey = "zhiyi:dashboard"
// redisPersistence 是否开启了 Redis 持久化(由 server.go 在启动时设置)
var redisPersistence *storage.RedisClient
// EnableRedisPersistence 启动 Dashboard 持久化(从 Redis 加载 + 每次变更同步)
func EnableRedisPersistence() {
rc := storage.GetRedisClient()
if rc == nil {
return
}
redisPersistence = rc
_ = loadDashboard()
}
// persist 写当前 Dashboard 到 Redis HASH
// 注意:调用方必须已持有 d.mu 锁(RLock 或 Lock)
func (d *Dashboard) persist() {
if redisPersistence == nil {
return
}
_ = redisPersistence.HSet(dashboardRedisKey, "useful_count", strconv.Itoa(d.UsefulCount))
_ = redisPersistence.HSet(dashboardRedisKey, "not_useful_count", strconv.Itoa(d.NotUsefulCount))
_ = redisPersistence.HSet(dashboardRedisKey, "total_recalls", strconv.Itoa(d.TotalRecalls))
_ = redisPersistence.HSet(dashboardRedisKey, "hit_count", strconv.Itoa(d.HitCount))
_ = redisPersistence.HSet(dashboardRedisKey, "closed_gaps", strconv.Itoa(d.ClosedGaps))
_ = redisPersistence.HSet(dashboardRedisKey, "total_gaps", strconv.Itoa(d.TotalGaps))
_ = redisPersistence.HSet(dashboardRedisKey, "cascade_fixed_total", strconv.Itoa(d.CascadeFixedTotal))
_ = redisPersistence.HSet(dashboardRedisKey, "total_fixes", strconv.Itoa(d.TotalFixes))
_ = redisPersistence.HSet(dashboardRedisKey, "deprecated_today", strconv.Itoa(d.DeprecatedToday))
_ = redisPersistence.HSet(dashboardRedisKey, "distill_loss_sum", strconv.FormatFloat(d.DistillLossSum, 'f', 4, 64))
_ = redisPersistence.HSet(dashboardRedisKey, "distill_loss_count", strconv.Itoa(d.DistillLossCount))
_ = redisPersistence.HSet(dashboardRedisKey, "auto_resolved_conflicts", strconv.Itoa(d.AutoResolvedConflicts))
_ = redisPersistence.HSet(dashboardRedisKey, "total_conflicts", strconv.Itoa(d.TotalConflicts))
_ = redisPersistence.Expire(dashboardRedisKey, 90*24*time.Hour) // 90 天 TTL
}
// loadDashboard 从 Redis 加载 Dashboard 数据
func loadDashboard() error {
if redisPersistence == nil {
return nil
}
data, err := redisPersistence.HGetAll(dashboardRedisKey)
if err != nil || len(data) == 0 {
return err
}
Dash.mu.Lock()
defer Dash.mu.Unlock()
if v, err := strconv.Atoi(data["useful_count"]); err == nil { Dash.UsefulCount = v }
if v, err := strconv.Atoi(data["not_useful_count"]); err == nil { Dash.NotUsefulCount = v }
if v, err := strconv.Atoi(data["total_recalls"]); err == nil { Dash.TotalRecalls = v }
if v, err := strconv.Atoi(data["hit_count"]); err == nil { Dash.HitCount = v }
if v, err := strconv.Atoi(data["closed_gaps"]); err == nil { Dash.ClosedGaps = v }
if v, err := strconv.Atoi(data["total_gaps"]); err == nil { Dash.TotalGaps = v }
if v, err := strconv.Atoi(data["cascade_fixed_total"]); err == nil { Dash.CascadeFixedTotal = v }
if v, err := strconv.Atoi(data["total_fixes"]); err == nil { Dash.TotalFixes = v }
if v, err := strconv.Atoi(data["deprecated_today"]); err == nil { Dash.DeprecatedToday = v }
if v, err := strconv.ParseFloat(data["distill_loss_sum"], 64); err == nil { Dash.DistillLossSum = v }
if v, err := strconv.Atoi(data["distill_loss_count"]); err == nil { Dash.DistillLossCount = v }
if v, err := strconv.Atoi(data["auto_resolved_conflicts"]); err == nil { Dash.AutoResolvedConflicts = v }
if v, err := strconv.Atoi(data["total_conflicts"]); err == nil { Dash.TotalConflicts = v }
return nil
}
// ─── 自优化仪表盘 ────────────────────────────────────────
type Dashboard struct {
@@ -83,6 +150,7 @@ func (d *Dashboard) RecordRecall(hit bool) {
if hit {
d.HitCount++
}
d.persist()
}
func (d *Dashboard) RecordFeedback(useful bool) {
@@ -96,10 +164,16 @@ func (d *Dashboard) RecordFeedback(useful bool) {
}
// RecordUseful 记录有用反馈
func (d *Dashboard) RecordUseful() { d.RecordFeedback(true) }
func (d *Dashboard) RecordUseful() {
d.RecordFeedback(true)
d.persist()
}
// RecordNotUseful 记录无用反馈
func (d *Dashboard) RecordNotUseful() { d.RecordFeedback(false) }
func (d *Dashboard) RecordNotUseful() {
d.RecordFeedback(false)
d.persist()
}
// QualityScore 计算当前质量分数
func (d *Dashboard) QualityScore() float64 {
@@ -380,6 +454,7 @@ func (d *Dashboard) RecordDeprecation() {
d.mu.Lock()
defer d.mu.Unlock()
d.DeprecatedToday++
d.persist()
}
// RecordCorrection 记录用户修正
@@ -390,6 +465,7 @@ func (d *Dashboard) RecordCorrection(source string) {
if source == "muchen_correction" {
d.CascadeFixedTotal++
}
d.persist()
}
// RecordGapClosed 记录缺口关闭
@@ -397,6 +473,7 @@ func (d *Dashboard) RecordGapClosed() {
d.mu.Lock()
defer d.mu.Unlock()
d.ClosedGaps++
d.persist()
}
// RecordConflictResolved 记录冲突解决
@@ -407,6 +484,7 @@ func (d *Dashboard) RecordConflictResolved(auto bool) {
if auto {
d.AutoResolvedConflicts++
}
d.persist()
}
func (pg *PrefetchGraph) GetPrefetch(query string) []string {