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.
545 lines
16 KiB
Go
545 lines
16 KiB
Go
// Package memory is what the agents know from earlier conversations, kept
|
||
// once for all of them: a preference the user stated in Claude Code is
|
||
// known to Codex the next day. Each memory is one short fact with where it
|
||
// holds (everywhere, a project, an agent), what kind of fact it is, the
|
||
// user's tags, and where it came from. The agents read and add to it
|
||
// through the memory MCP server (gateway/memory_mcp.go), and the ones the
|
||
// user picks are also given a digest of it in their instructions
|
||
// (library/instructions.go). The Library's Memory tab lists and edits it.
|
||
//
|
||
// It is a folder of Markdown files in magpie's config folder, one a memory
|
||
// (store.go): one writer at a time across magpies (filelock), a change to
|
||
// several written all or none (a journal), and a file that can't be read
|
||
// is unknown, never empty, so nothing is written over it.
|
||
package memory
|
||
|
||
import (
|
||
"crypto/rand"
|
||
"encoding/hex"
|
||
"errors"
|
||
"fmt"
|
||
"slices"
|
||
"strings"
|
||
"time"
|
||
"unicode/utf8"
|
||
)
|
||
|
||
// The kinds of memory. A kind decides how long a memory stays true and
|
||
// where it goes in a digest, so it is a field of its own, not a tag.
|
||
const (
|
||
Profile = "profile" // who the user is: role, languages, what they know
|
||
Preference = "preference" // how the user wants things done
|
||
Rule = "rule" // a rule learned from a correction, with why
|
||
Fact = "fact" // something true of a project the code doesn't say
|
||
Reference = "reference" // where something is: a host, a dashboard, a URL
|
||
Procedure = "procedure" // how something is done: steps that worked
|
||
Episode = "episode" // what happened or was decided, and when
|
||
)
|
||
|
||
// Kinds are the kinds in the order a digest gives them.
|
||
var Kinds = []string{Profile, Preference, Rule, Reference, Fact, Procedure, Episode}
|
||
|
||
// Where a memory came from.
|
||
const (
|
||
FromUser = "user" // typed or edited by the user
|
||
FromAgent = "agent" // saved by an agent through the MCP server
|
||
FromImported = "imported" // read from an agent's own memory files
|
||
FromInferred = "inferred" // distilled from a conversation
|
||
)
|
||
|
||
// A memory's state.
|
||
const (
|
||
Active = "active"
|
||
Candidate = "candidate" // waiting for the user to keep or drop it
|
||
Archived = "archived" // set aside: out of searches and digests, kept
|
||
Superseded = "superseded" // replaced by SupersededBy
|
||
)
|
||
|
||
// Global is the scope of a memory that holds everywhere. A project's is
|
||
// "project:" and its key (ProjectOf), an agent's "agent:" and its id.
|
||
const Global = "global"
|
||
|
||
// MaxText is the longest a memory's text may be: one fact, not a document.
|
||
const MaxText = 2000
|
||
|
||
// Memory is one thing known.
|
||
type Memory struct {
|
||
ID string `json:"id"`
|
||
Text string `json:"text"`
|
||
Kind string `json:"kind"`
|
||
Scope string `json:"scope"`
|
||
Tags []string `json:"tags,omitempty"`
|
||
// Why is the reason behind a rule or preference, when one was given
|
||
Why string `json:"why,omitempty"`
|
||
Origin string `json:"origin"`
|
||
Status string `json:"status"`
|
||
SupersededBy string `json:"supersededBy,omitempty"`
|
||
Pinned bool `json:"pinned,omitempty"`
|
||
Sources []Source `json:"sources,omitempty"`
|
||
Created time.Time `json:"created"`
|
||
Updated time.Time `json:"updated"`
|
||
// Edited is when the user last changed it (Edit by the user, Save
|
||
// of what they typed): a change made then is theirs, which a sync
|
||
// from another computer tells apart from an agent's or a tidy's
|
||
// (Combine)
|
||
Edited time.Time `json:"edited,omitzero"`
|
||
// Used is when a search last returned it, Uses how many times
|
||
Used time.Time `json:"used,omitzero"`
|
||
Uses int `json:"uses,omitempty"`
|
||
// Skill is the Library skill this procedure was made (MadeSkill)
|
||
Skill string `json:"skill,omitempty"`
|
||
}
|
||
|
||
// Source is where a memory was said or found.
|
||
type Source struct {
|
||
Agent string `json:"agent,omitempty"`
|
||
Session string `json:"session,omitempty"`
|
||
Dir string `json:"dir,omitempty"` // the folder the agent worked in
|
||
File string `json:"file,omitempty"` // an imported memory's file
|
||
Quote string `json:"quote,omitempty"`
|
||
At time.Time `json:"at"`
|
||
}
|
||
|
||
// Project is a project memories are kept for: a git repository by its
|
||
// remote, or a folder, with the folders it was seen in.
|
||
type Project struct {
|
||
Key string `json:"key"`
|
||
Name string `json:"name"`
|
||
Roots []string `json:"roots,omitempty"`
|
||
}
|
||
|
||
// file is all of memory as read from Dir (store.go).
|
||
type file struct {
|
||
Version int `json:"version"`
|
||
Memories []*Memory `json:"memories"`
|
||
Projects map[string]*Project `json:"projects,omitempty"`
|
||
// Digest is the text each scope's digest had when it was last
|
||
// published (Publish): what the agents' instructions are given, which
|
||
// changes only when the user changes memory or once a day, so an
|
||
// agent's save doesn't rewrite every agent's instructions mid-session
|
||
Digest map[string]string `json:"digest,omitempty"`
|
||
Published time.Time `json:"published,omitzero"`
|
||
// Dismissed are the texts (norm) of memories distilled from
|
||
// conversations that the user deleted or archived, the last
|
||
// maxDismissed: never proposed again
|
||
Dismissed []string `json:"dismissed,omitempty"`
|
||
// Removed is when each memory deleted for good was deleted, kept
|
||
// keepRemoved: a sync doesn't bring it back from a computer that
|
||
// still has it (Combine)
|
||
Removed map[string]time.Time `json:"removed,omitempty"`
|
||
// Declined are the suggestions the user turned down (Suggest), by
|
||
// key, the last maxDeclined
|
||
Declined []string `json:"declined,omitempty"`
|
||
|
||
// what read found on disk, for write to change only what changed
|
||
// (store.go): each memory file's hash as read, the ids whose file
|
||
// can't be read (never written over nor removed), the hashes magpie
|
||
// last wrote (written: another is the user's edit), and meta.json's
|
||
// bytes
|
||
seen map[string]string
|
||
broken map[string]bool
|
||
written map[string]string
|
||
metaB []byte
|
||
}
|
||
|
||
// maxDismissed is how many dismissed texts are kept.
|
||
const maxDismissed = 1000
|
||
|
||
// dismiss notes that the user threw m away, when it was distilled: the
|
||
// same text isn't proposed again.
|
||
func dismiss(f *file, m *Memory) {
|
||
if m.Origin != FromInferred {
|
||
return
|
||
}
|
||
n := norm(m.Text)
|
||
if slices.Contains(f.Dismissed, n) {
|
||
return
|
||
}
|
||
f.Dismissed = append(f.Dismissed, n)
|
||
if len(f.Dismissed) > maxDismissed {
|
||
f.Dismissed = f.Dismissed[len(f.Dismissed)-maxDismissed:]
|
||
}
|
||
}
|
||
|
||
// now is the clock; a variable for tests
|
||
var now = time.Now
|
||
|
||
// ErrNotFound is a memory id that isn't there.
|
||
var ErrNotFound = errors.New("no memory with that id")
|
||
|
||
// All is every memory, in every state, newest change first, and the
|
||
// projects they are kept for.
|
||
func All() ([]*Memory, map[string]*Project, error) {
|
||
f, err := view()
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
slices.SortStableFunc(f.Memories, func(a, b *Memory) int { return b.Updated.Compare(a.Updated) })
|
||
if f.Projects == nil {
|
||
f.Projects = map[string]*Project{}
|
||
}
|
||
return f.Memories, f.Projects, nil
|
||
}
|
||
|
||
// Get is the memory with id.
|
||
func Get(id string) (*Memory, error) {
|
||
f, err := view()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if m := find(f, id); m != nil {
|
||
return m, nil
|
||
}
|
||
return nil, ErrNotFound
|
||
}
|
||
|
||
func find(f *file, id string) *Memory {
|
||
for _, m := range f.Memories {
|
||
if m.ID == id {
|
||
return m
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func newID() string {
|
||
b := make([]byte, 6)
|
||
rand.Read(b)
|
||
return "m_" + hex.EncodeToString(b)
|
||
}
|
||
|
||
// Input is a memory as a caller gives it: to Save a new one, or to Edit
|
||
// one (where a nil field is left as it is).
|
||
type Input struct {
|
||
Text *string `json:"text,omitempty"`
|
||
Kind *string `json:"kind,omitempty"`
|
||
Scope *string `json:"scope,omitempty"`
|
||
Tags *[]string `json:"tags,omitempty"`
|
||
Why *string `json:"why,omitempty"`
|
||
Status *string `json:"status,omitempty"`
|
||
Pinned *bool `json:"pinned,omitempty"`
|
||
}
|
||
|
||
// Check says what is wrong with a memory's fields, nil when nothing.
|
||
func check(m *Memory) error {
|
||
switch {
|
||
case strings.TrimSpace(m.Text) == "":
|
||
return errors.New("a memory needs its text")
|
||
case utf8.RuneCountInString(m.Text) > MaxText:
|
||
return fmt.Errorf("a memory is one fact, up to %d characters: this one has %d", MaxText, utf8.RuneCountInString(m.Text))
|
||
case !slices.Contains(Kinds, m.Kind):
|
||
return fmt.Errorf("kind %q isn't one of %s", m.Kind, strings.Join(Kinds, ", "))
|
||
case !validScope(m.Scope):
|
||
return fmt.Errorf("scope %q isn't %q, project:<key> or agent:<id>", m.Scope, Global)
|
||
case !slices.Contains([]string{Active, Candidate, Archived, Superseded}, m.Status):
|
||
return fmt.Errorf("status %q isn't active, candidate, archived or superseded", m.Status)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func validScope(s string) bool {
|
||
if s == Global {
|
||
return true
|
||
}
|
||
for _, p := range []string{"project:", "agent:"} {
|
||
if rest, ok := strings.CutPrefix(s, p); ok && strings.TrimSpace(rest) != "" && !strings.ContainsAny(rest, "\n\r") {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// CleanTags are tags as kept: trimmed, lower case, no "#", each once, in
|
||
// the order given.
|
||
func CleanTags(tags []string) []string {
|
||
out := []string{}
|
||
for _, t := range tags {
|
||
t = strings.ToLower(strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(t), "#")))
|
||
t = strings.Join(strings.Fields(t), "-")
|
||
if t != "" && utf8.RuneCountInString(t) <= 40 && !slices.Contains(out, t) {
|
||
out = append(out, t)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
func apply(m *Memory, in Input) {
|
||
if in.Text != nil {
|
||
m.Text = strings.TrimSpace(*in.Text)
|
||
}
|
||
if in.Kind != nil {
|
||
m.Kind = strings.ToLower(strings.TrimSpace(*in.Kind))
|
||
}
|
||
if in.Scope != nil {
|
||
m.Scope = strings.TrimSpace(*in.Scope)
|
||
}
|
||
if in.Tags != nil {
|
||
m.Tags = CleanTags(*in.Tags)
|
||
}
|
||
if in.Why != nil {
|
||
m.Why = strings.TrimSpace(*in.Why)
|
||
}
|
||
if in.Status != nil {
|
||
m.Status = *in.Status
|
||
if m.Status != Superseded {
|
||
m.SupersededBy = ""
|
||
}
|
||
}
|
||
if in.Pinned != nil {
|
||
m.Pinned = *in.Pinned
|
||
}
|
||
}
|
||
|
||
// norm is a text as compared for sameness: case, spaces and the full stop
|
||
// at its end left out.
|
||
func norm(s string) string {
|
||
s = strings.ToLower(strings.Join(strings.Fields(s), " "))
|
||
return strings.TrimRight(s, ".。!! ")
|
||
}
|
||
|
||
// Save adds a memory from origin, with src where it came from (nil for
|
||
// none). A memory with the same text in the same scope, not archived, is
|
||
// not added again: it gets the source, the tags it lacked and the newer
|
||
// kind and why, and is returned with added false. A project scope's
|
||
// project is recorded when proj is given.
|
||
//
|
||
// One distilled from a conversation (FromInferred) is not added when the
|
||
// user threw the same text away before (dismissed), or set it aside in
|
||
// that scope: m is nil then, or the one set aside, and added false.
|
||
func Save(in Input, origin string, src *Source, proj *Project) (m *Memory, added bool, err error) {
|
||
t := now()
|
||
m = &Memory{Kind: Fact, Scope: Global, Origin: origin, Status: Active, Created: t, Updated: t}
|
||
if origin == FromUser {
|
||
m.Edited = t
|
||
}
|
||
apply(m, in)
|
||
if err := check(m); err != nil {
|
||
return nil, false, err
|
||
}
|
||
if src != nil {
|
||
s := *src
|
||
if s.At.IsZero() {
|
||
s.At = t
|
||
}
|
||
m.Sources = []Source{s}
|
||
}
|
||
err = change(func(f *file) error {
|
||
if origin == FromInferred {
|
||
n := norm(m.Text)
|
||
if slices.Contains(f.Dismissed, n) {
|
||
m = nil
|
||
return errSkip
|
||
}
|
||
for _, o := range f.Memories {
|
||
if o.Scope == m.Scope && (o.Status == Archived || o.Status == Superseded) && norm(o.Text) == n {
|
||
m = o
|
||
return errSkip
|
||
}
|
||
}
|
||
}
|
||
if proj != nil && proj.Key != "" && m.Scope == "project:"+proj.Key {
|
||
notePro(f, proj)
|
||
}
|
||
for _, o := range f.Memories {
|
||
if o.Scope != m.Scope || o.Status == Archived || o.Status == Superseded || norm(o.Text) != norm(m.Text) {
|
||
continue
|
||
}
|
||
for _, tg := range m.Tags {
|
||
if !slices.Contains(o.Tags, tg) {
|
||
o.Tags = append(o.Tags, tg)
|
||
}
|
||
}
|
||
if !o.Pinned && o.Origin != FromUser {
|
||
o.Kind = m.Kind
|
||
if m.Why != "" {
|
||
o.Why = m.Why
|
||
}
|
||
}
|
||
if o.Status == Candidate && m.Status == Active {
|
||
o.Status = Active
|
||
}
|
||
o.Sources = keepSources(append(o.Sources, m.Sources...))
|
||
o.Updated = t
|
||
if origin == FromUser {
|
||
o.Edited = t
|
||
}
|
||
m = o
|
||
return nil
|
||
}
|
||
m.ID = newID()
|
||
f.Memories = append(f.Memories, m)
|
||
added = true
|
||
return nil
|
||
})
|
||
if errors.Is(err, errSkip) {
|
||
return m, false, nil
|
||
}
|
||
if err != nil {
|
||
return nil, false, err
|
||
}
|
||
return m, added, nil
|
||
}
|
||
|
||
// errSkip ends a change without writing it.
|
||
var errSkip = errors.New("nothing to change")
|
||
|
||
// keepSources keeps a memory's last 20 sources.
|
||
func keepSources(s []Source) []Source {
|
||
if len(s) > 20 {
|
||
s = s[len(s)-20:]
|
||
}
|
||
return s
|
||
}
|
||
|
||
func notePro(f *file, p *Project) {
|
||
if f.Projects == nil {
|
||
f.Projects = map[string]*Project{}
|
||
}
|
||
have := f.Projects[p.Key]
|
||
if have == nil {
|
||
have = &Project{Key: p.Key, Name: p.Name}
|
||
f.Projects[p.Key] = have
|
||
}
|
||
if have.Name == "" {
|
||
have.Name = p.Name
|
||
}
|
||
for _, r := range p.Roots {
|
||
if r != "" && !slices.Contains(have.Roots, r) {
|
||
have.Roots = append(have.Roots, r)
|
||
}
|
||
}
|
||
if len(have.Roots) > 10 {
|
||
have.Roots = have.Roots[len(have.Roots)-10:]
|
||
}
|
||
}
|
||
|
||
// NoteProject records a project memories may be kept for, for its name to
|
||
// be shown and its folders known.
|
||
func NoteProject(p *Project) error {
|
||
if p == nil || p.Key == "" {
|
||
return nil
|
||
}
|
||
return change(func(f *file) error { notePro(f, p); return nil })
|
||
}
|
||
|
||
// Edit changes the memory with id. An edit by the user (byUser) makes it
|
||
// the user's: what is distilled or saved later never changes it.
|
||
func Edit(id string, in Input, byUser bool) (*Memory, error) {
|
||
var out *Memory
|
||
err := change(func(f *file) error {
|
||
m := find(f, id)
|
||
if m == nil {
|
||
return ErrNotFound
|
||
}
|
||
out = m
|
||
return editIn(f, m, in, byUser)
|
||
})
|
||
return out, err
|
||
}
|
||
|
||
// editIn applies in to m, held in f, as Edit does.
|
||
func editIn(f *file, m *Memory, in Input, byUser bool) error {
|
||
c := *m
|
||
apply(&c, in)
|
||
if err := check(&c); err != nil {
|
||
return err
|
||
}
|
||
if byUser && c.Status == Archived && m.Status != Archived {
|
||
dismiss(f, m)
|
||
}
|
||
if byUser && (in.Text != nil || in.Kind != nil || in.Scope != nil || in.Why != nil) {
|
||
c.Origin = FromUser
|
||
}
|
||
c.Updated = now()
|
||
if byUser {
|
||
c.Edited = c.Updated
|
||
}
|
||
*m = c
|
||
return nil
|
||
}
|
||
|
||
// SetStatus gives the user's status to the memories with ids, in one
|
||
// write: those to review kept at once, or archived or brought back, as
|
||
// Edit would one by one (RealZeroP27 on X). An id no longer there is
|
||
// passed over; n is how many were changed, and none found is ErrNotFound.
|
||
// A status that isn't one changes nothing.
|
||
func SetStatus(status string, ids ...string) (n int, err error) {
|
||
err = change(func(f *file) error {
|
||
for _, m := range f.Memories {
|
||
if !slices.Contains(ids, m.ID) {
|
||
continue
|
||
}
|
||
if err := editIn(f, m, Input{Status: &status}, true); err != nil {
|
||
return err
|
||
}
|
||
n++
|
||
}
|
||
if n == 0 {
|
||
return ErrNotFound
|
||
}
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
n = 0
|
||
}
|
||
return n, err
|
||
}
|
||
|
||
// Forget sets the memory with id aside (archived): out of searches and
|
||
// digests, kept to be brought back.
|
||
func Forget(id string) (*Memory, error) {
|
||
s := Archived
|
||
return Edit(id, Input{Status: &s}, false)
|
||
}
|
||
|
||
// Remove deletes the memories with ids for good.
|
||
func Remove(ids ...string) error {
|
||
return change(func(f *file) error {
|
||
n := len(f.Memories)
|
||
f.Memories = slices.DeleteFunc(f.Memories, func(m *Memory) bool {
|
||
if slices.Contains(ids, m.ID) {
|
||
dismiss(f, m)
|
||
removed(f, m.ID)
|
||
return true
|
||
}
|
||
return false
|
||
})
|
||
if len(f.Memories) == n {
|
||
return ErrNotFound
|
||
}
|
||
return nil
|
||
})
|
||
}
|
||
|
||
// touch notes that a search returned the memories with ids, at most once
|
||
// a minute each so a busy agent doesn't rewrite the file on every search.
|
||
func touch(ids []string) {
|
||
if len(ids) == 0 {
|
||
return
|
||
}
|
||
t := now()
|
||
f, err := view()
|
||
if err != nil {
|
||
return
|
||
}
|
||
stale := false
|
||
for _, m := range f.Memories {
|
||
if slices.Contains(ids, m.ID) && t.Sub(m.Used) > time.Minute {
|
||
stale = true
|
||
break
|
||
}
|
||
}
|
||
if !stale {
|
||
return
|
||
}
|
||
change(func(f *file) error {
|
||
for _, m := range f.Memories {
|
||
if slices.Contains(ids, m.ID) && t.Sub(m.Used) > time.Minute {
|
||
m.Used = t
|
||
m.Uses++
|
||
}
|
||
}
|
||
return nil
|
||
})
|
||
}
|