diff --git a/go/internal/api/routes/admin.go b/go/internal/api/routes/admin.go index 924f443..f6a89aa 100644 --- a/go/internal/api/routes/admin.go +++ b/go/internal/api/routes/admin.go @@ -15,12 +15,13 @@ import ( ) type AdminAPI struct { - LanceDB storage.LanceDB - Forgetter *governance.Forgetter + LanceDB storage.LanceDB + Forgetter *governance.Forgetter + GraphStore governance.GraphStore // E4.3: 图谱度参与遗忘决策 } -func NewAdminAPI(ldb storage.LanceDB, f *governance.Forgetter) *AdminAPI { - return &AdminAPI{LanceDB: ldb, Forgetter: f} +func NewAdminAPI(ldb storage.LanceDB, f *governance.Forgetter, gs governance.GraphStore) *AdminAPI { + return &AdminAPI{LanceDB: ldb, Forgetter: f, GraphStore: gs} } // DELETE /api/v1/distilled/{id} @@ -68,7 +69,14 @@ func (aa *AdminAPI) Forget(w http.ResponseWriter, r *http.Request) { recallCnt := intVal(mem["recall_count"]) tier := strVal(mem["tier"]) - if aa.Forgetter.ShouldForget(lastAccess, recallCnt, tier) { + // E4.3: 获取实体图谱度,取 namespace 和 content 中实体的最大度 + entity := strVal(mem["namespace"]) + degree := 0 + if entity != "" { + degree = aa.GraphStore.GetEntityDegree(entity) + } + + if aa.Forgetter.ShouldForget(lastAccess, recallCnt, tier, degree) { aa.LanceDB.SoftDelete(strVal(mem["id"]), "auto_forget") forgotten++ } diff --git a/go/internal/api/routes/consolidation_pipe.go b/go/internal/api/routes/consolidation_pipe.go index b8f3a22..4efecd5 100644 --- a/go/internal/api/routes/consolidation_pipe.go +++ b/go/internal/api/routes/consolidation_pipe.go @@ -111,10 +111,7 @@ func (cp *ConsolidationPipeline) runGraphMaintenance() int { // Step 2: PageRank 更新(§2.5.5 — 每次修剪后全部节点重新计算) ranks := cp.graph.PageRank(0.85, 20) if ranks != nil { - // 写回数据库(需要 SQLite 级别的接口) - if sqlite, ok := cp.graph.(*governance.SQLiteGraphStore); ok { - sqlite.UpdatePageRanks(ranks) - } + // FileGraph.PageRank 在内部已直接更新节点 PageRank 字段,无需额外持久化 log.Printf("[consolidation] PageRank 更新: %d nodes", len(ranks)) } diff --git a/go/internal/api/server.go b/go/internal/api/server.go index fe79b4b..eed0e6e 100644 --- a/go/internal/api/server.go +++ b/go/internal/api/server.go @@ -99,13 +99,13 @@ func NewServer() http.Handler { graphPath = "/var/lib/memoryweave/graph.db" } var graphStore governance.GraphStore - gs, err := governance.NewSQLiteGraphStore(graphPath) + gs, err := governance.NewFileGraph(graphPath) if err != nil { - log.Printf("[zhiyid] WARN: SQLite 图谱初始化失败 (%v),降级为 InMemoryGraph", err) + log.Printf("[zhiyid] WARN: FileGraph 初始化失败 (%v),降级为 InMemoryGraph", err) graphStore = governance.NewInMemoryGraph() } else { graphStore = gs - log.Printf("[zhiyid] 图谱后端: SQLite — %s", graphPath) + log.Printf("[zhiyid] 图谱后端: FileGraph — %s", graphPath) } graphUpdater := governance.NewAutoGraphUpdater(graphStore) graphAPI := routes.NewGraphAPI(graphStore) @@ -135,7 +135,7 @@ func NewServer() http.Handler { feedbackAPI := routes.NewFeedbackAPI(ldb) // 遗忘器:支持按 Agent 类型设置衰减率 forgetter := governance.NewForgetter() - adminAPI := routes.NewAdminAPI(ldb, forgetter) + adminAPI := routes.NewAdminAPI(ldb, forgetter, graphStore) agentRegistry := routes.NewAgentRegistry(nil) evalAPI := routes.NewEvalAPI(api.Pipeline, ldb) l3API := routes.WM @@ -899,10 +899,19 @@ func NewServer() http.Handler { recallCount = rc } tier := "normal" - if ts, ok := m["tier"].(string); ok { - tier = ts - } - if forgetter.ShouldForget(lastAccessed, recallCount, tier) { + if ts, ok := m["tier"].(string); ok { + tier = ts + } + // E4.3: 获取实体图谱度(namespace 作为实体) + entity := "" + if ns, ok := m["namespace"].(string); ok { + entity = ns + } + degree := 0 + if entity != "" { + degree = graphStore.GetEntityDegree(entity) + } + if forgetter.ShouldForget(lastAccessed, recallCount, tier, degree) { if id, ok := m["id"].(string); ok { forgetter.ScanAndForget(id) // 实际执行软删除(持久化到 LanceDB) diff --git a/go/internal/governance/graph_file.go b/go/internal/governance/graph_file.go index f398890..70ae050 100644 --- a/go/internal/governance/graph_file.go +++ b/go/internal/governance/graph_file.go @@ -552,6 +552,11 @@ func (fg *FileGraph) EvidenceCount(entity string) int { return count } +// GetEntityDegree E4.3: 返回实体的图谱度(入度+出度),度越高越优先保留 +func (fg *FileGraph) GetEntityDegree(entity string) int { + return fg.EvidenceCount(entity) // 与 EvidenceCount 相同逻辑:统计 entity 作为 source 或 target 的边数 +} + // ─── 强制保存 ──────────────────────────────────────────── func (fg *FileGraph) Save() error { @@ -599,3 +604,30 @@ func (fg *FileGraph) ListNodesByType(nodeType, namespace string) []map[string]in func (fg *FileGraph) ListNodes(namespace string) []map[string]interface{} { return fg.ListNodesByType("", namespace) } + +// GetGraph 导出完整图谱(供可视化),limit≤0 时不限制 +func (fg *FileGraph) GetGraph(namespace string, limit int) ([]map[string]interface{}, []map[string]interface{}) { + fg.mu.RLock() + defer fg.mu.RUnlock() + var nodes, edges []map[string]interface{} + for _, n := range fg.nodes { + if namespace == "" || n.Namespace == namespace { + nodes = append(nodes, map[string]interface{}{ + "id": n.ID, "name": n.Name, "type": n.Type, "namespace": n.Namespace, + "pagerank": n.PageRank, "evidence_count": n.EvidenceCount, + }) + if limit > 0 && len(nodes) >= limit { + break + } + } + } + for _, e := range fg.edges { + if namespace == "" || e.Namespace == namespace { + edges = append(edges, map[string]interface{}{ + "id": e.ID, "source": e.Source, "target": e.Target, + "relation": e.Relation, "weight": e.Weight, "namespace": e.Namespace, + }) + } + } + return nodes, edges +} diff --git a/go/internal/governance/graph_mem.go b/go/internal/governance/graph_mem.go index 89bc43a..4e65af6 100644 --- a/go/internal/governance/graph_mem.go +++ b/go/internal/governance/graph_mem.go @@ -233,6 +233,11 @@ func (g *InMemoryGraph) EvidenceCount(entity string) int { return count } +// GetEntityDegree E4.3: 返回实体的图谱度(入度+出度),度越高越优先保留 +func (g *InMemoryGraph) GetEntityDegree(entity string) int { + return g.EvidenceCount(entity) // 与 EvidenceCount 相同逻辑:统计 entity 作为 source 或 target 的边数 +} + func containsRelation(rel, substr string) bool { if len(substr) == 0 { return true diff --git a/go/internal/governance/graph_store.go b/go/internal/governance/graph_store.go index eba6ae0..864f731 100644 --- a/go/internal/governance/graph_store.go +++ b/go/internal/governance/graph_store.go @@ -32,6 +32,9 @@ type GraphStore interface { PageRank(damping float64, iterations int) map[string]float64 EvidenceCount(entity string) int + // E4.3: 获取实体的图谱度(连接数),度越高越优先保留 + GetEntityDegree(entity string) int + // 导出完整图谱(供可视化),limit≤0 时不限制 GetGraph(namespace string, limit int) (nodes []map[string]interface{}, edges []map[string]interface{}) } diff --git a/go/internal/storage/lancedb_ipc.go b/go/internal/storage/lancedb_ipc.go index 783e764..5df376e 100644 --- a/go/internal/storage/lancedb_ipc.go +++ b/go/internal/storage/lancedb_ipc.go @@ -453,6 +453,8 @@ func (rc *RustLanceDBClient) GetCandidatesForForgetting() ([]map[string]interfac if !m.IsDeleted && m.Tier != "core" { out = append(out, map[string]interface{}{ "id": id, + "content": m.Content, + "namespace": m.Namespace, "last_recalled_at": m.LastRecalledAt, "recall_count": m.RecallCount, "tier": m.Tier, diff --git a/go/internal/storage/sqlite.go b/go/internal/storage/sqlite.go index e21be48..668d442 100644 --- a/go/internal/storage/sqlite.go +++ b/go/internal/storage/sqlite.go @@ -485,14 +485,22 @@ func (sc *SQLiteClient) GetCandidatesForForgetting() ([]map[string]interface{}, defer sc.mu.RUnlock() rows, err := sc.query( - "SELECT id FROM memories WHERE is_deleted=0 AND tier!='core' ORDER BY last_recalled_at ASC LIMIT 100", + "SELECT id, content, namespace, last_recalled_at, recall_count, tier, importance FROM memories WHERE is_deleted=0 AND tier!='core' ORDER BY last_recalled_at ASC LIMIT 100", ) if err != nil { return nil, err } var results []map[string]interface{} for _, r := range rows { - results = append(results, map[string]interface{}{"id": r["id"]}) + results = append(results, map[string]interface{}{ + "id": r["id"], + "content": r["content"], + "namespace": r["namespace"], + "last_recalled_at": r["last_recalled_at"], + "recall_count": r["recall_count"], + "tier": r["tier"], + "importance": r["importance"], + }) } return results, nil }