AC1 lancedb_ipc.go:159/:200 硬编码 importance 1.0 改为规则调用: EffectiveMemoryImportance(m) 显式值优先; 规则开⇒档位; 关(默认)⇒回落1.0(零行为变化) EffectiveEpisodeImportance() episode 原始日志 规则开⇒0.3 预留档; 关⇒1.0 AC2 规则档位(全库实测标定, 每档>=1%, 无值==1.0): core=0.9(4.11%) / normal×wiki,llm_distill=0.7(70.06%) / normal×legacy,unknown=0.4(25.84%) / normal×recovery=0.3(预留档) 未纳入: 内容深度(78.1%<50字⇒切档违反>=1%) / 时间衰减(排序已有运行时recency项, 避免双衰减) / 图谱连接度(红线不碰图谱) AC3 存量回填: rust bin importance_backfill(仅写 importance 一列, id IN 组提交 25 版本, 提交冲突有界退避, verify 逐行复算, rollback=整表1.0 一个版本)。 生产执行: 46,930 行全部回填, 恒1.0 行数 46,930 -> 0 (verify mismatch=0) 备份: ~/.hermes/state/t_789898bd-importance-backup/memories-id-importance.csv (46,913 行, 全部 1.0) AC4 golden 集合 before/after: mrr 0.4345→0.4345, ndcg 0.4722→0.4722, keyword 14/14 不变(差 0%, <±10%) AC5 回滚开关 ZHIYI_IMPORTANCE_RULE(off=默认, 逐位等同改动前); 回滚命令: importance_backfill --data-dir /var/lib/memoryweave --mode rollback 单测: go test ./internal/storage/ 全过(5 个新 importance 用例); 回归: storage+models+api/routes 全过
119 lines
5.5 KiB
Go
119 lines
5.5 KiB
Go
package storage
|
||
|
||
// ─────────────────────────────────────────────────────────────────────────
|
||
// importance 规则(t_789898bd,2026-10-18)——「装温度计」
|
||
//
|
||
// 背景:写入侧 importance 恒硬编码 1.0(lancedb_ipc.go:159 episode / :200 memory),
|
||
// 全库 46,903 行恒 1.0 ⇒ 排序侧没有非恒定信号,任何排序修复都测不了 NDCG 提升
|
||
// (「修 importance = 先装温度计,才知道退烧药有没有用」)。本文件给写入侧接上
|
||
// 规则化 importance + env 回滚开关。
|
||
//
|
||
// 标定依据(2026-10-18 21:00 全库只读统计,/var/lib/memoryweave memories 46,903 行,
|
||
// 原始输出见交付 importance-fix-t_789898bd.md 的 stats_readonly 一节):
|
||
// tier: core=1,926 (4.11%) / normal=44,977 (95.89%)
|
||
// source: wiki=26,664 (56.85%) / llm_distill=6,197 (13.21%) /
|
||
// legacy=12,099 (25.80%) / recovery=1,926 (4.11%,全部 tier=core) /
|
||
// other=17 (0.04%)
|
||
// ⇒ 回填后分布恰好 3 档,每档 ≥1%(AC2 验收判据),且无任何值等于 1.0
|
||
// (「恒 1.0 行数 → 0」机械可判):
|
||
// 0.9 = core(4.11%)
|
||
// 0.7 = normal × wiki:*/llm_distill(70.06%)
|
||
// 0.4 = normal × legacy:*/unknown(25.84%,unknown=17 行并入低置信档,
|
||
// 0.04% 单拆会产生 <1% 档 ⇒ 违反 AC2,故并档)
|
||
// 0.3 = 预留档:episode 原始日志 / 非 core 的 recovery 行(存量 0 行,
|
||
// 只对未来写入生效,不参与存量直方图「每档 ≥1%」验收)
|
||
// 未纳入的微调项(每项实测依据):
|
||
// · 内容深度:长度 p50=32 字、78.1% <50 字 ⇒ 按深度切档必出 <1% 档
|
||
// · 时间衰减:排序已有运行时 recency 项(quality_fusion.computeImportance),
|
||
// 存进列会双衰减,故存量值取「静态信号」
|
||
// · 图谱连接度:需要 graph.db,本卡红线「不碰图谱」
|
||
//
|
||
// env 开关(AC5 回滚开关,默认 off = 零行为变化):
|
||
// ZHIYI_IMPORTANCE_RULE=on ⇒ 未显式传 importance 时走 ComputeImportance / EpisodeImportance
|
||
// ZHIYI_IMPORTANCE_RULE=off / 未设 / 其他值 ⇒ 写入回落恒 1.0(与改动前逐位一致)
|
||
// 每次调用读 env(测试可 t.Setenv 翻转,同 t_7c0538eb lowScoreDwGate 先例)。
|
||
// 本卡**不部署生产二进制**(部署另行拍板);存量回填用 rust 侧
|
||
// importance_backfill 工具(同 lancedb 0.15 crate,仅写 importance 一列,组更新 3 个版本)。
|
||
// ─────────────────────────────────────────────────────────────────────────
|
||
|
||
import (
|
||
"os"
|
||
"strings"
|
||
|
||
"github.com/xiaoxue/memoryweave/internal/models"
|
||
)
|
||
|
||
// legacyImportanceConstant 改动前恒 1.0 的字面量(规则关 / 调用方未传显式值时的回落)。
|
||
const legacyImportanceConstant = 1.0
|
||
|
||
// EpisodeImportance episode(原始日志)写入档:无蒸馏信号,预留 0.3 档。
|
||
const EpisodeImportance = 0.3
|
||
|
||
// importanceRuleEnabled env 开关:on/true/1 = 开;其余(含未设)= 关。
|
||
func importanceRuleEnabled() bool {
|
||
switch strings.ToLower(os.Getenv("ZHIYI_IMPORTANCE_RULE")) {
|
||
case "on", "true", "1":
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
|
||
// SourceClass 把 source 归入分档类。未知/空 source ⇒ "other"(按低置信类处理,
|
||
// 实测 0.04%,不独立成档 —— 拆档会产生 <1% 档,违反 AC2 验收)。
|
||
func SourceClass(source string) string {
|
||
if source == "llm_distill" {
|
||
return "llm_distill"
|
||
}
|
||
switch {
|
||
case strings.HasPrefix(source, "wiki:"):
|
||
return "wiki"
|
||
case strings.HasPrefix(source, "legacy:"):
|
||
return "legacy"
|
||
case strings.HasPrefix(source, "recovery:"):
|
||
return "recovery"
|
||
default:
|
||
return "other"
|
||
}
|
||
}
|
||
|
||
// ComputeImportance 由 (tier, source 类) 算出档位 importance。
|
||
// 返回值 ∈ {0.9, 0.7, 0.4, 0.3},**永不返回 1.0**(「恒 1.0 行数→0」机械可判的前提)。
|
||
func ComputeImportance(rec models.MemoryRecord) float64 {
|
||
if rec.Tier == "core" {
|
||
return 0.9 // 覆盖 1,926 行 = 4.11%
|
||
}
|
||
switch SourceClass(rec.Source) {
|
||
case "wiki", "llm_distill":
|
||
return 0.7 // 覆盖 32,861 行 = 70.06%
|
||
case "recovery":
|
||
return EpisodeImportance // 非 core 的 recovery(存量 0 行,预留档)
|
||
default: // legacy / other / 空
|
||
return 0.4 // 覆盖 12,116 行 = 25.84%
|
||
}
|
||
}
|
||
|
||
// EffectiveMemoryImportance 写入侧(InsertMemory :200)应写库的实际值(AC1 接线点):
|
||
// 1. m.Importance != 0 ⇒ 显式值优先(调用方声明即权威,AC1.1)
|
||
// 2. ZHIYI_IMPORTANCE_RULE=on ⇒ ComputeImportance(AC2 规则)
|
||
// 3. 否则 ⇒ 回落 1.0(保护存量调用方,零行为突变,AC1.2)
|
||
func EffectiveMemoryImportance(m models.MemoryRecord) float64 {
|
||
if m.Importance != 0 {
|
||
return m.Importance
|
||
}
|
||
if importanceRuleEnabled() {
|
||
return ComputeImportance(m)
|
||
}
|
||
return legacyImportanceConstant
|
||
}
|
||
|
||
// EffectiveEpisodeImportance episode 写入侧(InsertEpisode :159)应写库的实际值。
|
||
// InsertEpisode 接口签名(6 个实现 + 4 个调用点)不带 importance 参数,改签名超出
|
||
// 本卡范围;episode 原始日志也没有 tier/source 分档信号 ⇒ 规则档恒 0.3。
|
||
// 规则 off(默认)⇒ 回落 1.0,与改动前逐位一致。
|
||
func EffectiveEpisodeImportance() float64 {
|
||
if importanceRuleEnabled() {
|
||
return EpisodeImportance
|
||
}
|
||
return legacyImportanceConstant
|
||
}
|