heng91's relay was unreachable while their proxy was off; magpie answered the relay's 503 with its short same-one retry (or a group's replan), and they closed the Claude Code / Codex window in the pause. The gateway already stopped there: the pause selects on the request's context, the route ended with 499 and the upstream was asked nothing more. But the trace never said so: the last try kept only its Again, so the Routing page's story ended on "... it is tried again in 2.0 s" and the list said "499 · failed", which reads as the closed session still retrying. - gateway: the three pauses (the last one left asked again, the same after spentAfter, and the group's replan through pauseAlive) mark the try whose pause it was `Gone` when the agent leaves in it. - Routing page: such a try says the agent canceled the request before the retry and nobody is asked again (en, zh, zh-TW, ja, de); the request list and the outcome label say canceled; the replay's dot goes home. - docs/subsystems/gateway-routing.md says so. Reproduced first with a fake upstream (httptest) answering 503 and one hanging, a real gateway server and a client that disconnects: single provider, group replan, streamed and not, hang mid-try. In every case the route ended at once with nothing in flight and the upstream saw no further request (the hanging one saw its request canceled); only the trace's last line was wrong. The plugin fetch path already sends the host an abort on cancel (host.js "abort"). Run: - TestAgentGoneInTheRetryPause (new, -race): agent leaves in the pause, single and group; upstream count stays, route done 499, no try in flight, last try Gone. Red with the Gone marking removed (Gone false), and red with the pauses ignoring the context (route not done). - routing-agent-gone.test.cjs (new): Chromium and WebKit, en/zh/ja/de, 1100 and 420 wide, 16/16; red (0/4) with the story and label change reverted. - go test -tags nogui ./internal/gateway ./internal/gui; routing-*, rest-try-now and ledger-retried GUI tests on both engines; gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui on both packages. Not tried: the reporter's own relay and proxy, or a real Claude Code / Codex window closed mid-pause. The one other red in that GUI run, routing-wb-restricted at 420 in Chromium under the full run (passes alone 3/3), is filed as #1905.
123 lines
4.1 KiB
Go
123 lines
4.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// unreachable answers every request as a relay does that can't reach its
|
|
// own upstream (heng91 on #1894: a relay behind a proxy that was off).
|
|
type unreachable struct{ n atomic.Int32 }
|
|
|
|
func (u *unreachable) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
io.ReadAll(r.Body)
|
|
u.n.Add(1)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
io.WriteString(w, `{"error":{"type":"api_error","message":"upstream connect error or disconnect/reset before headers"}}`)
|
|
}
|
|
|
|
// An agent closed while magpie waits to ask a failing upstream again (#1894,
|
|
// heng91: the Claude Code or Codex window closed while its relay was down)
|
|
// ends the request there: the upstream is asked nothing more, the route is
|
|
// over with nothing in flight, and its last try says the agent left in the
|
|
// pause, so the Routing page doesn't end its story on a retry that never
|
|
// came. Both pauses: the last one left asked again, and a group's members
|
|
// asked again in turn (pauseAlive).
|
|
func TestAgentGoneInTheRetryPause(t *testing.T) {
|
|
for _, group := range []bool{false, true} {
|
|
name := "last one left"
|
|
if group {
|
|
name = "group replan"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
fresh(t)
|
|
old := retryPause
|
|
retryPause = 2 * time.Second // the pause the agent leaves in
|
|
t.Cleanup(func() { retryPause = old })
|
|
up := &unreachable{}
|
|
srv := httptest.NewServer(up)
|
|
t.Cleanup(srv.Close)
|
|
model := "a/m"
|
|
for _, id := range []string{"a", "b"} {
|
|
if err := provider.Save(provider.Provider{ID: id, Name: strings.ToUpper(id), Key: "k", Models: []string{"m"}, Anthropic: srv.URL}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
want := int32(1)
|
|
if group {
|
|
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
model, want = "group/g", 2
|
|
}
|
|
s := New()
|
|
gw := httptest.NewServer(s.Handler())
|
|
t.Cleanup(gw.Close)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
req, _ := http.NewRequestWithContext(ctx, "POST", gw.URL+"/v1/messages",
|
|
strings.NewReader(`{"model":"`+model+`","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
ended := make(chan struct{})
|
|
go func() {
|
|
defer close(ended)
|
|
if res, err := http.DefaultClient.Do(req); err == nil {
|
|
io.Copy(io.Discard, res.Body)
|
|
res.Body.Close()
|
|
}
|
|
}()
|
|
// every candidate has failed once: magpie is in its pause
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for up.n.Load() < want && time.Now().Before(deadline) {
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
if up.n.Load() != want {
|
|
t.Fatalf("upstream asked %d times before the pause, want %d", up.n.Load(), want)
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
cancel() // the agent's window is closed
|
|
<-ended
|
|
// the route ends at once, not when the pause would have
|
|
var r Route
|
|
for wait := time.Now().Add(time.Second); ; time.Sleep(5 * time.Millisecond) {
|
|
s.trace.mu.Lock()
|
|
r = *s.trace.routes[len(s.trace.routes)-1]
|
|
r.Tries = append([]Try{}, r.Tries...)
|
|
s.trace.mu.Unlock()
|
|
if r.Done || time.Now().After(wait) {
|
|
break
|
|
}
|
|
}
|
|
// and nobody is asked again once the pause has gone by
|
|
time.Sleep(3 * retryPause)
|
|
if n := up.n.Load(); n != want {
|
|
t.Errorf("upstream asked %d times after the agent left, want %d", n, want)
|
|
}
|
|
if !r.Done || r.Status != 499 {
|
|
t.Fatalf("route done %v status %d, want done 499", r.Done, r.Status)
|
|
}
|
|
for _, tr := range r.Tries {
|
|
if !tr.Done {
|
|
t.Errorf("a try still in flight: %+v", tr)
|
|
}
|
|
}
|
|
last := r.Tries[len(r.Tries)-1]
|
|
if last.Again == 0 || !last.Gone {
|
|
t.Errorf("last try %+v: want its pause and that the agent left in it", last)
|
|
}
|
|
if group && last.Replan == 0 {
|
|
t.Errorf("last try %+v: want the group's replan", last)
|
|
}
|
|
})
|
|
}
|
|
}
|