Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: a request held before routing is shown as its caller, read once as it comes

yetone9901303d1f54
diff --git a/internal/gateway/unrouted.go b/internal/gateway/unrouted.go
index b1c53a316..30da52386 100644
--- a/internal/gateway/unrouted.go
+++ b/internal/gateway/unrouted.go
@@ -190,9 +190,13 @@ func (h *heldWriter) answer(proto provider.Protocol, status int, msg string) boo
// unroutedHold is one watched request's watch: the route that shows it
// while it waits, and what ends it.
type unroutedHold struct {
- s *Server
- r *http.Request
- f *inflight
+ s *Server
+ r *http.Request
+ f *inflight
+ // agent is who it is shown as (callerFrom), read as it comes: the
+ // timers never read its headers, which withCaller and decodedBody
+ // change meanwhile
+ agent string
w *heldWriter
cancel context.CancelCauseFunc
asked atomic.Value // string: the model it asked for, once read
@@ -403,7 +407,7 @@ func (s *Server) watchUnrouted(w http.ResponseWriter, r *http.Request, f *inflig
return w, r, func() {}
}
ctx, cancel := context.WithCancelCause(r.Context())
- u := &unroutedHold{s: s, f: f, w: newHeldWriter(w), cancel: cancel}
+ u := &unroutedHold{s: s, f: f, w: newHeldWriter(w), cancel: cancel, agent: callerFrom(r).agent}
r = r.WithContext(context.WithValue(ctx, unroutedKey{}, u))
if r.Body != nil && r.Body != http.NoBody {
r.Body = waitedBody{r.Body, u}
@@ -455,7 +459,7 @@ func (u *unroutedHold) show() {
// showLocked begins the route that shows it; f.mu is held.
func (u *unroutedHold) showLocked() {
asked, _ := u.asked.Load().(string)
- rt := Route{Time: u.f.start, Agent: callerOf(u.r).agent, Session: u.f.session, Model: asked, Provider: "magpie"}
+ rt := Route{Time: u.f.start, Agent: u.agent, Session: u.f.session, Model: asked, Provider: "magpie"}
if rt.Agent == "" {
rt.Agent = u.f.agent
}
diff --git a/internal/gateway/unrouted_test.go b/internal/gateway/unrouted_test.go
index 07ab240c1..88bc1f385 100644
--- a/internal/gateway/unrouted_test.go
+++ b/internal/gateway/unrouted_test.go
@@ -461,3 +461,61 @@ func TestUnroutedMostLeavesOutMasking(t *testing.T) {
t.Fatal("a request held after masking was never answered")
}
}
+
+// A request another magpie passed on is shown, while it waits to be
+// routed, as the agent that made it there (callerFrom), read once as it
+// comes: the show's timer never reads its headers, which withCaller and
+// decodedBody change meanwhile (#3956's ubuntu race run).
+func TestUnroutedShownAsItsCaller(t *testing.T) {
+ unroutedLimits(t, 20*time.Millisecond, time.Hour)
+ s, _ := slowEyed(t, 0)
+ srv := httptest.NewServer(s.Handler())
+ t.Cleanup(srv.Close)
+ pr, pw := io.Pipe()
+ t.Cleanup(func() { pw.Close() })
+ got := make(chan unroutedReply, 1)
+ go func() {
+ req, _ := http.NewRequest(http.MethodPost, srv.URL+"/v1/chat/completions", pr)
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("User-Agent", "magpie/0.1.0")
+ req.Header.Set(AgentHeader, "pi")
+ resp, err := (&http.Client{Timeout: 30 * time.Second}).Do(req)
+ if err != nil {
+ got <- unroutedReply{err: err}
+ return
+ }
+ b, _ := io.ReadAll(resp.Body)
+ resp.Body.Close()
+ got <- unroutedReply{resp.StatusCode, string(b), nil}
+ }()
+ if _, err := pw.Write([]byte(unroutedHi[:10])); err != nil {
+ t.Fatal(err)
+ }
+ u := watchedHold(t, s)
+ var agent string
+ for deadline := time.Now().Add(10 * time.Second); ; {
+ u.f.mu.Lock()
+ if u.shown != nil {
+ agent = u.shown.Agent
+ }
+ shown := u.shown != nil
+ u.f.mu.Unlock()
+ if shown {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatal("the held request was never shown")
+ }
+ time.Sleep(5 * time.Millisecond)
+ }
+ if agent != "pi" {
+ t.Errorf("shown as %q while held, want pi, the agent another magpie passed it on for", agent)
+ }
+ if _, err := pw.Write([]byte(unroutedHi[10:])); err != nil {
+ t.Fatal(err)
+ }
+ pw.Close()
+ if r := <-got; r.err != nil || r.code != http.StatusOK {
+ t.Fatalf("answered %d %q %v", r.code, r.body, r.err)
+ }
+}