cherish_onlyy (#5315): magpie wrote providers.magpie as chat_completions at the gateway's /v1 for every model. On chat completions to a custom provider Hermes sends no cache marks (anthropic_prompt_cache_policy gives (False, False)), and the marks magpie adds for a Chat client last five minutes, so prompt_caching.cache_ttl: 1h never reached the vendor. They set api_mode: anthropic_messages and the gateway's root by hand and got 1h writes; magpie's next sync or connect wrote chat_completions at /v1 back. Hermes takes api_mode per providers entry, never per model (_resolve_named_custom_runtime, hermes_cli/runtime_provider_custom.py; aliases in config_providers.py's _API_MODE_ALIASES, transport read when api_mode is absent). On anthropic_messages its adapter strips a trailing /v1 and the SDK adds /v1/messages, and for a "claude" model on a third-party gateway it puts its own system_and_3 marks at the configured ttl (agent/prompt_caching.py, read at d914e233). So magpie now writes two entries, as dsh already splits its routes: - magpie: chat_completions at gw/v1, every model not on Messages; - magpie-anthropic: anthropic_messages at gw (no /v1), the models whose native API is Anthropic's Messages and not Responses (hermesOnMessages). model.provider follows the picked model (hermesRoute), in every profile; a sync moves a Claude model magpie had set under "magpie" onto magpie-anthropic, and drops that entry when no model is left for it. Disconnect takes both out. The user's own value: an api_mode or transport on one of magpie's entries that isn't the one magpie writes there (by Hermes's aliases, so "openai" is still magpie's chat) is the user's (hermesModeIn). It stays through sync and pick, base_url follows it, and on "magpie" that entry then serves every model and no magpie-anthropic is written, which is the reporter's set-up exactly. magpie only ever wrote chat_completions into providers.magpie, so nothing earlier is mistaken for the user's. Tests: - TestHermesClaudeGoesOnMessages: Claude on magpie-anthropic (mode, base without /v1, models), deepseek stays on magpie at /v1, Get/Check/Drift clean, a pick moves model.provider back, a bot profile too, Disconnect removes both entries from every profile. - TestHermesSyncMovesClaudeOntoMessages: an existing connected config is moved on sync, and back when the provider goes chat-only. - TestHermesKeepsTheUsersAPIMode: api_mode anthropic_messages at gw, transport: anthropic, codex_responses: each survives two syncs and two picks, base_url kept; api_mode: openai is still magpie's. - internal/gateway TestHermesHourMarksReachTheVendor: a Hermes-shaped /v1/messages turn with 1h marks reaches the Anthropic upstream with every mark at 1h; the same turn on /v1/chat/completions goes at 5m. This one passes on the old code too: the gateway already passed the marks through; it guards that and records why the mode matters. Run (mote-run, this tree): - go test ./internal/agent/ -count=1 -timeout 45m: ok - go test ./internal/gateway/ -run 'TestHermesHourMarksReachTheVendor|TestResponsesToClaudeMarksLastAnHour' -count=1: ok - go vet ./internal/agent/ ./internal/gateway/, GOOS=linux and GOOS=windows: ok - gofmt -l: clean Broken by hand: - hermesSplit returning (nil, false), the old one-entry behaviour: ClaudeGoesOnMessages, SyncMovesClaude and the openai-alias case fail. - hermesModeIn never reporting the user's mode: all three KeepsTheUsersAPIMode cases fail at "sync". Not tried: - Hermes itself: it was not run (running it under a temp HERMES_HOME rewrites the live launcher); its behaviour is read from its source at d914e233. No real 1h cache write was measured against a vendor. - Group models whose members are all Claude, and Claude Code sign-ins (no Native API), stay on chat_completions; GPT on codex_responses is #3080. - Windows and Linux runs of the agent package (vet only).
227 lines
9.3 KiB
Go
227 lines
9.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// hermesAnthropicHome is a sandbox HOME with a chat provider (deepseek)
|
|
// and one served on Anthropic's Messages (relay, Claude's models), and the
|
|
// path of Hermes's config there
|
|
func hermesAnthropicHome(t *testing.T) (home, path string) {
|
|
t.Helper()
|
|
home = t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("HERMES_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)
|
|
}
|
|
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Anthropic: "https://relay.example/v1", Key: "k", Models: []string{"claude-opus-5-5"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path = filepath.Join(appdir.HermesHomeIn(home), "config.yaml")
|
|
os.MkdirAll(filepath.Dir(path), 0o755)
|
|
return home, path
|
|
}
|
|
|
|
type hermesTestEntry struct {
|
|
BaseURL string `yaml:"base_url"`
|
|
APIKey string `yaml:"api_key"`
|
|
APIMode string `yaml:"api_mode"`
|
|
Transport string `yaml:"transport"`
|
|
Models []string `yaml:"models"`
|
|
}
|
|
|
|
type hermesTestConfig struct {
|
|
Model struct {
|
|
Default string `yaml:"default"`
|
|
Provider string `yaml:"provider"`
|
|
} `yaml:"model"`
|
|
Providers map[string]*hermesTestEntry `yaml:"providers"`
|
|
}
|
|
|
|
func readHermes(t *testing.T, path string) (hermesTestConfig, string) {
|
|
t.Helper()
|
|
raw := readFile(path)
|
|
var c hermesTestConfig
|
|
if err := yaml.Unmarshal([]byte(raw), &c); err != nil {
|
|
t.Fatalf("%v\n%s", err, raw)
|
|
}
|
|
return c, raw
|
|
}
|
|
|
|
// #5315 (cherish_onlyy): Hermes takes the API from the provider entry a
|
|
// model is under, and magpie's one entry was chat completions for every
|
|
// model, so a Claude model's prompt_caching.cache_ttl: 1h never reached
|
|
// the vendor. A model served best on Messages goes under
|
|
// "magpie-anthropic", spoken to as anthropic_messages at the gateway's
|
|
// root; every other model stays under "magpie" on chat completions, so
|
|
// both work from one config.
|
|
func TestHermesClaudeGoesOnMessages(t *testing.T) {
|
|
home, path := hermesAnthropicHome(t)
|
|
os.WriteFile(path, []byte("model:\n default: kimi-k3\n provider: moonshot\n"), 0o644)
|
|
bot := filepath.Join(filepath.Dir(path), "profiles", "coder", "config.yaml")
|
|
os.MkdirAll(filepath.Dir(bot), 0o755)
|
|
os.WriteFile(bot, []byte("model:\n default: kimi-k3\n provider: moonshot\n"), 0o644)
|
|
setUpInMagpie("hermes")
|
|
a := hermes(home)
|
|
f := a.Field("model")
|
|
if err := f.Set("magpie/relay/claude-opus-5-5"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gw := gateway.URL()
|
|
for _, p := range []string{path, bot} {
|
|
c, raw := readHermes(t, p)
|
|
chat, anth := c.Providers[magpieID], c.Providers[hermesAnthropicID]
|
|
if chat == nil || anth == nil {
|
|
t.Fatalf("%s: entries:\n%s", p, raw)
|
|
}
|
|
if anth.APIMode != "anthropic_messages" || anth.BaseURL != gw || anth.APIKey == "" || !slices.Equal(anth.Models, []string{"relay/claude-opus-5-5"}) {
|
|
t.Fatalf("%s: magpie-anthropic:\n%s", p, raw)
|
|
}
|
|
if chat.APIMode != "chat_completions" || chat.BaseURL != gw+"/v1" || !slices.Contains(chat.Models, "deepseek/pro") || slices.Contains(chat.Models, "relay/claude-opus-5-5") {
|
|
t.Fatalf("%s: magpie:\n%s", p, raw)
|
|
}
|
|
}
|
|
c, raw := readHermes(t, path)
|
|
if c.Model.Provider != hermesAnthropicID || c.Model.Default != "relay/claude-opus-5-5" {
|
|
t.Fatalf("model:\n%s", raw)
|
|
}
|
|
if f.Get() != "magpie/relay/claude-opus-5-5" || !a.Wired() || a.Drift() != nil || a.Check() != "" {
|
|
t.Fatalf("get %q, wired %v, drift %+v, check %q", f.Get(), a.Wired(), a.Drift(), a.Check())
|
|
}
|
|
// another of magpie's models, on chat: under "magpie"
|
|
if err := f.Set("magpie/deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c, raw = readHermes(t, path); c.Model.Provider != magpieID || c.Model.Default != "deepseek/pro" {
|
|
t.Fatalf("chat model:\n%s", raw)
|
|
}
|
|
if f.Get() != "magpie/deepseek/pro" || a.Check() != "" {
|
|
t.Fatalf("get %q, check %q", f.Get(), a.Check())
|
|
}
|
|
// a bot picked onto Claude on the Agents page goes under it too
|
|
if err := a.Field(hermesProfileKey + "coder").Set("magpie/relay/claude-opus-5-5"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c, raw = readHermes(t, bot); c.Model.Provider != hermesAnthropicID {
|
|
t.Fatalf("bot:\n%s", raw)
|
|
}
|
|
// Disconnect takes both entries out of every config, and leaves the
|
|
// bot on a model it can start with
|
|
if err := f.Set("magpie/relay/claude-opus-5-5"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range []string{path, bot} {
|
|
c, raw := readHermes(t, p)
|
|
if c.Providers[magpieID] != nil || c.Providers[hermesAnthropicID] != nil || hermesOurs(c.Model.Provider) {
|
|
t.Fatalf("%s after disconnect:\n%s", p, raw)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A config magpie connected before: the Claude model it set under "magpie"
|
|
// goes under "magpie-anthropic" on the next sync (an update), with the
|
|
// entry put in beside magpie's, and back when the model is no longer
|
|
// served on Messages.
|
|
func TestHermesSyncMovesClaudeOntoMessages(t *testing.T) {
|
|
home, path := hermesAnthropicHome(t)
|
|
gw := gateway.URL()
|
|
os.WriteFile(path, []byte("model:\n default: relay/claude-opus-5-5\n provider: magpie\nproviders:\n magpie:\n name: magpie\n api_key: "+keyAt(gw)+"\n api_mode: chat_completions\n base_url: "+gw+"/v1\n extra_headers:\n User-Agent: hermes-agent\n models:\n - relay/claude-opus-5-5\n - deepseek/pro\n"), 0o644)
|
|
setUpInMagpie("hermes")
|
|
a := hermes(home)
|
|
if err := a.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c, raw := readHermes(t, path)
|
|
if c.Model.Provider != hermesAnthropicID || c.Providers[hermesAnthropicID] == nil || c.Providers[hermesAnthropicID].APIMode != "anthropic_messages" ||
|
|
slices.Contains(c.Providers[magpieID].Models, "relay/claude-opus-5-5") {
|
|
t.Fatalf("after sync:\n%s", raw)
|
|
}
|
|
// the relay now serves it on chat only: back under "magpie", and the
|
|
// entry with no models goes
|
|
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: "https://relay.example/v1", Key: "k", Models: []string{"claude-opus-5-5"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c, raw = readHermes(t, path); c.Model.Provider != magpieID || c.Providers[hermesAnthropicID] != nil || !slices.Contains(c.Providers[magpieID].Models, "relay/claude-opus-5-5") {
|
|
t.Fatalf("after the relay went chat:\n%s", raw)
|
|
}
|
|
}
|
|
|
|
// cherish_onlyy (#5315) put api_mode: anthropic_messages and the gateway's
|
|
// root into magpie's entry by hand, and the next update or connect made it
|
|
// chat_completions at /v1 again. An API the user gives one of magpie's
|
|
// entries is theirs: it stays through sync and a pick, its address
|
|
// follows it, and that entry then serves every model, as they set it.
|
|
func TestHermesKeepsTheUsersAPIMode(t *testing.T) {
|
|
gw := gateway.URL()
|
|
for _, c := range []struct{ name, entry, mode string }{
|
|
{"api_mode", " api_mode: anthropic_messages\n base_url: " + gw + "\n", "anthropic_messages"},
|
|
{"transport, an alias", " transport: anthropic\n base_url: " + gw + "/v1\n", "anthropic_messages"},
|
|
{"responses", " api_mode: codex_responses\n base_url: " + gw + "/v1\n", "codex_responses"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
home, path := hermesAnthropicHome(t)
|
|
os.WriteFile(path, []byte("model:\n default: relay/claude-opus-5-5\n provider: magpie\nproviders:\n magpie:\n name: magpie\n api_key: "+keyAt(gw)+"\n"+c.entry+" models:\n - relay/claude-opus-5-5\n"), 0o644)
|
|
setUpInMagpie("hermes")
|
|
a := hermes(home)
|
|
check := func(when, model string) {
|
|
t.Helper()
|
|
cfg, raw := readHermes(t, path)
|
|
mp := cfg.Providers[magpieID]
|
|
if mp == nil || cfg.Model.Provider != magpieID || cfg.Model.Default != model || cfg.Providers[hermesAnthropicID] != nil {
|
|
t.Fatalf("%s:\n%s", when, raw)
|
|
}
|
|
if mode, mine := hermesModeIn(path, magpieID); !mine || mode != c.mode || mp.BaseURL != hermesBase(gw, c.mode) ||
|
|
!slices.Contains(mp.Models, "relay/claude-opus-5-5") || !slices.Contains(mp.Models, "deepseek/pro") {
|
|
t.Fatalf("%s: the user's API mode not kept:\n%s", when, raw)
|
|
}
|
|
if a.Check() != "" {
|
|
t.Fatalf("%s: check %q", when, a.Check())
|
|
}
|
|
}
|
|
for range 2 {
|
|
if err := a.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
check("sync", "relay/claude-opus-5-5")
|
|
// connecting again (a pick on the Agents page) keeps it too
|
|
if err := a.Field("model").Set("magpie/deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("pick", "deepseek/pro")
|
|
if err := a.Field("model").Set("magpie/relay/claude-opus-5-5"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("pick claude", "relay/claude-opus-5-5")
|
|
})
|
|
}
|
|
// chat_completions spelled another way is magpie's own, not a choice
|
|
home, path := hermesAnthropicHome(t)
|
|
os.WriteFile(path, []byte("model:\n default: relay/claude-opus-5-5\n provider: magpie\nproviders:\n magpie:\n api_mode: openai\n base_url: "+gw+"/v1\n"), 0o644)
|
|
setUpInMagpie("hermes")
|
|
if err := hermes(home).Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c, raw := readHermes(t, path); c.Model.Provider != hermesAnthropicID {
|
|
t.Fatalf("openai alias:\n%s", raw)
|
|
}
|
|
}
|