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).
This commit is contained in:
yetone
2026-10-13 14:14:41 +08:00
parent 250bdd5c4f
commit cefdbfd4e3
2 changed files with 71 additions and 1 deletions
+39
View File
@@ -171,6 +171,45 @@ func TestDroidGrokInstructions(t *testing.T) {
}
}
// 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) {
+32 -1
View File
@@ -7,6 +7,9 @@ import (
"slices"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/jsonc"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/agentplug"
"github.com/yetone/magpie/internal/appdir"
@@ -333,6 +336,15 @@ func targetOf(a *agent.Agent) *Target {
t.Instructions = filepath.Join(a.Dir, "ATOMCODE.md")
t.MCP = &mcpFile{Path: filepath.Join(a.Dir, "mcp.json"), Format: fmtOmp}
t.Skills = filepath.Join(a.Dir, "skills")
case "qwen":
// Qwen Code reads QWEN.md (and AGENTS.md) in its global folder,
// $QWEN_HOME else ~/.qwen, before every conversation; its settings'
// context.fileName renames them, the first name being the one it
// writes itself (getGlobalQwenDir in packages/core/src/config/
// storage.ts, getMemoryFilePaths in memory/memoryDiscovery.ts,
// getCurrentMemoryFilename in utils/memory-constants.ts,
// QwenLM/qwen-code c5098a27)
t.Instructions = filepath.Join(a.Dir, qwenContextFile(a.Path))
case "zed":
// Zed reads its MCP servers from context_servers in the settings.json
// magpie sets its model in (~/.config/zed, $XDG_CONFIG_HOME/zed,
@@ -366,6 +378,25 @@ func targetOf(a *agent.Agent) *Target {
return t
}
// qwenContextFile is the name of the file Qwen Code reads its user-wide
// instructions from: the first of its settings' context.fileName, a name
// or a list of them, else QWEN.md (packages/cli/src/config/config.ts sets
// it with setMemoryFilename, which trims each name and skips an empty one).
func qwenContextFile(settings string) string {
raw, _ := os.ReadFile(settings)
v := gjson.GetBytes(jsonc.ToJSON(raw), "context.fileName")
names := []gjson.Result{v}
if v.IsArray() {
names = v.Array()
}
for _, n := range names {
if name := strings.TrimSpace(n.String()); n.Type == gjson.String && name != "" {
return name
}
}
return "QWEN.md"
}
// pluginMCP is each MCP format an agent plugin may name
// (agentplug.MCPFormats), by name.
var pluginMCP = map[string]mcpFormat{
@@ -460,7 +491,7 @@ func wslTargetOf(a *agent.Agent) *Target {
var ownFolder = map[string]bool{
"hermes": true, "omp": true, "opencode": true, "mimocode": true, "droid": true,
"grok": true, "commandcode": true, "atomcode": true, "qoder": true, "qoder-cn": true,
"minimax-code": true, "fx": true, "dsh": true,
"minimax-code": true, "fx": true, "dsh": true, "qwen": true,
}
// wslOwnFolder is the target of a, an agent in a running WSL distro that