Files
magpie/internal/memory/digest.go
T
yetone 73ecef0b36 memory: each memory is a Markdown file of its own
memory.json held every memory in one JSON file, which grew without
bound, was hard to read or fix by hand, and was rewritten whole on every
change. Memory is now a folder, `memory/` beside where memory.json was:

- `<id>.md` is the memory: YAML front matter (kind, scope, status,
  origin, pinned, tags, why, sources, times) and the text as the body.
  The file is the source of truth.
- `meta.json` holds what isn't one memory's: projects, the digest,
  dismissed texts, deletions, declined suggestions, when a search last
  returned each memory (so a search never rewrites a memory's file), and
  the hash of each file as magpie last wrote it.
- `index.json` is a cache of the parsed files by size and mtime only:
  deleted or garbled, memory reads the same.
- `journal.json` holds a change to more than one file (a merge, a sync)
  before any is written; the next read finishes one cut off halfway.

A file whose hash isn't the one magpie wrote was edited or added by hand:
it is the user's (origin user, Edited), so a tidy leaves it and an
agent's change synced from another computer never writes over it. A file
with no front matter is a global fact named by its file. A write leaves
any file that changed on disk since it was read (a hand edit made
meanwhile), and so does a remove. A file that can't be parsed is left
out and never written over nor removed, sync included; meta.json that
can't be read is unknown (an error), as memory.json was.

memory.json is moved in on the first read, all under one journal, and
kept as memory.json.migrated. One that can't be read is an error and
nothing is written.

The scope of a hand-written file keeps its case (a project's holds a
path); "global" in any case is global.

Ran at this head (rebased on origin/main 50d88f17c), on mote:
- go test -count=1 ./internal/memory ./internal/memdistill
  ./internal/memembed ./internal/backup ./internal/library ./internal/gui:
  ./internal/davsync ./internal/appdir: ok; go vet ./internal/...: ok;
  go test -run 'Memory|memory|Doc|Subsystem' ./internal/...: 98 ok
- go vet ./... and GOOS=windows go vet ./... (before the rebase, whose
  three commits touch no memory code): ok; gofmt -l: clean
- win-run go test ./internal/memory ./internal/backup: ok
- wsl-run go vet of memory, backup, library, memdistill, memembed,
  davsync and go test of memory, backup, memdistill: ok
- A copy of the real memory.json (206 memories) migrated in a sandbox:
  every field of every memory the same on the first read, from the
  index, and with the index rebuilt; no memory read as the user's.

Broken by hand, each red, green restored:
- no journal: TestCutOffChangeIsFinished
- no on-disk hash check on write: TestHandEditDuringAChangeStays
- no on-disk hash check on remove: TestHandEditDuringARemoveStays
- broken files written: TestBrokenFileIsLeftAsItIs
- no hand-edit detection: TestHandEditIsTheUsers
- scope lowercased: TestEachMemoryIsAMarkdownFile (and memdistill's
  TestReadsQuietSessionsOnce, which found it)

Not tried: the full go test ./... (mote and mica both over their cores);
the live app's own memory moved in (a sandbox copy only);
`go vet ./...` on Linux (the WSL box has no gtk4 headers for Wails);
a hand edit through a sync service's own conflict copies.
2026-11-14 14:27:01 +08:00

214 lines
5.7 KiB
Go

package memory
import (
"maps"
"slices"
"strings"
"time"
"unicode/utf8"
)
// The digest is the part of memory an agent is given in its instructions,
// so it knows the user's preferences and rules from its first word, before
// it thinks to search. It is held to a budget, pinned memories and the
// kinds that hold longest first, and changes only when Publish is called:
// when the user changes memory, and once a day for what agents saved
// meanwhile. An agent's instructions change mid-session only then, so the
// prompt caches built on them aren't thrown away at every save.
//
// Its text goes to every agent's model vendor: it names neither magpie nor
// any vendor, only the memories and how to read them.
// Budgets, in characters, of the global part and of each project's.
var (
globalBudget = 4000
projectBudget = 1500
agentBudget = 1000
allBudget = 8000
)
var kindTitles = map[string]string{
Profile: "About the user",
Preference: "Preferences",
Rule: "Rules",
Reference: "References",
Fact: "Facts",
Procedure: "How things are done",
Episode: "Recent decisions",
}
// render is the digest of one scope's memories within budget characters:
// a heading per kind, a line per memory, pinned and newer ones first, and
// none cut in the middle. "" when the scope has none.
func render(ms []*Memory, scope string, budget int) string {
pool := []*Memory{}
for _, m := range ms {
if m.Scope == scope && m.Status == Active {
pool = append(pool, m)
}
}
slices.SortStableFunc(pool, func(a, b *Memory) int {
if a.Pinned != b.Pinned {
if a.Pinned {
return -1
}
return 1
}
if d := kindRank(a.Kind) - kindRank(b.Kind); d != 0 {
return d
}
return b.Updated.Compare(a.Updated)
})
// the lines picked within budget, then put back in kind order
picked := map[string][]string{}
used := 0
for _, m := range pool {
line := "- " + oneLine(m.Text)
if m.Why != "" {
line += " (why: " + oneLine(m.Why) + ")"
}
n := utf8.RuneCountInString(line) + 1
if used+n > budget {
continue
}
used += n
picked[m.Kind] = append(picked[m.Kind], line)
}
var b strings.Builder
for _, k := range Kinds {
if len(picked[k]) == 0 {
continue
}
if b.Len() > 0 {
b.WriteString("\n")
}
b.WriteString(kindTitles[k] + ":\n")
b.WriteString(strings.Join(picked[k], "\n"))
b.WriteString("\n")
}
return strings.TrimSpace(b.String())
}
func oneLine(s string) string { return strings.Join(strings.Fields(s), " ") }
// publish renders every scope's digest into f.
func publish(f *file) {
scopes := map[string]bool{}
for _, m := range f.Memories {
if m.Status == Active {
scopes[m.Scope] = true
}
}
d := map[string]string{}
for s := range scopes {
budget := globalBudget
switch {
case strings.HasPrefix(s, "project:"):
budget = projectBudget
case strings.HasPrefix(s, "agent:"):
budget = agentBudget
}
if t := render(f.Memories, s, budget); t != "" {
d[s] = t
}
}
f.Digest = d
f.Published = now()
}
// Publish makes what memory holds now the digest agents are given. It
// reports whether any digest changed, for the caller to write it into the
// agents' instructions.
func Publish() (changed bool, err error) {
err = change(func(f *file) error {
old := f.Digest
publish(f)
changed = !maps.Equal(old, f.Digest)
return nil
})
return changed, err
}
// PublishDue publishes when the digest was last published more than a day
// ago, and memory holds something or held something then. It reports
// whether any digest changed.
func PublishDue() (bool, error) {
f, err := view()
if err != nil {
return false, err
}
if now().Sub(f.Published) < 24*time.Hour || (len(f.Memories) == 0 && len(f.Digest) == 0) {
// nothing kept yet: no file written for it
return false, nil
}
return Publish()
}
// Digest is the published digest an agent is given: the global part, the
// agent's own, and each project's under the folders it was seen in, held
// to the whole budget, the projects with the newest memories first. ""
// when there is nothing, or memory can't be read (unknown: the
// caller keeps what the agent has).
func Digest(agent string) (string, error) {
f, err := cached()
if err != nil {
return "", err
}
return digestOf(f, agent), nil
}
func digestOf(f *file, agent string) string {
if len(f.Digest) == 0 {
return ""
}
parts := []string{}
used := 0
add := func(s string) bool {
n := utf8.RuneCountInString(s)
if used+n > allBudget {
return false
}
used += n
parts = append(parts, s)
return true
}
if g := f.Digest[Global]; g != "" {
add(g)
}
if a := f.Digest["agent:"+agent]; agent != "" && a != "" {
add("Only for you:\n" + a)
}
// projects, newest change first
latest := map[string]time.Time{}
for _, m := range f.Memories {
if m.Status == Active && strings.HasPrefix(m.Scope, "project:") && m.Updated.After(latest[m.Scope]) {
latest[m.Scope] = m.Updated
}
}
scopes := []string{}
for s := range f.Digest {
if strings.HasPrefix(s, "project:") {
scopes = append(scopes, s)
}
}
slices.SortFunc(scopes, func(a, b string) int {
if c := latest[b].Compare(latest[a]); c != 0 {
return c
}
return strings.Compare(a, b)
})
for _, s := range scopes {
key := strings.TrimPrefix(s, "project:")
where := key
if p := f.Projects[key]; p != nil && len(p.Roots) > 0 {
where = strings.Join(p.Roots, ", ")
}
add("When working in " + where + ":\n" + f.Digest[s])
}
if len(parts) == 0 {
return ""
}
return "## Memory\n\nWhat is known from earlier conversations with the user. Follow it unless the user says otherwise now. When a memory_search tool is at hand, search it for more before asking the user again, and save what they ask to be remembered with memory_save.\n\n" +
strings.Join(parts, "\n\n")
}