Files
magpie/internal/gui/memory_distill_test.go
T
yetone 2c2bd15d71 memory: Tidy up every night, a switch in the Memory tab
The Memory tab gets a second row under Learn from conversations. It is
off by default. Switched on, the first tidy is due at the next 03:00.
Tidy now runs one at once, and Undo puts the last one back. While
distillation is off, the row has its own model picker; both rows share
one model.
Routes: POST /api/memory/dream {on}, /dream/run and /dream/undo.
GET /api/memory/distill carries "dreaming", so a tidy no longer shows
as conversations being read.

The prompt's stale rule is narrower. On the first real run it set aside
two decisions because "the work is done". Now only a note that later
notes contradict, one past its date, or one about a finished one-off
task is stale; a decision, direction or preference stays.
docs/subsystems/memory.md gets a Dreaming section.

Real run: a sandbox memory.json of 62 real distilled memories, 12 active
and one each user-written and pinned, with auto/claude-haiku-5-5 through
the live gateway.
- Before the prompt change: 12 changes in 48s, 10 merges and 2 set aside.
  Both set-aside notes were decisions.
- After: 12 merges in 46s, none set aside. Each merge joins real
  duplicates (three ~/.codex/sessions cleanup rules into one, the
  TestFlight 1.66/1.68 pair). One went into the fixed memory, whose text
  was kept word for word.
- Undo: 25 restored. memory.json's ids and statuses equal the backup
  taken before the run, and every merged memory is gone.

Broken by hand, each red:
- Undo not reloading the list: library-memory 22 failed.
- Ignoring DreamSince: TestDreamsOnceANight.
- A plan applied after an error: TestDreamFailureChangesNothing.
- Tidy's fixed guard and Untidy's status check: their memory tests.

Run on mote at this tree:
- go test internal/memory, memdistill, library: ok.
- internal/gui -run 'Distill|Dream|I18n|Budget': ok.
- library-memory.test.cjs: 22/22 in both engines.
- i18n-keys and i18n-en-no-cjk: 4/4 each.
- go vet (darwin), GOOS=linux -tags nogui, GOOS=windows: ok.
Not run yet: the full suite at this head.
2026-11-10 03:25:45 +08:00

118 lines
4.4 KiB
Go

package gui
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/memdistill"
"github.com/yetone/magpie/internal/memory"
"github.com/yetone/magpie/internal/provider"
)
// Conversations are read into memory through the gateway this process
// serves: the request reaches the model's provider with the instructions
// and the conversation, is counted under the memory agent, and the
// reply's text alone comes back; a refusal comes back as an error that
// says the provider's reason. With no gateway served here, it says so.
func TestDistillCompleteThroughGateway(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
var got map[string]any
refuse := false
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
json.Unmarshal(b, &got)
w.Header().Set("Content-Type", "application/json")
if refuse {
w.WriteHeader(400)
io.WriteString(w, `{"type":"error","error":{"type":"invalid_request_error","message":"model not allowed on this plan"}}`)
return
}
io.WriteString(w, `{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[{"type":"thinking","thinking":"hmm","signature":"s"},{"type":"text","text":"[{\"text\":\"Likes tabs\"}]"}],"stop_reason":"end_turn","usage":{"input_tokens":90,"output_tokens":9}}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "dt", Name: "DT", Anthropic: up.URL, Models: []string{"m1"}, Key: "k"}); err != nil {
t.Fatal(err)
}
prev := served.Swap(nil)
t.Cleanup(func() { served.Store(prev) })
if _, err := distillComplete(context.Background(), "dt/m1", "sys", "conv"); err == nil || !strings.Contains(err.Error(), "isn't served") {
t.Fatalf("no gateway: %v", err)
}
gw := gateway.New()
served.Store(gw)
reply, err := distillComplete(context.Background(), "dt/m1", "Write down what lasts.", "Conversation:\n\nUser: use tabs")
if err != nil || reply != `[{"text":"Likes tabs"}]` {
t.Fatalf("%q %v", reply, err)
}
b, _ := json.Marshal(got)
if !strings.Contains(string(b), "Write down what lasts.") || !strings.Contains(string(b), "User: use tabs") {
t.Fatalf("sent: %s", b)
}
rs := gw.Trace(context.Background(), 0, 0).Routes
if len(rs) != 1 || rs[0].Agent != memdistill.Agent {
t.Fatalf("counted as %+v", rs)
}
refuse = true
if _, err := distillComplete(context.Background(), "dt/m1", "s", "u"); err == nil || !strings.Contains(err.Error(), "model not allowed on this plan") {
t.Fatalf("refused: %v", err)
}
}
// The nightly tidy's routes: switched on and off, and the last tidy
// undone once, the memories it set aside back where they were.
func TestDreamRoutes(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
mux := http.NewServeMux()
memoryDistillRoutes(mux)
post := func(path, body string) distillView {
t.Helper()
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
var v distillView
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &v) != nil {
t.Fatalf("%s: %d %s", path, rec.Code, rec.Body)
}
return v
}
if v := post("/api/memory/dream", `{"on":true}`); !v.Dream || v.On {
t.Fatalf("on: %+v", v.State)
}
text, kind, scope, st := "Old", memory.Fact, memory.Global, memory.Candidate
m, _, err := memory.Save(memory.Input{Text: &text, Kind: &kind, Scope: &scope, Status: &st}, memory.FromInferred, nil, nil)
if err != nil {
t.Fatal(err)
}
text2 := "Other"
if _, _, err := memory.Save(memory.Input{Text: &text2, Kind: &kind, Scope: &scope, Status: &st}, memory.FromInferred, nil, nil); err != nil {
t.Fatal(err)
}
cs, err := memdistill.Dream(context.Background(), "a/m", func(context.Context, string, string, string) (string, error) {
return `{"stale":[{"id":"n1"}]}`, nil
})
if err != nil || len(cs) != 1 {
t.Fatal(cs, err)
}
if got, _ := memory.Get(m.ID); got.Status != memory.Archived {
t.Fatalf("not set aside: %s", got.Status)
}
if v := post("/api/memory/dream/undo", `{}`); !v.Undone {
t.Fatalf("undo: %+v", v.State)
}
if got, _ := memory.Get(m.ID); got.Status != memory.Candidate {
t.Fatalf("not back: %s", got.Status)
}
if v := post("/api/memory/dream", `{"on":false}`); v.Dream {
t.Fatalf("off: %+v", v.State)
}
}