Codex's summary ask to a provider that isn't OpenAI's carries its developer messages as system ones (forVendor, #2955). A plugin provider fronting a ChatGPT account (magpie-sub2api -> sub2api -> Codex backend) is such a provider, and the backend behind it takes developer alone: every compaction came back "System messages are not allowed", the sessions that got through fell to the conversation-as-text summary, and the ones whose refusal came as an error event after a 200 skipped every retry and were held after three (compactFails). Now a summary refused "System messages are not allowed" (as passed through, or as a current sub2api words it), or with a relay's bare "Upstream request failed" (the reporter's sub2api), as a 4xx or as an error event in the stream (roleRefused), is asked once more with the roles as Codex sent them (askAsSent turns off the compaction marker, whose only reader is forVendor's swap), then as text, then made by magpie. A summary with no developer message to give back is not asked the same twice. Every route a compaction reaches a Responses provider by (CodexPath, Codex's own models through magpie's accounts, the mask route, a CC Switch table) goes through codexCompact and serve's one forVendor call, so all of them are covered. #2955's chat-only vendors take system and are unchanged. Ported from ggbdpq's patch (fix/5067-compaction-system-messages, 79ec15d26), which matched only the passed-through wording on a 400. The review added the reporter's own "Upstream request failed", the error-event shape, the developer gate, lazyre for the words, and the subsystem page. Run on mote at this change on 6a253f8af; rebased ontofbc1808f2(then15acba534, no gateway change) and the targeted run repeated on Linux (wsl-run, mote refused ssh): ok. - TestCodexCompactSystemRefusedAsksAgainDeveloper (5 wordings/shapes) and TestCodexCompactSystemRefusedEverywhereNeverHolds: ok. - Red on origin/main's product files: all 7 subtests fail (400 to Codex, 502 "so there is no summary", or the text downgrade). - Red on ggbdpq's patch alone: older sub2api, both in-stream shapes and the in-stream never-holds case fail. - Broken by hand: the developer gate dropped -> the existing TestCodexCompactUpstreamFailed400AsksAgainAsText goes red (3 asks). - go test -tags nogui ./internal/gateway/: queued behind ~/suite.lock on mote at push time (other sessions' suites); the full suite runs after the tag - gofmt -l clean; go vet darwin, linux -tags nogui, windows: clean. Not tried: a real sub2api relay of a ChatGPT account; the fakes answer sub2api's two wordings (writeOpenAIUpstreamClientError and the older upstream_error) and the backend's {"detail":...}. After an in-stream "Upstream request failed" serve itself asks the same account again (15 asks in the fake), unchanged here. The reporter's other 400 (reasoning effort 'none' on gpt-6.1-sol) is a separate path, not touched. Co-authored-by: ggbdpq <ggbdpq@gmail.com>
176 lines
7.1 KiB
Go
176 lines
7.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// chatgptSystem400 is what the ChatGPT backend answers a request carrying
|
|
// the system role with, passed through the relay above it unchanged
|
|
// (jian-cui's sub2api error log, #5067).
|
|
const chatgptSystem400 = `{"detail":"System messages are not allowed"}`
|
|
|
|
// sub2apiSystem400 is the same refusal as a current sub2api words it to
|
|
// its client (writeOpenAIUpstreamClientError: the upstream's detail as the
|
|
// message, the type invalid_request_error), and sub2apiOld400 as the
|
|
// reporter's sub2api did, every refusal "Upstream request failed"
|
|
// (app.log: "answered 400 (sub2api: Upstream request failed)").
|
|
const (
|
|
sub2apiSystem400 = `{"error":{"type":"invalid_request_error","message":"System messages are not allowed"}}`
|
|
sub2apiOld400 = `{"error":{"type":"upstream_error","message":"Upstream request failed"}}`
|
|
)
|
|
|
|
// systemCompaction is a Codex compaction whose history begins with Codex's
|
|
// developer message, as every one of Codex's does.
|
|
const systemCompaction = `{"model":"fake/m1","stream":true,"store":false,"prompt_cache_key":"thread-5067",
|
|
"input":[{"type":"message","role":"developer","content":[{"type":"input_text","text":"You are Codex, a coding agent."}]},
|
|
{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the login bug"}]},
|
|
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"I fixed main.go"}]},
|
|
{"type":"compaction_trigger"}]}`
|
|
|
|
var summaryReply = sse(
|
|
`data: {"type":"response.created","response":{"id":"resp_v1","status":"in_progress","output":[]}}`,
|
|
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"SUMMARY"}]}}`,
|
|
`data: {"type":"response.completed","response":{"id":"resp_v1","status":"completed","output":[]}}`)
|
|
|
|
// codexRelay puts the fake provider on a relay of a ChatGPT account: a
|
|
// body carrying the system role is answered with refusal, anything else
|
|
// is summarised. It records each body it was sent.
|
|
func codexRelay(t *testing.T, refusal func(w http.ResponseWriter)) *[]string {
|
|
t.Helper()
|
|
setup(t, provider.Responses, &fake{t: t})
|
|
var mu sync.Mutex
|
|
got := &[]string{}
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
mu.Lock()
|
|
*got = append(*got, string(b))
|
|
mu.Unlock()
|
|
if strings.Contains(string(b), `"role":"system"`) {
|
|
refusal(w)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, summaryReply)
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
p := provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Responses: up.URL + "/v1"}
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
func refuseWith(status int, body string) func(http.ResponseWriter) {
|
|
return func(w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
io.WriteString(w, body)
|
|
}
|
|
}
|
|
|
|
func errorEvent(typ, msg string) func(http.ResponseWriter) {
|
|
return func(w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, "event: error\ndata: {\"type\":\"error\",\"error\":{\"type\":\""+typ+"\",\"message\":\""+msg+"\"}}\n\n")
|
|
}
|
|
}
|
|
|
|
// #5067: Codex on a plugin provider whose Responses endpoint is a relay
|
|
// of a ChatGPT account: the summary ask went out with its developer
|
|
// messages as system ones (forVendor, #2955) and the backend behind the
|
|
// relay refused every one, 400 "System messages are not allowed", three
|
|
// of the failures holding the conversation (compactFails) — while the
|
|
// turns around it, developer as they came, are taken. The summary is
|
|
// asked again with the roles as the client sent them, and the backend
|
|
// takes it the way it takes the turns: the model's summary of the
|
|
// conversation's items, not of it as text. Each wording a relay passes
|
|
// the refusal on in, and an error event after a 200, which had skipped
|
|
// every retry and counted toward the guard.
|
|
func TestCodexCompactSystemRefusedAsksAgainDeveloper(t *testing.T) {
|
|
for name, refusal := range map[string]func(http.ResponseWriter){
|
|
"passed through": refuseWith(400, chatgptSystem400),
|
|
"sub2api": refuseWith(400, sub2apiSystem400),
|
|
"older sub2api": refuseWith(400, sub2apiOld400),
|
|
"in the stream": errorEvent("invalid_request_error", "System messages are not allowed"),
|
|
"older sub2api in the stream": errorEvent("upstream_error", "Upstream request failed"),
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
got := codexRelay(t, refusal)
|
|
code, body := codexPost(t, systemCompaction)
|
|
if code != 200 || !strings.Contains(body, `"compaction"`) {
|
|
t.Fatalf("%d %s\nupstream got %q", code, body, *got)
|
|
}
|
|
if sum := compactedText(t, body); sum != "SUMMARY" {
|
|
t.Errorf("summary %q", sum)
|
|
}
|
|
// serve asks again itself after an "Upstream request failed"
|
|
// in a stream, as after any reply broken off: the last ask is
|
|
// the one taken
|
|
if len(*got) < 2 || !strings.Contains(name, "older sub2api in") && len(*got) != 2 {
|
|
t.Fatalf("asks: %d %q", len(*got), *got)
|
|
}
|
|
last := (*got)[len(*got)-1]
|
|
if strings.Contains(last, `"role":"system"`) || !strings.Contains(last, `"role":"developer"`) ||
|
|
strings.Contains(last, "This is the conversation so far") {
|
|
t.Errorf("asked again with: %s", last)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Refused as sent too, the summary is asked as text, and made by magpie
|
|
// when that is refused as well: compaction after compaction goes on, and
|
|
// the conversation is never held (compactFails). An error event after a
|
|
// 200 the same.
|
|
func TestCodexCompactSystemRefusedEverywhereNeverHolds(t *testing.T) {
|
|
for name, refusal := range map[string]func(http.ResponseWriter){
|
|
"status": refuseWith(400, sub2apiSystem400),
|
|
"in stream": errorEvent("upstream_error", "Upstream request failed"),
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
setup(t, provider.Responses, &fake{t: t})
|
|
var mu sync.Mutex
|
|
calls := 0
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
mu.Lock()
|
|
calls++
|
|
mu.Unlock()
|
|
refusal(w)
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Responses: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
for i := range compactFailsMost + 1 {
|
|
mu.Lock()
|
|
calls = 0
|
|
mu.Unlock()
|
|
rec := codexPostTo(s, systemCompaction)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("compaction %d: %d %s", i+1, rec.Code, rec.Body)
|
|
}
|
|
if sum := compactedText(t, rec.Body.String()); !strings.Contains(sum, "fix the login bug") {
|
|
t.Errorf("compaction %d: local summary %s", i+1, sum)
|
|
}
|
|
// as it came (system), as sent (developer), as text; serve
|
|
// asks each again itself after a stream broken off
|
|
mu.Lock()
|
|
n := calls
|
|
mu.Unlock()
|
|
if name == "status" && n != 3 || n < 3 {
|
|
t.Errorf("compaction %d: asks %d", i+1, n)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|