Files
magpie/internal/gateway/remote_magpie_vendor_test.go
T
yetone ff16f6c552 gateway: a remote magpie's providers are each a vendor of their own to a conversation (fremontdango)
fremontdango on Discord ran two magpies. On the local one, a routing
group held one model from two providers of the remote magpie
(office/first/m, office/second/m). Its conversations were kept as if the
two were one vendor, because both are the one remote-magpie key here
(candidate.who()). The same group made on the remote behaved
differently:
- strict: when the first provider ran out, the conversation failed over
  to the second, though strict means it never moves
- session/auto: with the first gone, the "same account, other model"
  match kept the conversation on the second as "session", though nothing
  of it was cached there
- the second's refusal of the first's sealed reasoning was recorded
  against the whole remote, so the first's own seals were then taken out
  of requests to it
- a native compaction one sealed went to the other too

There is now a vendor identity for these: candidate.vendor() and
sameVendor(who, model). Behind a remote magpie it is the key plus the
remote's provider, the model id's first part (remoteUpstream). A remote
group is one vendor by its whole id, and a model named without a
provider is the key alone. It is used by:
- affine's widest match
- strictOnly and strictKeep
- unanswered
- refused/withoutRefused
- checkpointReaders (native compaction)
- Codex leadFirst
Lanes, requests a minute and leastHeld's per-model seat stay on who(),
the limits the user set on the key here. Sticks and compaction writers
are stored as before (who plus model), so the sticks saved on disk still
match after the update. Only the local magpie (the one with the group)
needs this version.

Run:
- go test -tags nogui -count=1 -timeout 45m ./internal/gateway: ok
  (559s) at 040a4ec29. After rebasing onto ab546a4ee (busy.go,
  draw.go, gateway.go changed upstream): vet, gofmt and -run
  'TestRemote|Strict|Affinity|Stick|NativeCompaction|Refused|Lead|Busy|Draw':
  ok
- go vet (darwin, GOOS=linux, GOOS=windows, -tags nogui)
  ./internal/gateway, and gofmt -l: clean
- Two sandbox magpies, each under its own HOME/XDG dirs: the remote on
  127.0.0.1:47611 with providers first and second (model m1, one fake
  upstream on :47613 that answers 429 to first's key once a flag file
  exists), and the local on :47612 with a Remote magpie provider and a
  strict, ordered group of office/first/m1 and office/second/m1. Turn 1
  of a session went to first. With the flag set, turn 2:
  - local on origin/main: 200 from office/second/m1 (moved)
  - local on this change: 429 "Office: First: usage limit reached"
    (kept), and a new session went to second

Broken by hand: sameVendor reduced to c.who() == who made
TestRemoteMagpieStrictKeepsTheRemotesProvider fail (went to second) and
TestRemoteMagpieOtherProviderIsNotTheSameAccount fail (traced
"session", want "gone").

Not tried: the sealed-reasoning and native-compaction paths through a
real remote. They use the same sameVendor/vendor, and the package tests
for them pass. Three layers (local, middle, remote): the middle's
provider is the vendor here, and the middle keeps its own providers
apart.
2026-10-27 16:05:23 +08:00

159 lines
6.1 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// fremontdango on Discord: a group of one model from two of a remote
// magpie's providers, made on the magpie that uses the remote, kept its
// conversations as if the two were one vendor, since both are the one
// remote magpie key here. The same group made on the remote itself
// doesn't. remoteVendors is the remote: a magpie serving m from two
// providers, "first/m" and "second/m", each answering "from <provider>",
// first out of its usage (429) while breaking is set.
type remoteVendors struct {
srv *httptest.Server
mu sync.Mutex
tried []string
breaking bool
}
func newRemoteVendors(t *testing.T) *remoteVendors {
rv := &remoteVendors{}
rv.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"object":"list","data":[{"id":"first/m","object":"model"},{"id":"second/m","object":"model"},{"id":"third/m","object":"model"}]}`)
return
}
var q struct {
Model string `json:"model"`
}
b, _ := io.ReadAll(r.Body)
json.Unmarshal(b, &q)
up, _, _ := strings.Cut(q.Model, "/")
rv.mu.Lock()
rv.tried = append(rv.tried, up)
br := rv.breaking && up == "first"
rv.mu.Unlock()
w.Header().Set("X-Magpie-Provider", up)
if br {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusTooManyRequests)
io.WriteString(w, `{"error":{"message":"first: usage limit reached","type":"rate_limit_error"}}`)
return
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`data: {"id":"c2","choices":[{"index":0,"delta":{"role":"assistant","content":"from `+up+`"}}]}`,
`data: {"id":"c2","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`,
`data: [DONE]`))
}))
t.Cleanup(rv.srv.Close)
return rv
}
func (rv *remoteVendors) post(t *testing.T, s *Server, session string) (string, string) {
t.Helper()
rv.mu.Lock()
rv.tried = nil
rv.mu.Unlock()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi `+session+`"}]}`))
req.Header.Set("x-session-id", session)
s.Handler().ServeHTTP(rec, req)
rv.mu.Lock()
defer rv.mu.Unlock()
return rec.Body.String(), strings.Join(rv.tried, ",")
}
func remoteVendorsGroup(t *testing.T, rv *remoteVendors, affinity string, members ...string) {
t.Helper()
if err := provider.Save(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Key: "sk-office", Chat: rv.srv.URL, Models: []string{"first/m", "second/m", "third/m"}}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{Name: "G", Members: members, Routing: provider.Ordered, Affinity: affinity}); err != nil {
t.Fatal(err)
}
}
// Strictly kept, a conversation the remote's first provider answered stays
// on it when it runs out, its error the agent's, as the same group on the
// remote keeps it: the remote's second provider is another vendor, not another model
// of the same account, and isn't asked.
func TestRemoteMagpieStrictKeepsTheRemotesProvider(t *testing.T) {
fresh(t)
rv := newRemoteVendors(t)
remoteVendorsGroup(t, rv, provider.AffinityStrict, "office/first/m", "office/second/m")
s := New()
if out, got := rv.post(t, s, "s1"); !strings.Contains(out, "from first") || got != "first" {
t.Fatalf("first turn: %s tried %s", out, got)
}
rv.mu.Lock()
rv.breaking = true
rv.mu.Unlock()
if out, got := rv.post(t, s, "s1"); !strings.Contains(out, "usage limit") || got != "first" {
t.Fatalf("strict, its provider out: went to the remote's other provider: %s tried %s", out, got)
}
// resting now, it is still its error the agent gets, the other not asked
if out, got := rv.post(t, s, "s1"); !strings.Contains(out, "usage limit") || got == "second" {
t.Fatalf("strict, its provider resting: %s tried %s", out, got)
}
if why := lastWhy(t, s); why != "strict" {
t.Fatalf("the retry traced as %q, want strict", why)
}
}
// A conversation whose remote provider has left the group is gone, as on
// the remote, not kept on the remote's other provider as its account: the
// trace said "session", kept, where nothing of it was cached.
func TestRemoteMagpieOtherProviderIsNotTheSameAccount(t *testing.T) {
fresh(t)
rv := newRemoteVendors(t)
remoteVendorsGroup(t, rv, provider.AffinitySession, "office/first/m", "office/second/m")
s := New()
if out, got := rv.post(t, s, "s1"); !strings.Contains(out, "from first") || got != "first" {
t.Fatalf("first turn: %s tried %s", out, got)
}
remoteVendorsGroup(t, rv, provider.AffinitySession, "office/second/m", "office/third/m")
if out, got := rv.post(t, s, "s1"); !strings.Contains(out, "from second") || got != "second" {
t.Fatalf("second turn: %s tried %s", out, got)
}
if why := lastWhy(t, s); why != "gone" {
t.Fatalf("traced as %q, want gone: the remote's other provider kept it as its own", why)
}
}
func TestRemoteUpstream(t *testing.T) {
remote := provider.Provider{ID: "office", Preset: provider.RemoteMagpiePreset, Key: "k"}
plain := provider.Provider{ID: "openrouter", Key: "k"}
for _, c := range []struct {
p provider.Provider
model, want string
}{
{remote, "anthropic/claude-sonnet-4-5", "anthropic"},
{remote, "openrouter/anthropic/claude-sonnet-4-5", "openrouter"},
{remote, "group/fast", "group/fast"},
{remote, "m", ""},
{plain, "anthropic/claude-sonnet-4-5", ""},
} {
if got := remoteUpstream(c.p, c.model); got != c.want {
t.Errorf("remoteUpstream(%s, %q) = %q, want %q", c.p.ID, c.model, got, c.want)
}
}
a, b := candidate{p: remote, model: "first/m"}, candidate{p: remote, model: "second/m"}
if a.who() != b.who() || a.vendor() == b.vendor() {
t.Errorf("who %q %q, vendor %q %q: one key here, two vendors there", a.who(), b.who(), a.vendor(), b.vendor())
}
if !a.sameVendor(b.who(), "first/x") || a.sameVendor(b.who(), "second/m") || !a.sameVendor(b.who(), "") {
t.Error("sameVendor")
}
}