Files
magpie/internal/memory/promote_test.go
T
yetone e0cadc3124 memory: goes with backups and sync, and suggests what holds everywhere or is a skill (P5)
Backups and sync. memory.json goes in every backup and in the WebDAV,
S3 and GitHub sync, under its own toggle (on unless turned off, as the
library is). A sync merges memories one by one (memory.Combine): the
newer change wins, a memory the user edited or pinned is never taken
over by one they didn't, an archived one is kept 30 days and the
suggestions turned down are joined. Settings > Sync has a Memory tick,
and the description lists memory.

Promotion. Library > Memory (In use, no search) shows what memory
suggests: a project memory that says the same in two or more projects
(the same text, or 60% of its words with 6 or more on each side), or
whose sources came from two or more, is offered as global; a procedure
searches returned 5 or more times is offered as a skill. Taking a
global one keeps the user's own or pinned memory first, then the most
used, makes it global and supersedes the rest (into a global with the
same text when there is one). Making a skill writes a library skill
from its text and why, named from its first words (procedure-<id> for
one in Chinese), for the agents ticked for instructions, and archives
the memory. A suggestion declined isn't made again, and that goes with
sync too. Routes: POST /api/memory/promote, /skill, /decline.

Docs: docs/subsystems/memory.md (Backups and sync, Promotion),
docs/subsystems/sync-backup.md.

Pushed on origin/main 14be7f69d, whose 5 commits since 8b2fb6b08 share no
file with this one (not run again). At 8b2fb6b08: i18n-keys, library-memory-suggest and
webdav-settings-refresh on WSL in both engines, and -run
"Memory|Skill|Market|ThirdParty|Promote|Suggest|Sync|Combine" of
library, memory and backup on Windows: ok. One rebase before, at
origin/main 69453574d: go vet of gui,
memory, library and -run "Memory|Settings|Parity|Budget|Skill|Promote|
Suggest" of gui and memory on Windows: ok; i18n-keys, library-memory*,
the five sync files below and sync-own-fields, library-backups-top on
WSL in both engines: ok but webkit s3-sync (below).
Before the rebase, Go code the same as at the run on the Windows box
(xiaomi-gpu, win-run; mote's disk is full, every run there stalled):
- go vet ./... (windows) and GOOS=linux go vet -tags nogui ./...: ok.
  gofmt -l: clean.
- go test -count=1 ./internal/memory ./internal/backup ./internal/davsync
  ./internal/memdistill ./internal/memembed: ok.
- ./internal/library: ok but three tests red on Windows that are red the
  same on origin/main d936be763 run alone there and are filed:
  TestPluginImportCarryOnOffRemove (#6065), TestGoneAgentListedToTakeOut
  and TestCheckMixedServerEachSide (#4115). -run "Memory|Skill": ok.
- ./internal/gui ./internal/cli -run
  "Memory|Settings|Parity|Budget|Distill|Dream|Skill|Promote|Suggest": ok.
GUI on the WSL box (magpie-desktop), chromium and webkit:
- library-memory-suggest (new), library-memory, library-memory-select,
  i18n-keys, sync-own-fields, sync-not-backup, auto-backup,
  library-backups-top, library-bar-more, login-export, login-import: ok.
- every sync-*.test.cjs, github-sync, s3-sync, usage-computers,
  webdav-settings-refresh, refresh-page, old-webkit: ok but
  old-webkit's two reds, the same on main and filed (#6030, #5919), and
  webkit s3-sync's "picking S3 must not scroll the page" (the page is
  at 0 after the click), red the same on origin/main 1ccacad63 2 of 2 at
  --test-concurrency=2 one step later ("picking S3 again", #3413).
  github-sync, s3-sync, sync-signins, sync-other-kind and
  webdav-settings-refresh were rerun after their expected bodies and
  descriptions took memory: ok in both engines.
Broken by hand: Suggest without the Declined filter turned
TestSuggestSkillAndDecline red, and keptFirst without the Fixed order
turned TestPromoteKeepsTheUsersOwn red; restored.
Not tried: a real sync between two computers with real memory, and the
GUI on macOS (mote's disk is full).
2026-11-11 15:08:58 +08:00

154 lines
5.4 KiB
Go

package memory
import (
"slices"
"testing"
"time"
)
func uses(t *testing.T, id string, n int) {
t.Helper()
if err := change(func(f *file) error { find(f, id).Uses = n; return nil }); err != nil {
t.Fatal(err)
}
}
func suggested(t *testing.T) []Suggestion {
t.Helper()
s, err := Suggest()
if err != nil {
t.Fatal(err)
}
return s
}
// A memory learned in two projects is suggested to hold everywhere: the
// same text, or the same words in another order; one project's memories
// alone, or two short ones sharing words, are not.
func TestSuggestGlobalAcrossProjects(t *testing.T) {
fresh(t)
a := save(t, "Commits list explicit filenames, never git add -A.", Rule, "project:a")
b := save(t, "commits list explicit filenames never git add -A", Rule, "project:b")
c := save(t, "Never git add -A: commits list explicit filenames.", Rule, "project:c")
save(t, "Builds run on mote.", Rule, "project:a")
save(t, "Builds run on CI.", Rule, "project:b") // short, alike in words: another fact
save(t, "Tests run with -race.", Rule, "project:a")
save(t, "Tests run with -race.", Rule, "agent:codex")
got := suggested(t)
if len(got) != 1 || got[0].To != ToGlobal || !slices.Equal(got[0].Projects, []string{"a", "b", "c"}) {
t.Fatalf("suggested %+v, want the one rule of three projects", got)
}
ids := slices.Sorted(slices.Values(got[0].IDs))
if want := slices.Sorted(slices.Values([]string{a.ID, b.ID, c.ID})); !slices.Equal(ids, want) {
t.Fatalf("ids %v, want %v", ids, want)
}
}
// One project's memory whose sources were in another project's folder
// was learned in both.
func TestSuggestGlobalBySourceFolder(t *testing.T) {
fresh(t)
NoteProject(&Project{Key: "a", Name: "a", Roots: []string{"/src/a"}})
NoteProject(&Project{Key: "b", Name: "b", Roots: []string{"/src/b"}})
k, s := Preference, "project:a"
text := "Replies are short."
Save(Input{Text: &text, Kind: &k, Scope: &s}, FromAgent, &Source{Agent: "codex", Dir: "/src/a"}, nil)
if got := suggested(t); len(got) != 0 {
t.Fatalf("one project's: %+v", got)
}
Save(Input{Text: &text, Kind: &k, Scope: &s}, FromAgent, &Source{Agent: "codex", Dir: "/src/b/web"}, nil)
if got := suggested(t); len(got) != 1 || !slices.Equal(got[0].Projects, []string{"a", "b"}) {
t.Fatalf("suggested %+v, want it from a and b", got)
}
}
// Taken, the one kept holds everywhere, carrying the others' sources and
// uses, and they are superseded by it; the user's own is the one kept,
// as written. It is the user's change, and isn't suggested again.
func TestPromoteKeepsTheUsersOwn(t *testing.T) {
at := fresh(t)
a := save(t, "Use pnpm, not npm.", Preference, "project:a")
uses(t, a.ID, 4)
text, k, s := "use pnpm, not npm", Preference, "project:b"
b, _, _ := Save(Input{Text: &text, Kind: &k, Scope: &s}, FromUser, nil, nil)
*at = at.Add(time.Hour)
m, err := Promote([]string{a.ID, b.ID})
if err != nil {
t.Fatal(err)
}
if m.ID != b.ID || m.Scope != Global || m.Text != "use pnpm, not npm" || m.Uses != 4 || !byUser(m) {
t.Fatalf("kept %+v, want the user's own, global", m)
}
o, _ := Get(a.ID)
if o.Status != Superseded || o.SupersededBy != b.ID {
t.Fatalf("the other: %+v", o)
}
if got := suggested(t); len(got) != 0 {
t.Fatalf("suggested again: %+v", got)
}
// a memory no longer in a project is no promotion
if _, err := Promote([]string{a.ID}); err != ErrNotSuggested {
t.Fatalf("promoted a superseded one: %v", err)
}
}
// The same text global already: the project ones go into it.
func TestPromoteIntoTheGlobalOne(t *testing.T) {
fresh(t)
g := save(t, "Dates are ISO 8601.", Rule, Global)
a := save(t, "Dates are ISO 8601.", Rule, "project:a")
b := save(t, "Dates are ISO 8601.", Rule, "project:b")
m, err := Promote([]string{a.ID, b.ID})
if err != nil || m.ID != g.ID || m.Scope != Global {
t.Fatalf("kept %+v, %v; want the global one", m, err)
}
for _, id := range []string{a.ID, b.ID} {
if o, _ := Get(id); o.Status != Superseded || o.SupersededBy != g.ID || o.Scope == Global {
t.Fatalf("%+v", o)
}
}
}
// A procedure searched for often is suggested as a skill until it is
// one, and a suggestion turned down stays down, on another computer too.
func TestSuggestSkillAndDecline(t *testing.T) {
a, b, _ := twoComputers(t)
a.use(t)
p := save(t, "To release: tag, wait for the assets, then post.", Procedure, Global)
r := save(t, "Never force-push main.", Rule, Global)
uses(t, p.ID, skillUses-1)
uses(t, r.ID, 50)
if got := suggested(t); len(got) != 0 {
t.Fatalf("suggested %+v before it was used enough", got)
}
uses(t, p.ID, skillUses)
got := suggested(t)
if len(got) != 1 || got[0].To != ToSkill || got[0].IDs[0] != p.ID || got[0].Uses != skillUses {
t.Fatalf("suggested %+v, want the procedure", got)
}
if err := Decline(got[0].Key); err != nil {
t.Fatal(err)
}
if got := suggested(t); len(got) != 0 {
t.Fatalf("declined, still suggested: %+v", got)
}
carry(t, a, b)
b.use(t)
if got := suggested(t); len(got) != 0 {
t.Fatalf("declined on a, suggested on b: %+v", got)
}
// made a skill: archived, noted, not suggested
a.use(t)
q := save(t, "To deploy the site: wrangler deploy, then curl it.", Procedure, Global)
uses(t, q.ID, skillUses)
if err := MadeSkill(q.ID, "deploy-site"); err != nil {
t.Fatal(err)
}
if m, _ := Get(q.ID); m.Status != Archived || m.Skill != "deploy-site" {
t.Fatalf("%+v", m)
}
if got := suggested(t); len(got) != 0 {
t.Fatalf("a skill already, suggested: %+v", got)
}
}