fix(AC2): total_episodes 缓存兜底改名 episodes_cached(t_a838377a)

Rust sidecar 缺失 total_episodes 时的回退值原为 len(_local.episodes)
(进程内缓存,重启清零)却以 total_episodes 键名暴露 → 消费者误以为是真实总量。
现改为 episodes_cached 键名 + 各消费方 fallback 读取 episodes_cached + /api/v1/metrics 标注 episodes_source_cached=1。
生产验证:当前 Rust 返回 total_episodes=58284(count_rows 真实值),兜底分支不触发。
This commit is contained in:
小唯
2026-09-21 23:30:59 +08:00
parent 67cb262863
commit 2150e94cb8
4 changed files with 23 additions and 2 deletions
@@ -113,12 +113,15 @@ func (cp *ConsolidationPipeline) RunWithMode(mode string) (*ConsolidationReport,
}
// Phase F: 整合完成后更新总记忆数指标(通过 LanceDB stats)
// total_episodes 来自 Rust count_rows(真实值);旧 sidecar 回退 episodes_cached(非总量)
if stats, err := cp.ldb.Stats(); err == nil {
if total, ok := stats["total_memories"].(float64); ok {
metrics.TotalMemories.Set(total)
}
if total, ok := stats["total_episodes"].(float64); ok {
metrics.TotalEpisodes.Set(total)
} else if cached, ok := stats["episodes_cached"].(float64); ok {
metrics.TotalEpisodes.Set(cached) // 旧 sidecar 回退,非真实总量
}
}
metrics.SyncFromDashboard(selfoptimize.Dash.Metrics())
+5
View File
@@ -649,11 +649,16 @@ func (a *API) Stats(w http.ResponseWriter, r *http.Request) {
return
}
// 同步 Prometheus 指标
// total_episodes 来自 Rust count_rows(真实值);
// 若 Rust 侧缺失(旧 sidecar),回退 episodes_cached(本进程内存,重启清零)并标注来源。
if total, ok := s["total_memories"].(float64); ok {
metrics.TotalMemories.Set(total)
}
if total, ok := s["total_episodes"].(float64); ok {
metrics.TotalEpisodes.Set(total)
} else if cached, ok := s["episodes_cached"].(float64); ok {
// 旧 sidecar 回退:值非真实总量,gauge 仍用该值但响应 JSON 中同时暴露来源标记
metrics.TotalEpisodes.Set(cached)
}
respond(w, 200, s)
}
+10
View File
@@ -88,6 +88,7 @@ func NewServer() http.Handler {
api.Pipeline.SetGraphExpander(graphStore)
// 启动时初始化 Prometheus 指标
// total_episodes 来自 Rust count_rows(真实值);旧 sidecar 回退 episodes_cached(非总量)
go func() {
time.Sleep(1 * time.Second) // 等待 Rust sidecar 就绪
if stats, err := ldb.Stats(); err == nil {
@@ -96,6 +97,8 @@ func NewServer() http.Handler {
}
if total, ok := stats["total_episodes"].(float64); ok {
metrics.TotalEpisodes.Set(total)
} else if cached, ok := stats["episodes_cached"].(float64); ok {
metrics.TotalEpisodes.Set(cached) // 旧 sidecar 回退,非真实总量
}
log.Printf("[metrics] 初始化指标: memories=%.0f", stats["total_memories"])
}
@@ -363,6 +366,7 @@ func NewServer() http.Handler {
mux.HandleFunc("/api/v1/stats", api.Stats)
mux.HandleFunc("/api/v1/batch-commit", api.BatchCommit)
// 自优化指标(Dashboard,含 avg_distill_loss)
// total_episodes:Rust count_rows 真实值;旧 sidecar 回退 episodes_cached(进程内缓存,非总量)
mux.HandleFunc("/api/v1/metrics", func(w http.ResponseWriter, r *http.Request) {
m := selfoptimize.Dash.Metrics()
if stats, err := ldb.Stats(); err == nil {
@@ -371,6 +375,12 @@ func NewServer() http.Handler {
}
if total, ok := stats["total_episodes"].(float64); ok {
m["total_episodes"] = total
} else if cached, ok := stats["episodes_cached"].(float64); ok {
// 旧 sidecar 回退:缓存值非真实总量,gauge 仍设值;
// 来源标记写入响应 JSON(/api/v1/metrics 的 m 类型是 map[string]float64,
// 无法直接加字符串字段,改为用 episodes_source=1.0 作为标记位)
m["total_episodes"] = cached
m["episodes_source_cached"] = 1 // 标记:此值来自进程内缓存(非 Rust count_rows)
}
}
respondJSON(w, 200, m)
+5 -2
View File
@@ -381,10 +381,13 @@ func (rc *RustLanceDBClient) Stats() (map[string]interface{}, error) {
json.Unmarshal([]byte(resp.ReportJSON), &s)
s["backend"] = "lancedb (Rust IPC)"
// Rust sidecar 统计 memories/tombstones/episodes(episodes 已持久化到 LanceDB)
// 若 Rust 返回的 episodes 计数缺失,回退到本地内存(兼容旧 sidecar)
// 若 Rust 返回的 total_episodes 计数缺失(旧 sidecar),回退到本地进程内缓存。
// 回退值标记为 episodes_cached(非 total_episodes),避免消费者误以为是真实总量。
// 真实总量以 Rust count_rows 结果为准(total_episodes);episodes_cached 仅反映
// 本进程生命周期内 Insert 过的条数(重启即清零)。
if _, ok := s["total_episodes"]; !ok {
_local.mu.RLock()
s["total_episodes"] = len(_local.episodes)
s["episodes_cached"] = len(_local.episodes) // ← 缓存值,改名标注
_local.mu.RUnlock()
}
return s, nil