Files
magpie/internal/gateway/side_deadline.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

74 lines
2.4 KiB
Go

package gateway
import (
"context"
"errors"
"net/http/httptrace"
"sync"
"time"
)
// sideTimedOut is why a request magpie asks of itself (an image's
// description, a search) ended when its own time limit did: not the agent
// leaving, which the usage log said it was ("the agent canceled the
// request", #2318).
type sideTimedOut struct {
what string
after time.Duration
}
func (e sideTimedOut) Error() string {
return "no answer in " + e.after.String()
}
// sideDeadline bounds a request magpie asks of itself through the gateway
// (s.serve). The vendor has d from when the request last went out to it —
// each try's own d, a try after a failure starting it again — and the whole
// of it, with the waits before a try (a lane's slot, room in the minute, a
// provider resting or paused between tries), most. A 2-minute limit on the
// whole left a reasoning model describing a dense screenshot only what the
// queue and the pauses hadn't spent, and the image's turn failed (#2318).
// The context's cause is a sideTimedOut once either limit ends it.
func sideDeadline(ctx context.Context, what string, d, most time.Duration) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancelCause(ctx)
whole := time.AfterFunc(most, func() { cancel(sideTimedOut{what, most}) })
var mu sync.Mutex
var try *time.Timer
ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
WroteRequest: func(httptrace.WroteRequestInfo) {
mu.Lock()
defer mu.Unlock()
if try != nil {
try.Stop()
}
try = time.AfterFunc(d, func() { cancel(sideTimedOut{what, d}) })
}})
return ctx, func() {
whole.Stop()
mu.Lock()
if try != nil {
try.Stop()
}
mu.Unlock()
cancel(context.Canceled)
}
}
// sideTimeout is the limit that ended ctx, if one of sideDeadline's did.
func sideTimeout(ctx context.Context) (sideTimedOut, bool) {
var t sideTimedOut
ok := errors.As(context.Cause(ctx), &t)
return t, ok
}
// canceled is what the usage log says of a request whose context ended
// before it was answered: the agent canceled it, or magpie's own limit for
// a request it asked of itself ran out — still no fault of the vendor's,
// told apart (#2318).
func canceled(ctx context.Context) (int, string) {
if t, ok := sideTimeout(ctx); ok {
return 499, "magpie stopped waiting: its " + t.what + " had " + t.Error()
}
return 499, "the agent canceled the request"
}