From cefdbfd4e3e8a8d9a0c77ffb56c8c417bb235600 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 13 Oct 2026 14:06:54 +0800 Subject: [PATCH] =?UTF-8?q?library:=20Library=20=E2=86=92=20Instructions?= =?UTF-8?q?=20lists=20Qwen=20Code=20and=20writes=20its=20~/.qwen/QWEN.md?= =?UTF-8?q?=20(wongxy=20on=20Discord)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 /), 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). --- internal/library/agent_skills_test.go | 39 +++++++++++++++++++++++++++ internal/library/targets.go | 33 ++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/internal/library/agent_skills_test.go b/internal/library/agent_skills_test.go index 6d6ce9ac9..14ab90d5f 100644 --- a/internal/library/agent_skills_test.go +++ b/internal/library/agent_skills_test.go @@ -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) { diff --git a/internal/library/targets.go b/internal/library/targets.go index e473056a6..b72a7cc6e 100644 --- a/internal/library/targets.go +++ b/internal/library/targets.go @@ -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