gateway: the TTFT and reasoning speed tests wait until the agent has each event (#5948)
TestGroupFailoverTimesEachTry went red on a loaded box with TTFT equal
to FirstText: "ttft_test.go:260: the answering try: {... Millis:304
TTFT:233 FirstText:233 ...}". The paced vendor kept its reasoning and
its text 80ms apart on the wire, and a gateway that read late got both
in one read and timed them as one. TestUsageKeepsTimeToFirstToken had
the same gap and was fixed for #2575 with paced's heard hook: the gap
before each marked event starts only once the agent has been sent the
one before. The failover test now posts with that hearing agent and
gives the answering vendor its heard, so the text comes a gap after the
agent had the reasoning, and the end a gap after it had the text.
The same gap stands under four more tests, which get the same hook.
TestCodexBackendTimesFirstToken, in the same file, checks u.Millis >
u.FirstText across the text and the end (its request is now built by
codexServe, which codexPost uses too). In reasoning_speed_test.go,
TestReasoningSpeedThroughGateway and TestSpeedEndsWithTheReply need
their thoughts read apart, 100ms at least (TestReasoningPacingEdges),
for a speed; the fixture's gap is 150ms. TestSpeedEndsWithTheReply's
first thought is now marked too, so the gap after it is guarded as
well (a first marked event is sent at once). In
reasoning_uncounted_test.go, TestStreamedReasoningWithoutCount's Chat
cases, paced 80ms apart, get it for their three thoughts; its
Anthropic cases replay DeepSeek's 33 deltas and are left as they were.
What was run (tree d6c1100b, on origin/main db0f2f20, go1.26.8,
linux/amd64, Linux 5.15), outside graftcp, umask 077 (these packages
don't run Bun; Bun 1.4.2 on PATH for the final runs, 1.3.11 for some
old-code runs):
- The five tests with the change, -count=5: ok. None waited out
heard's 10s.
- With 240ms slept by hand in holdWriter.Write on the vendor's first
event (fallback.go, a gateway passing it on late): the old failover
test red 3 of 3 at :260, "TTFT:242 FirstText:242".
- With 240ms slept by hand in the Codex backend's read loop once the
reasoning came (codex_backend.go): the old Codex test red 3 of 3 at
:337, "Millis:322 TTFT:322 FirstText:322". The new one: ok 3 of 3.
- With 400ms slept by hand in holdWriter.Write on the first thought:
the old TestReasoningSpeedThroughGateway red 2 of 2 at :76 on each of
its paths ("streamed reasoning missing"), and the old
TestSpeedEndsWithTheReply 2 of 2 at :133 ("a reply that came at its
pace tells no speed: 0/0").
- With 400ms slept by hand in holdWriter.Write on the first thought
("We..."): the old TestStreamedReasoningWithoutCount red 2 of 2 in
its "chat tool, counted" case at :94 ("speed 0/0 ms; want
told=true"), its other cases ok. With the same holds, the new
failover, the two reasoning speed tests and this one: ok 2 of 2 each.
None of these reds was reproduced without an injected delay.
- go test -count=1 -timeout 45m -v ./internal/gateway: 4009 tests ok
(15664 with subtests), 14 skipped, red in two,
TestClientPathCoversHandler and TestReadOnlyKeyEveryRoute (the CA
route /magpie-ca.crt in neither route list), red on origin/main
db0f2f20 too (#5993).
- gofmt -l clean. go vet -tags nogui for linux and windows, go vet for
windows, go build -tags nogui for linux and darwin and go build for
windows: exit 0 each. GOOS=darwin go vet -tags nogui: exit 1 at
internal/gui/traywindows_darwin_test.go, as on origin/main (#5263).
go vet and go build without the tag on linux: exit 1 (no libsoup-3.0
or gio-unix-2.0 pkg-config here).
- The full suite: see the comment on the issue.
Not changed: TestResponsesToolWithoutReasoningStillTimed, whose old
version stayed ok 2 of 2 with a 400ms hold on its first piece in
holdWriter.Write.
Not tried: a Mac or Windows, or the red under real load.
(cherry picked from commit 86e12bbdeb466a2cc4e3f8718c23d7a1236d77b2)
Landed for #5948 after it came back on CI at dbb2e5dd2 (run 43342362373,
macos-latest) as a second shape of the same gap: ttft_test.go:264, usage
Millis:450 == FirstText:450, the text and the end read at once.
Run at this commit on 422f90e80 (mica, macOS, load ~19):
- the six TTFT and reasoning speed tests, -count=5: ok.
- Broken by hand: a 240ms hold in holdWriter.Write after the reasoning;
the old TestGroupFailoverTimesEachTry red 3 of 3 at :264, as on CI, the
new one ok 3 of 3 with the same hold.
- go test -count=1 -timeout 45m ./internal/gateway: ok 854s.
- gofmt -l internal/gateway: clean.
Not tried: Windows; the full ./... suite (test-only change in one package).
This commit is contained in:
@@ -39,11 +39,17 @@ func chatgpt(t *testing.T, h http.HandlerFunc) *httptest.Server {
|
||||
func codexPost(t *testing.T, body string) (int, string) {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
codexServe(rec, body)
|
||||
return rec.Code, rec.Body.String()
|
||||
}
|
||||
|
||||
// codexServe sends body to a new Server's Codex path as Codex does, its
|
||||
// reply to w.
|
||||
func codexServe(w http.ResponseWriter, body string) {
|
||||
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(body))
|
||||
req.Header.Set("Authorization", "Bearer chatgpt-token")
|
||||
req.Header.Set("chatgpt-account-id", "acct-1")
|
||||
New().Handler().ServeHTTP(rec, req)
|
||||
return rec.Code, rec.Body.String()
|
||||
New().Handler().ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
// One of Codex's own models goes on to the ChatGPT backend as it came, the
|
||||
|
||||
@@ -58,7 +58,9 @@ func TestReasoningSpeedThroughGateway(t *testing.T) {
|
||||
for _, path := range []string{"/v1/chat/completions", "/v1/responses", "/v1/messages"} {
|
||||
t.Run(path, func(t *testing.T) {
|
||||
fresh(t)
|
||||
up := httptest.NewServer(&paced{events: events, gap: 150 * time.Millisecond})
|
||||
// each thought a gap after the agent had the one before (#5948)
|
||||
rec := newHearing(0, "first thought", "second thought", "third thought")
|
||||
up := httptest.NewServer(&paced{events: events, gap: 150 * time.Millisecond, heard: rec.heard})
|
||||
t.Cleanup(up.Close)
|
||||
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Chat: up.URL + "/v1", Key: "k", Models: []string{"m1"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -68,7 +70,6 @@ func TestReasoningSpeedThroughGateway(t *testing.T) {
|
||||
body = `{"model":"fake/m1","stream":true,"input":"hi"}`
|
||||
}
|
||||
s := New()
|
||||
rec := httptest.NewRecorder()
|
||||
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
|
||||
u := lastUsage(t)
|
||||
r := lastRoute(s)
|
||||
@@ -94,7 +95,7 @@ func TestReasoningSpeedThroughGateway(t *testing.T) {
|
||||
// stood in for by holding the sticks' save.
|
||||
func TestSpeedEndsWithTheReply(t *testing.T) {
|
||||
events := []string{
|
||||
`data: {"choices":[{"delta":{"reasoning_content":"first thought"}}]}`,
|
||||
`~data: {"choices":[{"delta":{"reasoning_content":"first thought"}}]}`,
|
||||
`~data: {"choices":[{"delta":{"reasoning_content":"second thought"}}]}`,
|
||||
`~data: {"choices":[{"delta":{"reasoning_content":"third thought"}}]}`,
|
||||
`~data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"check","arguments":"{}"}}]}}]}`,
|
||||
@@ -103,13 +104,14 @@ func TestSpeedEndsWithTheReply(t *testing.T) {
|
||||
}
|
||||
const held = 2 * time.Second
|
||||
fresh(t)
|
||||
up := httptest.NewServer(&paced{events: events, gap: 150 * time.Millisecond})
|
||||
// each thought a gap after the agent had the one before (#5948)
|
||||
rec := newHearing(0, "first thought", "second thought", "third thought")
|
||||
up := httptest.NewServer(&paced{events: events, gap: 150 * time.Millisecond, heard: rec.heard})
|
||||
t.Cleanup(up.Close)
|
||||
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Chat: up.URL + "/v1", Key: "k", Models: []string{"m1"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
rec := httptest.NewRecorder()
|
||||
sticksSaving.Lock()
|
||||
let := time.AfterFunc(held, sticksSaving.Unlock)
|
||||
t.Cleanup(func() {
|
||||
|
||||
@@ -62,7 +62,15 @@ func TestStreamedReasoningWithoutCount(t *testing.T) {
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
fresh(t)
|
||||
up := httptest.NewServer(&paced{events: c.events, gap: 80 * time.Millisecond})
|
||||
w := newHearing(0)
|
||||
pc := &paced{events: c.events, gap: 80 * time.Millisecond}
|
||||
if c.proto == provider.Chat {
|
||||
// each piece a gap after the agent had the one before, so
|
||||
// a gateway that reads late can't read them as one (#5948)
|
||||
w = newHearing(0, "We need", " to check", " it.")
|
||||
pc.heard = w.heard
|
||||
}
|
||||
up := httptest.NewServer(pc)
|
||||
t.Cleanup(up.Close)
|
||||
p := provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}}
|
||||
if c.proto == provider.Anthropic {
|
||||
@@ -74,7 +82,6 @@ func TestStreamedReasoningWithoutCount(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
w := httptest.NewRecorder()
|
||||
s.Handler().ServeHTTP(w, httptest.NewRequest("POST", c.path, strings.NewReader(c.body)))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("%d %s", w.Code, w.Body)
|
||||
|
||||
@@ -234,16 +234,21 @@ func TestUsageLeavesTTFTOfAWholeReply(t *testing.T) {
|
||||
|
||||
// In a group that fails over, each try keeps its own time to its first
|
||||
// token, from when it was sent, and the usage the request's, from when it
|
||||
// came: the time the failed try took is in it, as it is in ms.
|
||||
// came: the time the failed try took is in it, as it is in ms. The
|
||||
// answering try's text comes a gap after the agent was sent its
|
||||
// reasoning, so a loaded machine can't read the two at once and time them
|
||||
// as one (#5948, as #2575 in TestUsageKeepsTimeToFirstToken).
|
||||
func TestGroupFailoverTimesEachTry(t *testing.T) {
|
||||
fresh(t)
|
||||
rec := newHearing(0, "hmm", `"hi"`)
|
||||
serveOn(t, "a", "ka", []string{"m"}, &paced{fail: 429, gap: pacedGap})
|
||||
serveOn(t, "b", "kb", []string{"m"}, &paced{events: pacedChat, gap: pacedGap})
|
||||
serveOn(t, "b", "kb", []string{"m"}, &paced{events: pacedChat, gap: pacedGap, heard: rec.heard})
|
||||
if err := provider.SaveGroup(provider.Group{Name: "Mine", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
code, body := postAs(t, s, "", `{"model":"group/mine","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
||||
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"group/mine","stream":true,"messages":[{"role":"user","content":"hi"}]}`)))
|
||||
code, body := rec.Code, rec.Body.String()
|
||||
if code != 200 || !strings.Contains(body, `"hi"`) {
|
||||
t.Fatalf("%d %s", code, body)
|
||||
}
|
||||
@@ -317,15 +322,20 @@ func TestFirstTokenKinds(t *testing.T) {
|
||||
|
||||
// Codex's own models, relayed to the ChatGPT backend as they came, are
|
||||
// timed too — its stream says no Content-Type.
|
||||
// Each event after the first comes a gap after Codex was sent the one
|
||||
// before it, so a loaded machine can't read the text and the end at once
|
||||
// (#5948).
|
||||
func TestCodexBackendTimesFirstToken(t *testing.T) {
|
||||
setup(t, provider.Chat, &fake{t: t})
|
||||
p := &paced{events: pacedResponses, gap: pacedGap}
|
||||
rec := newHearing(0, "hmm", `"hi"`)
|
||||
p := &paced{events: pacedResponses, gap: pacedGap, heard: rec.heard}
|
||||
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header()["Content-Type"] = nil
|
||||
p.ServeHTTP(w, r)
|
||||
})
|
||||
if code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"hi"}`); code != 200 {
|
||||
t.Fatalf("%d %s", code, body)
|
||||
codexServe(rec, `{"model":"gpt-5.5","stream":true,"input":"hi"}`)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body)
|
||||
}
|
||||
gap := pacedGap.Milliseconds()
|
||||
if u := lastUsage(t); u.TTFT < gap || u.FirstText < 2*gap || u.Millis <= u.FirstText || u.Output != 20 {
|
||||
|
||||
Reference in New Issue
Block a user