Files
magpie/internal/memory/prune.go
T
yetone 8a59e76165 memory: candidates and set-aside memories don't stay forever
memory.json grew from distilled candidates nobody reviewed and from the
superseded members of merges, which nothing ever removed. memory.Prune
now applies retention on every hourly tick of memdistill.Keep, with
distilling and dreaming on or off:

- a candidate unchanged for 30 days (memory.Keep) is archived;
- an archived or superseded memory unchanged for 30 more is removed,
  recorded in removed (a sync doesn't bring it back) and, if distilled,
  dismissed (never proposed again);
- what the user wrote or pinned (Fixed) is never archived or removed.

Untidy keeps a merged memory when one of its members was removed since:
it is all that is left of that member.

Run on mote at base fc896a9f6, then again at base 90ad90646 (main moved
by GUI-only commits; memory, memdistill and backup rerun ok there):
go test -count=1 ./internal/memory/... ./internal/memdistill/...
./internal/memembed/... ./internal/backup/... ./internal/davsync/...
./internal/library/...: all ok. go vet of memory and memdistill for
darwin, windows and linux: clean. gofmt -l: clean.

Broken by hand, each red: Prune without the Fixed check (archived 3,
removed 3: the pinned and the user's own went); without removed()
(Removed empty, a sync would bring it back); Untidy without the gone
check (the merged memory deleted).

Real data (~/.config/magpie/memory.json, read only): 206 memories, all
changed since 11-13, so the first run changes nothing.

Not tried: the full suite and the GUI suite (no GUI or route change).
2026-11-14 14:00:48 +08:00

57 lines
1.4 KiB
Go

package memory
import (
"slices"
"time"
)
// Retention: what nobody acted on doesn't stay forever. A candidate left
// as it was for Keep is archived; an archived or superseded memory left
// so for Keep more is removed. What the user wrote or pinned (Fixed) is
// never removed, whatever its state, and a removal is recorded (Removed)
// so a sync from another computer doesn't bring it back. A distilled
// text removed is dismissed too: it is never proposed again.
// Keep is how long a candidate waits to be reviewed, and how long an
// archived or superseded memory is kept, before Prune moves it on.
const Keep = 30 * 24 * time.Hour
// Pruned is what one Prune did.
type Pruned struct {
Archived int // candidates set aside
Removed int // archived or superseded memories removed
}
// Prune applies retention now. It writes nothing when nothing is due.
func Prune() (Pruned, error) {
var p Pruned
err := change(func(f *file) error {
t := now()
cut := t.Add(-Keep)
f.Memories = slices.DeleteFunc(f.Memories, func(m *Memory) bool {
if Fixed(m) || !m.Updated.Before(cut) {
return false
}
switch m.Status {
case Candidate:
m.Status, m.Updated = Archived, t
p.Archived++
case Archived, Superseded:
dismiss(f, m)
removed(f, m.ID)
p.Removed++
return true
}
return false
})
if p == (Pruned{}) {
return errSkip
}
return nil
})
if err == errSkip {
err = nil
}
return p, err
}