背景(实测,34,749 条):
- 0.755 占 28.8%:旧 prompt 示例分数被 2B 照抄产生的常量 bug 值。
按创建日切片:09-27~10-06 占 61%~77%,10-07 降至 0.3%,**10-09 起 0.0%**
⇒ 纯历史遗留,不再新增(10-08 行协议修复已止血)。
- 0.000 占 25.4%:解析失败,语义是"没有分数",不是"质量为 0"。
- 合计 54.2% 不可信。
改动:
1) models.IsReliableQuality(qs):叶子包纯函数,qs<=0 或 qs≈0.755 判为不可信。
**只判定、不改数据**(不做 34k 行全表改写,零数据风险,回滚=还原代码)。
2) recall.go 融合:不可信时把权重全部让给关键词分(中性,不奖不罚)。
⚠️ 判定必须用 **RawQualityScore**(DB 原始列)——candidates[i].QualityScore 在
IPC 路径下是**已融合且饱和到 1.0** 的值,拿它判永远"可信"(实测踩到:
candidateQS=1.000000 / rawQS=0.755000)。
3) lancedb_ipc.go 融合:不可信时去掉质量的权重,其余按原比例归一(30:30→50:50)。
4) RecallResult 新增 quality_score_reliable:原值照常返回供审计,
但显式标注其可信度,调用方不再把占位值当真实质量判断。
诚实记录:因 importance 饱和,本修复**当前对排序是 no-op**(见下)。
重要性列 importance 全表恒为 1.0,查询期 computedImportance 恒 >1,
min(·,1.0) 使 finalQuality 对所有记忆都饱和成 1.0 ⇒ quality 轴在排序中是常量。
真正的问题是**饱和**(quality 完全不影响排序),已单独立项待拍板。
本提交的价值在于:① 显示值不再冒充真值;② 饱和一旦修好,判定立即生效。
测试:models 9 用例 + storage/distill/routes 全量回归通过。
实盘:部署后 [qstrust-ipc] 日志命中 176 条,证明判定在真实路径上执行。
153 lines
7.5 KiB
Go
153 lines
7.5 KiB
Go
// Package models 定义织忆 MemoryWeave 的核心数据模型。
|
||
package models
|
||
|
||
import "time"
|
||
|
||
// Time 是 time.Time 的安全版本,支持空字符串解析
|
||
type Time struct{ time.Time }
|
||
|
||
// UnmarshalJSON 实现 json.Unmarshaler:空字符串解析为当前时间(而非报错)
|
||
func (t *Time) UnmarshalJSON(data []byte) error {
|
||
s := string(data)
|
||
if s == `""` || s == `"0001-01-01T00:00:00Z"` {
|
||
t.Time = time.Time{}
|
||
return nil
|
||
}
|
||
// 去掉前后引号
|
||
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
|
||
s = s[1 : len(s)-1]
|
||
}
|
||
if s == "" || s == "null" {
|
||
t.Time = time.Time{}
|
||
return nil
|
||
}
|
||
return t.Time.UnmarshalJSON(data)
|
||
}
|
||
|
||
// VersionRecord 记忆版本记录(溯源链中的单次修改)
|
||
type VersionRecord struct {
|
||
Version int `json:"version"`
|
||
Content string `json:"content"`
|
||
UpdatedBy string `json:"updated_by"`
|
||
Source string `json:"source"`
|
||
Trigger string `json:"trigger"`
|
||
Reason string `json:"reason"`
|
||
Timestamp time.Time `json:"timestamp"`
|
||
}
|
||
|
||
// MemoryRecord 织忆中的单条记忆(存储在 LanceDB memories 表中)。
|
||
// v3.8 完整 Schema — 23 字段,与设计文档一致。
|
||
type MemoryRecord struct {
|
||
ID string `json:"id"`
|
||
AgentID string `json:"agent_id"`
|
||
Namespace string `json:"namespace"`
|
||
Content string `json:"content"`
|
||
ContentMD5 string `json:"content_md5"` // content 的 MD5 前 12 字符,用于精确去重
|
||
Category string `json:"category"` // system_fact, user_pref, proj_context, tool_usage, code_snippet
|
||
Vector []float32 `json:"vector"` // bge-m3 1024维,L2归一化
|
||
Tier string `json:"tier"` // normal, core(core永不衰减)
|
||
Importance float64 `json:"importance"` // 重要性得分 = recency_factor × (1+log(1+recall_count))
|
||
QualityScore float64 `json:"quality_score"`
|
||
UsefulCount int `json:"useful_count"` // 被标记 useful 的次数
|
||
NotUsefulCount int `json:"not_useful_count"` // 被标记 not-useful 的次数
|
||
Version int `json:"version"` // 当前版本号
|
||
VersionHistory []VersionRecord `json:"version_history,omitempty"` // 完整修改链
|
||
Source string `json:"source"` // 来源:牧尘口头 / 配置解析 / Agent推断 / LLM蒸馏
|
||
VolatileFlag bool `json:"volatile_flag"` // 频繁变更标记(≥3次修正自动设置)
|
||
RecallCount int `json:"recall_count"`
|
||
Freshness string `json:"freshness"` // fresh, stale, verified
|
||
// 2026-10-08 (t_7e3577c5):血缘字段。embedding_model=实际嵌入模型(bge-m3);
|
||
// script_version=写入脚本版本标识。写入方经 metadata 携带,server 蒸馏落库。
|
||
EmbeddingModel string `json:"embedding_model"`
|
||
ScriptVersion string `json:"script_version"`
|
||
// 2026-10-08 (Gap#2 来源加权):confidence=写入方显式声明的信任等级(0.4~1.0),
|
||
// 不从 source 反推(source 是「哪条管道进来」,confidence 是「内容可不可信」,两者正交)。
|
||
// 指针类型:nil ⇒ JSON null ⇒ Rust 写 NULL ⇒ 读路径 COALESCE→1.0(加权首日 no-op)。
|
||
// ConfidenceSource=authoritative(写入时声明) | estimated(事后回填猜测),用于日后定向纠偏。
|
||
Confidence *float64 `json:"confidence,omitempty"`
|
||
ConfidenceSource string `json:"confidence_source,omitempty"`
|
||
// 2026-10-11 (t_631ab13d):DB 原值直通通道。json:"-" 不参与任何 JSON 传输:
|
||
// Rust IPC Search 会把 QualityScore 融合成 0.4*importance+0.6*raw(排序用),
|
||
// 但 recall 响应需要展示 LanceDB 原始列值("响应值==库值"判据)。
|
||
// 只在进程内携带,不落库、不序列化。
|
||
RawQualityScore *float64 `json:"-"`
|
||
LastRecalledAt time.Time `json:"last_recalled_at"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
UpdatedAt time.Time `json:"updated_at"`
|
||
IsDeleted bool `json:"is_deleted"`
|
||
DependsOn []string `json:"depends_on,omitempty"` // 依赖的记忆 ID 列表
|
||
DerivedFrom string `json:"derived_from,omitempty"` // 蒸馏来源 episode ID
|
||
}
|
||
|
||
// EpisodeRecord 原始对话/任务日志(存储在 LanceDB episodes 表中)。
|
||
type EpisodeRecord struct {
|
||
ID string `json:"id"`
|
||
AgentID string `json:"agent_id"`
|
||
Namespace string `json:"namespace"`
|
||
Content string `json:"content"`
|
||
Category string `json:"category"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
}
|
||
|
||
// TombstoneRecord 已删除/废弃记忆的墓碑记录。
|
||
type TombstoneRecord struct {
|
||
ID string `json:"id"`
|
||
OriginalID string `json:"original_id"`
|
||
Reason string `json:"reason"` // deprecated, merged, corrected, evicted
|
||
CreatedAt time.Time `json:"created_at"`
|
||
}
|
||
|
||
// RecallResult recall 接口返回的单条结果。
|
||
type RecallResult struct {
|
||
ID string `json:"id"`
|
||
Content string `json:"content"`
|
||
Category string `json:"category"`
|
||
Score float64 `json:"score"`
|
||
Timestamp string `json:"timestamp"`
|
||
// 2026-09-22 新增:全库召回(namespace="" 时)调用方无法分辨记忆来源域,
|
||
// 会把分身的私有记忆当成自己的用。带上来源便于调用方按需过滤/标注。
|
||
// omitempty:旧调用方无感;图谱扩展路径(graph_*.go)无 ns 概念,留空。
|
||
Namespace string `json:"namespace,omitempty"`
|
||
AgentID string `json:"agent_id,omitempty"`
|
||
Source string `json:"source,omitempty"`
|
||
// 2026-10-08 (Gap#2):把信任等级直通给调用方。此前加列但不动本结构 ⇒
|
||
// confidence 对排序/调用方零影响(「字段加了没接线」的经典静默 no-op)。
|
||
// Confidence=nil 表示该条未声明(存量),调用方按 1.0 处理。
|
||
Confidence *float64 `json:"confidence,omitempty"`
|
||
ConfidenceSource string `json:"confidence_source,omitempty"`
|
||
// 2026-10-11 (t_631ab13d):把库里的 quality_score / v_value 直通给调用方。
|
||
// 此前 RecallResult 缺这两个字段 ⇒ JSON 序列化不带出 ⇒ 插件 `r.get("quality_score", 0)`
|
||
// 恒得默认 0("显示 0"),与库里 74.88% 记忆有真实值矛盾。排序不受影响(排序用内存
|
||
// candidates[i].QualityScore=库值,见 recall.go),本字段仅补"响应值==库值"的显示缺口。
|
||
// quality_score 恒输出(含 0);v_value 为 VValueProvider 提供,未注入时 0。
|
||
QualityScore float64 `json:"quality_score"`
|
||
// QualityScoreReliable:上面的 quality_score 是否可信。
|
||
// 2026-10-12:存量 54.2% 的 quality_score 不可信(0=未打分 / 0.755=历史常量 bug),
|
||
// 该值原样返回供审计,但**不应被当作真实质量判断**。调用方须看本字段。
|
||
QualityScoreReliable bool `json:"quality_score_reliable"`
|
||
VValue float64 `json:"v_value"`
|
||
}
|
||
|
||
// RerankResult rerank 重排后的单条结果。
|
||
type RerankResult struct {
|
||
Index int `json:"index"`
|
||
Score float64 `json:"score"`
|
||
Text string `json:"text"`
|
||
}
|
||
|
||
// GraphBFSResult BFS 图谱扩展结果(含汇总语句)
|
||
type GraphBFSResult struct {
|
||
ExpandedRelations []ExpandedRelation `json:"expanded_relations"`
|
||
Summary string `json:"summary"`
|
||
}
|
||
|
||
// ExpandedRelation 单条扩展关系
|
||
type ExpandedRelation struct {
|
||
From string `json:"from"`
|
||
To string `json:"to"`
|
||
Relation string `json:"relation"`
|
||
Hops int `json:"hops"`
|
||
Weight float64 `json:"weight"`
|
||
Score float64 `json:"score"`
|
||
}
|