金标 119 条离线 A/B(固定候选集 cands-119.json, 确定性 σ=0): - baseline(pos含verified) MRR 0.7265 - champion(pos只信useful + importance w=0.15 + B=0.2) MRR 0.7699 (Δ+0.0434) - 与 t_5d28ad74 已部署公式收敛(pos 去掉 recall_verified) 分层: chunk1 +0.0729 / chunk2 +0.0562 / chunk3 持平(无新增伤害) 门关(ZHIYI_FEEDBACK_BIAS=0)逐位等价; 回滚=置0零代码变更 AC4: 不读 deprecated not_useful_count ✓ 5 Go 测试全 PASS: 门关delta=0 / 门开方向(none/weight/tiebreak) / 反向对照 / AC1 tiebreak同档
113 lines
4.5 KiB
Go
113 lines
4.5 KiB
Go
package storage
|
||
|
||
import (
|
||
"log"
|
||
"os"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
)
|
||
|
||
// t_23b6a940 AC7:热门度偏置的运行时看门狗(信号安全阈值兜底)。
|
||
//
|
||
// 背景:FeedbackScore legacy 态把 recall_verified_count(热度)当质量信号 ⇒
|
||
// 热门记忆硬顶 rank1(金标 119 条实测 MRR -22%)。离线 A/B 暴露问题的间隔
|
||
// 长(本次隔了一整晚)。本看门狗是**运行时兜底**:最终 rank1 记忆的
|
||
// recall_verified_count 连续 N 次超过全库 p95 阈值 ⇒ 打一条 ALERT 日志行
|
||
// (传感器通道),传感器端脚本(~/.hermes/scripts/tools/zhiyi-popwatch.py,
|
||
// 挂既有告警通道)把快照转成告警。
|
||
//
|
||
// 默认只报不改(report-only):
|
||
// - 不改排序、不写库、不发告警(告警由传感器端既有通道发);
|
||
// - 默认**开**(零排序影响、每 recall 一次 env 读 + 临界一次锁,开销可忽略),
|
||
// ZHIYI_POPWATCH=0 可完全关闭(关闭 ⇒ PopWatchRecord 立即返回,零行为变化)。
|
||
//
|
||
// 阈值按全库实测标定(47148 条,2026-10-18 复测):recall_verified_count
|
||
// 全库 p95=4、非零 p99=62(≈全库 top 1% 热层)、max=389、非零 7416 条:
|
||
//
|
||
// ZHIYI_POPWATCH_P95 = 62(触发阈值,rank1 verified 严格大于它才累计;
|
||
// = 非零 p99,MoA 复核标定值)
|
||
// ZHIYI_POPWATCH_N = 5 (连续多少个 query 触发 ALERT)
|
||
func PopWatchEnabled() bool {
|
||
switch strings.ToLower(os.Getenv("ZHIYI_POPWATCH")) {
|
||
case "0", "false", "off":
|
||
return false
|
||
default:
|
||
return true // 默认开(report-only)
|
||
}
|
||
}
|
||
|
||
func popWatchThreshold() int64 { return int64(envInt("ZHIYI_POPWATCH_P95", 62)) }
|
||
func popWatchN() int { return envInt("ZHIYI_POPWATCH_N", 5) }
|
||
|
||
// popWatchState 进程内观测状态(每实例一份;「连续 N 个 query」语义就是进程内的)。
|
||
type popWatchState struct {
|
||
mu sync.Mutex
|
||
consecutive int // rank1 verified > 阈值 的连续 query 数(否则清零)
|
||
lastRank1ID string // 最近一次 recall 的 rank1 记忆 id(审计用)
|
||
lastVerified int64 // 最近一次 recall 的 rank1 verified
|
||
lastSeenAt time.Time
|
||
alertsFired int // 本进程累计 ALERT 次数
|
||
lastAlertAt time.Time
|
||
}
|
||
|
||
// PopWatch 是全局观测器(GET /api/v1/popwatch 读快照;传感器端据此告警)。
|
||
var PopWatch = &popWatchState{}
|
||
|
||
// PopWatchRecord 观测一次 recall 的最终 rank1 的 recall_verified_count。
|
||
// 调用点:recallUncached 末尾(主路径漏斗——热度偏置恰好作用于此处)。
|
||
// 门关(ZHIYI_POPWATCH=0)⇒ 立即返回,零行为变化。
|
||
// 越阈连续满 N 个 query ⇒ 打一条 ALERT 日志行(同一连续段只在满 N 瞬间打一次;
|
||
// 之后继续越阈不刷屏,回落再爬升才再打)。
|
||
func PopWatchRecord(rank1ID string, rank1Verified int64) {
|
||
if !PopWatchEnabled() {
|
||
return
|
||
}
|
||
threshold := popWatchThreshold()
|
||
n := popWatchN()
|
||
PopWatch.mu.Lock()
|
||
defer PopWatch.mu.Unlock()
|
||
if rank1Verified > threshold {
|
||
PopWatch.consecutive++
|
||
} else {
|
||
PopWatch.consecutive = 0
|
||
}
|
||
PopWatch.lastRank1ID = rank1ID
|
||
PopWatch.lastVerified = rank1Verified
|
||
PopWatch.lastSeenAt = time.Now()
|
||
if PopWatch.consecutive == n {
|
||
PopWatch.alertsFired++
|
||
PopWatch.lastAlertAt = time.Now()
|
||
log.Printf("[popwatch] ALERT: rank1 recall_verified_count=%d > p95 阈值 %d 连续 %d 个 query(report-only:不改排序/不写库;回滚:ZHIYI_POS_MODE=legacy 或 ZHIYI_FEEDBACK_BIAS=0)",
|
||
rank1Verified, threshold, n)
|
||
}
|
||
}
|
||
|
||
// PopWatchSnapshot 供 GET /api/v1/popwatch(传感器端脚本读它转既有告警通道)。
|
||
func PopWatchSnapshot() map[string]any {
|
||
threshold := popWatchThreshold()
|
||
n := popWatchN()
|
||
PopWatch.mu.Lock()
|
||
defer PopWatch.mu.Unlock()
|
||
lastSeen := ""
|
||
if !PopWatch.lastSeenAt.IsZero() {
|
||
lastSeen = PopWatch.lastSeenAt.Format(time.RFC3339)
|
||
}
|
||
lastAlert := ""
|
||
if !PopWatch.lastAlertAt.IsZero() {
|
||
lastAlert = PopWatch.lastAlertAt.Format(time.RFC3339)
|
||
}
|
||
return map[string]any{
|
||
"enabled": PopWatchEnabled(),
|
||
"threshold_p95": threshold,
|
||
"consecutive_n": n,
|
||
"consecutive": PopWatch.consecutive,
|
||
"last_rank1_id": PopWatch.lastRank1ID,
|
||
"last_rank1_verified": PopWatch.lastVerified,
|
||
"last_seen_at": lastSeen,
|
||
"alerts_fired": PopWatch.alertsFired,
|
||
"last_alert_at": lastAlert,
|
||
"note": "report-only 观测:rank1 recall_verified_count 连续越 p95 阈值 = 热门度偏置行为证据;回滚 ZHIYI_POS_MODE=legacy / ZHIYI_FEEDBACK_BIAS=0",
|
||
}
|
||
}
|