From 0030dcf57cd80699db39581304f0956a8239fae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF?= Date: Wed, 18 Nov 2026 09:10:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(distill):=20=E9=99=8D=E7=BA=A7=E8=92=B8?= =?UTF-8?q?=E9=A6=8F=E5=8F=AF=E5=88=86=E8=BE=A8=E6=80=A7=20=E2=80=94?= =?UTF-8?q?=E2=80=94=20llm=5Fdistill:rule=5Ffallback=20+=20memories=20sour?= =?UTF-8?q?ce/since=20=E5=AE=9A=E5=90=91=E6=8A=BD=E6=A0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ① distill.DistillResult.Mode(llm/llm_heuristic/fallback):fallbackSingle 与 extractFacts 回落显式标模式;写入侧 OnDistillComplete 对**无出处**的降级产物落 llm_distill:rule_fallback(有出处仍 wiki: 逐字不变,llm 路径逐字不变)。 ② 判据同步:governance.processSources 加该值(不写图);storage.SourceClass 前缀匹配 仍 0.7;selfoptimize.SourceTrust 同档。 ③ GET /api/v1/memories 新增 source=(前缀) / since=(unix|RFC3339),item 带 source。 🔴 实测教训:RustLanceDBClient.Scan 是存储序且硬上限 5000,46k 行下只看到 9 月老行 ⇒ 过滤路径改走 ScanAll(200000, 只投元数据列) + Go 侧排序;不可用时降级并回 degraded 字段自曝(不静默返回偏旧空结果)。 --- go/internal/api/distill_source_test.go | 44 ++++ go/internal/api/routes/admin.go | 112 ++++++++- .../api/routes/admin_memories_filter_test.go | 228 ++++++++++++++++++ go/internal/api/server.go | 18 +- go/internal/distill/engine.go | 25 +- go/internal/distill/mode_mark_test.go | 79 ++++++ go/internal/governance/graph_auto.go | 25 +- .../governance/rule_fallback_source_test.go | 48 ++++ go/internal/selfoptimize/selfoptimize.go | 4 +- go/internal/storage/importance_rule.go | 4 +- .../importance_rule_rulefallback_test.go | 34 +++ rust/src/bin/importance_backfill.rs | 4 +- 12 files changed, 598 insertions(+), 27 deletions(-) create mode 100644 go/internal/api/distill_source_test.go create mode 100644 go/internal/api/routes/admin_memories_filter_test.go create mode 100644 go/internal/distill/mode_mark_test.go create mode 100644 go/internal/governance/rule_fallback_source_test.go create mode 100644 go/internal/storage/importance_rule_rulefallback_test.go diff --git a/go/internal/api/distill_source_test.go b/go/internal/api/distill_source_test.go new file mode 100644 index 0000000..4ffa858 --- /dev/null +++ b/go/internal/api/distill_source_test.go @@ -0,0 +1,44 @@ +package api + +import ( + "testing" + + "github.com/xiaoxue/memoryweave/internal/distill" +) + +// 2026-11-18 降级可分辨性:写入侧 Source 判据(server.go OnDistillComplete 唯一落库点)。 +// 判据铁律: +// - 规则/启发式兜底 + 无出处 ⇒ llm_distill:rule_fallback(可被 source= 前缀定向抽样) +// - 有出处 ⇒ wiki: 语义不变(出处比兜底更具体,不能被兜底标记覆盖) +// - Mode=llm ⇒ 与历史逐字一致 +func TestDistillMemSource(t *testing.T) { + cases := []struct { + name string + source string + mode string + want string + }{ + {"fallback 无出处 → 打兜底标", "", distill.ModeFallback, "llm_distill:rule_fallback"}, + {"启发式 无出处 → 打兜底标", "", distill.ModeLLMHeuristic, "llm_distill:rule_fallback"}, + {"fallback 有出处 → 保留 wiki 语义", "vault/note.md", distill.ModeFallback, "wiki:vault/note.md"}, + {"启发式 有出处 → 保留 wiki 语义", "vault/note.md", distill.ModeLLMHeuristic, "wiki:vault/note.md"}, + {"llm 无出处 → 历史值", "", distill.ModeLLM, "llm_distill"}, + {"llm 有出处 → 历史值", "vault/note.md", distill.ModeLLM, "wiki:vault/note.md"}, + {"Mode 空(旧调用方)→ 不误标", "", "", "llm_distill"}, + } + for _, c := range cases { + if got := distillMemSource(c.source, c.mode); got != c.want { + t.Errorf("%s: distillMemSource(%q, %q) = %q, want %q", c.name, c.source, c.mode, got, c.want) + } + } +} + +// 反向守卫:srcOrLLMDistill 的历史语义未被改动(其它调用方/导出依赖它)。 +func TestSrcOrLLMDistillUnchanged(t *testing.T) { + if got := srcOrLLMDistill(""); got != "llm_distill" { + t.Errorf("srcOrLLMDistill(\"\") = %q, want llm_distill", got) + } + if got := srcOrLLMDistill("a/b.md"); got != "wiki:a/b.md" { + t.Errorf("srcOrLLMDistill(\"a/b.md\") = %q, want wiki:a/b.md", got) + } +} diff --git a/go/internal/api/routes/admin.go b/go/internal/api/routes/admin.go index 121902d..415bf63 100644 --- a/go/internal/api/routes/admin.go +++ b/go/internal/api/routes/admin.go @@ -10,11 +10,14 @@ import ( "os" "os/exec" "path/filepath" + "sort" + "strconv" "strings" "time" "github.com/xiaoxue/memoryweave/internal/governance" "github.com/xiaoxue/memoryweave/internal/memprotect" + "github.com/xiaoxue/memoryweave/internal/models" "github.com/xiaoxue/memoryweave/internal/storage" ) @@ -498,10 +501,19 @@ func isAllDigits(s string) bool { // GET /api/v1/memories — 批量列出记忆(按时间倒序,不含向量) // 2026-09-21:改用全表扫描(aa.LanceDB.Scan)替代「零向量 ANN 冒充列表」。 // 修前零向量无方向 ⇒ 只拿到存储序前 N 行再按 ns 过滤 ⇒ 列表/计数失真。 +// +// 2026-11-18 定向抽样:新增 source=(前缀匹配:source=llm_distill 同时命中 +// llm_distill 与 llm_distill:rule_fallback;source=llm_distill:rule_fallback 只命中后者) +// 与 since=(unix 秒或 RFC3339,created_at >= since)。两者皆空 ⇒ 行为与历史逐字一致。 +// 过滤在 Go 侧做:活体后端 RustLanceDBClient 的 Scan 只下推 namespace(Rust 侧 +// lancedb_scan 无 source/时间条件),故带过滤时先按 listScanCap 取满再筛后截 limit。 +const listScanCap = 5000 // = lancedb_ipc.go Scan 的 Rust 侧硬上限 + func (aa *AdminAPI) ListMemories(w http.ResponseWriter, r *http.Request) { limit := 100 - ns := r.URL.Query().Get("namespace") - if l := r.URL.Query().Get("limit"); l != "" { + q := r.URL.Query() + ns := q.Get("namespace") + if l := q.Get("limit"); l != "" { if v, err := fmt.Sscanf(l, "%d", &limit); err != nil || v != 1 || limit < 1 { limit = 100 } @@ -509,11 +521,56 @@ func (aa *AdminAPI) ListMemories(w http.ResponseWriter, r *http.Request) { limit = 1000 } } - results, err := aa.LanceDB.Scan(limit, ns) - if err != nil { - respondError(w, 500, "list memories: "+err.Error()) + sourcePrefix := strings.TrimSpace(q.Get("source")) + since, sinceErr := parseSinceParam(q.Get("since")) + if sinceErr != nil { + respondError(w, 400, "invalid since: "+sinceErr.Error()) return } + filtered := sourcePrefix != "" || !since.IsZero() + + // 候选池:带过滤时必须拿到「新→旧」且能覆盖全表的数据。 + // 教训(2026-11-18 实测):RustLanceDBClient.Scan 是**存储序**(46k 行下 limit 内 + // 全是 09 月的老行)且 Rust 侧硬上限 5000 ⇒ 用 Scan 做 since/source 过滤, + // 新写入的兜底产物永远进不了候选池,抽样恒空(since=now-1h 实测 count=0)。 + // 所以带过滤走 ScanAll(Rust 侧 200000 上限、只投影元数据列、heavy_read 信号量 + // 准入),Go 侧排序 + 过滤;ScanAll 不可用(后端不支持 / heavy_read 忙)时降级 + // 回 Scan 并在响应里显式标 degraded(不许静默给空结果)。 + var results []models.MemoryRecord + degraded := "" + scanAllOK := false + if filtered { + if sa, ok := aa.LanceDB.(interface { + ScanAll() ([]models.MemoryRecord, error) + }); ok { + rows, err := sa.ScanAll() + if err != nil { + degraded = "scan_all failed, fell back to Scan(5000 storage-order): " + err.Error() + } else { + results, scanAllOK = rows, true + } + } else { + degraded = "backend has no ScanAll, fell back to Scan(5000 storage-order)" + } + if !scanAllOK { + rows, err := aa.LanceDB.Scan(listScanCap, ns) + if err != nil { + respondError(w, 500, "list memories: "+err.Error()) + return + } + results = rows + } + sort.SliceStable(results, func(i, j int) bool { + return results[i].CreatedAt.After(results[j].CreatedAt) + }) + } else { + rows, err := aa.LanceDB.Scan(limit, ns) + if err != nil { + respondError(w, 500, "list memories: "+err.Error()) + return + } + results = rows + } // 去掉向量字段,减少响应体积 type memoryItem struct { ID string `json:"id"` @@ -522,18 +579,55 @@ func (aa *AdminAPI) ListMemories(w http.ResponseWriter, r *http.Request) { Namespace string `json:"namespace,omitempty"` Importance float64 `json:"importance"` CreatedAt int64 `json:"created_at"` + Source string `json:"source"` } - items := make([]memoryItem, 0, len(results)) + items := make([]memoryItem, 0, limit) for _, m := range results { + if filtered { + // ScanAll 不认 namespace ⇒ 在 Go 侧补(Scan 路径已由 Rust 下推,重复判断无害) + if ns != "" && m.Namespace != ns { + continue + } + if sourcePrefix != "" && !strings.HasPrefix(m.Source, sourcePrefix) { + continue + } + if !since.IsZero() && m.CreatedAt.Before(since) { + continue + } + } + if len(items) >= limit { + break + } items = append(items, memoryItem{ ID: m.ID, Content: m.Content, Category: m.Category, Namespace: m.Namespace, Importance: m.Importance, - CreatedAt: m.CreatedAt.Unix(), + CreatedAt: m.CreatedAt.Unix(), Source: m.Source, }) } - respond(w, 200, map[string]interface{}{ + resp := map[string]interface{}{ "memories": items, "count": len(items), "limit": limit, - }) + } + if degraded != "" { + // 抽样可信度自曝:降级路径下结果只覆盖存储序前 5000 行,可能偏旧 + resp["degraded"] = degraded + } + respond(w, 200, resp) +} + +// parseSinceParam 解析 since= 参数:空 ⇒ 零值(不过滤);unix 秒;RFC3339。 +func parseSinceParam(raw string) (time.Time, error) { + raw = strings.TrimSpace(raw) + if raw == "" { + return time.Time{}, nil + } + if sec, err := strconv.ParseInt(raw, 10, 64); err == nil { + return time.Unix(sec, 0), nil + } + t, err := time.Parse(time.RFC3339, raw) + if err != nil { + return time.Time{}, fmt.Errorf("want unix seconds or RFC3339, got %q", raw) + } + return t, nil } // ExportMemoriesMD 导出记忆为 Markdown(P5:EverOS 式 md 真相层) diff --git a/go/internal/api/routes/admin_memories_filter_test.go b/go/internal/api/routes/admin_memories_filter_test.go new file mode 100644 index 0000000..02c0a74 --- /dev/null +++ b/go/internal/api/routes/admin_memories_filter_test.go @@ -0,0 +1,228 @@ +// 织忆 MemoryWeave — /api/v1/memories 定向抽样(2026-11-18 降级可分辨性) +// +// 验收口径: +// +// source=llm_distill → 前缀命中 llm_distill 与 llm_distill:rule_fallback(同族) +// source=llm_distill:rule_fallback → 只命中兜底产物(子串/模糊都不行) +// since= → unix 秒 或 RFC3339,created_at >= since +// 空参数 → 行为与历史一致(limit 生效、按存储序返回) +package routes + +import ( + "encoding/json" + "net/http/httptest" + "strconv" + "testing" + "time" + + "github.com/xiaoxue/memoryweave/internal/models" +) + +// listMemLance 复用 admin_forget_test.go 的 mock 骨架(嵌入后覆写 Scan / ScanAll)。 +type listMemLance struct { + *forgetMockLance + recs []models.MemoryRecord + lastLimit int + lastNS string + scanAllCalls int +} + +// ScanAll 模拟活体后端 RustLanceDBClient.ScanAll:**全量 + 存储序(不排序)**。 +// 活体实测存储序是老行在前(46k 行下 Scan(5000) 全是 9 月行),这里故意把新行放最后, +// 用来锁「排序必须由 routes 层做」这条判据。 +func (m *listMemLance) ScanAll() ([]models.MemoryRecord, error) { + m.scanAllCalls++ + return append([]models.MemoryRecord(nil), m.recs...), nil +} + +func (m *listMemLance) Scan(limit int, namespaceFilter string) ([]models.MemoryRecord, error) { + m.lastLimit, m.lastNS = limit, namespaceFilter + out := make([]models.MemoryRecord, 0, len(m.recs)) + for _, r := range m.recs { + if namespaceFilter != "" && r.Namespace != namespaceFilter { + continue + } + out = append(out, r) + } + if limit > 0 && len(out) > limit { + out = out[:limit] + } + return out, nil +} + +type listMemItem struct { + ID string `json:"id"` + Content string `json:"content"` + Namespace string `json:"namespace"` + CreatedAt int64 `json:"created_at"` + Source string `json:"source"` +} + +type listMemResp struct { + Memories []listMemItem `json:"memories"` + Count int `json:"count"` + Limit int `json:"limit"` +} + +func newListMemAPI(now time.Time) (*listMemLance, *AdminAPI) { + mock := &listMemLance{ + forgetMockLance: &forgetMockLance{}, + recs: []models.MemoryRecord{ + {ID: "m1", Content: "LLM 蒸馏产物", Namespace: "ns1", CreatedAt: now.Add(-1 * time.Hour), Source: "llm_distill"}, + {ID: "m2", Content: "规则兜底产物", Namespace: "ns1", CreatedAt: now.Add(-2 * time.Hour), Source: "llm_distill:rule_fallback"}, + {ID: "m3", Content: "笔记蒸馏产物", Namespace: "ns1", CreatedAt: now.Add(-3 * time.Hour), Source: "wiki:vault/a.md"}, + {ID: "m4", Content: "旧兜底产物", Namespace: "ns1", CreatedAt: now.Add(-72 * time.Hour), Source: "llm_distill:rule_fallback"}, + {ID: "m5", Content: "别的命名空间", Namespace: "ns2", CreatedAt: now.Add(-1 * time.Hour), Source: "llm_distill:rule_fallback"}, + }, + } + return mock, NewAdminAPI(mock, nil, nil) +} + +func doListMem(t *testing.T, aa *AdminAPI, query string) (int, listMemResp) { + t.Helper() + req := httptest.NewRequest("GET", "/api/v1/memories"+query, nil) + rr := httptest.NewRecorder() + aa.ListMemories(rr, req) + var body listMemResp + if rr.Code == 200 { + if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil { + t.Fatalf("解析响应失败: %v (body=%s)", err, rr.Body.String()) + } + } + return rr.Code, body +} + +func ids(items []listMemItem) []string { + out := make([]string, 0, len(items)) + for _, it := range items { + out = append(out, it.ID) + } + return out +} + +func TestListMemoriesSourcePrefix(t *testing.T) { + now := time.Now() + _, aa := newListMemAPI(now) + + // 同族:llm_distill 必须同时命中带标记的兜底产物 + code, body := doListMem(t, aa, "?namespace=ns1&source=llm_distill") + if code != 200 { + t.Fatalf("status=%d, want 200", code) + } + if got := ids(body.Memories); len(got) != 3 || got[0] != "m1" || got[1] != "m2" || got[2] != "m4" { + t.Fatalf("source=llm_distill 命中 %v, want [m1 m2 m4](前缀匹配:含带标记的兜底产物)", got) + } + + // 精确到子串:只命中兜底产物 + _, body = doListMem(t, aa, "?namespace=ns1&source=llm_distill:rule_fallback") + if got := ids(body.Memories); len(got) != 2 || got[0] != "m2" || got[1] != "m4" { + t.Fatalf("source=llm_distill:rule_fallback 命中 %v, want [m2 m4]", got) + } + + // 不许被 wiki 误命中 + _, body = doListMem(t, aa, "?namespace=ns1&source=wiki:") + if got := ids(body.Memories); len(got) != 1 || got[0] != "m3" { + t.Fatalf("source=wiki: 命中 %v, want [m3]", got) + } +} + +func TestListMemoriesSince(t *testing.T) { + now := time.Now() + _, aa := newListMemAPI(now) + + // unix 秒 + code, body := doListMem(t, aa, "?namespace=ns1&since="+strconv.FormatInt(now.Add(-24*time.Hour).Unix(), 10)) + if code != 200 { + t.Fatalf("status=%d, want 200", code) + } + if got := ids(body.Memories); len(got) != 3 { + t.Fatalf("since=unixts 命中 %v, want 3 条(m1/m2/m3,排除 72h 前的 m4)", got) + } + + // RFC3339 + _, body = doListMem(t, aa, "?namespace=ns1&since="+now.Add(-90*time.Minute).UTC().Format(time.RFC3339)) + if got := ids(body.Memories); len(got) != 1 || got[0] != "m1" { + t.Fatalf("since=RFC3339 命中 %v, want [m1]", got) + } + + // 非法 since ⇒ 400(不许静默当空参) + if code, _ := doListMem(t, aa, "?since=yesterday"); code != 400 { + t.Fatalf("since=yesterday status=%d, want 400", code) + } +} + +func TestListMemoriesNoFilterKeepsLegacyBehavior(t *testing.T) { + now := time.Now() + mock, aa := newListMemAPI(now) + + code, body := doListMem(t, aa, "?namespace=ns1&limit=2") + if code != 200 { + t.Fatalf("status=%d, want 200", code) + } + if len(body.Memories) != 2 || body.Count != 2 || body.Limit != 2 { + t.Fatalf("无过滤时应原样截 limit:count=%d limit=%d items=%v", body.Count, body.Limit, ids(body.Memories)) + } + if mock.lastLimit != 2 { + t.Errorf("无过滤时 Scan limit=%d, want 2(不得为抽样放大扫描面)", mock.lastLimit) + } + // item 必须带 source(抽样与可视化都要用) + if body.Memories[0].Source != "llm_distill" { + t.Errorf("item.source = %q, want llm_distill", body.Memories[0].Source) + } + + // 带过滤时**不得**再走存储序 Scan(46k 行下够不到新行 ⇒ 抽样恒空),必须走 ScanAll + _, _ = doListMem(t, aa, "?namespace=ns1&limit=2&source=llm_distill:rule_fallback") + if mock.scanAllCalls != 1 { + t.Errorf("带过滤时 ScanAll 调用 %d 次, want 1(存储序 Scan 只能看到最旧行)", mock.scanAllCalls) + } +} + +// 活体存储序是老行在前:过滤结果必须按 created_at 新→旧,且能取到最新那条。 +func TestListMemoriesFilteredNewestFirst(t *testing.T) { + now := time.Now() + mock, aa := newListMemAPI(now) + + _, body := doListMem(t, aa, "?namespace=ns1&since="+strconv.FormatInt(now.Add(-96*time.Hour).Unix(), 10)) + if len(body.Memories) != 4 { + t.Fatalf("since=-96h 命中 %d 条, want 4(m1/m2/m3/m4,排除别的 ns)", len(body.Memories)) + } + for i := 1; i < len(body.Memories); i++ { + if body.Memories[i].CreatedAt > body.Memories[i-1].CreatedAt { + t.Fatalf("结果未按新→旧排序: %v", ids(body.Memories)) + } + } + if body.Memories[0].ID != "m1" { + t.Errorf("首条 = %s, want m1(最新)", body.Memories[0].ID) + } + _ = mock +} + +// 后端没有 ScanAll(或 heavy_read 忙)⇒ 允许降级,但必须在响应里自曝 degraded, +// 不许静默返回偏旧的空结果。 +func TestListMemoriesDegradedFlagWhenNoScanAll(t *testing.T) { + aa := NewAdminAPI(&forgetMockLance{}, nil, nil) // 只有 Scan,无 ScanAll + code, body := doListMem(t, aa, "?source=llm_distill") + if code != 200 { + t.Fatalf("status=%d, want 200(降级不是错误)", code) + } + raw := rawListMem(t, aa, "?source=llm_distill") + if _, ok := raw["degraded"]; !ok { + t.Fatalf("降级路径未自曝 degraded: %v", raw) + } + if len(body.Memories) != 0 { + t.Errorf("空后端应返回 0 条, got %d", len(body.Memories)) + } +} + +// rawListMem 拿原始 JSON 体(degraded 等可选字段不在固定结构里)。 +func rawListMem(t *testing.T, aa *AdminAPI, query string) map[string]interface{} { + t.Helper() + req := httptest.NewRequest("GET", "/api/v1/memories"+query, nil) + rr := httptest.NewRecorder() + aa.ListMemories(rr, req) + var out map[string]interface{} + if err := json.Unmarshal(rr.Body.Bytes(), &out); err != nil { + t.Fatalf("解析响应失败: %v", err) + } + return out +} diff --git a/go/internal/api/server.go b/go/internal/api/server.go index e09cb1c..6db9cdd 100644 --- a/go/internal/api/server.go +++ b/go/internal/api/server.go @@ -417,7 +417,7 @@ func NewServer() http.Handler { // AC-1.2(t_e3f5cb79):真矛盾的新行带 supersedes=[旧id](双向指针的 // 新行半边;旧行半边在 ldbWriteOpsStore.MarkConflict 写 superseded_by)。 Supersedes: supersedes, - Source: srcOrLLMDistill(input.Source), // 2026-09-26:有笔记出处→"wiki:<路径>";否则沿用 llm_distill(2026-09-07 方案C provenance) + Source: distillMemSource(input.Source, result.Mode), // 2026-09-26:有笔记出处→"wiki:<路径>";否则沿用 llm_distill(2026-09-07 方案C provenance);2026-11-18 规则兜底产物 → llm_distill:rule_fallback // 2026-10-08 (t_7e3577c5):血缘字段——写入方经 metadata 携带;缺省兜底 // embedding_model→bge-m3(实际嵌入模型),script_version→unknown。 EmbeddingModel: orDefault(input.EmbeddingModel, "bge-m3"), @@ -2353,6 +2353,22 @@ func srcOrLLMDistill(src string) string { return "llm_distill" } +// distillMemSource 蒸馏产物的 MemoryRecord.Source 赋值(唯一落库点的判据收口)。 +// +// 2026-11-18 降级可分辨性:LLM 不通/半通时产物来自规则或启发式(facts 只是正文截断 +// 或 extractFacts 抽取),质量与真 LLM 产物不同档,需要能被事后定向抽样复核。 +// 判据只认 DistillResult.Mode,不猜内容: +// - Mode=fallback | llm_heuristic 且无笔记出处 ⇒ "llm_distill:rule_fallback" +// (前缀保留 llm_distill ⇒ 进程层/importance 判据照旧命中) +// - 有出处(input.Source 非空)⇒ wiki: 语义逐字不变(出处比兜底更具体) +// - Mode=llm ⇒ 与历史逐字一致(llm_distill / wiki:) +func distillMemSource(inputSource, mode string) string { + if inputSource == "" && (mode == distill.ModeFallback || mode == distill.ModeLLMHeuristic) { + return "llm_distill:rule_fallback" + } + return srcOrLLMDistill(inputSource) +} + // orDefault 血缘字段缺省兜底:空值 → 默认值。 // 2026-10-08 (t_7e3577c5):embedding_model→bge-m3,script_version→unknown, // 使未发 metadata 的写入路径(worklog/cbm/会话蒸馏)也落非空血缘。 diff --git a/go/internal/distill/engine.go b/go/internal/distill/engine.go index 0fd91e4..780e4a1 100644 --- a/go/internal/distill/engine.go +++ b/go/internal/distill/engine.go @@ -66,8 +66,22 @@ type DistillResult struct { // 且解析出合法 EXP 行时非空;关态恒 nil ⇒ 下游经验分支不可达)。 // 🔴 不进 Facts/图谱/AAAK —— 独立 category="experience" 记录走自己的落库闸。 Experiences []ExperienceRecord + + // Mode 产物模式(2026-11-18 降级可分辨性):零 schema 变更,靠它 + 写入侧 + // source 标记区分「真 LLM 产物」与「规则/启发式兜底产物」(后者需要定向抽样复核)。 + // ModeLLM LLM 成功,facts 与 entities 均来自 LLM + // ModeLLMHeuristic LLM 通,但 facts 或 entities 回落到 extractFacts 启发式抽取 + // ModeFallback fallbackSingle 路径(端点空 或 callLLM5D 报错) + Mode string } +// DistillResult.Mode 取值。 +const ( + ModeLLM = "llm" + ModeLLMHeuristic = "llm_heuristic" + ModeFallback = "fallback" +) + // Entity 实体 type Entity struct { Name string `json:"name"` @@ -566,7 +580,7 @@ func (e *Engine) distillOne(input DistillInput) DistillResult { log.Printf("[distill] lane=drop reason=%s for %s, skip (failure+1)", laneWhy, input.EpisodeID) // 即使丢弃,仍然用 LLM 实体提取(如果有的话) if len(llmResp.Entities) == 0 { - return DistillResult{} + return DistillResult{Mode: ModeLLM} } } case LaneKeep: @@ -580,6 +594,8 @@ func (e *Engine) distillOne(input DistillInput) DistillResult { } // 优先使用 LLM 提取的实体和事实 + // mode 默认 llm;任一维回落到 extractFacts 启发式 ⇒ 降级标 llm_heuristic。 + mode := ModeLLM var entities []Entity var facts []string @@ -591,6 +607,7 @@ func (e *Engine) distillOne(input DistillInput) DistillResult { } } else { // 降级:启发式提取 + mode = ModeLLMHeuristic _, heuristicEntities := e.extractFacts(input.Content) entities = heuristicEntities } @@ -611,6 +628,8 @@ func (e *Engine) distillOne(input DistillInput) DistillResult { } else if len(llmResp.Facts) > 0 { facts = llmResp.Facts } else { + // LLM 既没给结构化字段也没给 facts ⇒ 回落启发式抽取(降级标记) + mode = ModeLLMHeuristic heuristicFacts, _ := e.extractFacts(input.Content) facts = heuristicFacts } @@ -661,6 +680,7 @@ func (e *Engine) distillOne(input DistillInput) DistillResult { Overall: overall, Index: index, Experiences: llmResp.Experiences, + Mode: mode, } } @@ -1315,7 +1335,8 @@ func fallbackSingle(input DistillInput) DistillResult { } // 质量门槛同样作用于 fallback 路径(t_cc07ef4b) facts = FilterQualityFacts(facts) - return DistillResult{Facts: facts} + // Mode=fallback:无 LLM 参与的规则兜底产物,写入侧据此打 llm_distill:rule_fallback + return DistillResult{Facts: facts, Mode: ModeFallback} } // fallbackDistill 批量降级蒸馏 diff --git a/go/internal/distill/mode_mark_test.go b/go/internal/distill/mode_mark_test.go new file mode 100644 index 0000000..dac7716 --- /dev/null +++ b/go/internal/distill/mode_mark_test.go @@ -0,0 +1,79 @@ +package distill + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" +) + +// 2026-11-18 降级可分辨性:DistillResult.Mode 的三个产出点必须各自正确赋值。 +// 判据是「产物从哪来」,不是「分数高低」—— 所以这里只锁 Mode,不去比 fact 内容。 + +const modeMarkContent = "用户今天在鹤壁买了咖啡并读了关于马哲经济学的书,下午讨论了剪辑软件选型。" + +// modeMarkEngine 构造最小引擎骨架:不经 NewEngine(它会起 ticker / 探活 / 读 env), +// ScoreCache 在测试包里已被 main_test.go 置 scoreCacheDisabled=true,不会短路桩调用。 +func modeMarkEngine(endpoint string) *Engine { + return &Engine{ + LLMEndpoint: endpoint, + LLMModel: "m", + OutputFormat: "json", // testLLMBody 是 JSON 协议体 + client: &http.Client{Timeout: 5 * time.Second}, + } +} + +// ① fallbackSingle 本身:无 LLM 参与的规则兜底 ⇒ Mode=fallback。 +func TestFallbackSingleModeIsFallback(t *testing.T) { + got := fallbackSingle(DistillInput{EpisodeID: "ep_mode_fb", Content: modeMarkContent}) + if got.Mode != ModeFallback { + t.Fatalf("fallbackSingle Mode = %q, want %q", got.Mode, ModeFallback) + } +} + +// ② 端点空 ⇒ distillOne 走 fallbackSingle ⇒ Mode=fallback(生产里最常见的那条降级)。 +func TestDistillOneModeFallbackWhenEndpointEmpty(t *testing.T) { + e := modeMarkEngine("") + got := e.distillOne(DistillInput{EpisodeID: "ep_mode_fb2", Content: modeMarkContent}) + if got.Mode != ModeFallback { + t.Fatalf("endpoint 空时 Mode = %q, want %q", got.Mode, ModeFallback) + } + if len(got.Facts) == 0 { + t.Fatal("前置条件不成立:fallback 应产出正文截断事实") + } +} + +// ③ LLM 成功且 facts/entities 都来自 LLM ⇒ Mode=llm(不许误标降级)。 +func TestDistillOneModeLLMOnSuccess(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(testLLMBody)) + })) + defer srv.Close() + + e := modeMarkEngine(srv.URL + "/v1/chat/completions") + got := e.distillOne(DistillInput{EpisodeID: "ep_mode_llm", Content: modeMarkContent}) + if got.Mode != ModeLLM { + t.Fatalf("LLM 成功路径 Mode = %q, want %q", got.Mode, ModeLLM) + } + if len(got.Entities) == 0 { + t.Fatal("前置条件不成立:testLLMBody 带 entities,本应非空") + } +} + +// ④ LLM 通了但 facts/entities 为空 ⇒ 回落 extractFacts 启发式 ⇒ Mode=llm_heuristic。 +func TestDistillOneModeLLMHeuristicWhenExtensionsEmpty(t *testing.T) { + // 合法分数行 + 空 facts/entities:模拟 2B 只肯打分、不肯抽事实 + const emptyExtract = `{"choices":[{"message":{"content":"{\"facts\":[],\"entities\":[],\"is\":0.8,\"su\":0.7,\"pa\":0.6,\"vd\":0.8,\"ru\":0.7}"}}]}` + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(emptyExtract)) + })) + defer srv.Close() + + e := modeMarkEngine(srv.URL + "/v1/chat/completions") + got := e.distillOne(DistillInput{EpisodeID: "ep_mode_heur", Content: modeMarkContent}) + if got.Mode != ModeLLMHeuristic { + t.Fatalf("facts/entities 皆空时 Mode = %q, want %q", got.Mode, ModeLLMHeuristic) + } +} diff --git a/go/internal/governance/graph_auto.go b/go/internal/governance/graph_auto.go index 7a82096..1f9faa2 100644 --- a/go/internal/governance/graph_auto.go +++ b/go/internal/governance/graph_auto.go @@ -259,18 +259,19 @@ func (agu *AutoGraphUpdater) RecordCoOccurrence(query, namespace string, resultI // ⚠️ 前缀归一(TrimPrefix "wiki:")后比对,与 auditSource 同规;库内真实值经 // srcOrLLMDistill 落成带前缀形式([srcprobe] 实测),裸串相等永远 miss。 var processSources = []string{ - "", // 空 source:sub-agent 会话蒸馏常无出处标记 - "llm_distill", // 无出处蒸馏的兜底名(srcOrLLMDistill 返回) - "session-compress", // 会话压缩蒸馏产物 - "skill_rule_extractor_v2", // 技能规则提取器(过程层) - "a06_skill_rule_tiera", // a06 技能规则 tierA(过程层,normalizeSrc 已 ToLower) - "a06_prefix_test", // a06 前缀探针 - "a06_probe_direct", // a06 直连探针 - "a06_distill_probe", // a06 蒸馏探针 - "a06_ns_test_1005", // a06 ns 测试 - "legacy:pre0926", // 迁移/历史层(2026-09-26 前导入) - "worklog-note", // 审计/工作流(1b 已立,保留) - "worklog-audit", // 审计/工作流(1b 已立,保留) + "", // 空 source:sub-agent 会话蒸馏常无出处标记 + "llm_distill", // 无出处蒸馏的兜底名(srcOrLLMDistill 返回) + "llm_distill:rule_fallback", // 规则/启发式兜底蒸馏(2026-11-18:fallbackSingle / extractFacts 回落,过程层) + "session-compress", // 会话压缩蒸馏产物 + "skill_rule_extractor_v2", // 技能规则提取器(过程层) + "a06_skill_rule_tiera", // a06 技能规则 tierA(过程层,normalizeSrc 已 ToLower) + "a06_prefix_test", // a06 前缀探针 + "a06_probe_direct", // a06 直连探针 + "a06_distill_probe", // a06 蒸馏探针 + "a06_ns_test_1005", // a06 ns 测试 + "legacy:pre0926", // 迁移/历史层(2026-09-26 前导入) + "worklog-note", // 审计/工作流(1b 已立,保留) + "worklog-audit", // 审计/工作流(1b 已立,保留) } // subAgentNamespaces 纯分身会话 namespace:命中则实体/概念不写图。 diff --git a/go/internal/governance/rule_fallback_source_test.go b/go/internal/governance/rule_fallback_source_test.go new file mode 100644 index 0000000..0284904 --- /dev/null +++ b/go/internal/governance/rule_fallback_source_test.go @@ -0,0 +1,48 @@ +// 织忆 MemoryWeave — 规则/启发式兜底蒸馏产物的图谱侧判据(2026-11-18 降级可分辨性) +// +// 背景:写入侧对 Mode=fallback|llm_heuristic 且无出处的产物打 source=llm_distill:rule_fallback。 +// 图谱侧必须把它归「过程层」—— 否则兜底产物(正文截断 + 启发式实体)会污染领域概念图。 +package governance + +import "testing" + +func TestSkipSourceClassRuleFallback(t *testing.T) { + // 四个形状都要命中:裸值、带 wiki: 前缀(normalizeSrc 会剥)、大小写混杂 + // (normalizeSrc 会 lowercase)、前后空白。 + for _, s := range []string{ + "llm_distill:rule_fallback", + "wiki:llm_distill:rule_fallback", + "LLM_DISTILL:Rule_Fallback", + " llm_distill:rule_fallback ", + } { + if !skipSourceClass(s) { + t.Errorf("skipSourceClass(%q) = false,兜底蒸馏产物必须归过程层(不写图)", s) + } + } + // 回归守卫:既有过程层来源仍命中 + if !skipSourceClass("llm_distill") { + t.Error("回归:llm_distill 本应仍属过程层") + } + // 反向守卫:领域知识来源不得被新枚举误伤 + if skipSourceClass("wiki:vault/2026-11-18.md") { + t.Error("wiki:vault 领域知识被误判为过程层") + } +} + +// 端到端口径:兜底产物带来的启发式实体不得进图(事实仍进记忆层,由 server.go 负责)。 +func TestUpdateFromDistillRuleFallbackWritesNoGraph(t *testing.T) { + g := NewInMemoryGraph() + up := NewAutoGraphUpdater(g) + up.UpdateFromDistill(&DistillInput{ + EpisodeID: "ep_rule_fallback_1", + Content: "用户今天在鹤壁买了咖啡并读了关于马哲经济学的书", + Facts: []string{"用户今天在鹤壁买了咖啡"}, + Entities: []string{"Docker", "Nginx"}, // 即便实体名很"领域",兜底来源也不许建节点 + Namespace: "ns_rule_fallback", + Source: "llm_distill:rule_fallback", + }) + + if nodes := g.ListNodes("ns_rule_fallback"); len(nodes) != 0 { + t.Fatalf("兜底蒸馏产物不该写图,实际建了 %d 个节点: %v", len(nodes), nodes) + } +} diff --git a/go/internal/selfoptimize/selfoptimize.go b/go/internal/selfoptimize/selfoptimize.go index cc63004..fefd695 100644 --- a/go/internal/selfoptimize/selfoptimize.go +++ b/go/internal/selfoptimize/selfoptimize.go @@ -1016,7 +1016,9 @@ func SourceTrust(source string) float64 { return 0.7 case "agent_infer": return 0.5 - case "llm_distill": + case "llm_distill", "llm_distill:rule_fallback": + // 2026-11-18 降级可分辨性:规则/启发式兜底产物与 llm_distill 同族同信任档 + // (此前落 default=0.3 属静默降档;本函数目前无生产调用方,仅口径对齐)。 return 0.4 default: return 0.3 diff --git a/go/internal/storage/importance_rule.go b/go/internal/storage/importance_rule.go index 2ec587e..2f1bbd0 100644 --- a/go/internal/storage/importance_rule.go +++ b/go/internal/storage/importance_rule.go @@ -60,8 +60,10 @@ func importanceRuleEnabled() bool { // SourceClass 把 source 归入分档类。未知/空 source ⇒ "other"(按低置信类处理, // 实测 0.04%,不独立成档 —— 拆档会产生 <1% 档,违反 AC2 验收)。 +// llm_distill 用**前缀**匹配:2026-11-18 起规则/启发式兜底产物落 +// "llm_distill:rule_fallback",与 llm_distill 同档(同为蒸馏管道产物,都给 0.7)。 func SourceClass(source string) string { - if source == "llm_distill" { + if strings.HasPrefix(source, "llm_distill") { return "llm_distill" } switch { diff --git a/go/internal/storage/importance_rule_rulefallback_test.go b/go/internal/storage/importance_rule_rulefallback_test.go new file mode 100644 index 0000000..47d941d --- /dev/null +++ b/go/internal/storage/importance_rule_rulefallback_test.go @@ -0,0 +1,34 @@ +// 织忆 MemoryWeave — 规则/启发式兜底蒸馏产物的 importance 分档(2026-11-18 降级可分辨性) +// +// 判据:source 前缀 llm_distill 与 llm_distill:rule_fallback 同档(0.7)—— +// 两者都是蒸馏管道产物;拆新档会产生 <1% 档,违反既有分档设计。 +package storage + +import ( + "testing" + + "github.com/xiaoxue/memoryweave/internal/models" +) + +func TestSourceClassRuleFallback(t *testing.T) { + if got := SourceClass("llm_distill:rule_fallback"); got != "llm_distill" { + t.Errorf("SourceClass(llm_distill:rule_fallback) = %q, want llm_distill", got) + } + // 反向守卫:hashPrefix 不能把别的类吸进来 + for _, s := range []string{"wiki:vault/a.md", "legacy:pre0926", "recovery:x", ""} { + if got := SourceClass(s); got == "llm_distill" { + t.Errorf("SourceClass(%q) = llm_distill,误伤", s) + } + } +} + +func TestComputeImportanceRuleFallback(t *testing.T) { + rec := models.MemoryRecord{Tier: "normal", Source: "llm_distill:rule_fallback"} + if got := ComputeImportance(rec); got != 0.7 { + t.Fatalf("ComputeImportance(rule_fallback) = %v, want 0.7", got) + } + // 同上档:普通 llm_distill 仍是 0.7(回归守卫) + if got := ComputeImportance(models.MemoryRecord{Tier: "normal", Source: "llm_distill"}); got != 0.7 { + t.Fatalf("ComputeImportance(llm_distill) = %v, want 0.7", got) + } +} diff --git a/rust/src/bin/importance_backfill.rs b/rust/src/bin/importance_backfill.rs index 32bff2a..7f18980 100644 --- a/rust/src/bin/importance_backfill.rs +++ b/rust/src/bin/importance_backfill.rs @@ -51,7 +51,9 @@ fn target_importance(tier: &str, source: &str) -> f64 { if tier == "core" { return 0.9; } - if source == "llm_distill" || source.starts_with("wiki:") { + // 2026-11-18 降级可分辨性:规则/启发式兜底产物落 llm_distill:rule_fallback, + // 与 llm_distill 同档(与 Go storage/importance_rule.go 的前缀判据保持一致)。 + if source.starts_with("llm_distill") || source.starts_with("wiki:") { return 0.7; } if source.starts_with("recovery:") {