Files
magpie/internal/provider/plugin_model_test_test.go
T
yetone ba37e78b4c provider: a plugin model's test is told a queue at once and warms its host first
mingxia_16024 on Discord (v0.1.1862): testing models on Qoder (CN and
international) and WorkBuddy always failed with "qfmodel: context deadline
exceeded", even though agents got answers from the same models.

Cause, checked with the owner's real Qoder account in a sandbox HOME:
- The test already takes the gateway's route: p.Do, then the plugin's
  fetch in the host.
- The Qoder plugin waits out Qoder's free-plan queue (plan queue p3) for
  up to 4 minutes, unless X-Magpie-Fallback is "1". magpie never sent
  that header.
- The probe gives up after 20s. A raw request with no deadline got its
  head after 3m49s.
- The probe's 20s also had to cover the plugin host's cold start and the
  account loader's first run, which can renew the sign-in. A first
  WorkBuddy AI test took 8.4s.

Changes:
- For a plugin provider, probe sends X-Magpie-Fallback: 1, so a queued
  model answers at once with the plugin's queue message.
- It starts the host and runs the account's loader (warmPlugin, through
  the account's proxy) under the caller's deadline, not the probe's.
- A plugin probe waits up to pluginTestWait (60s). The GUI gives a model
  test 3 minutes.
- host.js lists x-magpie-fallback in MAGPIE_ONLY, so no vendor receives
  it.

"Recent calls" lists only the gateway's own calls, for every provider. A
test is written to the Requests page as magpie's own test, timed-out
tries included, so that is unchanged.

The gateway still never sends the header when another candidate follows,
which Qoder 0.2.40 and CodeArts 0.2.3 expect: #4049.

Run (mote-run):
- New tests TestPluginModelTestSaysQueued and TestPluginModelTestColdHost.
  They use the fake plugin's new $FAKE_QUEUE and $FAKE_LOAD_WAIT, with a
  cold host from plugin.Restart.
- go test -tags nogui -timeout 45m ./... at 76218200a: every package ok (77), none FAIL.
- GOOS=linux and GOOS=windows go vet of internal/provider and
  internal/plugin, go build ./..., and gofmt -l.
- Real Qoder account, sandbox HOME, a TestModels of qfmodel:
  - before: failed at 20.00s, twice;
  - after: "Qoder queued the request for qfmodel (this plan's queue: p3);
    try again in 30s" in about 0.5s, twice. One more run got a 200 in
    767ms.

Broken by hand, each red:
- Without the header set: SaysQueued fails with "context deadline
  exceeded" at its 5s wait.
- Without warmPlugin: ColdHost fails with a deadline at 2001ms.
- Without the MAGPIE_ONLY entry: the vendor gets fallback="1".

Not tried:
- WorkBuddy CN with a real account. It reads the desktop app's sign-in,
  which the sandbox doesn't have.
- A cold start on a slow machine or on Windows.
- The GUI button itself. The handler is unchanged.
2026-10-28 18:24:50 +08:00

121 lines
4.0 KiB
Go

package provider
import (
"context"
"fmt"
"net/http"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/testenv"
)
// testingPlugin installs the fake plugin with one signed-in account and
// gives its provider; the vendor is vendor.
func testingPlugin(t *testing.T, ctx context.Context, vendor http.Handler) Provider {
t.Helper()
bun := testenv.Bun(t)
claudeHome(t)
t.Setenv("MAGPIE_BUN", bun)
testenv.FakeVendor(t, vendor)
t.Cleanup(plugin.Settle)
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
if _, err := plugin.Add(ctx, abs); err != nil {
t.Fatal(err)
}
k, err := plugin.Import(ctx, "fakeco", map[string]any{"type": "oauth", "refresh": "r-x", "access": "a", "expires": 9e15, "accountId": "a@fake"})
if err != nil {
t.Fatal(err)
}
saveLogins(t, savedLogin{Agent: "plugin:fakeco", User: "a@fake", Home: k, On: true})
if _, err := plugin.Providers(ctx); err != nil {
t.Fatal(err)
}
p, err := Find(PluginID("fakeco"))
if err != nil {
t.Fatal(err)
}
if !p.IsPlugin() {
t.Fatal("not a plugin's provider")
}
return *p
}
// answers is a vendor that streams one word on chat completions, and
// keeps the X-Magpie-Fallback and X-Saw-Fallback each request came with.
type answers struct {
mu sync.Mutex
seen []string
}
func (a *answers) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.mu.Lock()
a.seen = append(a.seen, fmt.Sprintf("fallback=%q saw=%q", r.Header.Get(FallbackHeader), r.Header.Get("X-Saw-Fallback")))
a.mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
fmt.Fprint(w, "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}]}\n\ndata: [DONE]\n\n")
}
// A plugin's model its vendor has queued (Qoder's free models) is said
// queued at once by the models' test, not waited out until the test's
// time ran out ("qfmodel: context deadline exceeded", mingxia_16024);
// the model beside it answers; and the header that said so reaches the
// plugin only, never its vendor.
func TestPluginModelTestSaysQueued(t *testing.T) {
t.Setenv("FAKE_QUEUE", "fake-1")
defer func(w, pw time.Duration) { testWait, pluginTestWait = w, pw }(testWait, pluginTestWait)
testWait, pluginTestWait = 5*time.Second, 5*time.Second
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
v := &answers{}
p := testingPlugin(t, ctx, v)
start := time.Now()
res := p.TestModels(ctx, []string{"fake-1", "fake-claude"})
took := time.Since(start)
got := map[string]Result{}
for _, r := range res {
got[r.Model] = r
}
q := got["fake-1"]
if q.OK || !strings.Contains(q.Error, "queued the request for fake-1") {
t.Errorf("the queued model's test: ok %v, %q (%v); want the plugin's queue message", q.OK, q.Error, took)
}
if strings.Contains(q.Error, "deadline") {
t.Errorf("the queued model's test ran out of time: %q", q.Error)
}
if r := got["fake-claude"]; !r.OK {
t.Errorf("fake-claude's test: %+v", r)
}
v.mu.Lock()
defer v.mu.Unlock()
if len(v.seen) == 0 {
t.Fatal("the vendor was never asked")
}
for _, s := range v.seen {
if s != `fallback="" saw="1"` {
t.Errorf("the vendor got %s; want the plugin to have seen it and the vendor not", s)
}
}
}
// A plugin whose host starts cold and whose loader is slow (a sign-in
// renewed) has its model's test answered when the vendor answers: the
// host's start and the loader don't spend the probe's time.
func TestPluginModelTestColdHost(t *testing.T) {
t.Setenv("FAKE_LOAD_WAIT", "2500")
defer func(w, pw time.Duration) { testWait, pluginTestWait = w, pw }(testWait, pluginTestWait)
testWait, pluginTestWait = 2*time.Second, 2*time.Second
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
p := testingPlugin(t, ctx, &answers{})
plugin.Restart() // cold: the next call starts a host, whose loader hasn't run
res := p.TestModels(ctx, []string{"fake-claude"})
if len(res) != 1 || !res[0].OK {
t.Fatalf("a cold host's model test: %+v", res)
}
}