The first part of the memory layer: internal/memory keeps memories in memory.json (lastgood + atomic writes under a file lock; a file that can't be read is unknown and never written over). Each memory has a scope (global, project:<git remote key>, agent:<id>), a kind (profile, preference, rule, reference, fact, procedure, episode), free tags, an origin and a status. Saving the same text in the same scope merges into the memory already there; an agent's save never changes the kind or why of a memory the user wrote or pinned. A project is keyed by its origin remote, so every clone and worktree of a repository shares its memories. Search is word-matched with CJK split into characters and pairs. The digest agents will be given is published on demand (a user's change, or once a day), never at an agent's save, so instructions don't change mid-session; its text names no app or vendor. Nothing uses the package yet: the MCP server, the Library's digest and the GUI come next. Run (on mote): go test -tags nogui ./internal/memory, -race -count=3, TZ=Asia/Shanghai; go vet for darwin, linux, windows; gofmt -l clean. Broken by hand: an agent's save allowed to change a user's memory (TestUsersMemoryNotChangedByAnAgentsSave red) and a digest computed live instead of published (TestDigestChangesOnlyWhenPublished red).
73 lines
2.6 KiB
Go
73 lines
2.6 KiB
Go
package memory
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestRemoteKey(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"https://github.com/yetone/magpie.git": "github.com/yetone/magpie",
|
|
"https://GitHub.com/yetone/magpie/": "github.com/yetone/magpie",
|
|
"git@github.com:yetone/magpie.git": "github.com/yetone/magpie",
|
|
"ssh://git@github.com:22/yetone/magpie": "github.com/yetone/magpie",
|
|
"https://user:tok@gitlab.example.com/g/sub/r": "gitlab.example.com/g/sub/r",
|
|
"file:///srv/repos/x.git": "",
|
|
"/srv/repos/x.git": "",
|
|
`C:\repos\x`: "",
|
|
"": "",
|
|
} {
|
|
if got := RemoteKey(in); got != want {
|
|
t.Errorf("RemoteKey(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func put(t *testing.T, p, s string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// A clone, its worktree and a folder deep in either are one project.
|
|
func TestProjectOfIsTheRepository(t *testing.T) {
|
|
d := t.TempDir()
|
|
main := filepath.Join(d, "magpie")
|
|
put(t, filepath.Join(main, ".git", "config"), "[core]\n\tbare = false\n[remote \"upstream\"]\n\turl = git@github.com:other/fork.git\n[remote \"origin\"]\n\turl = https://github.com/yetone/magpie.git\n")
|
|
wt := filepath.Join(d, "wt", "memory")
|
|
put(t, filepath.Join(wt, ".git"), "gitdir: "+filepath.Join(main, ".git", "worktrees", "memory")+"\n")
|
|
put(t, filepath.Join(main, ".git", "worktrees", "memory", "commondir"), "../..\n")
|
|
deep := filepath.Join(wt, "internal", "memory")
|
|
if err := os.MkdirAll(deep, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, dir := range []string{main, deep} {
|
|
p := ProjectOf(dir)
|
|
if p == nil || p.Key != "github.com/yetone/magpie" || p.Name != "magpie" {
|
|
t.Fatalf("ProjectOf(%s) = %+v", dir, p)
|
|
}
|
|
}
|
|
if p := ProjectOf(deep); p.Roots[0] != wt {
|
|
t.Fatalf("the worktree's root is %v", p.Roots)
|
|
}
|
|
|
|
// no remote: the main checkout's folder, from its worktree too
|
|
put(t, filepath.Join(main, ".git", "config"), "[core]\n\tbare = false\n")
|
|
if p := ProjectOf(deep); p.Key != "dir:"+filepath.ToSlash(main) {
|
|
t.Fatalf("with no remote: %+v", p)
|
|
}
|
|
// outside a repository: the folder itself
|
|
plain := filepath.Join(d, "notes")
|
|
if p := ProjectOf(plain); p.Key != "dir:"+filepath.ToSlash(plain) || ScopeOf(p) != "project:dir:"+filepath.ToSlash(plain) {
|
|
t.Fatalf("a plain folder: %+v", p)
|
|
}
|
|
if ProjectOf("") != nil || ProjectOf("relative/x") != nil || ScopeOf(nil) != "" {
|
|
t.Fatal("no folder made a project")
|
|
}
|
|
}
|