feat(selfcheck): pattern 段可见化 —— 零产出可被机器读到(写入端 0 生产者事故)
- governance.GraphStore 增 CountNodesByType(total, withID, lastUpdated),SQLite/内存/缓存三实现 - selfcheck cheap 增 pattern 段:rows / rows_with_id / last_produced_at / verdict verdict ∈ producing|zero_output|no_id|unknown_age|no_graph_store|unsupported - 实测:rows=0 verdict=zero_output(与 graph.db 只读复核一致),cheap 22ms - 动机:pattern 召回 0 命中时只能靠翻 7 天日志才发现「实现了≠出过数」
This commit is contained in:
@@ -145,6 +145,13 @@ func (a *API) Selfcheck(w http.ResponseWriter, r *http.Request) {
|
||||
degraded = append(degraded, "graph: store not initialised")
|
||||
}
|
||||
|
||||
// ── pattern 召回的两侧健康(2026-10-26 新增)──────────
|
||||
// 事故背景:`POST /api/v1/patterns/detect` 是短命单发端点,全库 0 cron、0 调用方,
|
||||
// 于是 graph_nodes(type='pattern') 恒 0 行、recall 恒 0 命中,而日志层面完全沉默
|
||||
// (只能靠人翻 7 天日志才发现「实现了 ≠ 出过数」)。这里把「写入端有没有产数」
|
||||
// 变成一次 GET 就能读到的事实:rows=0 ⇒ 生产者坏了或从未接线,别去查读侧 SQL。
|
||||
rep["pattern"] = patternHealth(a)
|
||||
|
||||
// ── 面板指标(反馈/蒸馏/gap/cascade,含 _valid/_samples 语义)──
|
||||
rep["dashboard"] = selfoptimize.Dash.Metrics()
|
||||
|
||||
@@ -283,3 +290,38 @@ func round4(f float64) float64 {
|
||||
}
|
||||
return math.Round(f*10000) / 10000
|
||||
}
|
||||
|
||||
// patternHealth 一眼判定 pattern 召回是「有生产者」还是「恒 0」。
|
||||
//
|
||||
// 为什么需要它:2026-10-26 排查发现 pattern 召回 0 命中,真根因是写入端从未产数
|
||||
// (detect 端点无 cron、无调用方),但表面症状看起来像读侧 SQL bug —— 白查一轮方向。
|
||||
// 判据三类:
|
||||
//
|
||||
// producing 有行、有 id、有生产时间 → 读侧再 0 命中就该查 Match 阈值/properties;
|
||||
// zero_output 0 行 → 生产端问题,别去动读侧 SQL;
|
||||
// no_id 有行但 id 全空 → store.ListPatterns 会全 skip(存量冻结的典型形态)。
|
||||
func patternHealth(a *API) map[string]interface{} {
|
||||
out := map[string]interface{}{
|
||||
"rows": -1, // -1 = 未知,别当 0 用
|
||||
"rows_with_id": -1,
|
||||
"last_produced_at": "",
|
||||
"verdict": "unknown",
|
||||
}
|
||||
if a == nil || a.GraphStore == nil {
|
||||
out["verdict"] = "no_graph_store"
|
||||
return out
|
||||
}
|
||||
rows, withID, last := a.GraphStore.CountNodesByType("pattern")
|
||||
out["rows"], out["rows_with_id"], out["last_produced_at"] = rows, withID, last
|
||||
switch {
|
||||
case rows == 0:
|
||||
out["verdict"] = "zero_output"
|
||||
case withID == 0:
|
||||
out["verdict"] = "no_id"
|
||||
case last == "":
|
||||
out["verdict"] = "unknown_age"
|
||||
default:
|
||||
out["verdict"] = "producing"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -80,10 +80,14 @@ func (m *mockLance) IncrementUseful(id string) error { r
|
||||
func (m *mockLance) IncrementNotUseful(id string) error { return nil }
|
||||
|
||||
// GA-P0-1 (t_718d8707):拆分字段
|
||||
func (m *mockLance) IncrementHumanNotUseful(id string) error { return nil }
|
||||
func (m *mockLance) AppendHitEvent(memoryID, outcome, reason string) error { return nil }
|
||||
func (m *mockLance) CountHitEvents(memoryID, outcome string, since time.Time) (int, error) { return 0, nil }
|
||||
func (m *mockLance) GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error) { return nil, nil }
|
||||
func (m *mockLance) IncrementHumanNotUseful(id string) error { return nil }
|
||||
func (m *mockLance) AppendHitEvent(memoryID, outcome, reason string) error { return nil }
|
||||
func (m *mockLance) CountHitEvents(memoryID, outcome string, since time.Time) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
func (m *mockLance) GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *mockLance) IncrementDedupDownweight(id string) error { return nil }
|
||||
func (m *mockLance) UpdateMemoryContent(id, newContent, source string) error { return nil }
|
||||
func (m *mockLance) InsertMemory(rec models.MemoryRecord) error { return nil }
|
||||
@@ -116,6 +120,7 @@ func (mockGraph) ListNodesByType(nodeType string, namespace string) []map[string
|
||||
}
|
||||
func (mockGraph) ListNodes(namespace string) []map[string]interface{} { return nil }
|
||||
func (mockGraph) Stats() (int, int, float64) { return 0, 0, 0 }
|
||||
func (mockGraph) CountNodesByType(string) (int, int, string) { return 0, 0, "" }
|
||||
func (mockGraph) Prune(minWeight float64) {}
|
||||
func (mockGraph) ExpandFromResults(results []models.RecallResult, namespace string, maxHops int) []models.RecallResult {
|
||||
return results
|
||||
|
||||
@@ -3,6 +3,7 @@ package governance
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -295,6 +296,22 @@ func (g *InMemoryGraph) AllEdges(namespace string) []GraphEdge {
|
||||
return out
|
||||
}
|
||||
|
||||
// CountNodesByType 内存实现:无时间列,lastUpdated 恒为空(调用方按 unknown_age 处理)。
|
||||
func (g *InMemoryGraph) CountNodesByType(typ string) (total, withID int, lastUpdated string) {
|
||||
g.mu.RLock()
|
||||
defer g.mu.RUnlock()
|
||||
for _, n := range g.nodes {
|
||||
if n.Type != typ {
|
||||
continue
|
||||
}
|
||||
total++
|
||||
if strings.TrimSpace(n.ID) != "" {
|
||||
withID++
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Stats 返回图谱统计
|
||||
func (g *InMemoryGraph) Stats() (nodeCount, edgeCount int, density float64) {
|
||||
g.mu.RLock()
|
||||
|
||||
@@ -584,6 +584,32 @@ func (gs *SQLiteGraphStore) Stats() (nodeCount, edgeCount int, density float64)
|
||||
return
|
||||
}
|
||||
|
||||
// CountNodesByType 返回某类节点的总量、id 非空量、最近一次更新时间(2026-10-26)。
|
||||
//
|
||||
// 存在的理由:selfcheck 需要暴露「pattern 召回有没有生产者」这一可观测事实。
|
||||
// 事故背景:`POST /api/v1/patterns/detect` 是短命单发端点,全库 0 cron、0 调用方,
|
||||
// 于是 graph_nodes(type='pattern') 恒 0 行、recall 恒 0 命中,而日志层面完全沉默。
|
||||
// 零产出必须能被机器读到,不能靠人翻 7 天日志才发现。
|
||||
func (gs *SQLiteGraphStore) CountNodesByType(typ string) (total, withID int, lastUpdated string) {
|
||||
gs.mu.RLock()
|
||||
defer gs.mu.RUnlock()
|
||||
|
||||
t := escape(typ)
|
||||
total = queryInt(gs.db, fmt.Sprintf("SELECT COUNT(*) FROM graph_nodes WHERE type='%s'", t))
|
||||
withID = queryInt(gs.db, fmt.Sprintf(
|
||||
"SELECT COUNT(*) FROM graph_nodes WHERE type='%s' AND id IS NOT NULL AND TRIM(id)<>''", t))
|
||||
rows := queryRows(gs.db, fmt.Sprintf(
|
||||
"SELECT MAX(last_updated_at) AS m FROM graph_nodes WHERE type='%s'", t))
|
||||
if len(rows) > 0 && rows[0]["m"] != nil {
|
||||
if v, ok := rows[0]["m"].(string); ok {
|
||||
lastUpdated = v
|
||||
} else {
|
||||
lastUpdated = fmt.Sprintf("%v", rows[0]["m"])
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (gs *SQLiteGraphStore) Prune(minWeight float64) {
|
||||
gs.mu.Lock()
|
||||
defer gs.mu.Unlock()
|
||||
|
||||
@@ -23,6 +23,9 @@ type GraphStore interface {
|
||||
|
||||
// 统计与维护
|
||||
Stats() (nodeCount, edgeCount int, density float64)
|
||||
// CountNodesByType 返回某类节点的总量、id 非空量、最近更新时间。
|
||||
// 用途:让「写入端有没有产数」可被机器读到(pattern 召回曾恒 0 而日志沉默)。
|
||||
CountNodesByType(typ string) (total, withID int, lastUpdated string)
|
||||
Prune(minWeight float64)
|
||||
|
||||
// 边枚举(供社区发现用)
|
||||
|
||||
@@ -156,6 +156,10 @@ func (c *cachedGraphStore) AllEdges(namespace string) []governance.GraphEdge {
|
||||
return c.inner.AllEdges(namespace)
|
||||
}
|
||||
func (c *cachedGraphStore) Stats() (int, int, float64) { return c.inner.Stats() }
|
||||
|
||||
func (c *cachedGraphStore) CountNodesByType(typ string) (int, int, string) {
|
||||
return c.inner.CountNodesByType(typ)
|
||||
}
|
||||
func (c *cachedGraphStore) Prune(minWeight float64) { c.inner.Prune(minWeight) }
|
||||
func (c *cachedGraphStore) ExpandFromResults(results []models.RecallResult, namespace string, maxHops int) []models.RecallResult {
|
||||
return c.inner.ExpandFromResults(results, namespace, maxHops)
|
||||
|
||||
Reference in New Issue
Block a user