From 41972b25fce9769f6d13b9a1888218a62835fe33 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 13 Oct 2026 15:21:12 +0800 Subject: [PATCH] zcode: magpie's provider reaches a ZCode whose data was moved to another folder in its settings (flower on Discord) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit What changed: ZCode 3.14 has a 数据存储路径 (data storage path) setting. Changing it copies ~/.zcode/v2 to /.zcode/v2, and from the next start ZCode reads its providers (provider_config.json, config.json), credentials.json and runtime/provider from there. The choice stays in the home folder's .zcode/v2/setting.json as dataBaseDir, which ZCode's main process reads first (readBootstrapDataBaseDirFromDisk, then setDataBaseDir; getDataBaseDir is that, else ZCODE_DATA_BASE_DIR, else HOME). magpie always wrote ~/.zcode/v2, so for such a user it said ZCode was connected while ZCode's 模型设置 showed only BigModel and Start Plan and no magpie. appdir.ZCodeDataIn reads dataBaseDir the way ZCode does (trimmed, ignored when empty or relative); the ZCode agent target, the ZCode subscription's credentials.json and its zcode-builtin.json glob use it. Skills, AGENTS.md and the CLI's cli/config.json stay under the home folder: ZCode reads those from HOME/USERPROFILE (getUserZcodeSkillRoot, the MCP config path), so internal/library is unchanged. ZCode's session database (internal/sessions) is not changed here: where ZCode's CLI keeps it under a moved folder was not checked. Read from ZCode 3.14.3's app.asar (copied to the scratchpad): out/main/index.js (bootstrap, setDataBaseDir, buildHostProcessEnv passing ZCODE_DATA_BASE_DIR), out/host/chunk-*.js (getAppConfigDir = /.zcode/v2) and out/host/index.js (provider_config.json under it). flower hasn't said they moved the folder; this is the one way found in ZCode's code for "magpie says connected, ZCode has no magpie" with a provider_config.json magpie wrote. That file, and the one this build writes, decode with ZCode's own decodeProviderConfigFile (run from a copy of glm/zcode.cjs): providers ["magpie"]. What was run: - TestZCodeMovedDataFolder (new): with dataBaseDir set, Set("magpie") writes both files under the moved folder and none in ~/.zcode/v2; an empty, blank or relative dataBaseDir keeps ~/.zcode. On the old zcode.go it fails: "no magpie in the moved folder's provider_config.json". - Sandbox, built binary: HOME=, setting.json dataBaseDir=/D, `magpie zcode magpie` wrote D/.zcode/v2/{config,provider_config}.json and nothing under the home folder's .zcode/v2 but setting.json. - go test -tags nogui ./internal/agent ./internal/appdir ./internal/provider under a temp HOME; gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui on the three packages. - Not run: the ZCode app itself with a moved folder (not launched against any config), and Windows. --- internal/agent/zcode.go | 7 ++-- internal/agent/zcode_test.go | 55 +++++++++++++++++++++++++++++++ internal/appdir/zcode.go | 35 ++++++++++++++++++++ internal/provider/zcode.go | 4 ++- internal/provider/zcode_models.go | 3 +- 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 internal/appdir/zcode.go diff --git a/internal/agent/zcode.go b/internal/agent/zcode.go index 5778d8eed..6cd24a556 100644 --- a/internal/agent/zcode.go +++ b/internal/agent/zcode.go @@ -8,12 +8,14 @@ import ( "path/filepath" "slices" + "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/gateway" ) // ZCode (Zhipu's desktop app) keeps its model providers in -// ~/.zcode/v2/config.json, OpenCode's provider shape with a kind of its own: +// ~/.zcode/v2/config.json (under the folder the user moved its data to, when +// they did: appdir.ZCodeDataIn), OpenCode's provider shape with a kind of its own: // // {"provider":{"magpie":{"name":"magpie","kind":"anthropic", // "options":{"apiKey":"magpie","baseURL":"http://127.0.0.1:3425"}, @@ -53,7 +55,8 @@ import ( // Anthropic Messages. func zcode(home string) *Agent { - dir := filepath.Join(home, ".zcode") + // where ZCode keeps its data, moved there in its settings or not + dir := filepath.Join(appdir.ZCodeDataIn(home), ".zcode") path := filepath.Join(dir, "v2", "config.json") rules := filepath.Join(dir, "v2", "provider_config.json") key := "provider." + magpieID diff --git a/internal/agent/zcode_test.go b/internal/agent/zcode_test.go index 8c6e88430..f05b31cb2 100644 --- a/internal/agent/zcode_test.go +++ b/internal/agent/zcode_test.go @@ -491,3 +491,58 @@ func TestZCodeKeepsUserFormat(t *testing.T) { t.Fatalf("unknown format: %q %q", k, api) } } + +// A ZCode whose data the user moved in its settings (数据存储路径) reads its +// providers from the new folder: magpie's provider goes there, and the home +// folder's .zcode, where only setting.json is left, gets none (flower on +// Discord: magpie said connected, and ZCode's settings had no magpie). +func TestZCodeMovedDataFolder(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro"}}); err != nil { + t.Fatal(err) + } + moved := filepath.Join(t.TempDir(), "ZCodeData") + homeV2, movedV2 := filepath.Join(home, ".zcode", "v2"), filepath.Join(moved, ".zcode", "v2") + os.MkdirAll(homeV2, 0o755) + os.MkdirAll(movedV2, 0o755) + // what ZCode writes when the folder is moved: the choice stays at home, + // the data (an empty provider list among it) is copied over + setting, _ := json.Marshal(map[string]any{"locale": "zh-CN", "dataBaseDir": moved}) + os.WriteFile(filepath.Join(homeV2, "setting.json"), setting, 0o644) + empty := `{"schemaVersion":1,"config":{"providerConfigRules":{"providerRules":[]},"modelConfigRules":{"providerModelRules":[],"manualProviderModelRules":[]}}}` + os.WriteFile(filepath.Join(movedV2, "provider_config.json"), []byte(empty), 0o600) + + a := zcode(home) + f := a.Field("provider") + if err := f.Set("magpie"); err != nil { + t.Fatal(err) + } + if !zcodeRuled(filepath.Join(movedV2, "provider_config.json")) { + b, _ := os.ReadFile(filepath.Join(movedV2, "provider_config.json")) + t.Fatalf("no magpie in the moved folder's provider_config.json: %s", b) + } + if _, ok := edit.GetJSON(filepath.Join(movedV2, "config.json"), "provider.magpie"); !ok { + t.Fatal("no magpie in the moved folder's config.json") + } + for _, n := range []string{"provider_config.json", "config.json"} { + if _, err := os.Stat(filepath.Join(homeV2, n)); err == nil { + t.Fatalf("%s written in the home folder ZCode no longer reads", n) + } + } + if f.Get() != "magpie" { + t.Fatalf("get: %q", f.Get()) + } + + // a relative or empty dataBaseDir is not a folder ZCode moved to + for _, d := range []string{"", " ", "ZCodeData"} { + setting, _ := json.Marshal(map[string]any{"dataBaseDir": d}) + os.WriteFile(filepath.Join(homeV2, "setting.json"), setting, 0o644) + if got := zcode(home).Dir; got != filepath.Join(home, ".zcode") { + t.Fatalf("dataBaseDir %q: %s", d, got) + } + } +} diff --git a/internal/appdir/zcode.go b/internal/appdir/zcode.go new file mode 100644 index 000000000..59a2d316a --- /dev/null +++ b/internal/appdir/zcode.go @@ -0,0 +1,35 @@ +package appdir + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" +) + +// ZCodeDataIn is the folder ZCode (Zhipu's desktop app) keeps its .zcode +// data in, with home as the user's home folder: the home folder, unless the +// user moved it in ZCode's settings (数据存储路径, "data storage path"). +// ZCode then copies its .zcode/v2 there and reads it from there from its +// next start, providers (provider_config.json, config.json) and sign-in +// (credentials.json) included; the choice stays in the home folder's +// .zcode/v2/setting.json as dataBaseDir, which ZCode's main process reads +// before anything else (readBootstrapDataBaseDirFromDisk). A value that +// isn't an absolute path is ignored, as Getenv ignores one. +// +// Skills, AGENTS.md and the CLI's config.json stay under the home folder +// whatever this says: ZCode reads those from HOME/USERPROFILE. +func ZCodeDataIn(home string) string { + var s struct { + DataBaseDir any `json:"dataBaseDir"` + } + b, err := os.ReadFile(filepath.Join(home, ".zcode", "v2", "setting.json")) + if err != nil || json.Unmarshal(b, &s) != nil { + return home + } + d, _ := s.DataBaseDir.(string) + if d = strings.TrimSpace(d); d == "" || !rooted(d) { + return home + } + return d +} diff --git a/internal/provider/zcode.go b/internal/provider/zcode.go index 397deb83e..a008fb835 100644 --- a/internal/provider/zcode.go +++ b/internal/provider/zcode.go @@ -53,6 +53,7 @@ import ( "sync" "time" + "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/catalog" ) @@ -97,7 +98,8 @@ type zcodeKey struct { func zcodeCredentialsPath() string { home, _ := os.UserHomeDir() - return filepath.Join(home, ".zcode", "v2", "credentials.json") + // under the folder ZCode's data was moved to, when it was + return filepath.Join(appdir.ZCodeDataIn(home), ".zcode", "v2", "credentials.json") } // zcodeSecret is the key ZCode encrypts its credentials with. diff --git a/internal/provider/zcode_models.go b/internal/provider/zcode_models.go index 3b57a7110..02a4ee1d9 100644 --- a/internal/provider/zcode_models.go +++ b/internal/provider/zcode_models.go @@ -32,6 +32,7 @@ import ( "slices" "strings" + "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/catalog" "github.com/yetone/magpie/internal/proc" ) @@ -229,7 +230,7 @@ func zcodeRemoteBuiltin(ctx context.Context) (zcodeBuiltin, error) { // it ships with, and the one it last took, under ~/.zcode. var zcodeBuiltinFiles = func() []string { home, _ := os.UserHomeDir() - fs, _ := filepath.Glob(filepath.Join(home, ".zcode", "v2", "runtime", "provider", "*", "*", "*", "zcode-builtin.json")) + fs, _ := filepath.Glob(filepath.Join(appdir.ZCodeDataIn(home), ".zcode", "v2", "runtime", "provider", "*", "*", "*", "zcode-builtin.json")) if runtime.GOOS == "darwin" { fs = append(fs, proc.SystemDirs("/Applications/ZCode.app/Contents/Resources/config/provider/zcode-builtin.json")...) }