Files
magpie/internal/agent/opencode_cache_key_test.go
T
yetone 6bc5702cd4 agent: magpie's OpenCode provider sets setCacheKey, so OpenCode sends its session's prompt_cache_key (#5468)
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
2026-11-06 15:02:53 +08:00

62 lines
2.8 KiB
Go

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