From 2150e94cb84c9993155afdafa07707c7ead8a5ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF?= Date: Mon, 21 Sep 2026 23:30:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(AC2):=20total=5Fepisodes=20=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=85=9C=E5=BA=95=E6=94=B9=E5=90=8D=20episodes=5Fcach?= =?UTF-8?q?ed=EF=BC=88t=5Fa838377a=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 真实值),兜底分支不触发。 --- go/internal/api/routes/consolidation_pipe.go | 3 +++ go/internal/api/routes/core.go | 5 +++++ go/internal/api/server.go | 10 ++++++++++ go/internal/storage/lancedb_ipc.go | 7 +++++-- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/go/internal/api/routes/consolidation_pipe.go b/go/internal/api/routes/consolidation_pipe.go index eb28684..3cf6d80 100644 --- a/go/internal/api/routes/consolidation_pipe.go +++ b/go/internal/api/routes/consolidation_pipe.go @@ -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()) diff --git a/go/internal/api/routes/core.go b/go/internal/api/routes/core.go index e25d438..d13b36e 100644 --- a/go/internal/api/routes/core.go +++ b/go/internal/api/routes/core.go @@ -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) } diff --git a/go/internal/api/server.go b/go/internal/api/server.go index da93fd2..9f046d8 100644 --- a/go/internal/api/server.go +++ b/go/internal/api/server.go @@ -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) diff --git a/go/internal/storage/lancedb_ipc.go b/go/internal/storage/lancedb_ipc.go index aa5fccc..9d29cab 100644 --- a/go/internal/storage/lancedb_ipc.go +++ b/go/internal/storage/lancedb_ipc.go @@ -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