Backups and sync. memory.json goes in every backup and in the WebDAV, S3 and GitHub sync, under its own toggle (on unless turned off, as the library is). A sync merges memories one by one (memory.Combine): the newer change wins, a memory the user edited or pinned is never taken over by one they didn't, an archived one is kept 30 days and the suggestions turned down are joined. Settings > Sync has a Memory tick, and the description lists memory. Promotion. Library > Memory (In use, no search) shows what memory suggests: a project memory that says the same in two or more projects (the same text, or 60% of its words with 6 or more on each side), or whose sources came from two or more, is offered as global; a procedure searches returned 5 or more times is offered as a skill. Taking a global one keeps the user's own or pinned memory first, then the most used, makes it global and supersedes the rest (into a global with the same text when there is one). Making a skill writes a library skill from its text and why, named from its first words (procedure-<id> for one in Chinese), for the agents ticked for instructions, and archives the memory. A suggestion declined isn't made again, and that goes with sync too. Routes: POST /api/memory/promote, /skill, /decline. Docs: docs/subsystems/memory.md (Backups and sync, Promotion), docs/subsystems/sync-backup.md. Pushed on origin/main14be7f69d, whose 5 commits since8b2fb6b08share no file with this one (not run again). At8b2fb6b08: i18n-keys, library-memory-suggest and webdav-settings-refresh on WSL in both engines, and -run "Memory|Skill|Market|ThirdParty|Promote|Suggest|Sync|Combine" of library, memory and backup on Windows: ok. One rebase before, at origin/main69453574d: go vet of gui, memory, library and -run "Memory|Settings|Parity|Budget|Skill|Promote| Suggest" of gui and memory on Windows: ok; i18n-keys, library-memory*, the five sync files below and sync-own-fields, library-backups-top on WSL in both engines: ok but webkit s3-sync (below). Before the rebase, Go code the same as at the run on the Windows box (xiaomi-gpu, win-run; mote's disk is full, every run there stalled): - go vet ./... (windows) and GOOS=linux go vet -tags nogui ./...: ok. gofmt -l: clean. - go test -count=1 ./internal/memory ./internal/backup ./internal/davsync ./internal/memdistill ./internal/memembed: ok. - ./internal/library: ok but three tests red on Windows that are red the same on origin/maind936be763run alone there and are filed: TestPluginImportCarryOnOffRemove (#6065), TestGoneAgentListedToTakeOut and TestCheckMixedServerEachSide (#4115). -run "Memory|Skill": ok. - ./internal/gui ./internal/cli -run "Memory|Settings|Parity|Budget|Distill|Dream|Skill|Promote|Suggest": ok. GUI on the WSL box (magpie-desktop), chromium and webkit: - library-memory-suggest (new), library-memory, library-memory-select, i18n-keys, sync-own-fields, sync-not-backup, auto-backup, library-backups-top, library-bar-more, login-export, login-import: ok. - every sync-*.test.cjs, github-sync, s3-sync, usage-computers, webdav-settings-refresh, refresh-page, old-webkit: ok but old-webkit's two reds, the same on main and filed (#6030, #5919), and webkit s3-sync's "picking S3 must not scroll the page" (the page is at 0 after the click), red the same on origin/main1ccacad632 of 2 at --test-concurrency=2 one step later ("picking S3 again", #3413). github-sync, s3-sync, sync-signins, sync-other-kind and webdav-settings-refresh were rerun after their expected bodies and descriptions took memory: ok in both engines. Broken by hand: Suggest without the Declined filter turned TestSuggestSkillAndDecline red, and keptFirst without the Fixed order turned TestPromoteKeepsTheUsersOwn red; restored. Not tried: a real sync between two computers with real memory, and the GUI on macOS (mote's disk is full).
205 lines
5.7 KiB
Go
205 lines
5.7 KiB
Go
package memory
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Tidying is memory's nightly pass (memdistill's Dream): memories that
|
|
// say the same thing are merged into one, ones a newer memory replaced
|
|
// or whose time has passed are set aside, and candidates nobody reviewed
|
|
// for long are set aside too. What the user wrote and what is pinned is
|
|
// never changed: it can be the one others are merged into, nothing more.
|
|
// Every change is recorded so the whole pass can be undone (Untidy).
|
|
|
|
// Merge is memories to merge: IDs into one that says Text.
|
|
type Merge struct {
|
|
IDs []string
|
|
Text string
|
|
Kind string
|
|
}
|
|
|
|
// Stale is a memory to set aside, and why.
|
|
type Stale struct {
|
|
ID string
|
|
Why string
|
|
}
|
|
|
|
// Plan is what a tidy does: the merges, the memories to set aside, and
|
|
// Before, when an unreviewed candidate distilled from a conversation is
|
|
// set aside if it hasn't changed since (zero: none).
|
|
type Plan struct {
|
|
Merges []Merge
|
|
Stale []Stale
|
|
Before time.Time
|
|
}
|
|
|
|
// What a Change did.
|
|
const (
|
|
Merged = "merge" // IDs were superseded by Into
|
|
SetAside = "aside" // IDs were archived
|
|
Unreviewed = "unreviewed" // Why of a candidate set aside for its age
|
|
)
|
|
|
|
// Change is one thing a tidy did, with what it takes to undo it.
|
|
type Change struct {
|
|
Action string `json:"action"`
|
|
// Into is the memory merged into; New when the merge made it
|
|
Into string `json:"into,omitempty"`
|
|
New bool `json:"new,omitempty"`
|
|
// IDs are the memories superseded or set aside, Was their states
|
|
// before
|
|
IDs []string `json:"ids"`
|
|
Was []string `json:"was"`
|
|
// Text is what Into says, or what was set aside
|
|
Text string `json:"text"`
|
|
Why string `json:"why,omitempty"`
|
|
}
|
|
|
|
// Fixed is a memory a tidy never changes: the user's own, or pinned.
|
|
func Fixed(m *Memory) bool { return m.Pinned || m.Origin == FromUser }
|
|
|
|
func live(m *Memory) bool { return m != nil && (m.Status == Active || m.Status == Candidate) }
|
|
|
|
// Tidy does p, as far as it still holds: a memory gone, no longer in use
|
|
// or the user's own is left alone, and a merge across scopes or of two
|
|
// of the user's own isn't done. It returns what it did.
|
|
func Tidy(p Plan) ([]Change, error) {
|
|
var out []Change
|
|
err := change(func(f *file) error {
|
|
t := now().UTC()
|
|
for _, mg := range p.Merges {
|
|
var ms []*Memory
|
|
for _, id := range mg.IDs {
|
|
m := find(f, id)
|
|
if !live(m) || slices.Contains(ms, m) {
|
|
continue
|
|
}
|
|
ms = append(ms, m)
|
|
}
|
|
if len(ms) < 2 || slices.ContainsFunc(ms, func(m *Memory) bool { return m.Scope != ms[0].Scope }) {
|
|
continue
|
|
}
|
|
var fixed []*Memory
|
|
for _, m := range ms {
|
|
if Fixed(m) {
|
|
fixed = append(fixed, m)
|
|
}
|
|
}
|
|
if len(fixed) > 1 {
|
|
continue
|
|
}
|
|
var into *Memory
|
|
c := Change{Action: Merged}
|
|
if len(fixed) == 1 {
|
|
into = fixed[0]
|
|
} else {
|
|
text := strings.TrimSpace(mg.Text)
|
|
if text == "" || len(text) > MaxText {
|
|
continue
|
|
}
|
|
kind := mg.Kind
|
|
if !slices.Contains(Kinds, kind) {
|
|
kind = ms[0].Kind
|
|
}
|
|
into = &Memory{ID: newID(), Text: text, Kind: kind, Scope: ms[0].Scope, Origin: FromInferred, Status: Candidate, Created: ms[0].Created, Updated: t}
|
|
for _, m := range ms {
|
|
if m.Status == Active {
|
|
into.Status = Active
|
|
}
|
|
if into.Why == "" {
|
|
into.Why = m.Why
|
|
}
|
|
for _, tg := range m.Tags {
|
|
if !slices.Contains(into.Tags, tg) {
|
|
into.Tags = append(into.Tags, tg)
|
|
}
|
|
}
|
|
into.Sources = append(into.Sources, m.Sources...)
|
|
if m.Created.Before(into.Created) {
|
|
into.Created = m.Created
|
|
}
|
|
if m.Used.After(into.Used) {
|
|
into.Used = m.Used
|
|
}
|
|
into.Uses += m.Uses
|
|
}
|
|
slices.SortStableFunc(into.Sources, func(a, b Source) int { return a.At.Compare(b.At) })
|
|
into.Sources = keepSources(into.Sources)
|
|
f.Memories = append(f.Memories, into)
|
|
c.New = true
|
|
}
|
|
c.Into, c.Text = into.ID, into.Text
|
|
for _, m := range ms {
|
|
if m == into {
|
|
continue
|
|
}
|
|
c.IDs, c.Was = append(c.IDs, m.ID), append(c.Was, m.Status)
|
|
m.Status, m.SupersededBy, m.Updated = Superseded, into.ID, t
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
aside := func(m *Memory, why string) {
|
|
out = append(out, Change{Action: SetAside, IDs: []string{m.ID}, Was: []string{m.Status}, Text: m.Text, Why: why})
|
|
m.Status, m.Updated = Archived, t
|
|
}
|
|
for _, s := range p.Stale {
|
|
if m := find(f, s.ID); live(m) && !Fixed(m) {
|
|
aside(m, strings.TrimSpace(s.Why))
|
|
}
|
|
}
|
|
if !p.Before.IsZero() {
|
|
for _, m := range f.Memories {
|
|
if m.Status == Candidate && m.Origin == FromInferred && !Fixed(m) && m.Updated.Before(p.Before) {
|
|
aside(m, Unreviewed)
|
|
}
|
|
}
|
|
}
|
|
if len(out) == 0 {
|
|
return errSkip
|
|
}
|
|
return nil
|
|
})
|
|
if err == errSkip {
|
|
err = nil
|
|
}
|
|
return out, err
|
|
}
|
|
|
|
// Untidy undoes cs: each memory a tidy superseded or set aside, and still
|
|
// left so, goes back to the state it had, and a memory a merge made is
|
|
// removed, unless the user has made it theirs since (it stays then, with
|
|
// the ones it was made from back beside it). It returns how many memories
|
|
// came back.
|
|
func Untidy(cs []Change) (int, error) {
|
|
n := 0
|
|
err := change(func(f *file) error {
|
|
t := now().UTC()
|
|
for i := len(cs) - 1; i >= 0; i-- {
|
|
c := cs[i]
|
|
for k, id := range c.IDs {
|
|
m := find(f, id)
|
|
if m == nil || k >= len(c.Was) {
|
|
continue
|
|
}
|
|
if c.Action == Merged && m.Status == Superseded && m.SupersededBy == c.Into || c.Action == SetAside && m.Status == Archived {
|
|
m.Status, m.SupersededBy, m.Updated = c.Was[k], "", t
|
|
n++
|
|
}
|
|
}
|
|
if c.New {
|
|
f.Memories = slices.DeleteFunc(f.Memories, func(m *Memory) bool {
|
|
if m.ID == c.Into && !Fixed(m) {
|
|
removed(f, m.ID)
|
|
return true
|
|
}
|
|
return false
|
|
})
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
return n, err
|
|
}
|