Files
magpie/internal/library/linked_set_test.go
T
yetone 692a779d3d library: an instruction set can be kept in a file of your own, such as ~/.agents/AGENTS.md (新v学员 on Discord)
新v学员 keeps one user-level ~/.agents/AGENTS.md by hand and wants every
agent to follow it. Only Factory Droid, omp and Cline CLI read that file
natively; Codex, Claude Code, Gemini, OpenCode, Pi, Amp and the rest
don't. Rather than teach magpie a new agent target, a set of shared
instructions can now be kept in that file instead of a copy in the
library, and the library writes it into every agent that is on, as it
does any set.

What changed:
- InstrSet.File: a set kept in a file of the user's own. Its text is
  read from that file, Save writes to it (kept aside in the backups
  first, written atomically, never removed: emptied, it stays empty),
  and Remove lets go of the set without touching the file.
- The file is refused when it is relative, a folder, an agent's own
  instructions or override file (magpie writes into those), a file
  holding magpie's marked block, inside the library, or another set's.
  A file not there yet is made on the first save.
- The GUI checks the active linked file every 30s (KeepLinked) and
  writes a change made in an editor into the agents.
- Carried by davsync or a backup, a linked set goes as its text; where
  the file is stays this computer's. Put back, the set keeps its file
  and the file gets the carried text, never emptied for a text not
  carried. A profile restored with no text doesn't empty it either.
- GUI: New set has "Keep in a file…" (filled with ~/.agents/AGENTS.md);
  a linked set is tagged "Your file", says where it is kept, and warns
  when Droid or omp is on, as they read ~/.agents/AGENTS.md themselves
  and would get it twice. Strings in zh, zh-TW, ja and de.

Run:
- gofmt -l; go vet for darwin, linux/amd64 -tags nogui and
  windows/amd64 on ./internal/library ./internal/gui.
- go test ./internal/library ./internal/gui -count=1: ok.
- New TestLinkedSet, TestLinkRefused, TestLinkedSetCarried. Each was
  broken by hand and went red: putText writing a linked file like a
  library one, Collect not stripping File, Put not restoring it,
  SyncLinked not syncing, linkable not checking agents' own files.
- library-linked-set.test.cjs in chromium and webkit, at 1100 and 420,
  in en, zh, zh-TW, ja, de: 22 pass; red with the file left out of
  create. library-instr-sets and i18n-keys still pass.

Not done or tried:
- No CLI to link a set (`magpie library instructions set <file>` still
  copies once), and the headless `magpie serve` doesn't watch the file:
  after an edit there, `magpie library sync` writes it out.
- Not tried with real Droid or omp reading the same file.
2026-10-13 17:28:26 +08:00

167 lines
6.4 KiB
Go

package library
import (
"os"
"path/filepath"
"strings"
"testing"
)
// A set can be kept in a file of the user's own, ~/.agents/AGENTS.md that
// Droid and omp read too, instead of a copy in the library: the agents get
// what the file says, a change made in it reaches them, the page's edits
// are written to it, and removing the set lets go of the file without
// touching it (新v学员 on Discord).
func TestLinkedSet(t *testing.T) {
h := sandbox(t)
cx := filepath.Join(h, ".codex/AGENTS.md")
shared := filepath.Join(h, ".agents/AGENTS.md")
write(t, shared, "Shared rules.\n")
ok(t)(SaveInstructions(InstructionsChange{Agents: []string{"codex"}}))
ok(t)(SaveInstructions(InstructionsChange{Create: &InstrSet{ID: "agents", Name: "Everyone", File: "~/.agents/AGENTS.md"}}))
if s := read(t, cx); !strings.Contains(s, "Shared rules.") {
t.Fatalf("codex didn't get the linked file's text (an empty set on gives way to a new one):\n%s", s)
}
iv, _ := ReadInstructions()
if len(iv.Sets) != 2 || iv.Sets[1].File != shared || iv.Sets[1].Text != "Shared rules." || !iv.Sets[1].Active {
t.Errorf("view: %+v", iv.Sets)
}
if _, err := os.Stat(setPath("agents")); !os.IsNotExist(err) {
t.Error("a linked set has a copy in the library too")
}
// edited outside magpie: the first look notes the file, the next one
// after a change writes it into the agents
if res, err := SyncLinked(); err != nil || res != nil {
t.Fatalf("first look: %v %v", res, err)
}
if res, err := SyncLinked(); err != nil || res != nil {
t.Fatalf("nothing changed, yet: %v %v", res, err)
}
write(t, shared, "Shared rules, edited by hand.\n")
ok(t)(SyncLinked())
if s := read(t, cx); !strings.Contains(s, "edited by hand") {
t.Errorf("a change in the file didn't reach codex:\n%s", s)
}
// the page's text goes into the file, the file kept aside first
page := "Written on the page."
res := ok(t)(SaveInstructions(InstructionsChange{Texts: map[string]*string{"agents": &page}}))
if s := read(t, shared); s != page+"\n" {
t.Errorf("the file has %q", s)
}
if res.Backup == "" || !strings.Contains(read(t, filepath.Join(res.Backup, "library", "AGENTS.md")), "edited by hand") {
t.Errorf("the file wasn't kept aside before it was written: %q", res.Backup)
}
if s := read(t, cx); !strings.Contains(s, page) {
t.Errorf("codex:\n%s", s)
}
// emptied, it stays, empty: it is the user's
empty := ""
ok(t)(SaveInstructions(InstructionsChange{Texts: map[string]*string{"agents": &empty}}))
if _, err := os.Stat(shared); err != nil {
t.Errorf("an emptied linked file was removed: %v", err)
}
write(t, shared, "Back again.\n")
// a profile taken with no text puts none back over the file
ok(t)(SaveInstructions(InstructionsChange{Activate: "default"}))
snap, _ := Snapshot()
ok(t)(SaveInstructions(InstructionsChange{Activate: "agents"}))
snap.Instructions.Set, snap.Instructions.Shared = "agents", ""
ok(t)(Restore(snap))
if _, err := os.Stat(shared); err != nil {
t.Errorf("a profile's empty text removed the linked file: %v", err)
}
ok(t)(SaveInstructions(InstructionsChange{Activate: "default"}))
ok(t)(SaveInstructions(InstructionsChange{Remove: "agents"}))
if _, err := os.Stat(shared); err != nil {
t.Errorf("removing the set removed the user's file: %v", err)
}
iv, _ = ReadInstructions()
if len(iv.Sets) != 1 {
t.Errorf("the set stayed: %+v", iv.Sets)
}
}
// A set can't be kept in a file magpie writes into, or in the library's
// own, or in a file another set has.
func TestLinkRefused(t *testing.T) {
h := sandbox(t)
write(t, filepath.Join(h, ".agents/AGENTS.md"), "Mine.\n")
ok(t)(SaveInstructions(InstructionsChange{Create: &InstrSet{ID: "a", Name: "A", File: filepath.Join(h, ".agents/AGENTS.md")}}))
marked := filepath.Join(h, "marked.md")
write(t, marked, "x\n"+blockBegin+"\ny\n"+blockEnd+"\n")
write(t, filepath.Join(h, ".codex/AGENTS.md"), "codex's own\n")
for name, file := range map[string]string{
"relative": "notes/AGENTS.md",
"a folder": filepath.Join(h, ".agents"),
"an agent's own": filepath.Join(h, ".codex/AGENTS.md"),
"the library's": filepath.Join(Dir(), "instructions.md"),
"magpie's part": marked,
"another set's": "~/.agents/AGENTS.md",
"another set's too": filepath.Join(h, ".agents/../.agents/AGENTS.md"),
} {
if _, err := SaveInstructions(InstructionsChange{Create: &InstrSet{ID: "b", Name: "B", File: file}}); err == nil {
t.Errorf("%s (%s) was linked", name, file)
}
}
// a file not there yet is made when the set is first saved
later := filepath.Join(h, "later/AGENTS.md")
text := "Later."
ok(t)(SaveInstructions(InstructionsChange{Create: &InstrSet{ID: "b", Name: "B", File: later}, Texts: map[string]*string{"b": &text}}))
if s := read(t, later); s != "Later.\n" {
t.Errorf("the new file has %q", s)
}
}
// Carried to another computer (davsync, a backup) a linked set goes as its
// text: where the file is stays this computer's. Put back here, the set
// keeps its file, which gets the text carried and is never emptied for a
// text not carried.
func TestLinkedSetCarried(t *testing.T) {
h := sandbox(t)
shared := filepath.Join(h, ".agents/AGENTS.md")
write(t, shared, "Here.\n")
ok(t)(SaveInstructions(InstructionsChange{Create: &InstrSet{ID: "agents", Name: "Everyone", File: shared}}))
b, err := Collect()
if err != nil || b == nil {
t.Fatal(b, err)
}
if b.Texts["agents"] != "Here." {
t.Errorf("texts: %v", b.Texts)
}
for _, s := range b.Instructions.Sets {
if s.File != "" {
t.Errorf("the file went with the set: %+v", s)
}
}
if l, _ := load(); l.instrSet("agents").File != shared {
t.Error("Collect took the file off the library's own set")
}
b.Texts["agents"] = "From the other computer."
ok(t)(Put(b))
if s := read(t, shared); s != "From the other computer.\n" {
t.Errorf("the file has %q", s)
}
iv, _ := ReadInstructions()
if iv.Sets[1].File != shared {
t.Errorf("Put let go of the file: %+v", iv.Sets)
}
if _, err := os.Stat(setPath("agents")); !os.IsNotExist(err) {
t.Error("Put wrote a copy into the library")
}
delete(b.Texts, "agents")
b.Instructions.Sets[len(b.Instructions.Sets)-1].File = filepath.Join(h, "elsewhere.md")
ok(t)(Put(b))
if s := read(t, shared); s != "From the other computer.\n" {
t.Errorf("a bundle without the text emptied the file: %q", s)
}
if l, _ := load(); l.instrSet("agents").File != shared {
t.Errorf("a carried file was taken: %q", l.instrSet("agents").File)
}
}