Files
magpie/internal/gateway/remote_magpie_apis_test.go
T
a998a36367 provider, gateway: remote magpie serves decision and retrieval APIs (#1282)
* provider, gateway: remote magpie serves decision and retrieval APIs

Configure System One from the remote address and discover explicitly marked
decision models, including Clef and custom names. Keep exposed retrieval
models when importing the remote catalog, without treating decisions as
chat or image generators. Refresh removes stale decisions and the editor
receives an explicit empty decision list when none remain.

Forward caller/session attribution for decision, retrieval and media APIs.
Preserve TUI model-list errors for a remote that cannot be reached. Document
the HTTP API surface and old-peer compatibility.

Verified with the full nogui Go suite, focused race checks, go vet, native
macOS and Linux/Windows cross-builds, and the Remote magpie browser tests in
Chromium and WebKit in English and Chinese. The new System One integration
test fails on the old code with an empty model list, and the retrieval test
fails because the remote embedding model is filtered out. Integration tests
exercise two gateways and simulated upstreams; live vendor accounts were
not used.

* gateway, provider: remote forwards keep official client headers

Claude Code and Codex keep their User-Agent through both gateways, so a
relay behind the remote that admits only official clients accepts their
requests. Only decision, retrieval and media calls set magpie's User-Agent
at their own call sites. The two-hop test covers Messages, Responses, Chat
Completions and a Codex originator with another User-Agent.

Keep entryFor limited to remote decision providers. Test rejection of
unlisted remote decisions and remove the redundant resolveDecideModel
early return: the explicit model marker and list-only Jev already exclude
vendor aliases. Document retrieval entries remaining in the shared agent
catalog and TUI list errors being shown for other mixed providers too.

Verified with the Remote magpie provider/gateway tests. Before the fix,
TestRemoteMagpieClientHeaders fails all four cases with the relay receiving
magpie/dev and returning 403 official client required; all pass after it.
Follow-up to yetone's review of #1282.

* provider, gateway, gui: remote discovery reports only the decisions it lists (#1282)

Show CLI list failures and catalog counts for mixed providers. Derive the
remote decision endpoint from its one editable address, name custom decisions
by their ids, and reuse successful discovery in endpoint tests without an
empty decide OK row. Bare-provider decision selection is documented.

Simplify decision classification around the explicit remote list and reuse
model markers while building Available, Catalog and Deciders, avoiding a
whole-list copy and scan for every model. Other providers keep their rules.

The regression tests failed before the fixes on swallowed CLI errors, blank
labels, extra endpoint fields, empty decide results and repeated discovery.
Deliberately removing retrieval/video User-Agent or System One caller
attribution makes all three receiving-side tests fail with lost agent/via.

Verified with sandboxed Go tests, targeted race checks, Linux GUI/nogui and
Windows vet/build, Chromium/WebKit en/zh browser tests and zh-TW/ja/de checks.
The two-hop tests use real gateway handlers with simulated vendor replies;
real vendor accounts and native Windows/macOS runs were not used.

Co-authored-by: XYenon <register@xyenon.bid>
Amp-Thread-ID: https://ampcode.com/threads/T-01a11cd6-93aa-767c-9d6e-f0db2a9c5bab

* gui, gateway tests: remote decision refresh leaves only its current model choices (#1282)

The empty-decision browser fixture cleared deciders but kept decision
entries in models, so its screenshot still showed the same chips. Remove
those entries and assert the visible choices in both states. Check the
real providerInfo model list after refresh too, with Jev Router retained
as chat, and verify an unnamed remote decision reaches the local picker
with its id and provider label.

No production code changes. Verified all Remote magpie tests in root,
provider, gateway and gui under a sandbox HOME with -race; the new
assertions in gateway/gui pass. Chromium and WebKit pass all six browser
cases in English and Chinese; the corrected screenshot run passes 3/3.
Targeted Linux and Windows nogui vet and gofmt are clean.

Amp-Thread-ID: https://ampcode.com/threads/T-01a11cd6-93aa-767c-9d6e-f0db2a9c5bab
Co-authored-by: XYenon <register@xyenon.bid>

---------

Co-authored-by: Amp <amp@ampcode.com>
2026-10-10 03:55:45 +08:00

242 lines
9.0 KiB
Go

package gateway
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// Discover decision models through a real gateway, including names that
// aren't Jev's, then ask them through another gateway with caller attribution.
func TestRemoteMagpieSystemOne(t *testing.T) {
fresh(t)
type hit struct {
path, body string
head http.Header
}
var mu sync.Mutex
var vendor, hops []hit
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
mu.Lock()
vendor = append(vendor, hit{r.URL.Path, string(b), r.Header.Clone()})
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"answers":{"ok":{"value":true}},"usage":{"input_tokens":3,"output_tokens":1}}`)
}))
t.Cleanup(up.Close)
models := []string{"jev-latest", "@cf/cloudflare/clef", "decision-model-preview", "custom-image-decision"}
if err := provider.Save(provider.Provider{ID: "judge", Name: "Judge", Key: "vendor-key", Decide: up.URL + "/v1", Models: models}); err != nil {
t.Fatal(err)
}
peer := New()
remoteHandler := peer.Handler()
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
r.Body = io.NopCloser(strings.NewReader(string(b)))
mu.Lock()
hops = append(hops, hit{r.URL.Path, string(b), r.Header.Clone()})
mu.Unlock()
remoteHandler.ServeHTTP(w, r)
}))
t.Cleanup(remote.Close)
if err := provider.Save(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Chat: remote.URL, Key: "remote-key"}); err != nil {
t.Fatal(err)
}
p, _ := provider.Find("office")
if _, err := p.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
var got []string
for _, e := range provider.Deciders() {
if e.Provider.ID == "office" {
got = append(got, e.Model)
if e.Model == "judge/custom-image-decision" && e.Name != "custom-image-decision · Judge" {
t.Errorf("unnamed remote decision in the local picker: %q", e.Name)
}
}
}
want := make([]string, len(models))
for i, m := range models {
want[i] = "judge/" + m
}
if !slices.Equal(got, want) {
t.Fatalf("remote decision models: %v; want %v", got, want)
}
if ms := p.DecisionModels(); len(ms) != len(models) {
t.Fatalf("decision picker has %v", ms)
}
for _, e := range provider.Catalog() {
if e.Provider.ID == "office" && slices.Contains(want, e.Model) {
t.Fatalf("decision model offered for chat: %s", e.ID)
}
}
// Ordinary agent lists never acquire decision models.
plain := httptest.NewRecorder()
remoteHandler.ServeHTTP(plain, httptest.NewRequest("GET", "/v1/models", nil))
if strings.Contains(plain.Body.String(), `"kind":"decision"`) || strings.Contains(plain.Body.String(), "judge/") {
t.Fatalf("decisions leaked into ordinary models: %s", plain.Body.String())
}
s := New()
for _, model := range models {
req := httptest.NewRequest("POST", "/v1/systemone", strings.NewReader(`{"model":"office/judge/`+model+`","state":{"message":"hello"},"questions":{"ok":{"type":"noul"}}}`))
req.Header.Set("User-Agent", "pi/1.0")
req.Header.Set(SessionHeader, "decision-session")
req.Header.Set("session_id", "native-session")
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, req)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"value":true`) {
t.Fatalf("%s: %d %s", model, rec.Code, rec.Body.String())
}
}
mu.Lock()
defer mu.Unlock()
if len(vendor) != len(models) {
t.Fatalf("vendor calls: %v", vendor)
}
for i, h := range vendor {
if h.path != "/v1/systemone" || gjsonModel([]byte(h.body)) != models[i] || h.head.Get("Authorization") != "Bearer vendor-key" || !strings.Contains(h.body, `"message":"hello"`) {
t.Errorf("vendor call: %+v", h)
}
if h.head.Get(AgentHeader) != "" || h.head.Get(SessionHeader) != "" || h.head.Get("session_id") != "" {
t.Errorf("caller headers reached vendor: %v", h.head)
}
}
for _, h := range hops {
if h.path != "/v1/systemone" {
continue
}
if h.head.Get("Authorization") != "Bearer remote-key" || h.head.Get(AgentHeader) != "pi" || h.head.Get(ViaHeader) == "" || h.head.Get(SessionHeader) != "decision-session" || h.head.Get("session_id") != "native-session" || !strings.HasPrefix(h.head.Get("User-Agent"), "magpie/") {
t.Errorf("remote caller: %v", h.head)
}
}
if route := s.trace.routes[0]; route.Agent != "pi" || route.Session != "decision-session" || !route.Done {
t.Errorf("decision trace: %+v", route)
}
for _, route := range peer.trace.routes {
if route.Agent != "pi" || route.Session != "decision-session" {
t.Errorf("remote decision trace lost caller: %+v", route)
}
}
var recorded int
for _, r := range usage.Load(time.Time{}) {
if r.Provider == "judge" {
recorded++
if r.Agent != "pi" || r.Via != hostName() {
t.Errorf("remote decision usage lost caller: %+v", r)
}
}
}
if recorded != len(models) {
t.Errorf("remote decision usage: %d records; want %d", recorded, len(models))
}
}
// Decision discovery applies the gateway key's model restrictions, while
// an ordinary /models request continues to contain conversation models only.
func TestRemoteMagpieDecisionListPermissions(t *testing.T) {
fresh(t)
if err := provider.Save(provider.Provider{ID: "judge", Decide: "http://127.0.0.1:1/v1", Key: "k", Models: []string{"custom-a", "custom-b"}}); err != nil {
t.Fatal(err)
}
keys, secrets := newCaller(t, "Held")
if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"judge/custom-a"}}); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest("GET", "/v1/models", nil)
req.Header.Set("Authorization", "Bearer "+secrets[0])
req.Header.Set(provider.DecidersHeader, "1")
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, req)
var list struct {
Data []struct {
ID, Kind string
Name string `json:"display_name"`
Label string `json:"magpie_label"`
}
}
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &list) != nil || len(list.Data) != 1 || list.Data[0].ID != "judge/custom-a" || list.Data[0].Kind != "decision" {
t.Fatalf("held decision list: %d %s", rec.Code, rec.Body.String())
}
if list.Data[0].Name != "custom-a" || list.Data[0].Label != "custom-a · judge" {
t.Errorf("unnamed decision lost its id: %+v", list.Data[0])
}
}
// A remote's exposed retrieval models survive discovery, and their requests
// keep the input and options through both gateways.
func TestRemoteMagpieRetrieval(t *testing.T) {
s, lib := shelved(t)
remote := httptest.NewServer(s.Handler())
t.Cleanup(remote.Close)
if err := provider.Save(provider.Provider{ID: "office", Preset: provider.RemoteMagpiePreset, Chat: remote.URL, Key: "remote-key"}); err != nil {
t.Fatal(err)
}
p, _ := provider.Find("office")
if _, err := p.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
live, _, _ := catalog.Live("office")
if !slices.ContainsFunc(live, func(m catalog.Model) bool { return m.ID == "lib/embed-1" }) {
t.Fatalf("remote embedding model lost: %v", live)
}
// The shared list has no separate retrieval kind, so these stay in
// the local catalog just as they do in the remote's agent list.
entries := provider.Catalog()
for _, id := range []string{"office/lib/embed-1", "office/lib/rerank-1"} {
if !slices.ContainsFunc(entries, func(e provider.Entry) bool { return e.ID == id }) {
t.Errorf("remote retrieval entry missing from catalog: %s", id)
}
}
for _, tc := range []struct{ path, body, result, model string }{
{"/v1/embeddings", `{"model":"office/lib/embed-1","input":["hello","world"],"dimensions":2,"encoding_format":"float"}`, `"embedding":[0.1,-0.2]`, "embed-1"},
{"/v1/rerank", `{"model":"office/lib/rerank-1","query":"magpie","documents":["a crow","a magpie"],"top_n":2,"return_documents":true}`, `"relevance_score":0.9`, "rerank-1"},
} {
req := httptest.NewRequest("POST", tc.path, strings.NewReader(tc.body))
req.Header.Set("User-Agent", "pi/1.0")
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, req)
code, raw := rec.Code, rec.Body.String()
if code != 200 || !strings.Contains(raw, tc.result) {
t.Fatalf("%s: %d %s", tc.path, code, raw)
}
got := lib.got(tc.path)
if len(got) != 1 || gjsonModel([]byte(got[0])) != tc.model {
t.Fatalf("vendor request: %v", got)
}
var asked, sent map[string]any
json.Unmarshal([]byte(tc.body), &asked)
json.Unmarshal([]byte(got[0]), &sent)
asked["model"] = tc.model
a, _ := json.Marshal(asked)
b, _ := json.Marshal(sent)
if string(a) != string(b) {
t.Errorf("retrieval options changed: %s; want %s", b, a)
}
}
var recorded int
for _, r := range usage.Load(time.Time{}) {
if r.Provider == "lib" {
recorded++
if r.Agent != "pi" || r.Via != hostName() {
t.Errorf("remote retrieval usage lost caller: %+v", r)
}
}
}
if recorded != 2 {
t.Errorf("remote retrieval usage: %d records; want 2", recorded)
}
}