memory_search printed a memory's Updated as a bare UTC date, so a memory
saved at 04:30 on the 11th in Shanghai read as the 10th, and the stored
times were written in UTC. Now:
- every memory time is written in the local zone (now() instead of
now().UTC()): created, updated, edited, used, sync tombstones, digest
publish time;
- memory_search shows 2026-11-11T04:30+08:00 (memoryStamp);
- the dream's notes list the creation day in local time, as "Today" there
already was.
Old data: the file already held full timestamps, only in UTC ("...Z").
They parse as the same instants and are shown in local time, so no
migration; they are rewritten with a local offset when next changed.
There is no date filter in memory_search, and the Memory tab shows no
dates, so nothing else read the UTC day.
Tests: TestMemorySearchDatesInLocalTime (gateway) with time.Local at +08:00
reads an old "Z" memory and checks a new save is stored with +08:00. Red on
the old code both halves, broken by hand: the DateOnly line printed
2026-11-10, and now().UTC() in memory.go failed the saved-zone check.
Run with TZ=UTC and TZ=Asia/Shanghai on mote: internal/memory,
internal/memdistill, internal/gui (memory tests) ok; gofmt clean;
go vet ./..., linux nogui, windows ok. Not run: full ./... suite before
the tag (started after it), GUI browser tests (no assets changed).
214 lines
5.7 KiB
Go
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.json 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")
|
|
}
|