From a293f7b784e1c2b7540ae2f0e3088c38a46b61cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF?= Date: Mon, 19 Oct 2026 19:42:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(storage):=20A10=20=E6=8B=86=E5=87=BA?= =?UTF-8?q?=E4=BA=BA=E5=B7=A5=E6=9C=89=E7=94=A8=20human=5Fuseful=5Fcount?= =?UTF-8?q?=20=E5=8D=95=E7=8B=AC=E5=88=97=EF=BC=88t=5Fd618dc7c=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Rust lance schema 追加 human_useful_count (Int64, nullable);open() 自动加列 迁移,存量回填 NULL(与 confidence/human_not_useful_count 迁移模式一致) - MemoryRecord 读侧接线(get_by_id/search/scan_all/scan_deleted/record_from_batch) 走 col_i64_opt(NULL 读回 None,区别于 0) - insert_batch 写入侧:未声明/NULL → 写 NULL;显式整数 → 写值(与 confidence 同款) - update() $inc 基数改 col_i64_opt(NULL 行按 0 起算,首次人工 useful NULL→1) - Go IncrementUseful 同一次 Update 三 $inc:useful_count + recall_verified_count + human_useful_count(只有人工 feedback 路径调用,机器自评路径不写此列) - Go MemoryRecord/memEntry 加 *int 字段(nil=NULL) - 新增 human_useful_count_test.go:人工 useful 同时 +1、机器路径不动反例 - 排序公式/权重零改动(AC#6 排序零变化) --- go/internal/models/memory.go | 6 ++ .../storage/human_useful_count_test.go | 80 +++++++++++++++++++ go/internal/storage/lancedb_ipc.go | 26 ++++++ go/internal/storage/memvector.go | 9 +++ rust/src/lancedb_ops.rs | 44 +++++++++- 5 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 go/internal/storage/human_useful_count_test.go diff --git a/go/internal/models/memory.go b/go/internal/models/memory.go index 35e9fe4..0e3244f 100644 --- a/go/internal/models/memory.go +++ b/go/internal/models/memory.go @@ -56,6 +56,12 @@ type MemoryRecord struct { // 历史列(deprecated),新代码不再读它(见 AC#3)。 HumanNotUsefulCount int `json:"human_not_useful_count"` // 用户人工点「没用」的次数 DedupDownweightCount int `json:"dedup_downweight_count"` // dedup/合并流程自动降权的次数 + // A10 (t_d618dc7c):把「人工有用」从 UsefulCount(人工+机器混写)拆出单独计数。 + // 指针类型:nil = NULL(存量行/从未被人工点过),非 nil = 真实整数。 + // 统计口径用时分母必须 WHERE human_useful_count IS NOT NULL(NULL≠0)。 + // 只有人工 feedback(/api/v1/feedback/useful → IncrementUseful)会 ++,机器自评/ + // 蒸馏质量路径走别的方法(IncrementDedupDownweight/IncrementHumanNotUseful/...)不写此列。 + HumanUsefulCount *int `json:"human_useful_count"` // nil=NULL(从未被人工点过);非 nil=真实计数 NotUsefulCount int `json:"not_useful_count"` // [DEPRECATED] 历史混写列,勿再读 Version int `json:"version"` // 当前版本号 VersionHistory []VersionRecord `json:"version_history,omitempty"` // 完整修改链 diff --git a/go/internal/storage/human_useful_count_test.go b/go/internal/storage/human_useful_count_test.go new file mode 100644 index 0000000..ad327a9 --- /dev/null +++ b/go/internal/storage/human_useful_count_test.go @@ -0,0 +1,80 @@ +package storage + +import "testing" + +// TestHumanUsefulCountIsolation — A10 (t_d618dc7c) AC#3 反例测试 +// +// 证明「只有人工 useful 才 +1 human_useful_count;机器自评/蒸馏质量路径不写此列」: +// +// 人工 useful ⇒ useful_count+1 且 human_useful_count+1 **同时发生**(同一次 IncrementUseful) +// 机器自评/降权(IncrementDedupDownweight / IncrementHumanNotUseful)⇒ human_useful_count 不变 +// +// 用相对增量断言(绝对值会被其它路径干扰)。起点设 human_useful_count = nil(模拟 +// 迁移后「从未被人工点过」的存量/NULL 行),验证首次人工 useful 从 NULL 起 +1 ⇒ 1。 +func TestHumanUsefulCountIsolation(t *testing.T) { + mlc := NewMemLanceClient(nil) + mlc.mu.Lock() + // 起点:useful_count=0,human_useful_count=nil(NULL 存量语义),其它计数 0 + mlc.memories["mem_huc_1"] = &memEntry{ID: "mem_huc_1", HumanUsefulCount: nil} + mlc.mu.Unlock() + + e := mlc.memories["mem_huc_1"] + + // ── 人工 useful ×2 ⇒ useful_count+2 且 human_useful_count nil→1→2 同时发生 ── + if err := mlc.IncrementUseful("mem_huc_1"); err != nil { + t.Fatalf("IncrementUseful #1 err: %v", err) + } + if e.UsefulCount != 1 { + t.Fatalf("人工 useful#1:useful_count 相对增量 +1,want 1 got %d", e.UsefulCount) + } + if e.HumanUsefulCount == nil || *e.HumanUsefulCount != 1 { + got := -1 + if e.HumanUsefulCount != nil { + got = *e.HumanUsefulCount + } + t.Fatalf("人工 useful#1:human_useful_count NULL→1,want 1 got %d (nil=%v)", got, e.HumanUsefulCount == nil) + } + + if err := mlc.IncrementUseful("mem_huc_1"); err != nil { + t.Fatalf("IncrementUseful #2 err: %v", err) + } + if e.UsefulCount != 2 || e.HumanUsefulCount == nil || *e.HumanUsefulCount != 2 { + huc := -1 + if e.HumanUsefulCount != nil { + huc = *e.HumanUsefulCount + } + t.Fatalf("人工 useful#2:want useful=2 human=%d got useful=%d human=%d", 2, e.UsefulCount, huc) + } + + // ── 反例:机器自评/dedup 降权路径 ⇒ human_useful_count 不变(仍 2),useful_count 不变 ── + if err := mlc.IncrementDedupDownweight("mem_huc_1"); err != nil { + t.Fatalf("IncrementDedupDownweight err: %v", err) + } + if e.HumanUsefulCount == nil || *e.HumanUsefulCount != 2 { + got := -1 + if e.HumanUsefulCount != nil { + got = *e.HumanUsefulCount + } + t.Fatalf("机器 dedup 路径不得污染 human_useful_count,want 2 got %d", got) + } + if e.UsefulCount != 2 { + t.Fatalf("机器 dedup 路径不得污染 useful_count,want 2 got %d", e.UsefulCount) + } + if e.DedupDownweightCount != 1 { + t.Fatalf("dedup_downweight_count 相对增量 +1,want 1 got %d", e.DedupDownweightCount) + } + + if err := mlc.IncrementHumanNotUseful("mem_huc_1"); err != nil { + t.Fatalf("IncrementHumanNotUseful err: %v", err) + } + if e.HumanUsefulCount == nil || *e.HumanUsefulCount != 2 { + got := -1 + if e.HumanUsefulCount != nil { + got = *e.HumanUsefulCount + } + t.Fatalf("人工 not-useful 路径不得污染 human_useful_count,want 2 got %d", got) + } + if e.HumanNotUsefulCount != 1 { + t.Fatalf("human_not_useful_count 相对增量 +1,want 1 got %d", e.HumanNotUsefulCount) + } +} diff --git a/go/internal/storage/lancedb_ipc.go b/go/internal/storage/lancedb_ipc.go index ac6f6b6..8a21b53 100644 --- a/go/internal/storage/lancedb_ipc.go +++ b/go/internal/storage/lancedb_ipc.go @@ -727,6 +727,27 @@ func (rc *RustLanceDBClient) Update(table, id string, fields map[string]any) err m.UsefulCount = inc } } + // A10 (t_d618dc7c):人工有用单独计数 —— 本地读缓存同步(*int,NULL 时先置 0 再自增) + if v, ok := fields["human_useful_count"]; ok { + switch inc := v.(type) { + case map[string]string: + if inc["$inc"] == "1" { + if m.HumanUsefulCount == nil { + m.HumanUsefulCount = new(int) + } + *m.HumanUsefulCount++ + } + case map[string]interface{}: + if inc["$inc"] == "1" { + if m.HumanUsefulCount == nil { + m.HumanUsefulCount = new(int) + } + *m.HumanUsefulCount++ + } + case int: + m.HumanUsefulCount = &inc + } + } if v, ok := fields["not_useful_count"]; ok { switch inc := v.(type) { case map[string]string: @@ -1512,9 +1533,14 @@ func (rc *RustLanceDBClient) GetAuditLog(limit int) ([]map[string]interface{}, e func (rc *RustLanceDBClient) IncrementUseful(id string) error { // GA-P0-2 (t_c6d41ac6):正反馈 → useful_count+1 且 recall_verified_count+1。 // 不再碰 recall_count(冻结,保留历史值——虚高保命来源已被切断)。 + // A10 (t_d618dc7c):同时把「人工有用」单独计到 human_useful_count($inc 1)。 + // 本方法只被人工 feedback 路径调用(feedback.go MarkUseful /api/v1/feedback/useful); + // 机器自评/蒸馏质量路径走其它方法(IncrementDedupDownweight / IncrementHumanNotUseful / + // IncrementRecallVerified,均不写 human_useful_count)⇒ 只有人工 useful 才 +1 此列。 if err := rc.Update("memories", id, map[string]any{ "useful_count": map[string]string{"$inc": "1"}, "recall_verified_count": map[string]string{"$inc": "1"}, + "human_useful_count": map[string]string{"$inc": "1"}, }); err != nil { log.Printf("[ipc] IncrementUseful failed id=%s: %v", id, err) return err diff --git a/go/internal/storage/memvector.go b/go/internal/storage/memvector.go index f6b7dfb..94a5934 100644 --- a/go/internal/storage/memvector.go +++ b/go/internal/storage/memvector.go @@ -36,6 +36,8 @@ type memEntry struct { // GA-P0-1 (t_718d8707):拆分字段 HumanNotUsefulCount int DedupDownweightCount int + // A10 (t_d618dc7c):人工有用单独计数(指针:nil=NULL=从未被人工点过) + HumanUsefulCount *int RecallCount int // GA-P0-2 (t_c6d41ac6):拆分字段 RecallSeenCount int @@ -277,6 +279,13 @@ func (mlc *MemLanceClient) IncrementUseful(id string) error { e.UsefulCount++ // GA-P0-2 (t_c6d41ac6):正反馈 → recall_verified_count++;不再动 recall_count(冻结) e.RecallVerifiedCount++ + // A10 (t_d618dc7c):人工 useful ⇒ human_useful_count 同步 +1(与生产 IncrementUseful 的 + // useful_count+recall_verified_count+human_useful_count 三 $inc 同一次 Update 一致)。 + // 机器自评/蒸馏路径(IncrementDedupDownweight / IncrementHumanNotUseful)不碰此列。 + if e.HumanUsefulCount == nil { + e.HumanUsefulCount = new(int) + } + *e.HumanUsefulCount++ } return nil } diff --git a/rust/src/lancedb_ops.rs b/rust/src/lancedb_ops.rs index 46962df..b6e112f 100644 --- a/rust/src/lancedb_ops.rs +++ b/rust/src/lancedb_ops.rs @@ -38,6 +38,13 @@ pub struct MemoryRecord { // not_useful_count 保留(deprecated),新代码不再读它;human_not_useful_count 与 // dedup_downweight_count 是新写入点。arrow_schema 尾部新增列(nullable,存量补 NULL)。 pub human_not_useful_count: i64, pub dedup_downweight_count: i64, + // A10 (t_d618dc7c):把「人工有用」从 useful_count(人工+机器混写)拆出单独计数。 + // Option(nullable)—— 与 confidence 同款「NULL≠0」语义:存量行/从未被人工 + // 点过的行保持 NULL(迁移默认 CAST(NULL AS BIGINT)),一旦被人工点过才变真实整数。 + // 统计口径用本列时分母必须 WHERE human_useful_count IS NOT NULL(NULL 是"未知"非"0 次")。 + // #[serde(default)] ⇒ IPC 解包缺字段/NULL 读回 None(区别于显式 0),不漏这层即静默 no-op。 + #[serde(default)] + pub human_useful_count: Option, pub freshness: String, pub version: i64, pub version_history: String, pub source: String, // 2026-10-08 (t_7e3577c5):血缘字段 @@ -148,6 +155,10 @@ fn arrow_schema() -> Schema { Field::new("valid_from", DataType::Utf8, true), Field::new("valid_to", DataType::Utf8, true), Field::new("provenance", DataType::Utf8, true), + // A10 (t_d618dc7c):人工有用单独计数。nullable Int64 —— 存量行由 open() schema 演进 + // 迁移补 NULL(CAST(NULL AS BIGINT),与 confidence/human_not_useful_count 迁移模式一致); + // 人工点过后才变真实整数。只加观察口径,不进任何排序公式/权重(AC#6 排序零变化)。 + Field::new("human_useful_count", DataType::Int64, true), ]) } @@ -303,6 +314,8 @@ impl LanceDBOps { // GA-P0-1 (t_718d8707):拆分字段读侧接线 human_not_useful_count: col_i64(&batch, i, "human_not_useful_count"), dedup_downweight_count: col_i64(&batch, i, "dedup_downweight_count"), + // A10 (t_d618dc7c):读侧接线 —— human_useful_count 走可空读法(NULL 读回 None) + human_useful_count: col_i64_opt(&batch, i, "human_useful_count"), freshness: col_str(&batch, i, "freshness"), version: col_i64(&batch, i, "version"), version_history: String::new(), @@ -370,6 +383,8 @@ impl LanceDBOps { // GA-P0-1 (t_718d8707):拆分字段读侧接线 human_not_useful_count: col_i64(&batch, i, "human_not_useful_count"), dedup_downweight_count: col_i64(&batch, i, "dedup_downweight_count"), + // A10 (t_d618dc7c):读侧接线 —— human_useful_count 走可空读法(NULL 读回 None) + human_useful_count: col_i64_opt(&batch, i, "human_useful_count"), freshness: col_str(&batch, i, "freshness"), version: col_i64(&batch, i, "version"), version_history: String::new(), @@ -433,6 +448,8 @@ impl LanceDBOps { // 2026-10-08 (Gap#2):nullable 列用 Option,未声明时写 None → 库里是 NULL let mut confidences: Vec> = Vec::new(); let mut confidence_sources: Vec<&str> = Vec::new(); + // A10 (t_d618dc7c):人工有用单独计数(nullable,与 confidence 同款「未声明写 NULL」) + let mut human_useful_counts: Vec> = Vec::new(); for r in &records { ids.push(r["id"].as_str().unwrap_or("")); @@ -455,6 +472,9 @@ impl LanceDBOps { not_useful_counts.push(r["not_useful_count"].as_i64().unwrap_or(0)); human_not_useful_counts.push(r["human_not_useful_count"].as_i64().unwrap_or(0)); // GA-P0-1 dedup_downweight_counts.push(r["dedup_downweight_count"].as_i64().unwrap_or(0)); // GA-P0-1 + // A10 (t_d618dc7c):未声明/NULL → None(写 NULL);显式给了整数 → Some(v)。 + // 与 confidence 同款:库里 NULL = 「从未被人工点过」,非 0。 + human_useful_counts.push(r["human_useful_count"].as_i64()); freshnesses.push(r["freshness"].as_str().unwrap_or("fresh")); versions.push(r["version"].as_i64().unwrap_or(1)); version_histories.push(r["version_history"].as_str().unwrap_or("[]")); @@ -540,6 +560,9 @@ impl LanceDBOps { Arc::new(StringArray::from(valid_froms)), Arc::new(StringArray::from(valid_tos)), Arc::new(StringArray::from(provenances)), + // A10 (t_d618dc7c):human_useful_count 列(nullable,NULL=从未被人工点过), + // 追加在 arrow_schema() 尾字段 human_useful_count 之后,顺序严格一致。 + Arc::new(Int64Array::from(human_useful_counts)), ]; let batch = RecordBatch::try_new(schema.clone(), columns)?; @@ -584,7 +607,11 @@ impl LanceDBOps { for f in &fields { let col = f["column"].as_str().unwrap_or(""); if !col.is_empty() { - let val = col_i64(&batch, i, col); + // A10 (t_d618dc7c):NULL-aware 自增基数 —— 可空列(human_useful_count) + // 的 NULL 行「从未被人工点过」,$inc 时按 0 起算(NULL + 1 ⇒ 1)。 + // 用 col_i64_opt(NULL→None)而非 col_i64(NULL→物理值 0), + // 不依赖 Arrow 物理缓冲约定,语义显式。非空列行为不变。 + let val = col_i64_opt(&batch, i, col).unwrap_or(0); current_values.insert(col.to_string(), val); } } @@ -1031,6 +1058,8 @@ impl LanceDBOps { not_useful_count: col_i64(batch, i, "not_useful_count"), human_not_useful_count: col_i64(batch, i, "human_not_useful_count"), dedup_downweight_count: col_i64(batch, i, "dedup_downweight_count"), + // A10 (t_d618dc7c):读侧接线 —— human_useful_count 走可空读法(NULL 读回 None) + human_useful_count: col_i64_opt(batch, i, "human_useful_count"), freshness: col_str(batch, i, "freshness"), version: col_i64(batch, i, "version"), version_history: String::new(), @@ -1071,6 +1100,7 @@ impl LanceDBOps { "source", "volatile_flag", "is_deleted", // GA-P0-1 (t_718d8707):scan_all 投影裁剪,漏加 ⇒ col_i64 取不到列 ⇒ 恒 0 "human_not_useful_count", "dedup_downweight_count", + "human_useful_count", // GA-P0-2 (t_c6d41ac6):拆分字段投影,漏加 ⇒ col_i64 取不到列 ⇒ 恒 0 "recall_seen_count", "recall_verified_count", "last_recalled_at", "created_at", "updated_at", @@ -1108,6 +1138,8 @@ impl LanceDBOps { // GA-P0-1 (t_718d8707):拆分字段读侧接线 human_not_useful_count: col_i64(&batch, i, "human_not_useful_count"), dedup_downweight_count: col_i64(&batch, i, "dedup_downweight_count"), + // A10 (t_d618dc7c):读侧接线 —— human_useful_count 走可空读法(NULL 读回 None) + human_useful_count: col_i64_opt(&batch, i, "human_useful_count"), freshness: col_str(&batch, i, "freshness"), version: col_i64(&batch, i, "version"), version_history: String::new(), @@ -1156,6 +1188,7 @@ impl LanceDBOps { "useful_count", "not_useful_count", "freshness", "version", "source", "volatile_flag", "is_deleted", "human_not_useful_count", "dedup_downweight_count", + "human_useful_count", "recall_seen_count", "recall_verified_count", "delete_reason", // GA-P0-3 "last_recalled_at", "created_at", "updated_at", @@ -1190,6 +1223,8 @@ impl LanceDBOps { not_useful_count: col_i64(&batch, i, "not_useful_count"), human_not_useful_count: col_i64(&batch, i, "human_not_useful_count"), dedup_downweight_count: col_i64(&batch, i, "dedup_downweight_count"), + // A10 (t_d618dc7c):读侧接线 —— human_useful_count 走可空读法(NULL 读回 None) + human_useful_count: col_i64_opt(&batch, i, "human_useful_count"), freshness: col_str(&batch, i, "freshness"), version: col_i64(&batch, i, "version"), version_history: String::new(), @@ -1341,6 +1376,13 @@ fn col_f64_opt(b: &RecordBatch, r: usize, c: &str) -> Option { .and_then(|col| col.as_any().downcast_ref::()) .and_then(|a| if a.is_null(r) { None } else { Some(a.value(r)) }) } +/// col_i64_opt (t_d618dc7c):可空 Int64 读法。列不存在或该行是 NULL 都返回 None(区别于 0)。 +/// 用于 human_useful_count —— 存量行/从未被人工点过的是 NULL,要读回 None("未知")而非 0。 +fn col_i64_opt(b: &RecordBatch, r: usize, c: &str) -> Option { + b.column_by_name(c) + .and_then(|col| col.as_any().downcast_ref::()) + .and_then(|a| if a.is_null(r) { None } else { Some(a.value(r)) }) +} fn col_i64(b: &RecordBatch, r: usize, c: &str) -> i64 { b.column_by_name(c) .and_then(|col| col.as_any().downcast_ref::())