From e4ac5e8712eb5750a0043bf9d206313eceadbf0d Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 20 Oct 2026 00:39:37 +0800 Subject: [PATCH] zcode: models magpie writes have native web search on, and a model the user unticked stays off (Gearclickist on Discord) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Gearclickist (#help, 10-19): every model magpie writes into ZCode came without web search, and each had to be ticked by hand in its settings. ZCode 3.14 keeps the switch ("原生联网搜索") as config.properties.supportsNativeWebSearch on a model rule in provider_config.json (read from ZCode.app's own bundle). It only works with an anthropic-messages provider, where ZCode sends a web_search_20260209 tool, which the gateway already serves; on other formats ZCode's WebSearch throws. Unticking writes nothing: ZCode drops the key from magpie's rule because false is its default. zcodeRules now: - turns it on for each model magpie lists, on Anthropic Messages only, including rules an older magpie wrote without the key; - remembers which models it turned on in the stash (zcode.web_search), so a model whose key went missing was unticked by the user and stays off on every later sync; - keeps any value already in the rule, true or false; - leaves manualProviderModelRules alone and drops those ids from the list; - on another format takes back only its own true; - turns nothing on when stash.json can't be read or parsed (unknown); - forgets the list on disconnect. config.json (pre-3.14 shape) has no such field and is unchanged. Known gap, not fixed here: other edits a user makes in ZCode to a model magpie lists (JSON output, mid-conversation system, context window) land in the same rule and are still rewritten on sync. Run at this head: - TestZCodeWebSearch (new) fails on the old zcode.go ("first sync: pro is , want true"). Broken by hand, it goes red at each guard: the untick check removed ("unticked: flash is true"), the take-back on another format removed ("chat completions: pro is true"), and an unreadable stash read as empty ("unreadable stash: flash is true"). - go test -run TestZCode ./internal/agent/: ok. - gofmt -l .: clean. go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows: clean. - set -o pipefail; go test -timeout 45m ./... (sandbox HOME, at 94df23926 + this change, load average ~180 from six other sessions' suites): 63 packages ok; internal/agent hit the 45m timeout, stacks show TestClaudeBehindLocalProxyKeepsIt running for 15s, no hang (the known #2723/#2544). Rerun alone: go test -timeout 45m ./internal/agent/ ok, 1633s. Not tried: the real ZCode app (no live config touched); the sync was run only against provider_config.json files written in a sandbox HOME in the shape ZCode 3.14 writes. --- docs/subsystems/agent-wiring.md | 1 + internal/agent/zcode.go | 109 ++++++++++++++++- internal/agent/zcode_websearch_test.go | 154 +++++++++++++++++++++++++ 3 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 internal/agent/zcode_websearch_test.go diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index 1d836f1e1..e1fd5f48e 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -54,6 +54,7 @@ something else shows up as drift. - Cline's `models.json` keeps the provider's other keys, and on each model magpie lists what magpie doesn't write, such as a VS Code override (`clineKept`). - Goose's custom provider file keeps a name, `timeout_seconds`, `toolshim`, headers and the like (`gooseProviderKept`). - ZCode's provider rule keeps its name, logo, visibility, API headers and the key's management link (`zcodeRules`). + - Each model magpie writes into ZCode's `providerModelRules` has native web search (`properties.supportsNativeWebSearch`, "原生联网搜索") on while the provider's format is Anthropic Messages, the only format ZCode sends it on (`zcodeWebSearch`). The models magpie turned it on for are kept in the stash under `zcode.web_search`, so a model the user unticked in ZCode (which takes the key out of the rule) stays off, a value in the rule stays, a manual rule is left alone, and a stash that can't be read turns nothing on. On another format magpie's own on is taken out. Disconnect forgets the list. - The YAML ones go through `yamlKept` and `mergeKept` in [`sync.go`](../../internal/agent/sync.go). Each is tested by a `TestKeepsTheUsersSettings`. - Copilot in JetBrains IDEs keeps its Bring Your Own Key providers in the Copilot language server's `byok.json`, shared with Xcode's and Eclipse's Copilot (`copilotJetBrains` in [`copilotjetbrains.go`](../../internal/agent/copilotjetbrains.go)). magpie writes its own three keys there, `magpie-provider-config`, `magpie-api-key` and `magpie-models-config`, in the bytes the server itself writes, and only when they differ; the user's providers and keys stay. A magpie model the user hid in Manage Models (`isRegistered: false`) stays hidden through a sync. A new file is made 0600 in a 0700 folder, as the server makes it. - dsh (DeepSeek Harness) sets one API per llm-pi-ai route, so magpie writes a route per protocol (`dshKinds` in [`dsh.go`](../../internal/agent/dsh.go); 01huadalang on Discord): `magpie` (Chat Completions) for most models, `magpie-responses` (`openai-responses`) for a model served on Responses, `magpie-anthropic` (`anthropic-messages`, the gateway root, `forceAdaptiveThinking` where `AdaptiveThinking`) for one served on Messages, by the model's `APIs` (`dshRouteOf`). A model's `APIs` is the one its provider serves it on best (`Provider.Native`, in `magpieModels`, [`route.go`](../../internal/agent/route.go)), and Pi, omp and droid read it the same way. A routing group has `responses` when every member is best on Responses (`allResponses`, #1594), so its tool-call arguments are relayed as they stream; any other group has none and stays on Chat, a Messages-only group too, as a Claude member's thinking is known by its own model. A route with no models is taken out. An api the user picked for a route in dsh's Models page is kept, and so are keys they added there. The start (`agent-default-model`) names its model's route, and Sync moves a start on one of magpie's routes to the route its model is now on (`dshStartOnItsRoute`), its effort and marks kept. Check asks the route the start names to list its model. A dsh session resumed on a model that has since moved routes asks for the model to be picked again, since dsh finds a model by route and id. diff --git a/internal/agent/zcode.go b/internal/agent/zcode.go index 0841d980e..fe5a04db1 100644 --- a/internal/agent/zcode.go +++ b/internal/agent/zcode.go @@ -2,6 +2,8 @@ package agent import ( "encoding/json" + "errors" + "io/fs" "net" "net/url" "os" @@ -42,7 +44,7 @@ import ( // "personalModelIds":[…],"modelOrder":[…]}}]}, // "modelConfigRules":{"providerModelRules":[{"providerId":"magpie", // "modelId":…,"config":{"properties":{"contextWindow":…, -// "inputFormat":{"supportsImage":…}}, +// "inputFormat":{"supportsImage":…},"supportsNativeWebSearch":true}, // "optionSpecs":{"maxOutputTokens":{"max":…},"reasoningLevel":{"values":[…]}}}}], // "manualProviderModelRules":[…]}}} // @@ -103,6 +105,9 @@ func zcode(home string) *Agent { return err } if v == "" { + if _, ok := stashLoad()[zcodeSearchedKey]; ok { + forget(zcodeSearchedKey) + } return edit.DelJSON(path, key) } return edit.SetJSON(path, edit.KV{Path: key, Value: zcodeProviderJSON(path, false)}) @@ -414,23 +419,37 @@ func zcodeRules(path string, on, keep bool) error { } manual = append(manual, r) } + // magpie's model rules as they are now, which ZCode's own settings + // write a model's edits into while it keeps its recommended config + ours := map[any]map[string]any{} for _, r := range zcodeList(mcr, "providerModelRules") { if !mine(r) { models = append(models, r) + continue } + m := r.(map[string]any) + ours[m["modelId"]] = m } if on { + format := zcodeFormat(filepath.Dir(path)) + searched, known := zcodeSearched() var ids []string for _, m := range magpieModels("zcode") { ids = append(ids, m.ID) if byHand[m.ID] { + // set by hand in ZCode: its own settings, and the default + // again should it go back to its recommended config + delete(searched, m.ID) continue } props := map[string]any{"inputFormat": map[string]any{"supportsImage": m.Images}} if m.Context > 0 { props["contextWindow"] = m.Context } + if v, set := zcodeWebSearch(ours[m.ID], m.ID, format == "anthropic-messages" && known, searched); set { + props["supportsNativeWebSearch"] = v + } config := map[string]any{"properties": props} // ZCode's own rules give a model it doesn't know 32000 tokens // out and thinking only on or off @@ -449,6 +468,9 @@ func zcodeRules(path string, on, keep bool) error { if ids == nil { ids = []string{} } + if known { + zcodeSearchedPut(searched, ids) + } base, token := gateway.URL(), hereKey() if keep { base, token = zcodeKept(filepath.Dir(path)) @@ -457,7 +479,7 @@ func zcodeRules(path string, on, keep bool) error { "config": map[string]any{ "group": "standard-personal", "access": map[string]any{"type": "api-key", "apiKey": token}, - "api": map[string]any{"type": zcodeFormat(filepath.Dir(path)), "baseUrl": base}, + "api": map[string]any{"type": format, "baseUrl": base}, "personalModelIds": ids, "modelOrder": ids, }} // turned off in ZCode, it stays off @@ -507,6 +529,89 @@ func zcodeRules(path string, on, keep bool) error { return edit.WriteAtomic(path, out) } +// ZCode offers a model its WebSearch tool only where the model's rule says +// it has native web search (properties.supportsNativeWebSearch, 原生联网搜索 +// in a model's settings), off by default. Its tool then asks the model +// again with Anthropic's web_search tool, which only an Anthropic Messages +// provider is sent: on another API format ZCode fails the search. magpie's +// gateway runs that tool for any of its models, so it turns native web +// search on for each model it lists while the format is Anthropic Messages +// (Gearclickist on Discord: every magpie model had to be ticked by hand). +// +// ZCode writes a change to a model with its recommended config into the +// model's rule, magpie's, and leaves a value equal to ZCode's default out of +// it: unticking native web search takes the key out. So the models magpie +// turned it on for are kept in the stash (zcodeSearchedKey), and one of +// them whose rule has no key now was unticked in ZCode and stays off. A +// value in the rule is kept as it is. +const zcodeSearchedKey = "zcode.web_search" + +// zcodeWebSearch is what magpie's rule for model id, which says old now, +// says of native web search: v, or nothing when set is false. anthropic is +// whether ZCode sends the model's provider-native search (the provider's +// format is Anthropic Messages, and the stash could be read). searched holds +// the models magpie turned it on for and is updated. +func zcodeWebSearch(old map[string]any, id string, anthropic bool, searched map[string]bool) (v, set bool) { + props, _ := old["config"].(map[string]any) + props, _ = props["properties"].(map[string]any) + was, had := props["supportsNativeWebSearch"].(bool) + switch { + case !anthropic: + // magpie's on can't be used there; the user's own value stays + if had && was && searched[id] { + delete(searched, id) + return false, false + } + return was, had + case had: + if was { + searched[id] = true + } + return was, true + case searched[id]: + // unticked in ZCode + return false, false + } + searched[id] = true + return true, true +} + +// zcodeSearched is the models magpie turned ZCode's native web search on +// for, and whether the stash could be read: a stash there that can't be +// read or parsed is unknown, and nothing is turned on. +func zcodeSearched() (map[string]bool, bool) { + out := map[string]bool{} + b, err := os.ReadFile(stashPath()) + if errors.Is(err, fs.ErrNotExist) { + return out, true + } + var m map[string]string + if err != nil || json.Unmarshal(b, &m) != nil { + return out, false + } + if v := m[zcodeSearchedKey]; v != "" { + for _, id := range strings.Split(v, "\n") { + out[id] = true + } + } + return out, true +} + +// zcodeSearchedPut keeps searched in the stash, only for models magpie +// still lists (ids), and writes it only when that changes it. +func zcodeSearchedPut(searched map[string]bool, ids []string) { + var keep []string + for _, id := range ids { + if searched[id] { + keep = append(keep, id) + } + } + v := strings.Join(keep, "\n") + if stashLoad()[zcodeSearchedKey] != v { + stash(map[string]string{zcodeSearchedKey: v}) + } +} + func zcodeObj(m map[string]any, k string) map[string]any { o, ok := m[k].(map[string]any) if !ok { diff --git a/internal/agent/zcode_websearch_test.go b/internal/agent/zcode_websearch_test.go new file mode 100644 index 000000000..51b40d01f --- /dev/null +++ b/internal/agent/zcode_websearch_test.go @@ -0,0 +1,154 @@ +package agent + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" +) + +// Every model magpie lists in ZCode has native web search (原生联网搜索) on, +// so ZCode offers it its WebSearch tool (Gearclickist on Discord: each model +// had to be ticked by hand). A model the user unticked in ZCode, which +// takes the key out of magpie's rule, stays unticked through syncs; a value +// in the rule stays; a manual rule is the user's; and on a format other +// than Anthropic Messages, where ZCode can't send the search, magpie's on +// goes while the user's own value stays. +func TestZCodeWebSearch(t *testing.T) { + home := syncHome(t) + if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", + Models: []string{"pro", "flash", "lite", "mine"}}); err != nil { + t.Fatal(err) + } + dir := filepath.Join(home, ".zcode", "v2") + rules := filepath.Join(dir, "provider_config.json") + os.MkdirAll(dir, 0o755) + // a rule an older magpie wrote, without the key, and one the user set + // by hand in ZCode + os.WriteFile(rules, []byte(`{"schemaVersion":1,"config":{ + "providerConfigRules":{"providerRules":[{"providerId":"magpie","providerName":"magpie","enabled":true,"config":{"group":"standard-personal", + "access":{"type":"api-key","apiKey":"magpie"},"api":{"type":"anthropic-messages","baseUrl":"http://127.0.0.1:3425"}, + "personalModelIds":["deepseek/pro"],"modelOrder":["deepseek/pro"]}}]}, + "modelConfigRules":{"providerModelRules":[{"providerId":"magpie","modelId":"deepseek/pro","config":{"properties":{"contextWindow":128000,"inputFormat":{"supportsImage":false}}}}], + "manualProviderModelRules":[{"providerId":"magpie","modelId":"deepseek/mine","config":{"enabled":true,"properties":{"contextWindow":64000,"supportsNativeWebSearch":false,"inputFormat":{"supportsImage":false}}}}]}}}`), 0o600) + os.WriteFile(filepath.Join(dir, "config.json"), []byte(`{"provider":{"magpie":{"name":"magpie","kind":"anthropic","enabled":true,"models":{}}}}`), 0o600) + + doc := func() map[string]any { + b, _ := os.ReadFile(rules) + d := map[string]any{} + if err := json.Unmarshal(b, &d); err != nil { + t.Fatalf("%v: %s", err, b) + } + return d + } + mcr := func(d map[string]any) map[string]any { + return d["config"].(map[string]any)["modelConfigRules"].(map[string]any) + } + props := func(d map[string]any, id string) map[string]any { + for _, r := range mcr(d)["providerModelRules"].([]any) { + if r := r.(map[string]any); r["providerId"] == "magpie" && r["modelId"] == "deepseek/"+id { + return r["config"].(map[string]any)["properties"].(map[string]any) + } + } + return nil + } + search := func() map[string]any { + d, out := doc(), map[string]any{} + for _, id := range []string{"pro", "flash", "lite", "mine"} { + if p := props(d, id); p != nil { + out[id] = p["supportsNativeWebSearch"] + } else { + out[id] = "no rule" + } + } + return out + } + // what ZCode's model settings write: the rule changed, after magpie's + // write + edit := func(id string, change func(p map[string]any)) { + d := doc() + change(props(d, id)) + b, _ := json.Marshal(d) + os.WriteFile(rules, b, 0o600) + later := time.Now().Add(time.Minute) + os.Chtimes(rules, later, later) + } + want := func(step string, w map[string]any) { + t.Helper() + got := search() + for id, v := range w { + if got[id] != v { + t.Fatalf("%s: %s is %v, want %v (all %v)", step, id, got[id], v, got) + } + } + } + a := zcode(home) + sync := func() { + t.Helper() + if err := a.Sync(); err != nil { + t.Fatal(err) + } + } + + // an older magpie's rule and new models get it; the manual one is left + sync() + want("first sync", map[string]any{"pro": true, "flash": true, "lite": true, "mine": "no rule"}) + d := doc() + manual := mcr(d)["manualProviderModelRules"].([]any) + if len(manual) != 1 || manual[0].(map[string]any)["config"].(map[string]any)["properties"].(map[string]any)["supportsNativeWebSearch"] != false { + t.Fatalf("manual rule changed: %v", manual) + } + + // unticked in ZCode: its value equals ZCode's default, so the key goes + edit("flash", func(p map[string]any) { delete(p, "supportsNativeWebSearch") }) + // set off outright + edit("lite", func(p map[string]any) { p["supportsNativeWebSearch"] = false }) + for range 2 { + sync() + want("unticked", map[string]any{"pro": true, "flash": nil, "lite": false}) + } + + // ticked again + edit("flash", func(p map[string]any) { p["supportsNativeWebSearch"] = true }) + sync() + want("ticked again", map[string]any{"pro": true, "flash": true, "lite": false}) + + // on Chat Completions ZCode can't send the search: magpie's on goes, + // the user's off stays + d = doc() + for _, r := range d["config"].(map[string]any)["providerConfigRules"].(map[string]any)["providerRules"].([]any) { + if r := r.(map[string]any); r["providerId"] == "magpie" { + r["config"].(map[string]any)["api"].(map[string]any)["type"] = "openai-chat-completions" + } + } + b, _ := json.Marshal(d) + os.WriteFile(rules, b, 0o600) + later := time.Now().Add(2 * time.Minute) + os.Chtimes(rules, later, later) + sync() + want("chat completions", map[string]any{"pro": nil, "flash": nil, "lite": false}) + + // back on Anthropic Messages, it is on again + d = doc() + for _, r := range d["config"].(map[string]any)["providerConfigRules"].(map[string]any)["providerRules"].([]any) { + if r := r.(map[string]any); r["providerId"] == "magpie" { + r["config"].(map[string]any)["api"].(map[string]any)["type"] = "anthropic-messages" + } + } + b, _ = json.Marshal(d) + os.WriteFile(rules, b, 0o600) + later = time.Now().Add(3 * time.Minute) + os.Chtimes(rules, later, later) + sync() + want("anthropic again", map[string]any{"pro": true, "flash": true, "lite": false}) + + // a stash that can't be read is unknown: nothing is turned on, and + // what the rules say stays + edit("flash", func(p map[string]any) { delete(p, "supportsNativeWebSearch") }) + os.WriteFile(stashPath(), []byte("{not json"), 0o600) + sync() + want("unreadable stash", map[string]any{"pro": true, "flash": nil, "lite": false}) +}