新增 append-only 事件表 hit_events(sqlite/Rust-LanceDB/memvector 三后端同步), 带 created_at 时间戳可按周窗口聚合,只由真实 outcome 写入(人工反馈/矛盾/取代/ 下游引用);dedup 降权/单纯召回出现**不算**(P0-1/P0-2 拆出的假信号,见 hit_event.go 排除项 + docs/hit-outcome-signal.md y 判据)。修正 已办.md:16623 的 hits+=useful_count 错误等同并标注更正依据。 写入点接线 feedback.go(/feedback/useful→pos, /not-useful→neg)。单测 5 例 PASS(含 dedup 降权不计入 hits− 反例 + sqlite 关库重开 read-back);Rust cargo build 通过; LanceDB 真实 sidecar IPC append/count/get read-back 取证(count 时区归一修掉跨时区错位)。 禁部署:未动运行中二进制、未碰 live 库。
61 lines
3.6 KiB
Go
61 lines
3.6 KiB
Go
// 织忆 MemoryWeave — LanceDB 接口定义
|
||
// 所有存储后端实现此接口,Go → Rust IPC 由 internal/consolidate 负责
|
||
|
||
package storage
|
||
|
||
import (
|
||
"time"
|
||
|
||
"github.com/xiaoxue/memoryweave/internal/models"
|
||
)
|
||
|
||
// VValueProvider 外部注入的 V 值查询函数(由 server.go 设置 → selfoptimize.VProp)
|
||
// 用于召回排序时融合用户决策信号
|
||
var VValueProvider func(memoryID string) float64
|
||
|
||
// LanceDB 存储后端统一接口
|
||
// 实现者: SQLiteClient (生产), MemLanceClient (开发/降级)
|
||
// 未来: Rust lancedb crate 通过 Unix Socket 提供 LanceDB 原生后端
|
||
type LanceDB interface {
|
||
InsertEpisode(agentID, namespace, content, category string) (string, error)
|
||
GetTopByQuality(agentID string, limit int) ([]models.MemoryRecord, error)
|
||
Stats() (map[string]interface{}, error)
|
||
SoftDelete(id, reason string) error
|
||
// SoftDeleteBatch 批量标记 is_deleted=true,updated_at=ts(单事务提交)
|
||
SoftDeleteBatch(table string, ids []string, updatedAt string) (int64, error)
|
||
GetVersionHistory(id string) ([]map[string]interface{}, error)
|
||
GetCandidatesForForgetting() ([]map[string]interface{}, error)
|
||
// G7.2: 获取适合结晶为 skill 的记忆候选
|
||
GetSkillCandidates(minRecalls, limit int) ([]models.MemoryRecord, error)
|
||
Backup(path string) error
|
||
GetAuditLog(limit int) ([]map[string]interface{}, error)
|
||
IncrementUseful(id string) error
|
||
// GA-P0-1 (t_718d8707):拆分 not_useful_count 的两种语义。两个写入点各写各的:
|
||
// - IncrementHumanNotUseful → human_not_useful_count(用户人工点「没用」)
|
||
// - IncrementDedupDownweight → dedup_downweight_count(dedup/合并流程自动降权)
|
||
// IncrementNotUseful 保留仅向后兼容(历史混写),新代码不再调用它(AC#3)。
|
||
IncrementHumanNotUseful(id string) error
|
||
IncrementDedupDownweight(id string) error
|
||
IncrementNotUseful(id string) error // [DEPRECATED] 历史混写列,勿再调(AC#3)
|
||
// GA-P0-4 (t_bb10e8bc):hits+/hits− outcome 事件(append-only 事件表,语义定义见 hit_event.go)。
|
||
// - AppendHitEvent(memoryID, outcome, reason) 追加一条真实 outcome 事件(带 created_at 时间戳)
|
||
// - CountHitEvents(memoryID, outcome, since) 统计某 memory 在某窗口内的 hits+ 或 hits− 次数(P2 自适应阈值用)
|
||
// - GetHitEvents(memoryID, limit) 读回事件明细(read-back 取证)
|
||
// 铁律:outcome 非 pos/neg 报错不写库;dedup 降权 / 单纯召回出现 **禁止** 调本方法(见 hit_event.go 排除项)。
|
||
AppendHitEvent(memoryID, outcome, reason string) error
|
||
CountHitEvents(memoryID, outcome string, since time.Time) (int, error)
|
||
GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error)
|
||
UpdateMemoryContent(id, newContent, source string) error
|
||
InsertMemory(m models.MemoryRecord) error
|
||
Search(table string, vector []float32, topK int, namespaceFilter string) ([]models.MemoryRecord, error)
|
||
// Scan 全表扫描存活记忆(is_deleted=false),按 namespace 可选过滤,跳向量。
|
||
// 2026-09-21:列表/导出端点的正确底座 —— 修前它们用「零向量 ANN」冒充列表,
|
||
// 零向量无方向 ⇒ 只拿到任意前 N 行再按 ns 过滤 ⇒ 计数完全失真。
|
||
Scan(limit int, namespaceFilter string) ([]models.MemoryRecord, error)
|
||
Insert(table string, record any) error
|
||
Update(table, id string, fields map[string]any) error
|
||
// PurgeInvalid 清理 id 为空的垃圾行(P1-1):按 id='' 条件一次性软删(is_deleted=true)。
|
||
// 返回受影响行数。这些行无 id 无法按 id 软删,只能走专用端点 /api/v1/admin/purge-invalid。
|
||
PurgeInvalid() (int64, error)
|
||
}
|