Files
magpie/internal/gateway/codex_ended_early_test.go
T
yetone 07f4d46c52 gateway: a Codex reply that ends early, and any failed call, is written to app.log
wjh66 (Discord, Windows, v0.1.2696) saw Codex on a ChatGPT sign-in with
gpt-6.1-sol show "reconnecting 5/5" and then `error decoding response
body`, and app.log around 15:27 held nothing about it. That text is
reqwest's for any read error in a body (a reset, a chunk cut short, a
timeout), so it says the connection broke mid-reply, not which side broke
it. The cause is not found; this change only makes the next report carry
evidence.

- record logs a failed call (status >= 400 or an error) when debug is off,
  once a minute per agent/provider/model/status/error, counting repeats.
- codexUpstream logs a reply that ended before its last event (or a
  non-SSE body cut by a read or write error): bytes passed on, whether
  OpenAI's stream failed or ended, and whether Codex went away, a write
  to it failed, or it was told response.failed.
- Shutdown with requests in flight says they are cut (Windows has no
  handover, so a quit or update cuts streams after shortDrain).

Run at this head (rebased on 4bd0d6f38; the new and stream-cut tests and vet run again after the rebase on mote and Windows, the full gateway run on Linux was at 4e764fc39):
- win-run: TestCodexReplyBrokenOffLogged, TestCodexClientGoneLogged,
  TestFailedCallLineOncePerMinute, TestCodexStreamCut* - ok.
- wsl-run: go test -tags nogui ./internal/gateway -timeout 45m - one red,
  TestCodexPoolSaysTheClientsVersion, which is #4807 (red on main too; the
  identity it sees is the machine's own Codex 0.165.0).
- mote-run: gofmt -l clean; go vet -tags nogui ./internal/gateway for
  darwin, GOOS=linux and GOOS=windows - ok.

Broken by hand: both new log.Print calls replaced with `_ = line`; the
two Codex tests went red ("no log line holds ...").

Not tried: a real Codex on Windows, a real cut from chatgpt.com, the
serveAgent (magpie's Codex accounts) path specifically, the full suite
on macOS or Windows.

Reported by wjh66.
2026-11-02 17:10:02 +08:00

211 lines
6.9 KiB
Go

package gateway
import (
"bufio"
"bytes"
"io"
"log"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// lockedLog is the standard logger's output for a test, safe to read
// while the gateway's goroutines write it.
type lockedLog struct {
mu sync.Mutex
buf bytes.Buffer
}
func (l *lockedLog) Write(p []byte) (int, error) {
l.mu.Lock()
defer l.mu.Unlock()
return l.buf.Write(p)
}
func (l *lockedLog) String() string {
l.mu.Lock()
defer l.mu.Unlock()
return l.buf.String()
}
func catchLog(t *testing.T) *lockedLog {
t.Helper()
l := &lockedLog{}
old := log.Writer()
log.SetOutput(l)
t.Cleanup(func() { log.SetOutput(old) })
return l
}
// waitLogged waits for the log to have a line holding every one of want.
func waitLogged(t *testing.T, l *lockedLog, want ...string) string {
t.Helper()
deadline := time.Now().Add(20 * time.Second)
for {
for _, line := range strings.Split(l.String(), "\n") {
all := true
for _, w := range want {
all = all && strings.Contains(line, w)
}
if all {
return line
}
}
if time.Now().After(deadline) {
t.Fatalf("no log line holds %q; the log:\n%s", want, l.String())
}
time.Sleep(10 * time.Millisecond)
}
}
func codexUpstreamAt(t *testing.T, h http.HandlerFunc) {
t.Helper()
codexSignedIn(t)
up := httptest.NewServer(h)
t.Cleanup(up.Close)
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = was })
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-6.1-sol"}}); err != nil {
t.Fatal(err)
}
}
// wjh66 on Discord (0.1.2696, Windows): a Codex turn on gpt-6.1-sol showed
// "reconnecting 5/5", then "error decoding response body" — reqwest's
// words for a body whose connection broke while it was read — and app.log
// had nothing of the turn. The ChatGPT backend's connection broken off
// inside a chunk, as a reset does, reaches Codex as a whole HTTP reply
// whose stream says it failed (no broken body of magpie's for Codex to
// fail to decode), and app.log says which side ended it and how.
func TestCodexReplyBrokenOffLogged(t *testing.T) {
logs := catchLog(t)
codexUpstreamAt(t, func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
conn, rw, err := w.(http.Hijacker).Hijack()
if err != nil {
t.Error(err)
return
}
defer conn.Close()
events := sse(
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-6.1-sol"}}`,
`data: {"type":"response.output_text.delta","delta":"half a reply"}`)
// no Content-Type, as the ChatGPT backend streams; a chunk
// promised longer than what comes, and the connection closed
rw.WriteString("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n")
rw.WriteString(strconv.FormatInt(int64(len(events)+64), 16) + "\r\n" + events)
rw.Flush()
})
server := httptest.NewServer(New().Handler())
defer server.Close()
req, _ := http.NewRequest("POST", server.URL+CodexPath+"/responses",
strings.NewReader(`{"model":"gpt-6.1-sol","stream":true,"input":"hi"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "codex_cli_rs/0.160.0 (Windows 10.0.26100; x86_64)")
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatalf("magpie's reply broke off under the client (Codex: error decoding response body): %v\n%s", err, body)
}
if !strings.Contains(string(body), "half a reply") || !strings.Contains(string(body), "response.failed") {
t.Fatalf("the reply's delta and its failure weren't both passed on:\n%s", body)
}
line := waitLogged(t, logs, "gateway: codex:", "gpt-6.1-sol", "reading OpenAI's stream failed", "before its last event")
if !strings.Contains(line, "told it as response.failed") {
t.Fatalf("the line doesn't say what the client was told: %s", line)
}
waitLogged(t, logs, "gpt-6.1-sol", "failed: 502", "before its last event")
}
// The other side: the client going away mid-reply (Codex's own timeout, a
// proxy between them, a window closed) is logged as the client's end, so
// a report of a broken reply tells the leg it broke on.
func TestCodexClientGoneLogged(t *testing.T) {
logs := catchLog(t)
codexUpstreamAt(t, func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-6.1-sol"}}`,
`data: {"type":"response.output_text.delta","delta":"half a reply"}`))
w.(http.Flusher).Flush()
// the reply goes on until magpie lets go of it: a write to the
// client that left is how magpie learns it did
for {
select {
case <-r.Context().Done():
return
case <-time.After(20 * time.Millisecond):
}
io.WriteString(w, sse(`data: {"type":"response.output_text.delta","delta":" more"}`))
w.(http.Flusher).Flush()
}
})
server := httptest.NewServer(New().Handler())
defer server.Close()
tr := &http.Transport{}
defer tr.CloseIdleConnections()
req, _ := http.NewRequest("POST", server.URL+CodexPath+"/responses",
strings.NewReader(`{"model":"gpt-6.1-sol","stream":true,"input":"hi"}`))
req.Header.Set("Content-Type", "application/json")
res, err := (&http.Client{Transport: tr}).Do(req)
if err != nil {
t.Fatal(err)
}
br := bufio.NewReader(res.Body)
for {
l, err := br.ReadString('\n')
if err != nil {
t.Fatalf("the delta never came: %v", err)
}
if strings.Contains(l, "half a reply") {
break
}
}
res.Body.Close() // the connection with it: the client is gone
waitLogged(t, logs, "gateway: codex:", "gpt-6.1-sol", "before its last event", "went away")
}
// The same failure over and over is logged once a minute, with how many
// times it came in between.
func TestFailedCallLineOncePerMinute(t *testing.T) {
failedLog.Lock()
failedLog.last = nil
failedLog.Unlock()
c := Call{Agent: "codex", Model: "gpt-6.1-sol", Provider: "openai", Status: 502, Millis: 1200, Error: "OpenAI closed the stream"}
now := time.Date(2026, 11, 2, 15, 27, 0, 0, time.UTC)
line, ok := failedCallLine(c, now)
if !ok || !strings.Contains(line, "codex's call to gpt-6.1-sol on openai failed: 502 after 1.2s: OpenAI closed the stream") {
t.Fatalf("first failure: %v %q", ok, line)
}
for i := range 3 {
if _, ok := failedCallLine(c, now.Add(time.Duration(i+1)*time.Second)); ok {
t.Fatal("the same failure was logged again within the minute")
}
}
other := c
other.Status = 499
if _, ok := failedCallLine(other, now.Add(time.Second)); !ok {
t.Fatal("another failure was held with the first")
}
line, ok = failedCallLine(c, now.Add(time.Minute+time.Second))
if !ok || !strings.Contains(line, "and 3 times more like it") {
t.Fatalf("after the minute: %v %q", ok, line)
}
if _, ok := failedCallLine(Call{Status: 200, Model: "x"}, now); ok {
t.Fatal("a call that didn't fail was logged")
}
}