Files
magpie/internal/gateway/codex_memory_test.go
T
ggbdpq 6de04a26e2 gateway: a Codex memory call on its own bare name follows the session when a provider serves a model under that name too (#4765)
raphael-y7: with magpie as Codex's model provider, the conversation on
orbis/gpt-6.1-sol, Codex's memory consolidation — its own background
request on its own default consolidation model, asked by its bare name
gpt-5.6-terra — went to workbuddy-ai, the only provider of magpie's with
a model under that name, and the account it landed on refused it: 400
"model [gpt-5.6-terra] is not allowed for user type
[personal_free_user]", three tries, 8.5s, and the memory was never
written. The bare name resolved, so codexMemoryStandIn (#742) left the
call where it stood; a model of Codex's own asked by its bare name is
not a pick of whichever provider has one under that name.

Now a memory call whose bare name resolves still follows the
conversation's model, as it does on the ChatGPT backend's path
(codexFollowTo), when Codex's config names no model for its memories:
codexMemoryStandIn asks codexMemorySession first and only falls through
to the name's own resolution when there is none to follow. What keeps
going as the name resolves: any other call, a model Codex's config
names (the user's own pick, a full id or a bare name), a config magpie
can't read, and a conversation magpie has no model of (on Codex's own,
or nothing answered yet).

What this doesn't change: the three tries on a capability refusal, and
the model catalog's exposure of a model an account can't serve — the
reporter's other two suggestions, both a bigger cut; the auto-review's
codex-auto-review and the app's title, description and summary calls,
whose as-Codex-sent default is deliberate (codexTitlesTo, codexSideTo;
shruru counts a provider serving a luna-named model picking titles up
as a feature, #4590).

TestCodexMemoryOnACollidingBareNameFollowsTheSession: a provider with a
model under gpt-5.6-terra, the conversation on another provider's — the
memory call followed the conversation and the named provider got
nothing (red before: it got the memory); the config's pick and a plain
turn on the name still go to it. Run (darwin arm64, -tags nogui):

- go test -tags nogui -run 'TestCodexMemoryOnACollidingBareNameFollowsTheSession|TestCodexMemoryCallsGoWhereItsTurnsGo|TestCodexMemoryStandInFollowsTheSwitch|TestCodexMemoriesFollowTheSession|TestCodexSideCallsKeepTheConfigsModel|TestCodexSideCallsBeforeTheFirstTurnFollowTheSwitch|TestCodexOwnModelOnMagpieThreadGoesOnSignIn' ./internal/gateway: ok (the new test was red before the fix, with the report's routing)

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 22:44:23 +08:00

147 lines
5.7 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// codexCall is a request Codex with magpie as its provider sends to
// /v1/responses: a turn of its own (source "") or a call of a kind of its.
func codexCall(t *testing.T, s *Server, model, source string) *httptest.ResponseRecorder {
t.Helper()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"`+model+`","stream":true,"input":"hi"}`))
req.Header.Set("Authorization", "Bearer "+TokenFor("codex"))
if source != "" {
req.Header.Set("x-codex-turn-metadata", `{"thread_source":"`+source+`"}`)
}
s.Handler().ServeHTTP(rec, req)
return rec
}
// Codex's memory calls come on models of its own by their bare names
// (gpt-5.6-terra, gpt-5.6-luna), whatever Codex is set to (#742,
// congee949: every one was a 404 "magpie knows no model"). They go to the
// model Codex's own turns go to — the last one answered, or the ledger's
// after a restart — and are recorded as the memory calls they are. Any
// other call on a model magpie doesn't know is still turned away.
func TestCodexMemoryCallsGoWhereItsTurnsGo(t *testing.T) {
f := &fake{t: t}
setup(t, provider.Chat, f)
reply := func() string {
return sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":"ok"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`,
`data: [DONE]`)
}
s := New()
// no turn of Codex's answered yet: nothing to go to
if rec := codexCall(t, s, "gpt-5.6-terra", "memory_consolidation"); rec.Code != 404 {
t.Fatalf("a memory call before any turn: %d %q", rec.Code, rec.Body.String())
}
f.reply = reply()
if rec := codexCall(t, s, "fake/m1", ""); rec.Code != 200 {
t.Fatalf("a turn: %d %q", rec.Code, rec.Body.String())
}
for _, c := range []struct{ model, source string }{
{"gpt-5.6-terra", "memory_consolidation"},
{"gpt-5.6-luna", "memgen"},
} {
f.reply = reply()
calls := f.calls
if rec := codexCall(t, s, c.model, c.source); rec.Code != 200 || f.calls != calls+1 {
t.Fatalf("%s on %s: %d %q", c.source, c.model, rec.Code, rec.Body.String())
}
var sent struct {
Model string `json:"model"`
}
json.Unmarshal(f.got, &sent)
if sent.Model != "m1" {
t.Errorf("%s: the vendor was asked for %q", c.source, sent.Model)
}
}
var last usage.Record
for _, u := range usage.Load(time.Time{}) {
last = u
}
if usage.PurposeOf(last.Kind) != "kind:memory_consolidation" || last.Provider != "fake" || last.Failed() {
t.Errorf("the ledger's memory call %+v", last)
}
// magpie restarted: the ledger has the turn
f.reply = reply()
if rec := codexCall(t, New(), "gpt-5.6-terra", "memory_consolidation"); rec.Code != 200 {
t.Fatalf("after a restart: %d %q", rec.Code, rec.Body.String())
}
// any other call on a model magpie doesn't know is turned away as before
if rec := codexCall(t, s, "gpt-5.6-terra", ""); rec.Code != 404 {
t.Fatalf("a turn on an unknown model: %d", rec.Code)
}
if rec := codexCall(t, s, "gpt-5.6-terra", "review"); rec.Code != 404 {
t.Fatalf("a review on an unknown model: %d", rec.Code)
}
}
// #4765 (raphael-y7): Codex's memory consolidation asks for its own bare
// default (gpt-5.6-terra), and a provider of magpie's happened to serve a
// model under that same name: the call went to it — the only one with the
// name — and its account refused the model (400, three tries, the memory
// lost) while the conversation ran on another provider's model. With
// Codex's config naming no model for its memories, the call follows the
// conversation's, as it does on the ChatGPT backend's path (codexFollowTo);
// a model Codex's config names, and a turn of Codex's on the name, go as
// they came.
func TestCodexMemoryOnACollidingBareNameFollowsTheSession(t *testing.T) {
fresh(t)
relay := &relayOfChatGPT{}
wb := httptest.NewServer(relay)
t.Cleanup(wb.Close)
if err := provider.Save(provider.Provider{ID: "wb", Name: "WB", Key: "sk-wb", Models: []string{"gpt-5.6-terra"}, Responses: wb.URL + "/v1"}); err != nil {
t.Fatal(err)
}
session := relayOn(t, "third")
was := CodexSideSet
CodexSideSet = func(string) bool { return false }
t.Cleanup(func() { CodexSideSet = was })
s := New()
if rec := codexCall(t, s, "third/gpt-6-astra", ""); rec.Code != 200 {
t.Fatalf("the turn: %d %s", rec.Code, rec.Body.String())
}
if rec := codexCall(t, s, "gpt-5.6-terra", "memory_consolidation"); rec.Code != 200 {
t.Fatalf("memory: %d %s", rec.Code, rec.Body.String())
}
if reqs := relay.requests(); len(reqs) != 0 {
t.Fatalf("the provider with the name got the memory (%d requests)", len(reqs))
}
reqs := session.requests()
if len(reqs) != 2 || modelOf([]byte(reqs[1])) != "gpt-6-astra" {
t.Fatalf("the session's provider got %d requests, the last on %q", len(reqs), modelOf([]byte(reqs[len(reqs)-1])))
}
// a model Codex's config names is the user's own pick: as it came
CodexSideSet = func(string) bool { return true }
if rec := codexCall(t, s, "gpt-5.6-terra", "memory_consolidation"); rec.Code != 200 {
t.Fatalf("memory on the config's model: %d %s", rec.Code, rec.Body.String())
}
if reqs := relay.requests(); len(reqs) != 1 || modelOf([]byte(reqs[0])) != "gpt-5.6-terra" {
t.Fatalf("the provider with the name got %d requests on the config's pick", len(reqs))
}
// a turn of Codex's on the name is the user's own pick of it: as it resolves
if rec := codexCall(t, s, "gpt-5.6-terra", ""); rec.Code != 200 {
t.Fatalf("a turn on the name: %d %s", rec.Code, rec.Body.String())
}
if reqs := relay.requests(); len(reqs) != 2 {
t.Fatalf("the provider with the name got %d requests, the turn among them", len(reqs))
}
}