TestFetchAsksASharedBaseOnce, TestFetchAsksOneListOnceForTwoBasesOfIt and TestFetchUnionsProtocolListsWithoutChat are red on main since6eb510f78("codex: a relay's own Codex entries reach Codex as it gave them"). Each counts every GET of the models list. For a provider added by its address (no preset, no account) with a Responses base,6eb510f78asks the Responses list once more with client_version (codexEntries), and says so: "fetchOne asks the same URL once more with client_version (codexEntries). Only where the list is fetched (add, key change, Refresh), never per Codex poll." The three tests' providers are such providers, so each counted that ask as the list asked a second time. They weren't seen red then: 6eb510f78's whole internal/provider run crashed in TestUnreadLoginsWriteErrorsKeepCredentials (#3632) first, and after its rebase provider ran with -run Codex. On CI that crash ends the package before them (run 40492644859, ate056779a, names none of the three). The ask is that change's own, and its TestCodexListedEntry counts it, so this keeps it and the three tests count it apart: a request carrying client_version is Codex's, any other is the list. Each still wants the list itself asked exactly once, what it is there for (#1006 for TestFetchAsksOneListOnceForTwoBasesOfIt), and now wants Codex's ask once at most: whether it is made is TestCodexListedEntry's to say. What a Refresh of such a provider asks is unchanged, its Responses list twice, the second time as Codex asks it. If that request isn't wanted, it is codexEntries' to change, and these tests then need nothing. What was run (tree 7e0975fa, on origin/maine056779a, go1.26.8, linux/amd64), outside graftcp, umask 077: - The three, -count=1 -v: red ate056779aas #3854 has them; here 3 PASS. -race -count=20: ok. - Broken by hand (restored after): - the fetch keeping no record of the lists it asked (a nil catalog.Listed, which pays for every list URL again): TestFetchAsksASharedBaseOnce ("asked 2 times, want once") and TestFetchAsksOneListOnceForTwoBasesOfIt ("the one list fetched 2 times, want once", "chat and responses fetched the list 2 times, want once") red; the third's two lists are on two servers, none shared. - codexEntries asking Codex's list twice: all three red ("the list asked as Codex asks 2 times, want once at most", "asked responses as Codex asks 2 times", "asked as Codex asks 2 times"). - internal/provider whole but TestUnreadLoginsWriteErrorsKeepCredentials (#3632's crash), Bun 1.4.2: TestCodexAppServersOnHome alone red (#3906); ate056779a, that and the three. - go test -tags nogui -count=1 -timeout 45m ./... outside graftcp, umask 077, Bun 1.4.2 (log kept): 70 packages ok, 6 red, none in what this changes. internal/provider ends at #3632's crash before the three run, so the run above without that test is their record. The reds: agent TestFolderVarsAreListed (#3884, main's CI red), TestCodexDriftReached (#3398), TestInstalledVersion, TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir and TestZedAppPath (#3141), TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout (#3641); edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram (umask 077, #3640); gateway TestPluginToldWhereTheAgentWorks (#3675); gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie (#3649); provider TestCodexAppServersOnHome (#3906), then "close of synctest channel from outside bubble" from TestUnreadLoginsWriteErrorsKeepCredentials (#3632), both main's CI red. - gofmt -l clean. go vet -tags nogui and go build -tags nogui for linux, windows and darwin, and go vet and go build for windows without the tag: exit 0 each. go vet and go build without the tag on linux: exit 1 (this box has no GTK or WebKitGTK dev packages). Merged from JoeyWu-tech's branch, cherry-picked onto origin/maind6c459c79with the other of #3906/#3854. Run on mote (macOS) at that tree: - go test ./internal/proc/ ./internal/provider/: ok, both whole (provider's first whole pass since #3632). - the six tests these two touch, -count=20: ok. Each was red on main before. - GOOS=linux and GOOS=windows go vet ./internal/proc/ ./internal/provider/: ok. Not run here: Linux, where #3906 was red; its record is the author's above and the CI run of this push.
222 lines
6.9 KiB
Go
222 lines
6.9 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/agentenv"
|
|
)
|
|
|
|
// isolateFetchHome keeps a fetch's fixtures out of the user's own magpie
|
|
// home: the list a fetch reads and writes is under HOME, and the tests
|
|
// below save providers of their own.
|
|
func isolateFetchHome(t *testing.T) {
|
|
t.Helper()
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
}
|
|
|
|
// One base string is not one request (#1006). A vendor is configured with
|
|
// the same URL for Chat, Anthropic and Gemini, and answers each of them
|
|
// only under the authentication that protocol uses: the Chat request is
|
|
// answered 401, and the Anthropic and Gemini lists are there for the
|
|
// asking. Deduping by the configured base string took the latter two for
|
|
// lists already had, because a request had been made at that base, and
|
|
// the user was left with "no model to try" for a vendor whose models were
|
|
// there the whole time.
|
|
func TestFetchAsksEachAuthenticationAtOneBase(t *testing.T) {
|
|
isolateFetchHome(t)
|
|
var mu sync.Mutex
|
|
asked := map[string]int{}
|
|
note := func(k string) {
|
|
mu.Lock()
|
|
asked[k]++
|
|
mu.Unlock()
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch {
|
|
case r.Header.Get("x-goog-api-key") != "":
|
|
note("gemini")
|
|
io.WriteString(w, `{"models":[{"name":"models/gemini-native","supportedGenerationMethods":["generateContent"]}]}`)
|
|
case r.Header.Get("anthropic-version") != "":
|
|
note("anthropic")
|
|
io.WriteString(w, `{"data":[{"id":"claude-native","display_name":"Claude Native"}]}`)
|
|
default:
|
|
note("chat")
|
|
http.Error(w, `{"error":{"message":"the key is not for this API"}}`, http.StatusUnauthorized)
|
|
}
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
base := srv.URL + "/v1beta"
|
|
if err := Save(Provider{ID: "triple", Name: "Triple", Chat: base, Anthropic: base, Gemini: base, Key: "sk-t"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("triple")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ms, err := q.Fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("fetch: %v", err)
|
|
}
|
|
got := map[string]bool{}
|
|
for _, m := range ms {
|
|
got[m.ID] = true
|
|
}
|
|
for _, id := range []string{"claude-native", "gemini-native"} {
|
|
if !got[id] {
|
|
t.Errorf("%s not listed: %v", id, ms)
|
|
}
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if asked["chat"] != 1 {
|
|
t.Errorf("chat asked %d times, want once", asked["chat"])
|
|
}
|
|
// each list at this base asked under its own authentication, and
|
|
// neither taken for the refusal the one before it met
|
|
if asked["anthropic"] != 1 {
|
|
t.Errorf("anthropic asked %d times, want once", asked["anthropic"])
|
|
}
|
|
if asked["gemini"] != 1 {
|
|
t.Errorf("gemini asked %d times, want once", asked["gemini"])
|
|
}
|
|
}
|
|
|
|
// Two bases are not two lists (#1006). A remote magpie is configured with
|
|
// its Chat base at …/v1 and its Anthropic base at the bare host beside
|
|
// it, and both resolve to one list URL, GET /v1/models: the test of a
|
|
// saved remote is to fetch that list once, because its decision models
|
|
// are marked out of it. Deduping by the configured base string asked the
|
|
// one list twice, once per string.
|
|
func TestFetchAsksOneListOnceForTwoBasesOfIt(t *testing.T) {
|
|
isolateFetchHome(t)
|
|
var mu sync.Mutex
|
|
asked := map[string]int{}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
k := r.URL.Path
|
|
if r.URL.Query().Has("client_version") {
|
|
// Codex's own ask of the Responses list (codexEntries),
|
|
// a request of its own
|
|
k += "?client_version"
|
|
}
|
|
mu.Lock()
|
|
asked[k]++
|
|
mu.Unlock()
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
io.WriteString(w, `{"data":[{"id":"lib/m1","display_name":"M1"}]}`)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
if err := Save(Provider{ID: "paired", Name: "Paired", Chat: srv.URL + "/v1", Anthropic: srv.URL, Key: "sk-p"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("paired")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ms, err := q.Fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("fetch: %v", err)
|
|
}
|
|
if len(ms) != 1 || ms[0].ID != "lib/m1" {
|
|
t.Fatalf("listed %v, want lib/m1", ms)
|
|
}
|
|
mu.Lock()
|
|
if asked["/v1/models"] != 1 {
|
|
t.Errorf("the one list fetched %d times, want once", asked["/v1/models"])
|
|
}
|
|
if asked["/models"] != 0 {
|
|
t.Errorf("/models asked %d times although the /v1/models beside it had answered", asked["/models"])
|
|
}
|
|
mu.Unlock()
|
|
// and the same base as Chat and Responses is one list too, the one
|
|
// case the base string already covered; Codex's own ask of the
|
|
// Responses list is counted apart (#3854)
|
|
if err := Save(Provider{ID: "one", Name: "One", Chat: srv.URL + "/v1", Responses: srv.URL + "/v1", Key: "sk-p"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
o, err := Find("one")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mu.Lock()
|
|
asked["/v1/models"] = 0
|
|
mu.Unlock()
|
|
if _, err := o.Fetch(context.Background()); err != nil {
|
|
t.Fatalf("fetch: %v", err)
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if asked["/v1/models"] != 1 {
|
|
t.Errorf("chat and responses fetched the list %d times, want once", asked["/v1/models"])
|
|
}
|
|
if n := asked["/v1/models?client_version"]; n > 1 {
|
|
t.Errorf("the list asked as Codex asks %d times, want once at most", n)
|
|
}
|
|
}
|
|
|
|
// What a base's list refused is reported once (#1006). Chat and Responses
|
|
// at one base are one request, and the second of them has nothing left to
|
|
// ask — which is not a second refusal, and saying so would put a sentence
|
|
// about the dedup in the middle of the message the user reads about their
|
|
// key. The one refusal stands alone.
|
|
func TestFetchReportsOneRefusalForOneBaseAskedTwice(t *testing.T) {
|
|
isolateFetchHome(t)
|
|
var mu sync.Mutex
|
|
asked := 0
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
asked++
|
|
mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
http.Error(w, `{"error":{"message":"the key is not for this API"}}`, http.StatusUnauthorized)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
if err := Save(Provider{ID: "twice", Name: "Twice", Chat: srv.URL + "/v1", Responses: srv.URL + "/v1", Key: "sk-x"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("twice")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = q.Fetch(context.Background())
|
|
if err == nil {
|
|
t.Fatal("a list refused for every protocol fetched")
|
|
}
|
|
msg := err.Error()
|
|
if n := strings.Count(msg, "401 Unauthorized"); n != 1 {
|
|
t.Errorf("the one refusal named %d times: %s", n, msg)
|
|
}
|
|
for _, noise := range []string{"already asked", "no model to try"} {
|
|
if strings.Contains(msg, noise) {
|
|
t.Errorf("the message says %q about the dedup: %s", noise, msg)
|
|
}
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if asked != 1 {
|
|
t.Errorf("the list asked %d times, want once", asked)
|
|
}
|
|
}
|