From 6bc5702cd4605d284ae6e2c3769714da66e2b992 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 6 Nov 2026 15:02:50 +0800 Subject: [PATCH] agent: magpie's OpenCode provider sets setCacheKey, so OpenCode sends its session's prompt_cache_key (#5468) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mariamatrix (#5468) and mahiro233 on Discord: magpie writes OpenCode's magpie provider with @ai-sdk/openai-compatible and only baseURL/apiKey, so OpenCode sends no prompt_cache_key and an upstream that caches by it misses. Checked against OpenCode's source and real runs (sandbox HOME on mote, a recording Chat server): - 2.2.5: v1/config/migrate.ts maps options.setCacheKey to compatibility.supportsPromptCacheKey, and openai-chat.ts sends prompt_cache_key only then. Without it: no key; with it: "prompt_cache_key":"ses_…" on the turn and the title request. A config with magpie's provider under providers, compatibility.supportsPromptCacheKey true, sends it too; without, none. - 1.19.5: setCacheKey true sends camelCase promptCacheKey (the previous commit sends it on as prompt_cache_key). - 2.0.25: ignores the option; the run answers as before, no error. Now: - the "opencode" shape's options have setCacheKey: true. Every writer of provider.magpie goes through it and openCodeKept: OpenCode, its OPENCODE_CONFIG_DIR copies, MiMo Code, DevEco, Air's OpenCode and OpenChamber's. - openCodeNative writes it as OpenCode 2 does: compatibility .supportsPromptCacheKey, not a setting. - a setCacheKey the file has already under provider.magpie.options, or a supportsPromptCacheKey under providers.magpie.compatibility, is the user's (false too) and stays, through a sync and a pick. Kilo has no wiring in magpie; no other agent's config has this option. TestAirProviderWritesKeepCustomFields compared every option as a string; it now compares a bool as its JSON. Run on mote: go test ./internal/agent (full package) ok; go test -run 'OpenCode|CacheKey|Air|OpenChamber|Deveco|Mimo|KeepTheir' ./internal/agent ./internal/gateway ok; go vet on darwin, GOOS=linux, GOOS=windows; go build ./... (darwin, windows); gofmt clean. Broken by hand, each failing TestOpenCodeSetsCacheKey: no setCacheKey in the shape ("no setCacheKey"); no keep under provider ("the user's false not kept"); no keep under providers (same); no move out of settings ("setCacheKey in OpenCode 2's settings"). Not tried: a config written by a running magpie fed to a real OpenCode (the real runs used configs of the same shape, written by hand); a real upstream's cache hit rate before and after. Reported-by: mariamatrix Reported-by: mahiro233 --- internal/agent/agents.go | 38 +++++++++++++- internal/agent/air_test.go | 4 +- internal/agent/opencode_cache_key_test.go | 61 +++++++++++++++++++++++ internal/agent/opencode_providers.go | 17 +++++++ 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 internal/agent/opencode_cache_key_test.go diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 4a515fe9f..fb0ded69a 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io/fs" + "maps" "os" "path/filepath" "reflect" @@ -585,8 +586,15 @@ func magpieProviderJSONAt(shape, catalog, gw string) any { } ms = append(ms, jsonPair{m.ID, e}) } + // setCacheKey: without it OpenCode sends no prompt_cache_key, and an + // upstream that caches by it misses every turn (#5468): OpenCode + // 2.1.4 and later read it as compatibility.supportsPromptCacheKey + // (v1/config/migrate.ts) and send the session's prompt_cache_key, + // OpenCode 1 its camelCase promptCacheKey (the gateway's + // cacheKeyCamel), and 2.0.25 leaves it be. A setCacheKey of the + // user's own stays (openCodeKept) return map[string]any{"npm": "@ai-sdk/openai-compatible", "name": "magpie", - "options": map[string]any{"baseURL": gw + "/v1", "apiKey": openCodeKeyAt(catalog, gw)}, "models": ms} + "options": map[string]any{"baseURL": gw + "/v1", "apiKey": openCodeKeyAt(catalog, gw), "setCacheKey": true}, "models": ms} case "crush": var ms []map[string]any for _, m := range models { @@ -664,9 +672,14 @@ func piBlockKept(path string, value func() any) func() any { // they wrote over under a name of magpie's stays theirs. A variant as magpie // writes it under its name is magpie's: one the model has no longer, a // level gone or a fast mode lost, goes. +// +// A setCacheKey the file has already is the user's, false too (#5468). func openCodeKept(path string, value func() any) func() any { return theirsKept(path, "provider."+magpieID, func() any { v := value() + if _, set := edit.GetJSON(path, "provider."+magpieID+".options.setCacheKey"); set { + v = withoutOption(v, "options", "setCacheKey") + } raw, ok := edit.GetJSON(path, "provider."+magpieID+".models") if !ok { return v @@ -706,6 +719,29 @@ func openCodeKept(path string, value func() any) func() any { }, "models") } +// withoutOption is magpie's provider block v without key in its object +// under in, for a value the user set there themselves: a copy, so a block +// another writer holds is left as it was; in goes when nothing else is in it. +func withoutOption(v any, in, key string) any { + m, ok := v.(map[string]any) + if !ok { + return v + } + o, ok := m[in].(map[string]any) + if !ok { + return v + } + out := maps.Clone(m) + o = maps.Clone(o) + delete(o, key) + if len(o) == 0 { + delete(out, in) + } else { + out[in] = o + } + return out +} + // objectPairs are a JSON object's keys and values in the order it lists // them; none when it is not an object. func objectPairs(raw []byte) orderedJSON { diff --git a/internal/agent/air_test.go b/internal/agent/air_test.go index ccedd4f9a..821b29de7 100644 --- a/internal/agent/air_test.go +++ b/internal/agent/air_test.go @@ -2,6 +2,7 @@ package agent import ( "encoding/json" + "fmt" "os" "path/filepath" "strings" @@ -228,7 +229,8 @@ func TestAirProviderWritesKeepCustomFields(t *testing.T) { } want := magpieProviderJSONFor("opencode", "air").(map[string]any) for k, v := range want["options"].(map[string]any) { - if got, _ := edit.GetJSON(cfg, "provider.magpie.options."+k); got != v { + // a string as it is, any other value as its JSON + if got, _ := edit.GetJSON(cfg, "provider.magpie.options."+k); got != fmt.Sprint(v) { t.Errorf("options.%s = %q, want %v", k, got, v) } } diff --git a/internal/agent/opencode_cache_key_test.go b/internal/agent/opencode_cache_key_test.go new file mode 100644 index 000000000..9d85fe0dc --- /dev/null +++ b/internal/agent/opencode_cache_key_test.go @@ -0,0 +1,61 @@ +package agent + +import ( + "path/filepath" + "testing" +) + +// #5468: magpie's provider in OpenCode's config sets setCacheKey, or +// OpenCode sends no prompt_cache_key and an upstream that caches by it +// misses every turn. A real OpenCode 2.2.5 sent prompt_cache_key with it +// and none without; 1.19.5 sent promptCacheKey (the gateway's +// cacheKeyCamel); 2.0.25 ignored it. Under providers, in OpenCode 2's +// words, it is compatibility.supportsPromptCacheKey, no setting. A value +// the user set themselves, false too, stays, through a sync and a pick. +func TestOpenCodeSetsCacheKey(t *testing.T) { + home := syncHome(t) + cfg := filepath.Join(home, ".config", "opencode", "opencode.json") + oc := opencode(home, filepath.Join(home, ".config")) + if err := oc.Fields[0].Set("magpie/relay/glm-4.6"); err != nil { + t.Fatal(err) + } + opts, _ := providerAt(t, cfg, "provider")["options"].(map[string]any) + if opts["setCacheKey"] != true { + t.Fatalf("no setCacheKey:\n%s", readFile(cfg)) + } + + // the user's own false, under provider and under providers + writeFile(t, cfg, `{"model":"magpie/relay/glm-4.6","provider":{"magpie":{"name":"magpie","npm":"@ai-sdk/openai-compatible",`+ + `"options":{"baseURL":"http://127.0.0.1:3425/v1","apiKey":"magpie","setCacheKey":false},"models":{}}},`+ + `"providers":{"magpie":{"name":"magpie","package":"aisdk:@ai-sdk/openai-compatible","compatibility":{"supportsPromptCacheKey":false},`+ + `"settings":{"baseURL":"http://127.0.0.1:3425/v1","apiKey":"magpie"},"models":{}}}}`) + for _, step := range []func() error{oc.Sync, func() error { return oc.Fields[0].Set("magpie/relay/glm-4.6") }} { + if err := step(); err != nil { + t.Fatal(err) + } + opts, _ := providerAt(t, cfg, "provider")["options"].(map[string]any) + native := providerAt(t, cfg, "providers") + compat, _ := native["compatibility"].(map[string]any) + settings, _ := native["settings"].(map[string]any) + ms, _ := native["models"].(map[string]any) + if opts["setCacheKey"] != false || compat["supportsPromptCacheKey"] != false || ms["relay/glm-4.6"] == nil { + t.Fatalf("the user's false not kept:\n%s", readFile(cfg)) + } + if _, in := settings["setCacheKey"]; in { + t.Fatalf("setCacheKey in OpenCode 2's settings:\n%s", readFile(cfg)) + } + } + + // under providers with no word of the user's: magpie's + writeFile(t, cfg, `{"model":"magpie/relay/glm-4.6","providers":{"magpie":{"name":"magpie","package":"aisdk:@ai-sdk/openai-compatible",`+ + `"settings":{"baseURL":"http://127.0.0.1:3425/v1","apiKey":"magpie"},"models":{}}}}`) + if err := oc.Sync(); err != nil { + t.Fatal(err) + } + native := providerAt(t, cfg, "providers") + compat, _ := native["compatibility"].(map[string]any) + settings, _ := native["settings"].(map[string]any) + if compat["supportsPromptCacheKey"] != true || settings["setCacheKey"] != nil || settings["baseURL"] == nil { + t.Fatalf("providers.magpie:\n%s", readFile(cfg)) + } +} diff --git a/internal/agent/opencode_providers.go b/internal/agent/opencode_providers.go index 0626fca55..1f9a5ca93 100644 --- a/internal/agent/opencode_providers.go +++ b/internal/agent/opencode_providers.go @@ -2,6 +2,7 @@ package agent import ( "encoding/json" + "maps" "slices" "github.com/yetone/magpie/internal/edit" @@ -37,6 +38,17 @@ func openCodeNative(block any) any { out["package"] = "aisdk:" + npm } if o, ok := b["options"]; ok { + // setCacheKey is no setting of the package's but OpenCode 2's + // supportsPromptCacheKey, for the whole provider (migrate.ts, + // migrateStandardProvider; #5468) + if om, ok := o.(map[string]any); ok { + if ck, has := om["setCacheKey"]; has { + out["compatibility"] = map[string]any{"supportsPromptCacheKey": ck} + om = maps.Clone(om) + delete(om, "setCacheKey") + o = om + } + } out["settings"] = o } // in the order magpie lists them (#4575): OpenCode lists a provider's @@ -107,6 +119,11 @@ func openCodeNativeVariant(id string, opts any) orderedJSON { func openCodeNativeKept(path string, block func() any) func() any { return theirsKept(path, openCodeNativeKey, func() any { v := openCodeNative(block()) + // a supportsPromptCacheKey the file has already is the user's + // (#5468) + if _, set := edit.GetJSON(path, openCodeNativeKey+".compatibility.supportsPromptCacheKey"); set { + v = withoutOption(v, "compatibility", "supportsPromptCacheKey") + } raw, ok := edit.GetJSON(path, openCodeNativeKey+".models") if !ok { return v