Files
magpie/internal/memory/digest_test.go
T
yetone a18ed4995f memory: a memory of several lines keeps them, folded to its first line (_meouwu on X)
_meouwu on X: a memory added by hand in several lines showed as one line
with its breaks gone; it should keep its format and, folded, show the
first line and an ellipsis.

Cause: the store never lost the breaks. A memory file's body is
m.Text as written (encode/apply trim only the ends). They were lost
where it was shown: the Library row clamped the name to two lines with
white-space normal, the digest given to agents went through oneLine
(strings.Fields), and so did memory_search / memory_list in the memory
MCP.

Now:
- Library: a folded row and a suggestion show firstLine(text) (first
  line, " …" when more follow); an open row shows the whole text
  pre-wrap. The editor already kept "\n".
- Digest and memory MCP: memory.Indented keeps each line, the lines
  after the first indented two spaces so the memory stays one list
  item.
- Dream and distill listings stay one line on purpose: the model's
  reply is read line by line, and a user's memory is Fixed, never
  rewritten by them. Written in docs/subsystems/memory.md.

Memories saved before: their files already hold the breaks, so after
updating they show, edit and reach agents in their own lines. Nothing
to redo, no migration.

Run at 3fabaf8f2 (on origin/main b332ef7ef, the pushed head),
f6452d777 (on 1622484a8) and ee84e288a (on 54117164c), on the WSL VM magpie-desktop: mote and mica were over
their cores with other sessions' suites for ~2h.
- go vet -tags gtk3 ./... ok; gofmt -l clean.
- go test -tags gtk3 -count=1 -timeout 45m ./... at ee84e288a: 101 ok,
  2 FAIL, both red at origin/main 2416beed6 on the same VM:
  internal/gui TestClipboardReadBack (#7074, #7073), internal/usage
  TestRequestStateKeptAcrossACatalogFetchedAgain (#6572).
- Whole GUI suite (concurrency 8): 1355 of 29368 red in 221 files under
  load. Those 221 again at concurrency 3: 71 red; 66 of them red at
  base 54117164c too. Of the other 5, 3 pass alone;
  codex-auto-reset-settings (#6108) and copilot-ghe (#1949) are red at
  base alone too (2 of 3 and 3 of 3 rounds), commented there.
- After the rebase (f6452d777): internal/memory, internal/memdistill
  and -run "Memory|Budget|I18n" in gateway, gui, library ok. All 84
  library-*.test.cjs: 1920 of 1950 pass; each red file alone, beside
  1622484a8: commands-import, group-steady, mcp-copy-config,
  mcp-off-fold, open-again and all are red at base too (#7097; all 3 of
  3 at base); memory-suggest, mcp-json and bili pass alone.
- At 3fabaf8f2 (b332ef7ef brought flying_crp's digest header and
  memory_save note; conflicts were only both sides adding tests and
  doc text): go vet memory, gateway, memdistill; go test memory,
  memdistill, access, and -run "Memory|Budget|I18n|Digest" in gateway,
  gui, library ok; library-memory*.test.cjs 112 of 112.
- library-memory-lines.test.cjs: 5 langs x 980/420 x chromium/webkit.

Broken by hand (old code, new tests): TestMultiLineMemoryKeepsItsLines
red at the digest step only (save, edit and the file passed: the store
kept the breaks), TestIndented and TestMemorySearchKeepsTheLines red,
library-memory-lines red ("Release steps: …" expected).

Not tried: a macOS run (mote, mica busy), Windows, the real app.
2026-11-19 06:32:40 +08:00

172 lines
5.7 KiB
Go

package memory
import (
"os"
"strings"
"testing"
"time"
)
// What agents save reaches the digest when it is published, never at the
// save itself, so an agent's instructions don't change mid-session.
func TestDigestChangesOnlyWhenPublished(t *testing.T) {
at := fresh(t)
save(t, "Answer in Chinese", Preference, Global)
if d, _ := Digest("codex"); d != "" {
t.Fatalf("unpublished digest %q", d)
}
if changed, err := Publish(); err != nil || !changed {
t.Fatalf("publish: %v, %v", changed, err)
}
d, _ := Digest("codex")
if !strings.Contains(d, "Preferences:\n- Answer in Chinese") {
t.Fatalf("digest %q", d)
}
save(t, "Keep commits small", Rule, Global)
if d2, _ := Digest("codex"); d2 != d {
t.Fatal("a save changed the digest")
}
*at = at.Add(time.Hour)
if changed, _ := PublishDue(); changed {
t.Fatal("published again within a day")
}
*at = at.Add(24 * time.Hour)
if changed, _ := PublishDue(); !changed {
t.Fatal("not published after a day")
}
if d, _ := Digest("codex"); !strings.Contains(d, "Rules:\n- Keep commits small") {
t.Fatalf("digest after a day %q", d)
}
if changed, _ := Publish(); changed {
t.Fatal("an unchanged publish said it changed")
}
}
func TestDigestPartsAndBudget(t *testing.T) {
at := fresh(t)
old := globalBudget
globalBudget = 120
t.Cleanup(func() { globalBudget = old })
save(t, strings.Repeat("a", 60), Fact, Global)
*at = at.Add(time.Minute)
save(t, strings.Repeat("b", 60), Fact, Global)
pin := save(t, "pinned one", Episode, Global)
tr := true
if _, err := Edit(pin.ID, Input{Pinned: &tr}, true); err != nil {
t.Fatal(err)
}
save(t, "only codex sees this", Rule, "agent:codex")
p := &Project{Key: "github.com/yetone/magpie", Name: "magpie", Roots: []string{"/w/magpie"}}
if _, _, err := Save(Input{Text: str("tests run on mote"), Kind: str(Rule), Scope: str(ScopeOf(p))}, FromAgent, nil, p); err != nil {
t.Fatal(err)
}
gone := save(t, "archived one", Rule, Global)
if _, err := Forget(gone.ID); err != nil {
t.Fatal(err)
}
if _, err := Publish(); err != nil {
t.Fatal(err)
}
d, _ := Digest("codex")
for _, want := range []string{"pinned one", strings.Repeat("b", 60), "Only for you:\n" + "Rules:\n- only codex sees this", "When working in /w/magpie:\nRules:\n- tests run on mote"} {
if !strings.Contains(d, want) {
t.Errorf("digest lacks %q:\n%s", want, d)
}
}
// over budget: the older of the two long ones is left out, whole
for _, not := range []string{strings.Repeat("a", 60), "archived one"} {
if strings.Contains(d, not) {
t.Errorf("digest has %q:\n%s", not, d)
}
}
if other, _ := Digest("claude"); strings.Contains(other, "only codex") {
t.Errorf("another agent got codex's own:\n%s", other)
}
for _, name := range []string{"magpie", "Magpie", "OpenAI", "Anthropic"} {
if strings.Contains(strings.SplitN(d, "\n\n", 3)[1], name) {
t.Errorf("the digest's header names %s", name)
}
}
}
// The digest defers to the project's own instruction files: a memory that
// disagrees with them loses, and a project's rules are pointed to its
// files (flying_crp on X: memory and AGENTS.md said opposite things, and
// a project's rule went into memory). The header names neither this app
// nor a vendor.
func TestDigestDefersToTheProjectsFiles(t *testing.T) {
fresh(t)
save(t, "Answer in Chinese", Preference, Global)
if _, err := Publish(); err != nil {
t.Fatal(err)
}
d, _ := Digest("codex")
head := strings.SplitN(d, "\n\n", 3)[1]
for _, want := range []string{
"The project's own instruction files (AGENTS.md and the like) and docs come first",
"where a memory disagrees with them, follow the project's files",
"A rule or fact of one project belongs in that project's files",
"memory keeps what holds for the user across projects",
} {
if !strings.Contains(head, want) {
t.Errorf("the header lacks %q:\n%s", want, head)
}
}
for _, name := range []string{"magpie", "claude", "codex", "openai", "anthropic", "gemini"} {
if strings.Contains(strings.ToLower(head), name) {
t.Errorf("the header names %s", name)
}
}
}
// A memory the user writes in several lines keeps them: on save, on an
// edit, in its file, and in the digest, where the lines after the first
// are indented under its item (_meouwu on X: the line breaks were gone).
func TestMultiLineMemoryKeepsItsLines(t *testing.T) {
fresh(t)
text := "Release steps:\n1. tag the commit\n2. push the tag by name\n\n - never --tags"
m, _, err := Save(Input{Text: str(text + "\n"), Kind: str(Procedure), Scope: str(Global)}, FromUser, nil, nil)
if err != nil {
t.Fatal(err)
}
if m.Text != text {
t.Fatalf("saved %q, want %q", m.Text, text)
}
text += "\n3. wait for the assets"
if _, err := Edit(m.ID, Input{Text: str(text)}, true); err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(in(m.ID + ".md"))
if err != nil {
t.Fatal(err)
}
if got, err := decode(m.ID, b, time.Now()); err != nil || got.Text != text {
t.Fatalf("the file reads %q (%v), want %q", got.Text, err, text)
}
if got, _ := Get(m.ID); got.Text != text {
t.Fatalf("Get %q, want %q", got.Text, text)
}
if _, err := Publish(); err != nil {
t.Fatal(err)
}
d, _ := Digest("codex")
want := "How things are done:\n- Release steps:\n 1. tag the commit\n 2. push the tag by name\n\n - never --tags\n 3. wait for the assets"
if !strings.Contains(d, want) {
t.Fatalf("digest %q, want it to hold %q", d, want)
}
}
func TestIndented(t *testing.T) {
for in, want := range map[string]string{
"one line": "one line",
" a\r\nb \n\n\tc\n": "a\n> b\n\n> \tc",
"a \t\n \nb": "a\n\n> b",
"\n\nonly after blanks\n\n": "only after blanks",
} {
if got := Indented(in, "> "); got != want {
t.Errorf("Indented(%q) = %q, want %q", in, got, want)
}
}
}