Mistral routes a conversation to the server holding its KV cache by the
x-affinity header. Checked against the sources: mistral-vibe's CHANGELOG
("KV cache warming via x-affinity in count tokens"), its
_runtime_config.py request_headers (sets x-affinity every turn), and
pi-ai's buildMistralHeaders (earendil-works/pi, mistral-conversations.ts:
sessionId unless the model's or call's headers name one).
forwardOnce now sets x-affinity for a Mistral API key provider
(mistralAffinity): the client's own value as sent, else a hash of the
session or conversation, never the id itself, the same every turn. Other
vendors, accounts and plugins get none; the user's own provider headers
are put on after it. Every route (relayed, translated, count_tokens)
goes through forwardOnce.
Run: go vet on darwin, linux nogui and windows for internal/gateway, and
go test -count=1 ./internal/gateway at the branch head via mote-run, ok
(1370s). New: TestMistralAffinityHeader.
Broken by hand: header setter off, red ("affinity \"\" \"\" \"\""); the
isMistral scope removed, red ("another vendor got x-affinity").
Not tried: a real Mistral key, so whether Mistral's cache hit rate
changes is unmeasured. Full ./... not run.
48 lines
1.8 KiB
Go
48 lines
1.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Mistral routes a conversation to the server that holds its KV cache by
|
|
// x-affinity (Vibe and pi-ai send it every turn; daoche_wuzui on X). A turn
|
|
// gets a stable opaque one per session; a client's own is kept as sent;
|
|
// another vendor is sent none.
|
|
func TestMistralAffinityHeader(t *testing.T) {
|
|
const msg = `{"model":"m1","messages":[{"role":"user","content":"hello there"}]}`
|
|
run := func(preset string, h map[string]string) string {
|
|
f := &fake{t: t, reply: deepseekReply}
|
|
up := setup(t, provider.Chat, f)
|
|
p, _ := provider.Find("fake")
|
|
p.Preset, p.Chat = preset, up.URL+"/v1"
|
|
if err := provider.Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if code, _ := postWith(t, "/v1/chat/completions", msg, h); code != 200 {
|
|
t.Fatalf("status %d", code)
|
|
}
|
|
return f.head.Get("X-Affinity")
|
|
}
|
|
a := run("mistral", map[string]string{"X-Session-Id": "sess-secret-1"})
|
|
b := run("mistral", map[string]string{"X-Session-Id": "sess-secret-1"})
|
|
c := run("mistral", map[string]string{"X-Session-Id": "sess-secret-2"})
|
|
if a == "" || a != b || a == c {
|
|
t.Errorf("affinity %q %q %q: want the same for one session, another for another", a, b, c)
|
|
}
|
|
if strings.Contains(a, "sess-secret") {
|
|
t.Errorf("affinity %q holds the session id", a)
|
|
}
|
|
if got := run("mistral", map[string]string{"X-Session-Id": "s", "x-affinity": "mine"}); got != "mine" {
|
|
t.Errorf("client affinity became %q", got)
|
|
}
|
|
if got := run("deepseek", map[string]string{"X-Session-Id": "s", "x-affinity": "mine"}); got != "" {
|
|
t.Errorf("another vendor got x-affinity %q", got)
|
|
}
|
|
if got := run("deepseek", map[string]string{"X-Session-Id": "s"}); got != "" {
|
|
t.Errorf("another vendor got x-affinity %q", got)
|
|
}
|
|
}
|