Library -> Memory reviewed one memory at a time: To review held
candidates that had to be opened and kept or deleted one by one.
- Select (beside the filters, shown when two or more are listed) puts a
box on each memory. A click picks one, Shift-click the range from the
last one clicked, up/down move between the boxes (Shift picks as it
goes), Cmd/Ctrl+A picks all shown, Escape ends it. A search or filter
unpicks what it hides.
- The bar under the list (the library's lib-pickbar, as Skills' Select)
keeps (To review), archives (In use) or restores (Archived) the picked
ones, and deletes them on a second click.
- POST /api/memory/status {ids, status}: memory.SetStatus changes them in
one write, each as Edit by the user (an archived inferred memory is
dismissed, so it isn't proposed again). Unknown ids are skipped; none
found, or a bad status, is a 400 and changes nothing.
Delete reuses POST /api/memory/remove {ids}.
Run (mote, at this head):
- go test ./internal/memory ./internal/memdistill ./internal/library: ok
- go test -run 'Memory|Distill|Dream' ./internal/gui: ok
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
./..., and the three builds: ok. gofmt -l: clean.
- library-memory-select.test.cjs (new): 22/22, chromium and webkit,
en/zh/zh-TW/ja/de at 980 and 420 wide (no native select, no sideways
overflow).
- Every library-*, i18n-* and gui-* GUI file (67), both engines: 1309 of 1309 pass.
- Rebased on 2219fdd49: i18n-keys, library-memory, library-memory-select
and library-bili 132/132; go test ./internal/memory and -run Memory
./internal/gui ok (at d374168d0; only a
client_icons test changed in those packages since).
Broken by hand:
- SetStatus without editIn: TestSetStatusOfSome red ("after keeping two:
candidate candidate candidate candidate"), and
TestMemoryStatusRouteActsOnSome red.
- memPick with no Shift range: the GUI test red (got [c1, c4], wanted
c1..c4).
Not tried: the real app with a real memory.json, the WebKitGTK Linux app,
Windows' WebView2.
276 lines
9.2 KiB
Go
276 lines
9.2 KiB
Go
package memory
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// fresh points memory.json at a file of the test's own, and holds the
|
|
// clock at a time the test moves.
|
|
func fresh(t *testing.T) *time.Time {
|
|
t.Helper()
|
|
p := filepath.Join(t.TempDir(), "memory.json")
|
|
old, oldNow := Path, now
|
|
Path = func() string { return p }
|
|
at := time.Date(2026, 11, 9, 10, 0, 0, 0, time.UTC)
|
|
now = func() time.Time { return at }
|
|
t.Cleanup(func() { Path, now = old, oldNow })
|
|
return &at
|
|
}
|
|
|
|
func str(s string) *string { return &s }
|
|
|
|
func save(t *testing.T, text, kind, scope string, tags ...string) *Memory {
|
|
t.Helper()
|
|
m, _, err := Save(Input{Text: &text, Kind: &kind, Scope: &scope, Tags: &tags}, FromAgent, &Source{Agent: "codex"}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestSaveKeepsOneOfTheSameText(t *testing.T) {
|
|
fresh(t)
|
|
a := save(t, "UI uses no gradients.", Preference, Global, "ui")
|
|
b, added, err := Save(Input{Text: str(" ui uses no gradients "), Kind: str(Preference), Scope: str(Global), Tags: &[]string{"Design"}}, FromAgent, &Source{Agent: "claude"}, nil)
|
|
if err != nil || added {
|
|
t.Fatalf("the same text again: added %v, %v", added, err)
|
|
}
|
|
if b.ID != a.ID || len(b.Sources) != 2 || strings.Join(b.Tags, ",") != "ui,design" {
|
|
t.Fatalf("merged into %+v", b)
|
|
}
|
|
// another scope is another memory
|
|
if _, added, _ := Save(Input{Text: str("UI uses no gradients."), Kind: str(Preference), Scope: str("project:github.com/a/b")}, FromAgent, nil, nil); !added {
|
|
t.Fatal("the same text in a project's scope wasn't added")
|
|
}
|
|
all, _, _ := All()
|
|
if len(all) != 2 {
|
|
t.Fatalf("%d memories, want 2", len(all))
|
|
}
|
|
}
|
|
|
|
// What the user typed is theirs: an agent saving the same text later
|
|
// adds its source, never its kind or why.
|
|
func TestUsersMemoryNotChangedByAnAgentsSave(t *testing.T) {
|
|
fresh(t)
|
|
m := save(t, "Commit with explicit file names", Rule, Global)
|
|
if _, err := Edit(m.ID, Input{Why: str("other sessions' work sits in the tree")}, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _, err := Save(Input{Text: str("commit with explicit file names."), Kind: str(Preference), Scope: str(Global), Why: str("an agent's guess")}, FromAgent, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Kind != Rule || got.Why != "other sessions' work sits in the tree" || got.Origin != FromUser {
|
|
t.Fatalf("the user's memory became %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestSaveRefusesWhatIsntAMemory(t *testing.T) {
|
|
fresh(t)
|
|
for name, in := range map[string]Input{
|
|
"no text": {Text: str(" ")},
|
|
"bad kind": {Text: str("x"), Kind: str("mood")},
|
|
"bad scope": {Text: str("x"), Scope: str("project:")},
|
|
"too long": {Text: str(strings.Repeat("字", MaxText+1))},
|
|
} {
|
|
if _, _, err := Save(in, FromUser, nil, nil); err == nil {
|
|
t.Errorf("%s: saved", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestForgetSetsAsideAndRemoveDeletes(t *testing.T) {
|
|
fresh(t)
|
|
m := save(t, "The test box is mote", Reference, Global)
|
|
if f, err := Forget(m.ID); err != nil || f.Status != Archived {
|
|
t.Fatalf("forget: %+v, %v", f, err)
|
|
}
|
|
if hits, _ := Search(Query{Text: "mote"}); len(hits) != 0 {
|
|
t.Fatalf("an archived memory was found: %+v", hits)
|
|
}
|
|
if hits, _ := Search(Query{Text: "mote", Status: Archived}); len(hits) != 1 {
|
|
t.Fatal("not found among the archived")
|
|
}
|
|
// saved again, an archived memory is a new one
|
|
if _, added, _ := Save(Input{Text: str("The test box is mote"), Kind: str(Reference)}, FromAgent, nil, nil); !added {
|
|
t.Fatal("an archived memory took the save")
|
|
}
|
|
if err := Remove(m.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Get(m.ID); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("removed memory: %v", err)
|
|
}
|
|
if err := Remove("m_nothing"); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("removing nothing: %v", err)
|
|
}
|
|
}
|
|
|
|
// A memory.json that can't be read is unknown: a save fails, and the file
|
|
// is left as it was for the user to recover.
|
|
func TestUnreadableFileIsNeverWrittenOver(t *testing.T) {
|
|
fresh(t)
|
|
bad := []byte(`{"version":1,"memories":[{"id":"m_1","text":"kept`)
|
|
if err := os.WriteFile(Path(), bad, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := Save(Input{Text: str("new")}, FromUser, nil, nil); err == nil {
|
|
t.Fatal("saved over a file that can't be read")
|
|
}
|
|
if b, _ := os.ReadFile(Path()); string(b) != string(bad) {
|
|
t.Fatalf("the file became %q", b)
|
|
}
|
|
if _, _, err := All(); err == nil {
|
|
t.Fatal("an unreadable file listed as empty")
|
|
}
|
|
if _, err := Digest("codex"); err == nil {
|
|
t.Fatal("an unreadable file's digest read as empty")
|
|
}
|
|
}
|
|
|
|
// The last good generation is read when the file is cut short.
|
|
func TestCutShortFileReadFromItsBackup(t *testing.T) {
|
|
fresh(t)
|
|
save(t, "first", Fact, Global)
|
|
save(t, "second", Fact, Global) // keeps the first generation as .bak
|
|
if err := os.WriteFile(Path(), []byte(`{"memo`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
all, _, err := All()
|
|
if err != nil || len(all) != 1 || all[0].Text != "first" {
|
|
t.Fatalf("read %v, %v; want the backup's one memory", all, err)
|
|
}
|
|
}
|
|
|
|
func TestCleanTags(t *testing.T) {
|
|
got := strings.Join(CleanTags([]string{" #Go ", "go", "Release Notes", "", strings.Repeat("x", 41)}), ",")
|
|
if got != "go,release-notes" {
|
|
t.Fatalf("tags %q", got)
|
|
}
|
|
}
|
|
|
|
// A memory distilled from a conversation that the user deleted or
|
|
// archived isn't proposed again; an agent's own save of the same text
|
|
// still is kept, and one the user archived stays set aside.
|
|
func TestDismissedNotProposedAgain(t *testing.T) {
|
|
fresh(t)
|
|
cand := Candidate
|
|
propose := func(text, scope string) (*Memory, bool) {
|
|
t.Helper()
|
|
k := Preference
|
|
m, added, err := Save(Input{Text: &text, Kind: &k, Scope: &scope, Status: &cand}, FromInferred, &Source{Agent: "codex", Session: "s1"}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m, added
|
|
}
|
|
a, added := propose("Prefers pnpm.", Global)
|
|
if !added || a.Status != Candidate || a.Origin != FromInferred {
|
|
t.Fatalf("first proposal: %+v %v", a, added)
|
|
}
|
|
if err := Remove(a.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m, added := propose("prefers PNPM", Global); added || m != nil {
|
|
t.Fatalf("deleted, then proposed again: %+v", m)
|
|
}
|
|
if m, added := propose("Prefers pnpm.", "project:p1"); added || m != nil {
|
|
t.Fatalf("deleted, then proposed again in another scope: %+v", m)
|
|
}
|
|
// the user's or an agent's own save is theirs to make
|
|
if m := save(t, "Prefers pnpm.", Preference, Global); m == nil || m.Status != Active {
|
|
t.Fatalf("an agent's save of a dismissed text: %+v", m)
|
|
}
|
|
|
|
// archived by the user: dismissed too
|
|
b, _ := propose("Answers in Chinese.", Global)
|
|
if _, err := Edit(b.ID, Input{Status: str(Archived)}, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m, added := propose("Answers in Chinese.", Global); added || m != nil {
|
|
t.Fatalf("archived, then proposed again: %+v", m)
|
|
}
|
|
// archived by an agent (memory_forget): set aside, not proposed again
|
|
// in that scope, but not dismissed
|
|
c, _ := propose("Runs tests on mote.", Global)
|
|
if _, err := Forget(c.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m, added := propose("Runs tests on mote.", Global); added || m == nil || m.ID != c.ID || m.Status != Archived {
|
|
t.Fatalf("set aside, then proposed again: %+v %v", m, added)
|
|
}
|
|
if m, added := propose("Runs tests on mote.", "project:p1"); !added || m == nil {
|
|
t.Fatalf("set aside by an agent in one scope, not proposed in another: %+v", m)
|
|
}
|
|
f, err := view()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(f.Dismissed) != 2 {
|
|
t.Fatalf("dismissed: %q", f.Dismissed)
|
|
}
|
|
}
|
|
|
|
// Some memories to review, kept or archived at once (RealZeroP27 on X):
|
|
// the ones named change in one write as Edit changes one, an archived
|
|
// candidate is not proposed again, an id no longer there is passed over,
|
|
// and a status that isn't one changes none of them.
|
|
func TestSetStatusOfSome(t *testing.T) {
|
|
fresh(t)
|
|
cand := Candidate
|
|
propose := func(text string) *Memory {
|
|
t.Helper()
|
|
k, s := Preference, Global
|
|
m, _, err := Save(Input{Text: &text, Kind: &k, Scope: &s, Status: &cand}, FromInferred, &Source{Agent: "codex"}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m
|
|
}
|
|
a, b, c, d := propose("Prefers pnpm."), propose("Ships on Fridays."), propose("Answers in Chinese."), propose("Runs tests on mote.")
|
|
status := func(id string) string {
|
|
t.Helper()
|
|
m, err := Get(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m.Status
|
|
}
|
|
|
|
if n, err := SetStatus(Active, a.ID, b.ID, "m_gone"); err != nil || n != 2 {
|
|
t.Fatalf("kept %d, %v", n, err)
|
|
}
|
|
if status(a.ID) != Active || status(b.ID) != Active || status(c.ID) != Candidate || status(d.ID) != Candidate {
|
|
t.Fatalf("after keeping two: %s %s %s %s", status(a.ID), status(b.ID), status(c.ID), status(d.ID))
|
|
}
|
|
if hits, _ := Search(Query{Text: "pnpm"}); len(hits) != 1 {
|
|
t.Fatalf("a kept memory isn't searched: %+v", hits)
|
|
}
|
|
|
|
if n, err := SetStatus("gone", c.ID, d.ID); err == nil || n != 0 {
|
|
t.Fatalf("a bad status: %d, %v", n, err)
|
|
}
|
|
if status(c.ID) != Candidate || status(d.ID) != Candidate {
|
|
t.Fatal("a bad status changed a memory")
|
|
}
|
|
|
|
if n, err := SetStatus(Archived, c.ID, d.ID); err != nil || n != 2 {
|
|
t.Fatalf("archived %d, %v", n, err)
|
|
}
|
|
if status(c.ID) != Archived || status(d.ID) != Archived {
|
|
t.Fatal("not archived")
|
|
}
|
|
if m := propose("answers in chinese"); m != nil {
|
|
t.Fatalf("archived, then proposed again: %+v", m)
|
|
}
|
|
|
|
if _, err := SetStatus(Active, "m_gone", "m_none"); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("none of them there: %v", err)
|
|
}
|
|
}
|