The agents ticked under Library → Instructions get a "## Memory" part in
their instructions: the global memories, the agent's own, and each
project's under the folders it was seen in (memory.Digest), unless
Instructions.NoMemory. The digest changes only when published, so an
agent's instructions (and the prompt cache on them) change mid-session
only then: at once when the user changes memory (PublishMemory(false),
called by the GUI), and once a day from KeepLinked's loop for what
agents saved meanwhile (PublishMemory(true) -> memory.PublishDue).
PublishDue writes nothing while memory has never held anything, so a
user who never uses memory gets no memory.json.
Run (mote-run, whole worktree, with the rest of the memory layer):
- go test ./internal/memory ./internal/library: ok (library 130.9s)
- go vet ./internal/memory ./internal/library ./internal/gui, GOOS
darwin, linux, windows: ok
- gofmt -l: clean
Broken by hand (mote-run -run TestMemoryDigestInInstructions):
- memoryDigest's NoMemory check removed: red. It first stayed green,
since the test switched NoMemory on with memory.json deleted; the test
now gives memory back and sees the part return before switching it off.
- PublishDue's "nothing kept" guard removed: red ("a due publish with
nothing kept wrote memory.json").
Not tried: a real agent reading the digest in its instructions.
93 lines
3.3 KiB
Go
93 lines
3.3 KiB
Go
package library
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/memory"
|
|
)
|
|
|
|
func memSave(t *testing.T, text, kind, scope string) {
|
|
t.Helper()
|
|
if _, _, err := memory.Save(memory.Input{Text: &text, Kind: &kind, Scope: &scope}, memory.FromUser, nil, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Memory's published digest is written into a ticked agent's part after
|
|
// the shared text, with what is kept for that agent alone; an agent not
|
|
// ticked gets none. A save shows only once published, and a memory.json
|
|
// that can't be read leaves the part as it was (unknown, not empty).
|
|
// NoMemory leaves the digest out.
|
|
func TestMemoryDigestInInstructions(t *testing.T) {
|
|
h := sandbox(t)
|
|
shared := "Be brief."
|
|
ok(t)(SaveInstructions(InstructionsChange{Shared: &shared, Agents: []string{"codex"}}))
|
|
codex := filepath.Join(h, ".codex/AGENTS.md")
|
|
if res, err := PublishMemory(true); err != nil || res != nil {
|
|
t.Fatalf("nothing kept, but published: %+v %v", res, err)
|
|
}
|
|
if _, err := os.Stat(memory.Path()); err == nil {
|
|
t.Fatal("a due publish with nothing kept wrote memory.json")
|
|
}
|
|
|
|
memSave(t, "Answer in Chinese.", memory.Preference, memory.Global)
|
|
memSave(t, "Codex runs tests on mote.", memory.Rule, "agent:codex")
|
|
memSave(t, "Claude keeps notes short.", memory.Rule, "agent:claude")
|
|
if s := read(t, codex); strings.Contains(s, "Answer in Chinese.") {
|
|
t.Fatalf("written before it was published:\n%s", s)
|
|
}
|
|
ok(t)(PublishMemory(false))
|
|
s := read(t, codex)
|
|
if !strings.Contains(s, "Be brief.") || !strings.Contains(s, "Answer in Chinese.") || !strings.Contains(s, "Codex runs tests on mote.") || strings.Contains(s, "Claude keeps notes") {
|
|
t.Fatalf("codex's instructions:\n%s", s)
|
|
}
|
|
if strings.Index(s, "Be brief.") > strings.Index(s, "Answer in Chinese.") {
|
|
t.Errorf("the digest before the shared text:\n%s", s)
|
|
}
|
|
if c := filepath.Join(h, ".claude/CLAUDE.md"); strings.Contains(readMaybe(c), "Answer in Chinese.") {
|
|
t.Errorf("claude isn't ticked, but was given memory:\n%s", readMaybe(c))
|
|
}
|
|
|
|
// memory.json unreadable: the part is left as it is
|
|
memSave(t, "Use tabs.", memory.Preference, memory.Global)
|
|
if _, err := memory.Publish(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(memory.Path(), []byte("{not json"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Remove(memory.Path() + ".bak")
|
|
res, err := changeInstructions(func(*Library) error { return nil })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := read(t, codex); got != s {
|
|
t.Fatalf("an unreadable memory.json changed the part:\n%s", got)
|
|
}
|
|
if len(res.Problems) == 0 {
|
|
t.Errorf("an unreadable memory.json wasn't said: %+v", res)
|
|
}
|
|
if err := os.Remove(memory.Path()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// readable again: the part is back, then NoMemory takes it out
|
|
memSave(t, "Answer in Chinese.", memory.Preference, memory.Global)
|
|
ok(t)(PublishMemory(false))
|
|
if got := read(t, codex); !strings.Contains(got, "## Memory") {
|
|
t.Fatalf("memory readable again, but no digest:\n%s", got)
|
|
}
|
|
ok(t)(changeInstructions(func(l *Library) error { l.Instructions.NoMemory = true; return nil }))
|
|
if got := read(t, codex); !strings.Contains(got, "Be brief.") || strings.Contains(got, "## Memory") {
|
|
t.Fatalf("NoMemory:\n%s", got)
|
|
}
|
|
}
|
|
|
|
func readMaybe(p string) string {
|
|
b, _ := os.ReadFile(p)
|
|
return string(b)
|
|
}
|