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).
64 lines
2.7 KiB
Go
64 lines
2.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// #5315 (cherish_onlyy): Hermes with prompt_caching.cache_ttl: 1h, on
|
|
// anthropic_messages, marks its system prompt and the last three messages
|
|
// itself (agent/prompt_caching.py's system_and_3, each
|
|
// {"type":"ephemeral","ttl":"1h"}). Those marks reach the vendor as sent,
|
|
// every one at an hour. On chat completions Hermes sends no marks, and
|
|
// the ones magpie adds for a Chat client last five minutes, which is why
|
|
// magpie now connects a Claude model to Hermes on Messages.
|
|
func TestHermesHourMarksReachTheVendor(t *testing.T) {
|
|
f := &fake{t: t, reply: anthropicAnswer}
|
|
up := setup(t, provider.Anthropic, f)
|
|
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"claude-sonnet-5"}, Anthropic: up.URL}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := New()
|
|
hour := `{"type":"ephemeral","ttl":"1h"}`
|
|
code, body := postTo(t, srv, "/v1/messages", `{"model":"fake/claude-sonnet-5","max_tokens":64,"stream":false,
|
|
"system":[{"type":"text","text":"You are Hermes Agent.","cache_control":`+hour+`}],
|
|
"tools":[{"name":"terminal","description":"run a command","input_schema":{"type":"object"}}],
|
|
"messages":[
|
|
{"role":"user","content":[{"type":"text","text":"list files"}]},
|
|
{"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"terminal","input":{"command":"ls"}}]},
|
|
{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"a.go b.go","cache_control":`+hour+`}]},
|
|
{"role":"assistant","content":[{"type":"text","text":"two files","cache_control":`+hour+`}]},
|
|
{"role":"user","content":[{"type":"text","text":"read a.go","cache_control":`+hour+`}]}]}`)
|
|
if code != 200 {
|
|
t.Fatalf("%d: %s", code, body)
|
|
}
|
|
ttls := markTTLs(t, f.got)
|
|
if len(ttls) < 4 {
|
|
t.Fatalf("%d marks upstream: %s", len(ttls), f.got)
|
|
}
|
|
for _, ttl := range ttls {
|
|
if ttl != "1h" {
|
|
t.Fatalf("marks upstream %q, want every one 1h: %s", ttls, f.got)
|
|
}
|
|
}
|
|
|
|
// the same turn on chat completions, as magpie connected Hermes before:
|
|
// no marks from Hermes, and magpie's last five minutes
|
|
code, body = postTo(t, srv, "/v1/chat/completions", `{"model":"fake/claude-sonnet-5","max_tokens":64,"stream":false,
|
|
"tools":[{"type":"function","function":{"name":"terminal","parameters":{"type":"object"}}}],
|
|
"messages":[{"role":"system","content":"You are Hermes Agent."},{"role":"user","content":"list files"}]}`)
|
|
if code != 200 {
|
|
t.Fatalf("chat: %d: %s", code, body)
|
|
}
|
|
for _, ttl := range markTTLs(t, f.got) {
|
|
if ttl != "" {
|
|
t.Fatalf("chat marks %q: %s", ttl, f.got)
|
|
}
|
|
}
|
|
if strings.Contains(string(f.got), `"1h"`) {
|
|
t.Fatalf("chat went at an hour: %s", f.got)
|
|
}
|
|
}
|