zen: Test with the public key asks each API for a free model Zen serves on it, and leaves out an API no free model is on (z'x on Discord; #2385)

z'x on Discord: Test on an OpenCode Zen provider with the key public always
failed. Reproduced with Zen's real list: Test asked public for
claude-fable-5 on Messages and gemini-3-flash on Gemini, which public
doesn't reach (Zen's free models alone answer it), and big-pickle on
Responses, a chat-only model. Only chat (big-pickle) could pass, so the
card was red every time.

- testModel: on OpenCode's gateway a model models.dev names no API for
  (no per-model provider.npm) is served on chat alone, so no other
  endpoint is tried with it (a Claude id on Messages still is). This also
  stops a paid key's Responses probe asking big-pickle.
- With public, only a free model (OpenCodeFree) is tried, and with none on
  an API that API is left out of the result; with none on any, one row
  says why (zenPublicNone), and a client-scoped Test says the same.

#2385 itself (deepseek-v4.1-flash-free not offered): the built-in Zen
lists whatever Zen's /models lists, with no table of free ones;
TestZenPublicListsNewFreeModels pins that with the real bytes. The model
was missing in the opencode-zen-free plugin, fixed there (zen-free 0.1.9,
magpie-community/plugins).

Fixtures: testdata/zen_public_models.json is Zen's /inference/v1/models
as the public key read it on 2026-10-15 (one GET, byte for byte);
zen_catalog.json is models.dev's opencode entries for those ids from the
same day's synced catalog.

Run (worktree at origin/main a1f7b8c13, -tags nogui -p 1):
- go test -run 'TestZen|TestOpenCode' ./internal/provider: ok
- the 77 provider tests that call Test, testModel or tiny, plus
  TestZen*/TestOpenCode*: ok
- broken by hand: test.go back to main's, the two new tests kept:
  TestZenPublicTestAsksFreeModels red (responses asked big-pickle,
  anthropic claude-fable-5, gemini gemini-3-flash, all 401; own key's
  Responses model big-pickle); green again with the fix
- gofmt -l clean; go vet -tags nogui ./internal/provider for darwin,
  linux and windows: ok

Not tried: Test against real Zen. The fake answers 401 to a paid model
with public and to a model off its API, as the reporter's red rows say,
but Zen's exact status there wasn't asked. Responses now asks
muse-spark-1.2-contributor-free (first in Zen's list, "deprecated" in
models.dev though Zen lists it); whether Zen still answers it wasn't
asked.

Full suite: SUITE_LINE
This commit is contained in:
yetone
2026-10-15 23:08:04 +08:00
parent cc09c5bd97
commit 934263f543
4 changed files with 181 additions and 2 deletions
+144
View File
@@ -1,12 +1,23 @@
package provider
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/catalog"
)
@@ -156,3 +167,136 @@ func TestOpenCodeFreeURL(t *testing.T) {
t.Fatalf("paid probe %s", url)
}
}
// zenFake serves Zen's /models as the public key read it on 2026-10-15
// (testdata/zen_public_models.json, byte for byte) beside models.dev's
// opencode entries for those ids from the same day (zen_catalog.json),
// and answers a probe as Zen answers public: 200 for a free model at the
// inference API on the API the catalog names for it, 401 for any other.
// It returns the probes asked, as "path model".
func zenFake(t *testing.T) func() []string {
t.Helper()
isolate(t)
isolateFetchHome(t)
cat, err := os.ReadFile("testdata/zen_catalog.json")
if err != nil {
t.Fatal(err)
}
list, err := os.ReadFile("testdata/zen_public_models.json")
if err != nil {
t.Fatal(err)
}
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
if err := os.WriteFile(catalog.CachePath(), cat, 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
var mu sync.Mutex
var asked []string
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/v1/models") {
w.Write(list)
return
}
b, _ := io.ReadAll(r.Body)
model := gjson.GetBytes(b, "model").String()
mu.Lock()
asked = append(asked, r.URL.Path+" "+model)
mu.Unlock()
api := catalog.APIOf("opencode", model)
if api == "" {
api = "chat"
}
path := map[string]string{"chat": "/inference/v1/chat/completions", "responses": "/inference/v1/responses", "anthropic": "/inference/messages"}[api]
if !strings.HasSuffix(model, "-free") && model != "big-pickle" || r.URL.Path != path {
w.WriteHeader(http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}],\"type\":\"response.output_text.delta\",\"delta\":\"hi\"}\n\n"))
}))
t.Cleanup(srv.Close)
addr := srv.Listener.Addr().String()
old := http.DefaultClient.Transport
http.DefaultClient.Transport = &http.Transport{
DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, network, addr)
},
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
t.Cleanup(func() { http.DefaultClient.Transport = old })
return func() []string {
mu.Lock()
defer mu.Unlock()
return slices.Clone(asked)
}
}
// #2385: the built-in OpenCode Zen, asked with public, lists the free
// models Zen lists, deepseek-v4.1-flash-free among them, as Zen lists
// them: there is no table of free models to fall behind.
func TestZenPublicListsNewFreeModels(t *testing.T) {
zenFake(t)
p, _ := FromPreset("opencode-zen")
id, err := Add(p)
if err != nil {
t.Fatal(err)
}
q, _ := Find(id)
if _, err := q.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
q, _ = Find(id)
var ids []string
for _, m := range q.Available() {
ids = append(ids, m.ID)
}
if !slices.Contains(ids, "deepseek-v4.1-flash-free") || !q.OpenCodeFree("deepseek-v4.1-flash-free") {
t.Fatalf("listed %v", ids)
}
}
// z'x on Discord: Test on an OpenCode Zen provider with the public key
// always failed. It asked public for claude-fable-5 on Messages and
// gemini-3-flash on Gemini, which public doesn't reach, and big-pickle on
// Responses, where Zen doesn't serve it. Now each API is asked for a free
// model it serves, and an API no free model is on is left out.
func TestZenPublicTestAsksFreeModels(t *testing.T) {
asked := zenFake(t)
p, _ := FromPreset("opencode-zen")
id, err := Add(p)
if err != nil {
t.Fatal(err)
}
q, _ := Find(id)
if q.Key != OpenCodeAnonymousKey {
t.Fatalf("key %q", q.Key)
}
got := map[Protocol]Result{}
for _, r := range q.Test(context.Background()) {
got[r.Protocol] = r
}
for proto, r := range got {
if !r.OK {
t.Errorf("%s: %+v (asked %v)", proto, r, asked())
}
}
if got[Chat].Model != "big-pickle" || !strings.HasPrefix(got[Responses].Model, "muse-spark-") || !strings.HasSuffix(got[Responses].Model, "-free") {
t.Errorf("models %+v", got)
}
if _, ok := got[Anthropic]; ok {
t.Errorf("Messages tried with public: %+v", got[Anthropic])
}
if _, ok := got[Gemini]; ok {
t.Errorf("Gemini tried with public: %+v", got[Gemini])
}
// a Zen key of the user's own is still asked for paid models
q.Key = "sk-own"
if m := q.testModel(*q, Anthropic); m != "claude-fable-5" {
t.Errorf("own key's Messages model %q", m)
}
if m := q.testModel(*q, Responses); m == "big-pickle" {
t.Errorf("own key's Responses model %q, a chat model", m)
}
}
+35 -2
View File
@@ -50,6 +50,9 @@ func (p Provider) Test(ctx context.Context) []Result {
if !ok {
return []Result{{Protocol: proto, Model: model, Error: "no key is on for this endpoint"}}
}
if model == "" && q.zenPublic() {
return []Result{{Protocol: proto, Error: zenPublicNone}}
}
url, body := tiny(q, proto, UpstreamName(p, model))
return []Result{probe(ctx, q, proto, url, q.tested(asClient(client, proto, body)), model, testWait)}
}
@@ -67,9 +70,17 @@ func (p Provider) Test(ctx context.Context) []Result {
out = append(out, Result{Protocol: proto, Model: model, Error: "no key is on for this endpoint"})
continue
}
if model == "" && q.zenPublic() {
// an API Zen serves no free model on: public reaches none
// of its models, which is no fault of the key's
continue
}
url, body := tiny(q, proto, UpstreamName(p, model))
out = append(out, probe(ctx, q, proto, url, q.tested(body), model, testWait))
}
if len(out) == 0 && p.zenPublic() {
out = append(out, Result{Protocol: Chat, Error: zenPublicNone})
}
if p.IsRemoteMagpie() {
// The model list already checked its key and named its decisions.
if model := p.Jev(); listErr == nil && model != "" {
@@ -362,12 +373,27 @@ func (p Provider) keyFor(proto Protocol) (Provider, bool) {
return p, false
}
// zenPublic is whether p is OpenCode Zen asked with the key OpenCode
// sends signed out (public), which Zen's free models alone answer.
func (p Provider) zenPublic() bool {
return p.IsOpenCode() && p.Key == OpenCodeAnonymousKey
}
// zenPublicNone is why Zen's public key has nothing to test.
const zenPublicNone = "no free model of OpenCode Zen is listed for this API: the public key reaches only the free ones (refresh the model list, or add a Zen key for the rest)"
// testModel is the model a probe of proto's endpoint asks for: the first
// exposed one q's key sees, else the first it sees at all — preferring a
// Claude model on the Anthropic endpoint, and one that chats to one that
// draws on an images API.
// draws on an images API. On OpenCode's gateway a model models.dev names
// no API for is served on chat alone, so no other endpoint is tried with
// it; and with Zen's public key only a free model is tried, on an API it
// is served on, or none (z'x on Discord: Test asked public for
// claude-fable-5 on Messages, gemini-3-flash on Gemini and big-pickle on
// Responses, and every one failed, though big-pickle answers on chat).
func (p Provider) testModel(q Provider, proto Protocol) string {
k := q.first()
public := q.zenPublic()
off := disabledNow() // a model the user disabled is never asked (#1867)
var pools [][]catalog.Model
if ms := p.Exposed(); len(ms) > 0 {
@@ -382,12 +408,19 @@ func (p Provider) testModel(q Provider, proto Protocol) string {
if apis := p.APIs(id); apis != nil {
return slices.Contains(apis, proto)
}
if p.IsOpenCode() {
// each model on one API, chat for the rest (openCodeEndpoints)
return proto == Chat || proto == Anthropic && isClaude(id)
}
return (proto != Anthropic || isClaude(id)) && !p.NamedOnlyOn(proto)
},
func(string) bool { return true },
func(string) bool { return !public },
} {
for _, pool := range pools {
for _, m := range pool {
if public && !p.OpenCodeFree(m.ID) {
continue
}
if !p.DecidesModel(m.ID) && !off(p.ID, m.ID) && want(m.ID) && (k.Key == "" || p.Serves(k, m.ID)) {
return m.ID
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long