gateway: a resumed Codex subagent leaves a spawn model magpie no longer serves
jockyer on X: after removing a provider and switching Codex to another model, requests kept naming the removed provider's model. Codex's resume_agent asks for the lead's current model, and codexResumeTo put the subagent back on the model its thread was spawned on (codex-subagents.json, kept 30 days and across restarts) without asking whether that model was still served. Every resume answered 404 "magpie knows no model a/x". A spawn model of magpie's that is now unserved (provider removed or switched off, as standIn judges it) is no longer gone back to. The request goes on as asked, and that model is noted as the thread's model from then on. Other state checked after an in-process provider.Delete (the GUI's and CLI's path inside agent.Reseat), each with a probe test. None of these sent a request to the removed provider: - chat completions on its own ids and on bare shared ids, with and without session affinity; - /v1/responses with Codex headers, and /v1/messages with Claude Code headers, on the new model and on the old one; - the old one with StandIn wired, including claude-haiku-*; - a routing group with a member on it, with the catalog held or not; - a delete written by another process with no request in flight. Server-instance state was read: codexLeads, codexTurns and codexSubagentPick validate with Resolve or CodexSubagentUnserved. While one request holds the catalog without a break, another process's removal is routed past for up to about 1s (heldFor). That is filed as #5541 and not changed here. Run on mote (mote-run), at6f5d48ea0and again after the rebase on25b201eb6: - go test ./internal/gateway -count=1 -timeout 45m: ok (776s, at6f5d48ea0); - after the rebase: go test ./internal/gateway -run TestCodex: ok (49s); - go vet ./internal/gateway for darwin, linux and windows: ok; - gofmt -l: clean. Broken by hand: with codex_resume.go at the old code, TestCodexResumedSubagentLeavesARemovedProvider is red (404 on the first resume). Not tried: a real Codex app resuming a subagent after a real provider removal. The reporter's "until the gateway restarted" was not reproduced. A restart doesn't clear codex-subagents.json, so this fix may not be their whole case. Settings' own model picks (CodexTitles, CodexDescriptions and the like) that name a removed provider were not checked.
This commit is contained in:
@@ -105,7 +105,30 @@ func (s *Server) codexResumeTo(r *http.Request, agent, kind, asked string, meta
|
||||
if thread == "" {
|
||||
return ""
|
||||
}
|
||||
return s.codexSpawns.keep(thread, asked)
|
||||
to := s.codexSpawns.keep(thread, asked)
|
||||
// a spawn model of magpie's it no longer serves (its provider removed
|
||||
// or switched off) is not gone back to: the subagent was sent there,
|
||||
// to a 404, after the user had removed the provider and moved Codex
|
||||
// to another model (jockyer on X). It goes on as asked, and that is
|
||||
// its model from now on.
|
||||
if strings.Contains(to, "/") && unserved(to) {
|
||||
s.codexSpawns.moved(thread, to, asked)
|
||||
return ""
|
||||
}
|
||||
return to
|
||||
}
|
||||
|
||||
// moved notes asked as thread's model in place of from, the model it was
|
||||
// spawned on, which magpie no longer serves.
|
||||
func (c *codexSpawns) moved(thread, from, asked string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if v, ok := c.m[thread]; !ok || v.Model != from {
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
c.m[thread] = codexSpawn{Model: asked, At: now}
|
||||
c.save(c.from, now)
|
||||
}
|
||||
|
||||
// keep notes asked as thread's model when thread is new, and says the
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
@@ -98,3 +101,57 @@ func TestCodexSubagentThreadOf(t *testing.T) {
|
||||
t.Fatalf("thread_id: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// jockyer on X: a provider removed, Codex moved to another model, and a
|
||||
// subagent spawned on the removed provider's model was still put back on
|
||||
// it when resumed, a 404 each time. A spawn model magpie no longer serves
|
||||
// is not gone back to: the subagent goes on as asked, and stays there,
|
||||
// after a restart too.
|
||||
func TestCodexResumedSubagentLeavesARemovedProvider(t *testing.T) {
|
||||
up := codexSubagentSetup(t)
|
||||
a, b := &askedUp{name: "a"}, &askedUp{name: "b"}
|
||||
serveOn(t, "a", "ka", []string{"x"}, a)
|
||||
serveOn(t, "b", "kb", []string{"y"}, b)
|
||||
s := New()
|
||||
for _, st := range []struct{ thread, parent string }{{"root-1", ""}, {"worker-1", "root-1"}} {
|
||||
if rec := codexThreadAsks(s, "a/x", st.thread, st.parent); rec.Code != 200 {
|
||||
t.Fatalf("%s on a/x: %d %s", st.thread, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
if err := provider.Delete("a"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
na := a.n()
|
||||
for i, srv := range []*Server{s, s, New()} {
|
||||
if rec := codexThreadAsks(srv, "b/y", "worker-1", "root-1"); rec.Code != 200 {
|
||||
t.Fatalf("resume %d after a was removed: %d %s", i, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
if a.n() != na || b.n() != 3 {
|
||||
t.Fatalf("resumed subagent went to a %v, b %v", a.tried[na:], b.tried)
|
||||
}
|
||||
if got := up.asked(); len(got) != 0 {
|
||||
t.Fatalf("went to ChatGPT: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// askedUp is an OpenAI-compatible provider that notes each model asked.
|
||||
type askedUp struct {
|
||||
mu sync.Mutex
|
||||
name string
|
||||
tried []string
|
||||
}
|
||||
|
||||
func (u *askedUp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
u.mu.Lock()
|
||||
u.tried = append(u.tried, modelOf(b))
|
||||
u.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
io.WriteString(w, sse(
|
||||
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"from `+u.name+`"}}]}`,
|
||||
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}`,
|
||||
`data: [DONE]`))
|
||||
}
|
||||
|
||||
func (u *askedUp) n() int { u.mu.Lock(); defer u.mu.Unlock(); return len(u.tried) }
|
||||
|
||||
Reference in New Issue
Block a user