The ChatGPT backend cut every relayed stream about two minutes into the reply (121-125s, deltas flowing to the end): no response.completed, no error event, no usage, and Codex read a bare EOF, "stream disconnected before completion". Nothing in magpie bounds a stream that way — the gateway's client sets no Timeout, the upstream transport's only waits are ResponseHeaderTimeout (10 minutes, to the headers) and IdleConnTimeout (idle connections alone), the server sets no WriteTimeout, and the body's read deadline is dropped once the body is read — so the relay passed the cut on as it came, and was silent about it: the client's stream just ended, and the usage row recorded a clean 200 with nothing of why. Now a relayed stream that ends mid-reply — SSE, no failure said, no last event reached, the client still there — is answered with the stream's response.failed saying how long it had gone and that the reply is lost, and the row records the 502 with it. What magpie cannot do is keep the upstream talking past its cut: the wall itself stands on the magpie-to-chatgpt.com leg (the reporter's direct Codex ran 480s and 719s on the same machine and network), and telling it apart is what the honest failure is for. TestCodexStreamCutToldTheClient fails without the change: the fake upstream ends its stream after one delta, and the client heard no failure and the row said 200. go vet clean; go test -tags nogui ./internal/gateway/ passes in full. Landing record (yetone): Run at the pushed batch head (tree a6b084d0b, on origin/main616405061), go1.26.8: - go test -count=1 -timeout 45m on Linux (wsl-run) of internal/gateway, internal/redact, internal/provider and internal/plugin (this batch's packages and those main changed since the full run): all ok. gofmt -l clean; go vet ./... (darwin), GOOS=linux and GOOS=windows go vet -tags nogui ./... clean. internal/gui on macOS (mote-run): TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners (#3949), both red at main too. - The full go test ./... -timeout 45m -count=1 ran one rebase earlier, at tree 66a7a4c17 on origin/main3a3bdb9f2(main then gained 5bfa9f076's group stand-in,b19d08593,f856d381aand616405061). It ran on Linux (wsl-run): 82 packages ok. internal/agent has 4 reds, all red at origin/main on that box as well: TestInstalledVersion, TestGrokInstalled, TestOmpDetectedWithPiDir and TestCopilotFolderFromAnIDEPluginIsNotTheCLI (#4033, #3548). The root package and internal/cli need gtk4 to build, so they ran on macOS (mote-run): ok. TestColdTurnsTaken (#4510) was red there once. An earlier full run at based307aa648was red once in TestUnroutedHeldReportSaved (#4300), which passes 5 of 5 alone at both the head and main. - Each landed patch's diff matches the head ggbdpq posted last on its issue (patch-id). - No GUI file is touched, so no GUI test file was run. Broken by hand: gua's (TestCodexStreamCutToldTheClient red without the change). The mid-line case is broken by hand in this batch's #3377 follow-up. Not tried: a real two-minute cut from chatgpt.com. magpie cannot keep the upstream talking past it; this only makes the client and the row say it happened. Codex's identity line and prompts are unchanged. Landing record (yetone): Run at tree 7d7955eb9 (this batch on origin/main3cfba2e37), go1.26.8: - go test -count=1 -timeout 45m on Linux (wsl-run) of internal/gateway, internal/redact, internal/provider, internal/agent and internal/usage (this batch's packages, plus those main changed since the full run): all ok except internal/agent's TestInstalledVersion, TestGrokInstalled and TestOmpDetectedWithPiDir, red at origin/main on that box too (#4033, #3548). gofmt -l clean; go vet ./... (darwin), GOOS=linux and GOOS=windows go vet -tags nogui ./... clean. On macOS (mote-run), the root package and internal/cli ok; internal/gui red in TestSettingsSaveKeepsWhatItDoesNotSend (#4514), TestMainWindowCorners (#3949) and TestColdTurnsTaken (#4510), the first two red at main too. - The full go test ./... -timeout 45m -count=1 ran two rebases earlier, at tree 66a7a4c17 on origin/main3a3bdb9f2, on Linux (wsl-run): 82 packages ok, internal/agent's 4 reds as above, plus TestCopilotFolderFromAnIDEPluginIsNotTheCLI (#4033, red at main). The root package, internal/cli and internal/gui need gtk4 to build, so they ran on macOS as above. An earlier full run at based307aa648was red once in TestUnroutedHeldReportSaved (#4300), which passes 5 of 5 alone at both the head and main. - Each landed patch's diff matches the head ggbdpq posted last on its issue (patch-id). - No GUI file is touched, so no GUI test file was run. - Rebased after these runs onto origin/mainc1457f2ee. Its two commits since3cfba2e37(3f2faef62gui,c1457f2eeagent) touch neither internal/gateway nor internal/redact, nor any file this batch changes, and were not run again here. Broken by hand: gua's (TestCodexStreamCutToldTheClient red without the change). The mid-line case is broken by hand in this batch's #3377 follow-up. Not tried: a real two-minute cut from chatgpt.com. magpie cannot keep the upstream talking past it; this only makes the client and the row say it happened. Codex's identity line and prompts are unchanged. Generated-by: GLM-5.3-Flash (ZCode)
75 lines
2.7 KiB
Go
75 lines
2.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// TestCodexStreamCutToldTheClient (#3377): the ChatGPT backend closed every
|
|
// relayed stream about two minutes into the reply, its deltas flowing to the
|
|
// end — no response.completed, no error event, no usage. Codex read a bare
|
|
// EOF, "stream disconnected before completion", and the row recorded a clean
|
|
// 200 that said nothing of why. A stream the upstream breaks mid-reply is
|
|
// told to the client still there as its failure, and the row says why.
|
|
func TestCodexStreamCutToldTheClient(t *testing.T) {
|
|
codexSignedIn(t)
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, sse(
|
|
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-6-astra"}}`,
|
|
`data: {"type":"response.output_text.delta","delta":"half a report"}`))
|
|
w.(http.Flusher).Flush()
|
|
// the handler returns with no terminal event: the stream ends the
|
|
// way the backend's cut streams did, mid-reply
|
|
}))
|
|
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-astra"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := httptest.NewServer(New().Handler())
|
|
defer server.Close()
|
|
req, _ := http.NewRequest("POST", server.URL+CodexPath+"/responses",
|
|
strings.NewReader(`{"model":"gpt-6-astra","stream":true,"input":"write a long report"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer res.Body.Close()
|
|
body, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
said := string(body)
|
|
delta, failed := strings.Index(said, "half a report"), strings.Index(said, "response.failed")
|
|
if delta < 0 {
|
|
t.Fatalf("the reply's deltas never reached the client:\n%s", said)
|
|
}
|
|
if failed < 0 || failed < delta || !strings.Contains(said, "before its last event") {
|
|
t.Fatalf("a stream cut mid-reply ended without its failure said after the deltas:\n%s", said)
|
|
}
|
|
seen := 0
|
|
usage.Visit(time.Time{}, func(r usage.Record) {
|
|
if r.Endpoint != CodexPath+"/responses" {
|
|
return
|
|
}
|
|
seen++
|
|
if r.Status != http.StatusBadGateway || !strings.Contains(r.Error, "before its last event") {
|
|
t.Fatalf("the row said %d %q, not why the reply was lost", r.Status, r.Error)
|
|
}
|
|
})
|
|
if seen != 1 {
|
|
t.Fatalf("the ledger kept %d rows for the turn", seen)
|
|
}
|
|
}
|