Files
memoryweave/go/internal/selfoptimize/quality_monitor.go
T
xiaowei 945e5dfc66 feat: 自优化5项全链路补齐
P1: GapDetector Redis 持久化
- 13个缺口字段序列化为 JSON 写入 zhiyi:gaps HASH
- misses 计数器写入 zhiyi:gap_misses
- RecordMiss/Close 后自动 persist + 启动时 loadFromRedis
- 重启不丢缺口记录

P2: VPropagator Redis 持久化 + useful/not-useful 接入
- 决策记录写入 zhiyi:vprop_decisions HASH
- V 值写入 zhiyi:vprop_values HASH
- RecordDecision 后自动 persist
- feedback/useful → success, feedback/not-useful → failure

P3: 中文分类改进
- classifyGap 降级时检测中文同义词标记(又称/别名/aka)
- 含标记的 query 归为 GapSynonym(B) 而非 GapUnknown(A)

P4: QualityDropMonitor 阈值 5→2 + 蒸馏质量分
- minFeedbacks 从 5 降至 2,更早触发质量监控

P5: CoOccurTracker Redis 持久化
- RedisCoOccurPersist() 写入 zhiyi:cooccur HASH
- RecordRecall 中调用 ct.persist() 持久化每条边
- recall pipeline 中独立 goroutine 触发共访追踪
- LoadCoOccurFromRedis() 启动时加载

测试: 全部 5 项重启验证通过
2026-05-30 12:01:37 +08:00

105 lines
2.5 KiB
Go

// 织忆 MemoryWeave — 质量下降自动动作
// quality_score < 0.3 → 自动降权 → 7天未改善 → deprecated → tombstones
package selfoptimize
import (
"sync"
"time"
)
// QualityDropMonitor 质量下降监控器
type QualityDropMonitor struct {
mu sync.Mutex
// memoryID → 质量下降记录
dropped map[string]*DropRecord
// 阈值
lowThreshold float64 // < 0.3 → 触发
minFeedbacks int // ≥ 2 次反馈 → 有效(降低阈值从 5→2)
deprecationDays int // 7 天未改善 → deprecated
}
type DropRecord struct {
MemoryID string `json:"memory_id"`
Score float64 `json:"score"`
NotifiedAt time.Time `json:"notified_at"`
DaysSinceDrop int `json:"days_since_drop"`
Status string `json:"status"` // notified / deprecating / deprecated
}
var QualityMonitor = &QualityDropMonitor{
dropped: make(map[string]*DropRecord),
lowThreshold: 0.3,
minFeedbacks: 2,
deprecationDays: 7,
}
// Check 检查质量分数
// 返回: 是否需要通知
func (qm *QualityDropMonitor) Check(memoryID string, score float64, feedbackCount int) *DropRecord {
qm.mu.Lock()
defer qm.mu.Unlock()
// 不满足阈值 → 清除记录
if score >= qm.lowThreshold || feedbackCount < qm.minFeedbacks {
delete(qm.dropped, memoryID)
return nil
}
record, exists := qm.dropped[memoryID]
if !exists {
record = &DropRecord{
MemoryID: memoryID,
Score: score,
NotifiedAt: time.Now(),
Status: "notified",
}
qm.dropped[memoryID] = record
return record
}
// 更新分数
record.Score = score
record.DaysSinceDrop = int(time.Since(record.NotifiedAt).Hours() / 24)
// 7 天未改善 → 标记 deprecated
if record.DaysSinceDrop >= qm.deprecationDays {
record.Status = "deprecating"
}
return record
}
// MarkDeprecated 标记为已淘汰
func (qm *QualityDropMonitor) MarkDeprecated(memoryID string) {
qm.mu.Lock()
defer qm.mu.Unlock()
if record, ok := qm.dropped[memoryID]; ok {
record.Status = "deprecated"
}
}
// GetDeprecating 获取待淘汰列表(用于批量淘汰)
func (qm *QualityDropMonitor) GetDeprecating() []*DropRecord {
qm.mu.Lock()
defer qm.mu.Unlock()
var list []*DropRecord
for id, record := range qm.dropped {
if record.Status == "deprecating" {
list = append(list, record)
_ = id
}
}
return list
}
// ActiveAlerts 活跃告警数
func (qm *QualityDropMonitor) ActiveAlerts() int {
qm.mu.Lock()
defer qm.mu.Unlock()
return len(qm.dropped)
}