From 092535cfc56eee4a2fbec06dce4f986f69d1e866 Mon Sep 17 00:00:00 2001 From: xiaoyi Date: Wed, 18 Nov 2026 02:40:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(graph):=20=E5=9B=BE=E8=B0=B1=E5=A4=8D?= =?UTF-8?q?=E6=B4=BB=E7=AC=AC=E4=B8=80=E6=AD=A5=20=E2=80=94=E2=80=94=20?= =?UTF-8?q?=E8=AF=BB=E4=BE=A7=E5=9F=8B=E7=82=B9=20+=20=E7=9F=A5=E8=AF=86?= =?UTF-8?q?=E5=BD=A2=E6=80=81=E5=BD=B1=E5=AD=90=E9=97=A8=EF=BC=88=E9=9B=B6?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA=E5=8F=98=E5=8C=96=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 背景(实测):graph_nodes MAX(created_at) 停在 11-16,24h 内 [graph] skip-entity 59 次(worklog-note 43 / 空 source 15)全部被来源分档挡掉 ⇒ 图谱 2 天零新增; navigate/query 此前完全无日志,'图有没有被读' 无观测(retrieval_count 只由 AddEdgeFeedback 显式 +1,navigate 不更新 —— 拿它当读计数是判据错误)。 本提交只做两件事,均可关(默认零行为变化): 1. [graph][read] 读侧埋点:/graph/navigate|query 每次打 op/entity/hops/paths。 2. ZHIYI_GRAPH_KNOWLEDGE_GATE=shadow|on:知识形态门(判据 = 蒸馏 facts 至少一条过 distill.DensityKeep 信息密度≥2 项,零 LLM)。shadow 只记 [graph][kg-shadow] '若开闸会写多少实体';on 才放行来源分档,§2 合成边仍由 GRAPH_AUTO_SYNTH 独立控制。 为什么不按 tier=core:蒸馏直写记忆 Tier 恒为 normal(api/server.go),全仓无写者置 core,现存 1926 条 core 全是历史 recovery 导入 ⇒ 按 tier 门等于零增量。 go build ./... ok;go test ./internal/governance ./internal/api/routes -run Graph ok; 生产已切 zhiyid-kgraph-20261118(shadow),回滚=删 zhiyid.service.d/zzzzzzzzzzzzzzz-kgraph-20261118.conf。 --- go/internal/api/routes/graph.go | 12 ++++++++++++ go/internal/api/server.go | 14 ++++++++++++-- go/internal/governance/graph_auto.go | 27 ++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/go/internal/api/routes/graph.go b/go/internal/api/routes/graph.go index 56959cf..f6ed322 100644 --- a/go/internal/api/routes/graph.go +++ b/go/internal/api/routes/graph.go @@ -3,6 +3,7 @@ package routes import ( "encoding/json" + "log" "net/http" "strings" "unicode" @@ -10,6 +11,14 @@ import ( "github.com/xiaoxue/memoryweave/internal/governance" ) +// graphReadLog 读侧埋点(2026-11-18):此前 Navigate/Query 完全无日志, +// 导致「图谱有没有被读」只能靠猜(retrieval_count 只由 AddEdgeFeedback 显式+1, +// navigate 不更新它 —— 拿它当读计数是判据错误)。埋这一行是为了让 +// 「消费者是否存在」变成可证伪的数字,而非脑补。 +func graphReadLog(op, entity string, hops int, count int) { + log.Printf("[graph][read] op=%s entity=%q hops=%d paths=%d", op, entity, hops, count) +} + type GraphAPI struct { Graph governance.GraphStore } @@ -61,6 +70,7 @@ func (ga *GraphAPI) Query(w http.ResponseWriter, r *http.Request) { } // 默认跨 namespace 搜索(空 = 匹配所有,§2.5.6) results := ga.Graph.Query(NormalizeEntity(req.Entity), req.Relation, req.Namespace) + graphReadLog("query", req.Entity, 0, len(results)) respond(w, 200, map[string]interface{}{"results": results, "count": len(results)}) } @@ -110,6 +120,7 @@ func (ga *GraphAPI) Navigate(w http.ResponseWriter, r *http.Request) { if totalPaths > maxNavigatePaths { paths = paths[:maxNavigatePaths] } + graphReadLog("navigate-bidir", req.Source+"->"+req.Target, req.MaxHops, totalPaths) respond(w, 200, map[string]interface{}{ "paths": paths, "source": req.Source, "target": req.Target, "bidirectional": true, "count": totalPaths, @@ -136,6 +147,7 @@ func (ga *GraphAPI) Navigate(w http.ResponseWriter, r *http.Request) { if totalPaths > maxNavigatePaths { paths = paths[:maxNavigatePaths] } + graphReadLog("navigate", req.Entity, req.MaxHops, totalPaths) // 新增:按 relation 分组,更直观 grouped := make(map[string][]map[string]interface{}) diff --git a/go/internal/api/server.go b/go/internal/api/server.go index e09cb1c..8b90b22 100644 --- a/go/internal/api/server.go +++ b/go/internal/api/server.go @@ -261,11 +261,21 @@ func NewServer() http.Handler { for i, e := range result.Entities { entityNames[i] = e.Name } + // 2026-11-18 图谱复活:知识形态标记(至少一条 fact 过信息密度判据 ⇒ 这是知识不是流水)。 + // 只做一次遍历,零 LLM;仅供 ZHIYI_GRAPH_KNOWLEDGE_GATE=shadow|on 使用,off 时下游不读 ⇒ 逐位等价。 + knowledgeShape := false + for _, f := range result.Facts { + if distill.DensityKeep(f) { + knowledgeShape = true + break + } + } graphUpdater.UpdateFromDistill(&governance.DistillInput{ Content: input.Content, Facts: result.Facts, Entities: entityNames, Namespace: input.Namespace, - EpisodeID: input.EpisodeID, - Source: input.Source, // 2026-10-12 t_67d0279a 方案A:来源分档透传(auditSource 前缀归一后比对) + EpisodeID: input.EpisodeID, + Source: input.Source, // 2026-10-12 t_67d0279a 方案A:来源分档透传(auditSource 前缀归一后比对) + KnowledgeShape: knowledgeShape, }) // 回写蒸馏产物到记忆库(LanceDB) diff --git a/go/internal/governance/graph_auto.go b/go/internal/governance/graph_auto.go index d9e703a..d24e285 100644 --- a/go/internal/governance/graph_auto.go +++ b/go/internal/governance/graph_auto.go @@ -96,11 +96,31 @@ func (agu *AutoGraphUpdater) UpdateFromDistill(distilled *DistillInput) { // 本层改为「按来源类 + 内容形态」:命中来源类(过程/会话/迁移层)或纯分身会话 // namespace → 实体/概念不写图;事实仍经同一次蒸馏进 LanceDB 记忆层(facts 照写)。 // 判据零改动(echo_guard.go 未动),语义边界:跳过的是「进图」,不是「进记忆」。 - if skipSourceClass(distilled.Source) || inSubAgentNamespace(distilled.Namespace) { + // 2026-11-18 知识形态门(env ZHIYI_GRAPH_KNOWLEDGE_GATE=shadow|on,默认 off ⇒ 逐位等价)。 + // 起因(实测):24h 内 §1 实体写图 59 次全被来源分档挡掉(worklog-note 43 / 空 source 15), + // graph_nodes 的 MAX(created_at) 停在 11-16 ⇒ 图谱 2 天零新增。 + // 「只对 tier=core 建」实测不可行:蒸馏直写记忆 Tier 恒为 "normal"(api/server.go), + // 全仓无任何写者置 core,现存 1926 条 core 全是历史 recovery 导入 ⇒ 按 tier 门等于零增量。 + // 故改用蒸馏侧已有的确定性判据 DensityKeep(信息密度三项满足 ≥2,零 LLM)作为「知识形态」: + // shadow:行为不变,只记「若开闸会写多少」(用于观察期取数,不改任何库存); + // on:知识形态 ⇒ 放行来源分档;§2 合成边仍由 ZHIYI_GRAPH_AUTO_SYNTH 独立控制(默认停写)。 + kgMode := strings.ToLower(strings.TrimSpace(os.Getenv("ZHIYI_GRAPH_KNOWLEDGE_GATE"))) + kgPass := kgMode == "on" && distilled.KnowledgeShape + if kgMode == "shadow" && distilled.KnowledgeShape && len(distilled.Entities) > 0 { + blocked := skipSourceClass(distilled.Source) || inSubAgentNamespace(distilled.Namespace) + log.Printf("[graph][kg-shadow] ep=%s source=%q ns=%q ents=%d blocked_by_source=%v", + distilled.EpisodeID, distilled.Source, distilled.Namespace, len(distilled.Entities), blocked) + } + + if !kgPass && (skipSourceClass(distilled.Source) || inSubAgentNamespace(distilled.Namespace)) { log.Printf("[graph] skip-entity for source=%q namespace=%q episode=%s(来源分档:实体/概念不写图,事实仍进记忆层)", distilled.Source, distilled.Namespace, distilled.EpisodeID) return } + if kgPass { + log.Printf("[graph][kg-on] pass ep=%s source=%q ns=%q ents=%d(知识形态放行来源分档)", + distilled.EpisodeID, distilled.Source, distilled.Namespace, len(distilled.Entities)) + } // 0. 密钥过滤(2026-09-10 P0,见 internal/redact):含凭证形状的实体不进图, // 否则实体名会被 ObsidianSync 当文件名,把密钥落成可读镜像文件 @@ -393,6 +413,11 @@ type DistillInput struct { // commit 写入方 metadata.source → distill.DistillInput.Source → 本字段。 // UpdateFromDistill 用它做来源分档:审计/工作流类来源跳过实体/概念写图。 Source string + // KnowledgeShape 知识形态标记(2026-11-18 图谱复活):本次蒸馏产出的 facts 中 + // 至少一条通过 distill.DensityKeep(信息密度 ≥2 项:可执行命令/路径、判据阈值、因果)。 + // 由 api 层算出并透传(governance 不 import distill,避免包循环)。 + // 只在 ZHIYI_GRAPH_KNOWLEDGE_GATE=shadow|on 时有语义;off 时整段不读 ⇒ 逐位等价。 + KnowledgeShape bool } func entityID(name string) string {