Files
magpie/internal/agent/zcode_websearch_test.go
T
yetone e4ac5e8712 zcode: models magpie writes have native web search on, and a model the user unticked stays off (Gearclickist on Discord)
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
  <nil>, 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.
2026-10-20 00:45:12 +08:00

155 lines
5.9 KiB
Go

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})
}