Files
Cyrene_v2/internal/runtime/memory/decay.go
T

24 lines
826 B
Go

package memory
import (
"math"
"time"
)
// decayFactor computes the decay multiplier for a single item: the stored
// decay_weight scaled by a time-decay that halves every recencyHalfLife days
// (design §2 衰减: 久未使用的记忆降权). A freshly touched item keeps its full
// decay_weight; an item unused for N half-lives is scaled to
// decay_weight * 0.5^N, so it ranks progressively lower unless it stays relevant.
//
// age is measured from UpdatedAt; a future timestamp (clock skew) is clamped to
// zero, so a just-recorded item is never scored above its base weight.
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)
}