Version: 0.9.21.dev.260416
后端: 1. Memory 写入链路新增"召回→比对→汇总"去重决策层 - 新增决策流程:Runner 根据decision.enabled 配置走决策路径(语义召回候选 → Hash 精确命中 → LLM 逐对比对 → 汇总决策 → 执行 ADD/UPDATE/DELETE/NONE),默认关闭,旧路径完全保留 - 新增 LLMDecisionOrchestrator:单对关系判断编排器,输出 duplicate/update/conflict/unrelated 四种关系 - 新增 decision_flow / apply_actions:决策流程主循环与动作落地(新增、更新内容、软删除、跳过) - 新增 aggregate_decision / decision_validate:汇总规则(按优先级判定动作)与 LLM 输出校验 - 新增 decision model:CandidateSnapshot / ComparisonResult / FinalDecision 等决策层核心类型 - ItemRepo 新增 FindActiveByHash / UpdateContentByID / SoftDeleteByID 三个决策层专用方法 - RAG Runtime / Pipeline / Service 新增 DeleteMemory 向量删除能力,MilvusStore 补充 duplicate collection 错误识别 - Runner 新增 syncVectorDeletes 处理决策层 DELETE 动作的向量清理 - config 新增 decision(enabled/candidateTopK/candidateMinScore/fallbackMode)和 write.mode 配置项,config_loader 增加默认值兜底 - 删除 HANDOFF-RAG复用后续实施计划.md 和旧 log.txt,新增 Log.txt 记录决策流程调试日志 - normalize_facts 导出 HashContent 供决策层复用,audit 新增 update 操作常量 前端:无 仓库:无
This commit is contained in:
115
backend/memory/utils/aggregate_decision.go
Normal file
115
backend/memory/utils/aggregate_decision.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
memorymodel "github.com/LoveLosita/smartflow/backend/memory/model"
|
||||
)
|
||||
|
||||
// AggregateComparisons 把一轮 LLM 比对结果汇总为最终动作。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 纯确定性逻辑,不调 LLM,不调外部服务;
|
||||
// 2. 按优先级从高到低判定:duplicate > update > conflict > unrelated;
|
||||
// 3. 多条 update 时选 Score 最高的候选执行 UPDATE。
|
||||
//
|
||||
// 汇总规则:
|
||||
// 1. 出现 duplicate → 最终动作 NONE(新 fact 完全重复,不需要写入);
|
||||
// 2. 出现 update → 最终动作 UPDATE(更新 Score 最高的那条旧记忆);
|
||||
// 3. 出现 conflict → 最终动作 DELETE + 后续按 ADD 处理(旧记忆过时,先删旧的再写新的);
|
||||
// 4. 全部 unrelated → 最终动作 ADD(没有相关旧记忆,直接新增)。
|
||||
func AggregateComparisons(
|
||||
fact memorymodel.NormalizedFact,
|
||||
comparisons []memorymodel.ComparisonResult,
|
||||
candidates []memorymodel.CandidateSnapshot,
|
||||
) *memorymodel.FinalDecision {
|
||||
// 1. 无候选时直接 ADD,无需走任何判断。
|
||||
if len(comparisons) == 0 {
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionAdd,
|
||||
Reason: "无相关旧记忆,直接新增",
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 建立 memoryID → CandidateSnapshot 映射,用于查找 Score。
|
||||
snapshotMap := make(map[int64]memorymodel.CandidateSnapshot, len(candidates))
|
||||
for _, c := range candidates {
|
||||
snapshotMap[c.MemoryID] = c
|
||||
}
|
||||
|
||||
hasDuplicate := false
|
||||
var bestUpdate *memorymodel.ComparisonResult
|
||||
bestUpdateScore := -1.0
|
||||
var conflictResult *memorymodel.ComparisonResult
|
||||
|
||||
for i := range comparisons {
|
||||
comp := &comparisons[i]
|
||||
|
||||
switch comp.Relation {
|
||||
case memorymodel.RelationDuplicate:
|
||||
// 3. 出现一条 duplicate 即可确定最终动作为 NONE。
|
||||
hasDuplicate = true
|
||||
|
||||
case memorymodel.RelationUpdate:
|
||||
// 4. 多条 update 时,选 Score 最高的那条执行 UPDATE。
|
||||
snapshot, ok := snapshotMap[comp.MemoryID]
|
||||
score := 0.0
|
||||
if ok {
|
||||
score = snapshot.Score
|
||||
}
|
||||
if score > bestUpdateScore {
|
||||
bestUpdateScore = score
|
||||
bestUpdate = comp
|
||||
}
|
||||
|
||||
case memorymodel.RelationConflict:
|
||||
// 5. 记录第一条 conflict,用于后续 DELETE + ADD 处理。
|
||||
if conflictResult == nil {
|
||||
conflictResult = comp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 按优先级判定最终动作。
|
||||
if hasDuplicate {
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionNone,
|
||||
Reason: "存在完全重复的旧记忆,跳过写入",
|
||||
}
|
||||
}
|
||||
|
||||
if bestUpdate != nil {
|
||||
// 7. UPDATE 动作:使用 LLM 提供的合并后内容。
|
||||
title := bestUpdate.UpdatedTitle
|
||||
if title == "" {
|
||||
title = fact.Title
|
||||
}
|
||||
content := bestUpdate.UpdatedContent
|
||||
reason := bestUpdate.Reason
|
||||
if reason == "" {
|
||||
reason = "新事实是对旧记忆的修正或补充"
|
||||
}
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionUpdate,
|
||||
TargetID: bestUpdate.MemoryID,
|
||||
Title: title,
|
||||
Content: content,
|
||||
Reason: fmt.Sprintf("更新旧记忆(id=%d): %s", bestUpdate.MemoryID, reason),
|
||||
}
|
||||
}
|
||||
|
||||
if conflictResult != nil {
|
||||
// 8. conflict → 先 DELETE 旧记忆,后续由上层按 ADD 写入新 fact。
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionDelete,
|
||||
TargetID: conflictResult.MemoryID,
|
||||
Reason: fmt.Sprintf("旧记忆(id=%d)与新事实冲突,删除后新增: %s", conflictResult.MemoryID, conflictResult.Reason),
|
||||
}
|
||||
}
|
||||
|
||||
// 9. 全部 unrelated → 直接 ADD。
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionAdd,
|
||||
Reason: "无相关旧记忆,直接新增",
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
const (
|
||||
// AuditOperationCreate 表示系统新建一条记忆。
|
||||
AuditOperationCreate = "create"
|
||||
// AuditOperationUpdate 表示决策层更新已有记忆的内容。
|
||||
AuditOperationUpdate = "update"
|
||||
// AuditOperationDelete 表示对已有记忆做软删除。
|
||||
AuditOperationDelete = "delete"
|
||||
)
|
||||
|
||||
49
backend/memory/utils/decision_validate.go
Normal file
49
backend/memory/utils/decision_validate.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
memorymodel "github.com/LoveLosita/smartflow/backend/memory/model"
|
||||
)
|
||||
|
||||
// 合法关系类型集合,用于校验 LLM 输出的 relation 字段。
|
||||
var validRelations = map[string]struct{}{
|
||||
memorymodel.RelationDuplicate: {},
|
||||
memorymodel.RelationUpdate: {},
|
||||
memorymodel.RelationConflict: {},
|
||||
memorymodel.RelationUnrelated: {},
|
||||
}
|
||||
|
||||
// ValidateComparisonResult 校验单次比对结果的基本合法性。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只校验 LLM 输出的结构合法性,不校验业务语义;
|
||||
// 2. relation 必须是四种合法值之一,update 时必须有 UpdatedContent;
|
||||
// 3. 校验失败直接返回 error,调用方决定丢弃或重试。
|
||||
func ValidateComparisonResult(result *memorymodel.ComparisonResult) error {
|
||||
if result == nil {
|
||||
return fmt.Errorf("比对结果不能为空")
|
||||
}
|
||||
|
||||
// 1. MemoryID 必须大于 0,确保能定位到旧记忆。
|
||||
if result.MemoryID <= 0 {
|
||||
return fmt.Errorf("比对结果 memory_id 无效: %d", result.MemoryID)
|
||||
}
|
||||
|
||||
// 2. relation 必须是四种合法值之一,防止 LLM 输出非法值。
|
||||
relation := strings.TrimSpace(strings.ToLower(result.Relation))
|
||||
if _, ok := validRelations[relation]; !ok {
|
||||
return fmt.Errorf("比对结果 relation 非法: %s", result.Relation)
|
||||
}
|
||||
|
||||
// 3. relation=update 时,UpdatedContent 不能为空。
|
||||
// 原因:update 需要合并后的完整内容,不能只写差异部分。
|
||||
if relation == memorymodel.RelationUpdate {
|
||||
if strings.TrimSpace(result.UpdatedContent) == "" {
|
||||
return fmt.Errorf("relation=update 时 updated_content 不能为空")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -56,7 +56,7 @@ func NormalizeFacts(candidates []memorymodel.FactCandidate) []memorymodel.Normal
|
||||
sensitivityLevel := clampInt(candidate.SensitivityLevel, 0, 2)
|
||||
|
||||
normalizedContent := strings.ToLower(content)
|
||||
contentHash := hashContent(memoryType, normalizedContent)
|
||||
contentHash := HashContent(memoryType, normalizedContent)
|
||||
dedupKey := fmt.Sprintf("%s:%s", memoryType, contentHash)
|
||||
if _, exists := seen[dedupKey]; exists {
|
||||
continue
|
||||
@@ -126,7 +126,10 @@ func defaultImportanceByType(memoryType string) float64 {
|
||||
}
|
||||
}
|
||||
|
||||
func hashContent(memoryType, normalizedContent string) string {
|
||||
// HashContent 计算记忆内容的去重哈希。
|
||||
// 算法:sha256(memoryType + "::" + normalizedContent)
|
||||
// 说明:导出此函数是为了让决策层 apply_actions 也能复用同一算法,避免哈希不一致导致去重失效。
|
||||
func HashContent(memoryType, normalizedContent string) string {
|
||||
sum := sha256.Sum256([]byte(memoryType + "::" + normalizedContent))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user