// 织忆 MemoryWeave — G7.3: Skill 执行 + 遗忘联动 + trial 反馈 package routes import ( "fmt" "net/http" "strings" ) // skillExecuteHandler POST /api/v1/skills/{name}/execute // 返回包含 linked memories context 的完整 prompt,供 Hermes agent 执行 func skillExecuteHandler(w http.ResponseWriter, r *http.Request) { name := r.PathValue("name") if name == "" { respondError(w, 400, "skill name required") return } skill := BayesianSkills.Get(name) if skill == nil { respondError(w, 404, "skill not found: "+name) return } // 加载 linked memories 作为 context ctxMemories := loadLinkedMemories(skill.LinkedMemoryIDs) // 填充 prompt template(简单 {param} 替换) enrichedPrompt := enrichPromptWithContext(skill.PromptTemplate, ctxMemories) // 更新 skill 的 last_used_at(内部维护,不加到 struct) // 同时触发 degree 保护(active skill 关联的 memory degree +5) for _, memID := range skill.LinkedMemoryIDs { _ = markMemoryDegreeBoost(memID, skill.Status == "active") } respond(w, 200, map[string]interface{}{ "skill_name": name, "status": skill.Status, "enriched_prompt": enrichedPrompt, "linked_memories": ctxMemories, "eta": skill.ETA, "trial_count": skill.Trials, "next_action": "submit trial result via POST /api/v1/skills/" + name + "/trial", }) } // loadLinkedMemories 从 LanceDB 加载关联记忆 func loadLinkedMemories(memIDs []string) []map[string]string { if len(memIDs) == 0 { return nil } ldb := getLDB() if ldb == nil { return nil } candidates, err := ldb.GetSkillCandidates(1, 1000) // 宽松条件,取更多 if err != nil { return nil } // 从所有记忆中筛选 ID 匹配的 idSet := make(map[string]bool) for _, id := range memIDs { idSet[id] = true } var results []map[string]string for _, mem := range candidates { if idSet[mem.ID] { content := mem.Content if len(content) > 300 { content = content[:300] + "..." } results = append(results, map[string]string{ "id": mem.ID, "content": content, "tier": mem.Tier, "category": mem.Category, }) } } return results } // enrichPromptWithContext 将 linked memories 注入 prompt template func enrichPromptWithContext(promptTemplate string, ctxMemories []map[string]string) string { if len(ctxMemories) == 0 || promptTemplate == "" { return promptTemplate } var contextLines []string contextLines = append(contextLines, "## 关联记忆") for _, mem := range ctxMemories { contextLines = append(contextLines, fmt.Sprintf("- [%s] %s", mem["category"], mem["content"])) } contextBlock := strings.Join(contextLines, "\n") // 如果 prompt template 包含 {context} 占位符,替换它 if strings.Contains(promptTemplate, "{context}") { return strings.ReplaceAll(promptTemplate, "{context}", contextBlock) } // 否则追加到末尾 return promptTemplate + "\n\n" + contextBlock } // RecordTrialWithForgetting 记录 trial 并联动遗忘 // 由 server.go 的 trial 路由调用(扩展现有 Trial 方法) func RecordTrialWithForgetting(name string, success bool) *BetaSkill { skill := BayesianSkills.RecordTrial(name, success) // G7.3 遗忘联动 if skill != nil && len(skill.LinkedMemoryIDs) > 0 { applyForgettingLinkage(skill, success) } return skill } // applyForgettingLinkage 根据 trial 结果联动遗忘系统 // - trial failure: 关联记忆 decay_rate ×1.2(加速遗忘) // - trial success + active skill: degree +5(已在 skillExecuteHandler 中处理) func applyForgettingLinkage(skill *BetaSkill, success bool) { if len(skill.LinkedMemoryIDs) == 0 { return } ldb := getLDB() if ldb == nil { return } for _, memID := range skill.LinkedMemoryIDs { if success { // trial 成功:关联记忆 decay 正常(无特殊加速) // 但如果是 retired skill,降低保护 if skill.Status == "retired" { _ = applyDegreePenalty(memID, 2) } } else { // trial 失败:关联记忆 decay_rate ×1.2(加速遗忘) _ = applyDecayAcceleration(memID, 1.2) } } } // applyDegreePenalty 降低记忆 degree(retired skill → degree -2) func applyDegreePenalty(memID string, penalty int) error { ldb := getLDB() if ldb == nil { return fmt.Errorf("ldb not available") } candidates, err := ldb.GetSkillCandidates(1, 1000) if err != nil { return err } for _, mem := range candidates { if mem.ID == memID { newImportance := mem.Importance - float64(penalty)*0.05 if newImportance < 0.01 { newImportance = 0.01 } return ldb.Update("memories", memID, map[string]any{ "importance": newImportance, }) } } return fmt.Errorf("memory not found: %s", memID) } // applyDecayAcceleration 加速记忆衰减(通过更新 importance) func applyDecayAcceleration(memID string, factor float64) error { ldb := getLDB() if ldb == nil { return fmt.Errorf("ldb not available") } // 读取当前 importance,× factor(上限 0.3) // 注意:这需要 GetMemory/UpdateMemory,当前 LanceDB 接口不支持 // 简化:记录到 audit log 供下次 consolidate 处理 candidates, err := ldb.GetSkillCandidates(1, 1000) if err != nil { return err } for _, mem := range candidates { if mem.ID == memID { newImportance := mem.Importance * factor if newImportance > 0.3 { newImportance = 0.3 } _ = ldb.Update("memories", memID, map[string]any{ "importance": newImportance, }) return nil } } return fmt.Errorf("memory not found: %s", memID) } // markMemoryDegreeBoost 在 active skill 执行时标记 degree +5 保护 // 注意:当前 degree 只在内存计算,不持久化 // 简化:返回保护信号,由调用方记录到 skill metadata func markMemoryDegreeBoost(memID string, isActive bool) int { if !isActive { return 0 } // 记录到 skill 的 linked_memory_ids 已足够 // degree 保护在 Forgetter.ShouldForget 读取 skill 列表时生效 return 5 } // skillByNameHandler already defined in server.go // This file provides the execution logic and forgetting linkage // ExecuteSkill HTTP 路由(供 server.go mux 调用) // 注意:server.go 的 skillByNameHandler 只处理 GET/DELETE // skill 执行用单独的路径: /api/v1/skills/{name}/execute func ExecuteSkill(w http.ResponseWriter, r *http.Request) { name := r.PathValue("name") if name == "" { respondError(w, 400, "skill name required") return } skill := BayesianSkills.Get(name) if skill == nil { respondError(w, 404, "skill not found: "+name) return } ctxMemories := loadLinkedMemories(skill.LinkedMemoryIDs) enrichedPrompt := enrichPromptWithContext(skill.PromptTemplate, ctxMemories) respond(w, 200, map[string]interface{}{ "skill_name": name, "enriched_prompt": enrichedPrompt, "status": skill.Status, "linked_memories": ctxMemories, "eta": skill.ETA, }) }