diff --git a/docs/subsystems/memory.md b/docs/subsystems/memory.md index 17e106feb..47876e6e9 100644 --- a/docs/subsystems/memory.md +++ b/docs/subsystems/memory.md @@ -239,6 +239,17 @@ off until switched on under Memory → Tidy up every night. by it. A stale memory is archived with the model's reason. - An inferred candidate left unreviewed for 30 days (`Unreviewed`) is archived as well, with no request needed. +- Retention (`memory.Prune`, [prune.go](../../internal/memory/prune.go)) + runs on every hourly tick of `Keep`, with distilling and tidying on or + off. A candidate unchanged for 30 days (`memory.Keep`) is archived; an + archived or superseded memory unchanged for 30 more is removed. A + removal is recorded in `removed`, so a sync from a computer that still + holds it doesn't bring it back, and a distilled one's text is dismissed + so it isn't proposed again. What the user wrote or pinned + (`memory.Fixed`) is never archived or removed by it + (`TestPruneKeepsTheUsersAndTheRecent`). Undo keeps a merged memory + whose member was removed since: it is all that is left of it + (`TestUntidyKeepsAMergeWhoseMemberWasPruned`). - A failed request changes nothing and is kept to show (`TestDreamFailureChangesNothing`). - What the last tidy did is kept in `distill.json` (`changes`). Undo puts diff --git a/internal/memdistill/memdistill.go b/internal/memdistill/memdistill.go index 9dd01f47d..6f341d21f 100644 --- a/internal/memdistill/memdistill.go +++ b/internal/memdistill/memdistill.go @@ -615,7 +615,8 @@ func keep(fs []found, s sessions.Session, proj *memory.Project, their string) in // Keep runs Run first after first, then every interval, until ctx ends, // and Dream after it when one is due (Dreams); auto gives the automatic -// model at each run. +// model at each run. Each tick applies memory's retention first +// (memory.Prune), distilling on or off. func Keep(ctx context.Context, first, interval time.Duration, auto func() string, complete Complete) { t := time.NewTimer(first) defer t.Stop() @@ -626,6 +627,11 @@ func Keep(ctx context.Context, first, interval time.Duration, auto func() string case <-t.C: } t.Reset(interval) + // retention holds whether distilling is on or not: memories saved + // by agents leave candidates and archived ones too + if _, err := memory.Prune(); err != nil { + log.Println("memory: retention:", err) + } // switched off (the default): nothing to read, nor a model to // ask. auto picks one from every provider's models, and that // loaded the whole catalog (3.6 MB) for a run that read nothing diff --git a/internal/memory/prune.go b/internal/memory/prune.go new file mode 100644 index 000000000..e031b3e06 --- /dev/null +++ b/internal/memory/prune.go @@ -0,0 +1,56 @@ +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 +} diff --git a/internal/memory/prune_test.go b/internal/memory/prune_test.go new file mode 100644 index 000000000..abe2c1dd7 --- /dev/null +++ b/internal/memory/prune_test.go @@ -0,0 +1,135 @@ +package memory + +import ( + "slices" + "testing" + "time" +) + +// A candidate nobody reviewed for Keep is archived; one archived or +// superseded for Keep more is removed, recorded as removed and, distilled, +// dismissed so it isn't proposed again. What the user wrote or pinned +// stays whatever its state and age, and so does anything changed within +// Keep. +func TestPruneKeepsTheUsersAndTheRecent(t *testing.T) { + at := fresh(t) + cand := infer(t, "Uses pnpm", Global) + gone := infer(t, "Deploys from CI", Global) + sup := infer(t, "Tabs, not spaces", Global) + pinned := infer(t, "Pinned candidate", Global) + mine := save(t, "My own, archived", Fact, Global) + if _, err := SetStatus(Archived, gone.ID); err != nil { // the user dropped it + t.Fatal(err) + } + if _, err := SetStatus(Archived, mine.ID); err != nil { + t.Fatal(err) + } + if _, err := Edit(mine.ID, Input{Text: str("My own, archived.")}, true); err != nil { + t.Fatal(err) + } + pin := true + if _, err := Edit(pinned.ID, Input{Pinned: &pin}, false); err != nil { + t.Fatal(err) + } + if _, err := Tidy(Plan{Merges: []Merge{{IDs: []string{sup.ID, cand.ID}, Text: "x"}}}); err != nil { + t.Fatal(err) + } + // the merge made cand superseded too; bring it back as a candidate + if _, err := Edit(cand.ID, Input{Status: ptr(Candidate)}, false); err != nil { + t.Fatal(err) + } + + *at = at.Add(Keep - time.Hour) + if p, err := Prune(); err != nil || p != (Pruned{}) { + t.Fatalf("within Keep: %+v %v", p, err) + } + + *at = at.Add(2 * time.Hour) + p, err := Prune() + if err != nil { + t.Fatal(err) + } + // gone and sup removed; cand and the merged one (both candidates) archived + if p.Removed != 2 || p.Archived != 2 { + t.Fatalf("first: %+v", p) + } + if get(t, cand.ID).Status != Archived { + t.Fatal("candidate not archived") + } + for _, id := range []string{gone.ID, sup.ID} { + if _, err := Get(id); err != ErrNotFound { + t.Fatalf("%s kept: %v", id, err) + } + } + if m := get(t, pinned.ID); m.Status != Candidate { + t.Fatalf("pinned changed: %+v", m) + } + if m := get(t, mine.ID); m.Status != Archived { + t.Fatalf("user's own changed: %+v", m) + } + b, err := Collect() + if err != nil { + t.Fatal(err) + } + if _, ok := b.Removed[sup.ID]; !ok || !slices.Contains(b.Dismissed, norm(sup.Text)) { + t.Fatalf("not recorded: %+v", b) + } + // a sync from a computer that still has it doesn't bring it back + old := &Bundle{Memories: []*Memory{sup}} + if _, err := Put(old); err != nil { + t.Fatal(err) + } + if _, err := Get(sup.ID); err != ErrNotFound { + t.Fatalf("synced back: %v", err) + } + // nor does distilling it again + if m := infer(t, "Tabs, not spaces", Global); m != nil { + t.Fatalf("proposed again: %+v", m) + } + + *at = at.Add(Keep + time.Hour) + if p, err := Prune(); err != nil || p.Removed != 2 { + t.Fatalf("second: %+v %v", p, err) + } + all, _, _ := All() + var left []string + for _, m := range all { + left = append(left, m.ID) + } + slices.Sort(left) + want := []string{pinned.ID, mine.ID} + slices.Sort(want) + if !slices.Equal(left, want) { + t.Fatalf("left %v, want %v", left, want) + } +} + +// Undoing a tidy keeps the memory a merge made when one it was made from +// has been removed since: it is all that is left of that one. +func TestUntidyKeepsAMergeWhoseMemberWasPruned(t *testing.T) { + at := fresh(t) + a := infer(t, "Ships on Tuesdays", Global) + b := infer(t, "Releases go out every Tuesday", Global) + cs, err := Tidy(Plan{Merges: []Merge{{IDs: []string{a.ID, b.ID}, Text: "Releases ship on Tuesdays"}}}) + if err != nil || len(cs) != 1 { + t.Fatalf("%+v %v", cs, err) + } + *at = at.Add(Keep + time.Hour) + if _, err := Edit(cs[0].Into, Input{Pinned: ptr(true)}, false); err != nil { // the merged one stays + t.Fatal(err) + } + if _, err := Prune(); err != nil { + t.Fatal(err) + } + if _, err := Edit(cs[0].Into, Input{Pinned: ptr(false)}, false); err != nil { + t.Fatal(err) + } + if _, err := Untidy(cs); err != nil { + t.Fatal(err) + } + if m := get(t, cs[0].Into); m.Text != "Releases ship on Tuesdays" { + t.Fatalf("merged one: %+v", m) + } +} + +func ptr[T any](v T) *T { return &v } diff --git a/internal/memory/tidy.go b/internal/memory/tidy.go index 3c49f1cfb..228bed83c 100644 --- a/internal/memory/tidy.go +++ b/internal/memory/tidy.go @@ -171,16 +171,19 @@ func Tidy(p Plan) ([]Change, error) { // left so, goes back to the state it had, and a memory a merge made is // removed, unless the user has made it theirs since (it stays then, with // the ones it was made from back beside it). It returns how many memories -// came back. +// came back. A merged memory stays, too, when one it was made from has +// been removed since (Prune): it is all that is left of it. func Untidy(cs []Change) (int, error) { n := 0 err := change(func(f *file) error { t := now() for i := len(cs) - 1; i >= 0; i-- { c := cs[i] + gone := false // a member retention removed since (Prune) for k, id := range c.IDs { m := find(f, id) if m == nil || k >= len(c.Was) { + gone = true continue } if c.Action == Merged && m.Status == Superseded && m.SupersededBy == c.Into || c.Action == SetAside && m.Status == Archived { @@ -188,7 +191,9 @@ func Untidy(cs []Change) (int, error) { n++ } } - if c.New { + // the merged memory is all that is left of a member removed + // since: it stays + if c.New && !gone { f.Memories = slices.DeleteFunc(f.Memories, func(m *Memory) bool { if m.ID == c.Into && !Fixed(m) { removed(f, m.ID)