Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

provider: a failed model test's request log row carries its error

yetone4b57e5b658fe
diff --git a/internal/provider/probelog_test.go b/internal/provider/probelog_test.go
index e5674c61d..92191bca5 100644
--- a/internal/provider/probelog_test.go
+++ b/internal/provider/probelog_test.go
@@ -2,6 +2,7 @@ package provider
import (
"context"
+ "io"
"net/http"
"net/http/httptest"
"path/filepath"
@@ -133,3 +134,53 @@ func TestDetectWritesItsRequestsDown(t *testing.T) {
t.Fatal("an agent's request was written down as magpie's own")
}
}
+
+// A model's failed test is written down with why it failed: the vendor's
+// error for a refusal, and a stream's error after its 200, not a bare
+// status or a 200 that reads as answered (ysm119 on Discord: a model "didn't
+// answer", and the request log had no error to show).
+func TestFailedModelTestIsWrittenWithItsError(t *testing.T) {
+ probeHome(t)
+ rows := probeRows(t)
+ srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+ b, _ := io.ReadAll(r.Body)
+ switch {
+ case strings.Contains(string(b), "refused-model"):
+ rw.WriteHeader(http.StatusBadRequest)
+ _, _ = rw.Write([]byte(`{"error":{"message":"AI Model Not Found: refused-model"}}`))
+ case strings.Contains(string(b), "stream-fails"):
+ rw.Header().Set("Content-Type", "text/event-stream")
+ _, _ = rw.Write([]byte("data: {\"error\":{\"message\":\"the stream broke off\"}}\n\n"))
+ default:
+ rw.Header().Set("Content-Type", "text/event-stream")
+ _, _ = rw.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}]}\n\ndata: [DONE]\n\n"))
+ }
+ }))
+ defer srv.Close()
+ p := Provider{ID: "relay", Name: "Relay", Chat: srv.URL + "/v1", Key: "sk", KeyProtocol: Chat,
+ Models: []string{"refused-model", "stream-fails", "fine"}, Account: &Account{Stream: true}} // a plugin's: streamed
+ res := p.TestModels(context.Background(), []string{"refused-model", "stream-fails", "fine"})
+ errs := map[string]string{}
+ for _, r := range res {
+ errs[r.Model] = r.Error
+ }
+ byModel := map[string]probelog.Row{}
+ for _, r := range rows() {
+ if r.Kind == probelog.KindTest {
+ if _, twice := byModel[r.Model]; twice {
+ t.Errorf("%s written twice", r.Model)
+ }
+ byModel[r.Model] = r
+ }
+ }
+ for model, want := range map[string]string{"refused-model": "AI Model Not Found", "stream-fails": "the stream broke off", "fine": ""} {
+ r, ok := byModel[model]
+ if !ok {
+ t.Errorf("%s: no row in %+v", model, rows())
+ continue
+ }
+ if want == "" && r.Error != "" || !strings.Contains(r.Error, want) || r.Error != errs[model] {
+ t.Errorf("%s: row error %q, the test's %q, want %q", model, r.Error, errs[model], want)
+ }
+ }
+}
diff --git a/internal/provider/test.go b/internal/provider/test.go
index 4d67752f7..9b5296c2c 100644
--- a/internal/provider/test.go
+++ b/internal/provider/test.go
@@ -6,6 +6,7 @@ import (
"cmp"
"context"
"encoding/json"
+ "errors"
"fmt"
"html"
"io"
@@ -536,15 +537,24 @@ func (p Provider) ownRequests(ctx context.Context, kind string) context.Context
return probelog.With(ctx, kind, p.ID, account)
}
-func probe(ctx context.Context, p Provider, proto Protocol, url string, body []byte, model string, wait time.Duration) Result {
- r := Result{Protocol: proto, Model: model}
+// The test is one row of the request log, written here with what came of
+// it: the vendor's error, a stream's error after its 200 too. Do would
+// write the status alone, so a failed test's row said nothing of why
+// (ysm119 on Discord: "no answer", and no error to find anywhere).
+func probe(ctx context.Context, p Provider, proto Protocol, url string, body []byte, model string, wait time.Duration) (r Result) {
+ r = Result{Protocol: proto, Model: model}
if model == "" {
r.Error = "no model to try: expose one, or refresh the model list"
return r
}
ctx, cancel := context.WithTimeout(probelog.WithModel(ctx, model), wait)
defer cancel()
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
+ rctx := ctx
+ logged := probelog.Pending(ctx)
+ if logged {
+ rctx = probelog.Taken(ctx)
+ }
+ req, err := http.NewRequestWithContext(rctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
r.Error = err.Error()
return r
@@ -570,14 +580,47 @@ func probe(ctx context.Context, p Provider, proto Protocol, url string, body []b
return r
}
start := time.Now()
- res, err := p.Do(http.DefaultClient, req)
+ // each request sent, a retry's too, is a row; the last one carries
+ // what the test came to
+ type try struct {
+ url string
+ start time.Time
+ status int
+ err error
+ }
+ var tries []try
+ send := func(q *http.Request) (*http.Response, error) {
+ t := try{url: q.URL.String(), start: time.Now()}
+ res, err := p.Do(http.DefaultClient, q)
+ if t.err = err; err == nil {
+ t.status = res.StatusCode
+ }
+ tries = append(tries, t)
+ return res, err
+ }
+ if logged {
+ defer func() {
+ for i, t := range tries {
+ if i == len(tries)-1 {
+ if r.Status != 0 {
+ t.status = r.Status
+ }
+ if r.Error != "" {
+ t.err = errors.New(r.Error)
+ }
+ }
+ probelog.Note(ctx, t.url, http.MethodPost, t.start, t.status, t.err)
+ }
+ }()
+ }
+ res, err := send(req)
if err == nil && proto == Anthropic {
// a vendor that takes one of the two key headers alone
- res, err = RetryOneAuth(p, req, res, func(r *http.Request) (*http.Response, error) { return p.Do(http.DefaultClient, r) })
+ res, err = RetryOneAuth(p, req, res, send)
}
if err == nil && proto == Chat {
// a GLM Coding Plan's key at Zhipu's pay-as-you-go endpoint
- res, err = RetryZhipuCoding(p, req, res, func(r *http.Request) (*http.Response, error) { return p.Do(http.DefaultClient, r) })
+ res, err = RetryZhipuCoding(p, req, res, send)
}
if err == nil {
// Zhipu's Responses API refuses a key 200, in an envelope: the