Files
magpie/internal/gateway/vision_wait_test.go
T
yetone 4594d66d2d gateway: an image description's time limit is the describer's, not its wait in the queue, and the usage log no longer calls it the agent canceling (#2318, davisjiahao)
askVisionWith put a 2-minute context.WithTimeout around the whole
description, which goes through the gateway itself (s.serve): its wait for
a slot (MaxConcurrency, QueueWait), for room in the minute, and the pauses
between tries all spent it. A reasoning seer (gpt-6.1-sol on a Codex
account) queued or retried had little or nothing left to describe in, and
the image's turn failed. The usage log said 499 "the agent canceled the
request" for magpie's own limit.

sideDeadline (side_deadline.go) now bounds a request magpie asks of itself:
the vendor has its limit from when the request last went out to it
(httptrace WroteRequest, each try's own), and the whole of it, waits
included, a larger one. Image descriptions get 3 minutes per try (a dense
screenshot at a high reasoning level can take over two) and 10 in all; a
model's search (modelSearch, the same 2-minute limit, the #1522 message)
keeps 2 minutes per try and 5 in all. The context's cause says which limit
ended it, and the 499 sites in serve record "magpie stopped waiting: its
image description had no answer in 3m0s" instead of the agent canceling.
Still 499, no fault of the vendor's.

Not done: the issue's setting for the limit (none needed once the wait
isn't counted) and sending the raw image to a model that can't see it.
A pause between two tries still runs on the first try's limit.

Tests (vision_wait_test.go): TestVisionWaitIsntTheDescribersTime queues
the description behind a held request for twice the limit;
TestVisionLimitEndsASlowDescriber has a describer slower than the limit
and checks the 502 and the usage row. Broken by hand (WithTimeout back,
the old 499 text) both fail: "no answer in 300ms" and "vision usage: 499
the agent canceled the request". modelSearch has no test of its own; it
uses the same sideDeadline.

Run: go test -tags nogui ./internal/gateway -run
'Vision|Search|Image|Seer|Describ|Concurren|Queue|Cancel|Usage|Lane|RPM'
ok (58s); gofmt clean; go vet -tags nogui ./internal/gateway on darwin,
linux, windows clean. Not tried with a real queued seer.

Full suite: SUITE_LINE
2026-10-15 23:08:01 +08:00

125 lines
4.1 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// slowEye is eyeUp with the describing model's answer held: until gate is
// closed for a request that says "hold", and by think for a description.
type slowEye struct {
eyeUp
held chan struct{} // a request that says "hold" has arrived
gate chan struct{}
think time.Duration
open func() // closes gate, once
}
func (u *slowEye) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
if strings.Contains(string(body), "hold") {
u.held <- struct{}{}
<-u.gate
} else if strings.HasSuffix(modelOf(body), "eye") {
time.Sleep(u.think)
}
r.Body = io.NopCloser(strings.NewReader(string(body)))
u.eyeUp.ServeHTTP(w, r)
}
// slowEyed is eyed with one request at a time to the provider, so a
// request waits in its lane while another is out.
func slowEyed(t *testing.T, think time.Duration) (*Server, *slowEye) {
t.Helper()
fresh(t)
u := &slowEye{held: make(chan struct{}, 1), gate: make(chan struct{}), think: think}
up := httptest.NewServer(u)
t.Cleanup(up.Close)
var once sync.Once
t.Cleanup(func() { once.Do(func() { close(u.gate) }) })
u.open = func() { once.Do(func() { close(u.gate) }) }
one := 1
if err := provider.Save(provider.Provider{ID: "probe", Name: "Probe", Chat: up.URL + "/v1", Key: "key", Models: []string{"text", "eye"}, MaxConcurrency: &one, QueueWait: 60}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("probe", up.URL+"/v1", []catalog.Model{
{ID: "text", ImageInput: imageInputBool(false)},
{ID: "eye", Images: true, ImageInput: imageInputBool(true)},
}); err != nil {
t.Fatal(err)
}
return New(), u
}
func visionLimits(t *testing.T, try, most time.Duration) {
t.Helper()
d, m := visionTimeout, visionWaitMost
visionTimeout, visionWaitMost = try, most
t.Cleanup(func() { visionTimeout, visionWaitMost = d, m })
}
const pastedImage = `{"model":"probe/text","messages":[{"role":"user","content":[{"type":"text","text":"read"},{"type":"image_url","image_url":{"url":"data:image/png;base64,aGVsbG8="}}]}]}`
// #2318 (davisjiahao): the describer's time limit held the wait for its
// provider too, so a description queued behind another request had only
// what the queue left of it, and the image's turn failed. Queued for twice
// the limit, it is still described.
func TestVisionWaitIsntTheDescribersTime(t *testing.T) {
visionLimits(t, 300*time.Millisecond, 10*time.Second)
s, u := slowEyed(t, 0)
held := make(chan int, 1)
go func() {
code, _ := postAs(t, s, "", `{"model":"probe/eye","messages":[{"role":"user","content":"hold"}]}`)
held <- code
}()
// the held request has the provider's one slot
select {
case <-u.held:
case <-time.After(5 * time.Second):
t.Fatal("the held request never reached the provider")
}
time.AfterFunc(2*visionTimeout, u.open)
code, body := postAs(t, s, "", pastedImage)
if code != 200 {
t.Fatalf("a description that waited in its lane: %d %s", code, body)
}
if got := u.sent("text"); len(got) != 1 || !strings.Contains(got[0], "HELLO") {
t.Fatalf("text model was sent %v", got)
}
if c := <-held; c != 200 {
t.Fatalf("held request: %d", c)
}
}
// The limit still ends a describer that takes too long once asked, and the
// usage log says magpie stopped waiting, not that the agent canceled.
func TestVisionLimitEndsASlowDescriber(t *testing.T) {
visionLimits(t, 200*time.Millisecond, 10*time.Second)
s, _ := slowEyed(t, time.Second)
code, body := postAs(t, s, "", pastedImage)
if code != 502 || !strings.Contains(body, "no answer in 200ms") {
t.Fatalf("slow describer: %d %s", code, body)
}
var vision *usage.Record
for _, r := range usage.Load(time.Time{}) {
if r.Kind == "vision" {
vision = &r
}
}
if vision == nil {
t.Fatal("no vision request in the usage log")
}
if vision.Status != 499 || !strings.Contains(vision.Error, "magpie stopped waiting") || strings.Contains(vision.Error, "agent canceled") {
t.Fatalf("vision usage: %d %q", vision.Status, vision.Error)
}
}