package memory import ( "math" "time" ) // decayFactor 计算单条条目的衰减乘数:已存的 // decay_weight 乘以每 recencyHalfLife 天减半的时间衰减 // (design §2 衰减: 久未使用的记忆降权)。刚被触碰的条目保留完整的 // decay_weight;N 个半衰期未使用的条目被缩放到 // decay_weight * 0.5^N,因此除非持续相关,其排名会逐步下降。 // // age 自 UpdatedAt 起算;未来时间戳(时钟偏移)被夹到 // 零,因此刚记录的条目得分绝不会高于其基础权重。 func (r *Retriever) decayFactor(it MemoryItem, now time.Time) float64 { age := now.Sub(it.UpdatedAt) if age < 0 { age = 0 } halfLives := float64(age) / float64(r.recencyHalfLife) return it.DecayWeight * math.Pow(0.5, halfLives) }