Files
magpie/internal/library/agent_skills_test.go
T
yetone cefdbfd4e3 library: Library → Instructions lists Qwen Code and writes its ~/.qwen/QWEN.md (wongxy on Discord)
What: Qwen Code is a Library instructions target. magpie's block goes into
QWEN.md in Qwen's global folder ($QWEN_HOME, else ~/.qwen), after what the
user wrote there. When the user's settings.json sets context.fileName (a
name or a list, JSONC), the first non-empty name, trimmed, is used instead,
which is the file Qwen Code writes itself. Qwen joins ownFolder, so one in
a running WSL distro gets the same place under the distro's home.

Why: wongxy found that Qwen Code has a user-level instructions file but the
Library didn't list Qwen Code. Qwen Code had no Library target at all.
Source: QwenLM/qwen-code c5098a27: Storage.getGlobalQwenDir
(config/storage.ts, QWEN_HOME), getMemoryFilePaths (memory/
memoryDiscovery.ts, reads <global dir>/<each memory filename>),
memory-constants.ts (QWEN.md then AGENTS.md by default;
getCurrentMemoryFilename skips empty entries), packages/cli
config.ts (settings.context.fileName → setMemoryFilename).

Run:
- TestQwenInstructions (new): default ~/.qwen/QWEN.md, the user's text
  kept, settings.json untouched, Takes("qwen-code"); QWEN_HOME with a JSONC
  context.fileName list whose first entry is blank; a single name.
  Broken by hand three ways, each FAIL: qwen case removed (no target),
  context.fileName ignored (got qwen-home/QWEN.md), no trim/skip of empty
  names (got the folder itself).
- Real Qwen Code 0.21.9 (Homebrew) in a sandbox HOME against a fake OpenAI
  endpoint: `qwen -p hi` sent the model the magpie block from
  ~/.qwen/QWEN.md; with QWEN_HOME and context.fileName ["AGENTS.md"] it
  sent the block from AGENTS.md and not the QWEN.md beside it. The files
  were written in the format the test checks, not by a built magpie.
- go test -tags nogui ./internal/library, gofmt -l, go vet (darwin),
  GOOS=linux go vet -tags nogui, GOOS=windows go vet on ./internal/library,
  after rebasing on origin/main.
Not done: Qwen Code's MCP servers and skills are still not given by the
Library. No GUI strings changed (the page draws rows from the target data).
2026-10-13 14:14:41 +08:00

239 lines
9.5 KiB
Go

package library
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// Every agent on the Agents page that reads skills has a place for them in
// the library (Kimi Code had none, so a skill never reached it): each its
// own folder, but Kimi Code, whose own would hide Claude Code's from it,
// the shared ~/.agents/skills.
func TestAgentsSkillsFolders(t *testing.T) {
h := sandbox(t)
for _, f := range []string{".kimi/config.toml", ".commandcode/settings.json", ".factory/settings.json", ".cline/data/globalState.json",
".hermes/config.yaml", ".grok/config.toml", ".qoder/settings.json", ".workbuddy/models.json", ".fx/settings.json", ".config/devin/config.json"} {
write(t, filepath.Join(h, f), "")
}
want := map[string]string{
"kimi": ".agents/skills",
"goose": ".agents/skills",
"commandcode": ".commandcode/skills",
"droid": ".factory/skills",
"cline": ".cline/skills",
"hermes": ".hermes/skills",
"grok": ".grok/skills",
"qoder": ".qoder/skills",
"workbuddy": ".workbuddy/skills",
"fx": ".fx/skills",
"devin": ".config/devin/skills",
}
for id, d := range want {
tg := targetByID(id)
if tg == nil || tg.Skills != filepath.Join(h, d) {
t.Errorf("%s: %+v", id, tg)
}
}
// what Kimi Code reads first of the shared ones, when it's there
os.MkdirAll(filepath.Join(h, ".config/agents/skills"), 0o755)
if tg := targetByID("kimi"); tg == nil || tg.Skills != filepath.Join(h, ".config/agents/skills") {
t.Errorf("kimi with ~/.config/agents/skills: %+v", tg)
}
// Alma, there once it has its data folder, reads ~/.config/alma/skills
// on every system, and Claude Code's, Codex's and the shared ones (#824).
// Its data folder on Windows is under %APPDATA%, which sandbox clears:
// without one there is no Alma there. Goose's config moves there with
// it, so Goose is there too
app := filepath.Join(h, "AppData", "Roaming")
t.Setenv("APPDATA", app)
write(t, filepath.Join(app, "Block", "goose", "config", "config.yaml"), "")
cfg, err := os.UserConfigDir()
if err != nil {
t.Fatal(err)
}
os.MkdirAll(filepath.Join(cfg, "alma"), 0o755)
if tg := targetByID("alma"); tg == nil || tg.Skills != filepath.Join(h, ".config/alma/skills") ||
!slices.Contains(tg.SkillsAlso, "claude") || !slices.Contains(tg.SkillsAlso, "codex") || !slices.Contains(tg.SkillsAlso, "goose") {
t.Errorf("alma: %+v", tg)
}
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"alma"}))
if !ours(filepath.Join(h, ".config/alma/skills/pdf"), "pdf") {
t.Error("a skill given to Alma isn't in ~/.config/alma/skills")
}
if ProjectSkillsDir("kimi") != ".agents/skills" {
t.Error("kimi reads a project's .agents/skills")
}
}
// A skill given to Kimi Code is one link in ~/.agents/skills, which Goose
// shares; Codex and Gemini CLI, which read that folder as well, get no
// second link in their own, and get one again once it's not there.
func TestSharedSkillsWrittenOnce(t *testing.T) {
h := sandbox(t)
write(t, filepath.Join(h, ".kimi/config.toml"), "")
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
shared := filepath.Join(h, ".agents/skills/pdf")
cx, cl := filepath.Join(h, ".codex/skills/pdf"), filepath.Join(h, ".claude/skills/pdf")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude", "codex", "kimi"}))
if !ours(shared, "pdf") {
t.Error("~/.agents/skills/pdf isn't the library's")
}
if !ours(cl, "pdf") {
t.Error("claude hasn't it")
}
if _, err := os.Lstat(cx); !os.IsNotExist(err) {
t.Error("codex has a second link to it")
}
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
also := map[string][]string{}
for _, a := range v.Agents {
also[a.ID] = a.SkillsAlso
}
for id, w := range map[string]string{"codex": "kimi", "gemini": "goose", "kimi": "goose", "goose": "kimi"} {
if !slices.Contains(also[id], w) {
t.Errorf("%s reads %s's skills too: %v", id, w, also[id])
}
}
if slices.Contains(also["claude"], "kimi") {
t.Errorf("claude reads no ~/.agents/skills: %v", also["claude"])
}
// Kimi Code (and Goose) off: Codex gets its own again, the shared goes
ok(t)(SkillAgents("pdf", []string{"claude", "codex"}))
if !ours(cx, "pdf") {
t.Error("codex hasn't it")
}
if _, err := os.Lstat(shared); !os.IsNotExist(err) {
t.Error("~/.agents/skills still has it")
}
// on again: codex's own goes, not the skill
ok(t)(SkillAgents("pdf", []string{"claude", "codex", "goose"}))
if _, err := os.Lstat(cx); !os.IsNotExist(err) || !ours(shared, "pdf") {
t.Error("not once in ~/.agents/skills")
}
if _, err := os.Stat(filepath.Join(src, "pdf/SKILL.md")); err != nil {
t.Error("the user's folder went")
}
}
// A skill of the user's own in ~/.agents/skills brought into the library
// stays there as it is, and Kimi Code, whose folder it is, has it without
// a word of its "own skill by that name".
func TestImportSharedSkillForKimi(t *testing.T) {
h := sandbox(t)
os.RemoveAll(filepath.Join(h, ".config/goose"))
write(t, filepath.Join(h, ".kimi/config.toml"), "")
shared := filepath.Join(h, ".agents/skills/orca")
skill(t, shared, "orca", "Orca")
ok(t)(ImportSkill("orca"))
if fi, err := os.Lstat(shared); err != nil || !fi.IsDir() {
t.Errorf("the shared folder's skill: %v", err)
}
v, _ := Read(nil)
if len(v.Skills) != 1 || !slices.Equal(v.Skills[0].Agents, []string{"kimi"}) || len(v.Skills[0].Problems) != 0 {
t.Errorf("skills: %+v", v.Skills)
}
ok(t)(Sync())
}
// Droid and Grok Build read a user-wide AGENTS.md in their folders
// (~/.factory, $GROK_HOME else ~/.grok): the library's instructions reach
// them there, after what the user wrote, which stays.
func TestDroidGrokInstructions(t *testing.T) {
h := sandbox(t)
write(t, filepath.Join(h, ".factory/settings.json"), "")
g := filepath.Join(h, "grok-home")
t.Setenv("GROK_HOME", g)
write(t, filepath.Join(g, "config.toml"), "")
files := map[string]string{"droid": filepath.Join(h, ".factory/AGENTS.md"), "grok": filepath.Join(g, "AGENTS.md")}
for id, f := range files {
if tg := targetByID(id); tg == nil || tg.Instructions != f {
t.Fatalf("%s: %+v", id, tg)
}
write(t, f, "# Mine\n")
}
shared := "Use tabs."
ok(t)(SaveInstructions(InstructionsChange{Shared: &shared, Agents: []string{"droid", "grok"}}))
for id, f := range files {
if s := read(t, f); s != "# Mine\n\n"+blockBegin+"\nUse tabs.\n"+blockEnd+"\n" {
t.Errorf("%s's AGENTS.md:\n%s", id, s)
}
}
}
// Qwen Code reads QWEN.md in its folder ($QWEN_HOME, else ~/.qwen) before
// every conversation (wongxy on Discord): the library's instructions reach
// it there, after what the user wrote, which stays; its settings'
// context.fileName, a JSONC list whose first non-empty name Qwen Code
// writes itself, names another file.
func TestQwenInstructions(t *testing.T) {
h := sandbox(t)
dir := filepath.Join(h, ".qwen")
write(t, filepath.Join(dir, "settings.json"), "{\n \"model\": {\"name\": \"qwen3.8-max\"}\n}\n")
inst := filepath.Join(dir, "QWEN.md")
write(t, inst, "# Mine\n")
if tg := targetByID("qwen"); tg == nil || tg.Instructions != inst {
t.Fatalf("Qwen Code's instructions: %+v", tg)
}
if id, err := Takes("qwen-code", "instructions"); err != nil || id != "qwen" {
t.Fatalf("Takes(qwen-code): %q %v", id, err)
}
shared := "Use tabs."
ok(t)(SaveInstructions(InstructionsChange{Shared: &shared, Agents: []string{"qwen"}}))
if s := read(t, inst); s != "# Mine\n\n"+blockBegin+"\nUse tabs.\n"+blockEnd+"\n" {
t.Fatalf("QWEN.md:\n%s", s)
}
if cfg := read(t, filepath.Join(dir, "settings.json")); !strings.Contains(cfg, "qwen3.8-max") {
t.Fatalf("instructions rewrote the settings:\n%s", cfg)
}
// $QWEN_HOME moves the folder, and context.fileName renames the file
q := filepath.Join(h, "qwen-home")
t.Setenv("QWEN_HOME", q)
write(t, filepath.Join(q, "settings.json"), "{\n // mine\n \"context\": {\"fileName\": [\" \", \" AGENTS.md \", \"QWEN.md\"]},\n}\n")
if tg := targetByID("qwen"); tg == nil || tg.Instructions != filepath.Join(q, "AGENTS.md") {
t.Fatalf("Qwen Code under QWEN_HOME with context.fileName: %+v", tg)
}
write(t, filepath.Join(q, "settings.json"), "{\"context\": {\"fileName\": \"MY.md\"}}")
if tg := targetByID("qwen"); tg == nil || tg.Instructions != filepath.Join(q, "MY.md") {
t.Fatalf("context.fileName as one name: %+v", tg)
}
}
// Grok App's independent home (Use on a custom provider) reads AGENTS.md
// and skills beside agent-home/config.toml, not Grok Build's ~/.grok.
func TestGrokAppInstructions(t *testing.T) {
h := sandbox(t)
root := filepath.Join(h, "grok-app-data")
t.Setenv("GROK_APP_HOME", root)
home := filepath.Join(root, "agent-home")
write(t, filepath.Join(home, "config.toml"), "[models]\ndefault = \"deepseek\"\n")
inst := filepath.Join(home, "AGENTS.md")
write(t, inst, "# Mine\n")
tg := targetByID("grok-app")
if tg == nil || tg.Instructions != inst || tg.Skills != filepath.Join(home, "skills") || tg.MCP == nil || tg.MCP.Path != filepath.Join(home, "config.toml") {
t.Fatalf("Grok App library places: %+v", tg)
}
if !slices.Contains(readsShared, "grok-app") {
t.Fatal("Grok App doesn't read ~/.agents/skills")
}
shared := "Use tabs."
ok(t)(SaveInstructions(InstructionsChange{Shared: &shared, Agents: []string{"grok-app"}}))
if s := read(t, inst); s != "# Mine\n\n"+blockBegin+"\nUse tabs.\n"+blockEnd+"\n" {
t.Fatalf("AGENTS.md:\n%s", s)
}
if cfg := read(t, filepath.Join(home, "config.toml")); !strings.Contains(cfg, `default = "deepseek"`) {
t.Fatalf("instructions rewrote the route:\n%s", cfg)
}
}