Files
magpie/internal/gateway/reasoning_speed_test.go
T
yetone 69167bc299 usage: a reply let go under a second after a wait four times longer tells no speed
pluviophile714 on Discord: a gpt-6.1-sol row on Routing read 1,539
tok/s (input 21,617, output 392, first token 8.2 s, total 8.3 s), where
another monitor measures ~70. The reply came whole about 255 ms after an
8.2 s wait. Its content was spread over that short window (flow 230 ms),
so the HeldShare flow check passed, and so did the 100 ms floor, and
392/0.255 s read as 1,539 tok/s.

New rule, usage.HeldWindowMs: a window under 1,000 ms whose start
(first token, first text or first reasoning) is more than HeldShare (4)
times the window tells no speed. One rule in every place that computes
one:
- usage.DecodeOf: the Usage page's speed and totals, the CLI, the GUI's
  model speeds, and the gateway's Speed first pacing (noteTrySpeed);
- routing/client.js decodeOf: the Routing row's speed and its story line;
- usage/page.js ledDecode, plus a ledNoSpeed_ reason for the dash's
  tooltip, in en, zh, zh-TW, ja and de.
docs/reference.md and docs/subsystems/gateway-sessions.md say so.

On the owner's usage.jsonl (read only), 103 of the 106 rows over 600
tok/s were such windows (e.g. 448 tokens in 107 ms after 8.1 s) and are
dropped. The 3 that stay have windows of 2 to 5 s. The overall speed
goes from 103.8 to 97.9 tok/s.

What it costs: a short reply that really streamed after a long wait
tells no speed either. The live visible-reasoning fixture (76 tokens
over 731 ms after 6.8 s, ~104 tok/s) and #2637's grok record (307 tokens
403 ms after a 4.6 s first token, 531 tok/s) now read "—". Their tests
keep their guarantees with the same timings after a short wait, and also
check the long-wait case.

Tests:
- New: internal/usage TestShortWindowAfterLongWaitTellsNoSpeed (the
  reporter's numbers, a reasoned row from the ledger, the boundary, a
  second after a long wait, Totals) and internal/gateway
  TestShortWindowAfterLongWaitNotesNoPace.
- GUI: usage-speed-every-row has the reporter's row (dash, and the
  tooltip in all five languages); routing-burst-speed has it (first
  token 8.2 s, no tok/s).
- Updated to the new rule: TestVisibleReasoningSpeed's decode-minimum
  and max-speed cases (short wait now), TestCodexTurnMeasured...'s
  gateway/Claude rows, TestLiveVisibleReasoningFixture and
  TestUsageIngestTellsSpeed.

Run on mote at this tree (rebased on e41ef6288):
- go test -timeout 45m: internal/usage ok, internal/tui ok.
- internal/gateway: red only in TestCompactionOverByteCapEveryTime
  (#4862, full package run; passes 3 of 3 alone).
- internal/gui: red only in TestMainWindowCorners (#3949, macOS 27).
- internal/cli: red only in TestEveryRouteHasACommandOrIsNamed (#5026,
  red on main since 0872bc2a0).
- The two speed fixtures that failed in the gateway run pass on rerun.
- GUI, chromium and webkit: 36 files (every speed, ledger, i18n,
  routing-request and burst file), 609 tests, 603 pass. The 6 reds are
  usage-ledger's "newest live route needs no return-to-live button"
  (#5048, #4078, #3104 on main).
- Rebased on 1af868a15 (f0e0385a0 changed routing/client.js), then
  run again at the pushed head: internal/usage ok; the gateway speed
  tests (-run ...Speed|Pace|Decode|TTFT...) ok; 8 GUI files (both
  speed files, routing-request-metrics/-model, routing-speed-first,
  i18n-keys, reasoning-speed, usage-speed-tip) 94 of 94 in both engines.
- gofmt -l clean. GOOS=windows go vet of usage, gateway, gui and cli ok.
  GOOS=linux go vet of usage and gateway ok; gui and cli need cgo/GTK
  for Linux, the same on main.

Broken by hand: with the check off in usage.go, client.js and page.js,
the new Go tests, TestCodexTurnMeasured..., TestLiveVisibleReasoningFixture,
TestUsageIngestTellsSpeed and both GUI files (1537 tok/s shown) go red.

Not tried:
- the reporter's own request;
- a real GPT stream, to confirm that a 255 ms window after 8.2 s is a
  held reply from this vendor;
- the whole GUI suite (no new API call or layout change);
- the Linux and Windows app.
2026-11-03 17:01:51 +08:00

390 lines
17 KiB
Go

package gateway
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
func TestReasoningSpeedEvidence(t *testing.T) {
thought := `data: {"choices":[{"delta":{"reasoning_content":"a thought"}}]}` + "\n\n"
for _, c := range []struct {
name string
proto provider.Protocol
event string
hidden bool
want bool
}{
{"chat", provider.Chat, thought, false, true},
{"anthropic", provider.Anthropic, `data: {"type":"content_block_delta","delta":{"type":"thinking_delta","thinking":"a thought"}}` + "\n\n", false, true},
{"late Responses summary", provider.Responses, `data: {"type":"response.reasoning_summary_text.delta","delta":"a thought"}` + "\n\n", false, false},
{"translated hidden Responses", provider.Responses, thought, false, false},
{"translated redacted thinking", provider.Anthropic, thought, true, false},
{"Gemini summary", provider.Gemini, thought, false, false},
} {
t.Run(c.name, func(t *testing.T) {
f := firstToken{start: time.Now().Add(-time.Second), hidden: c.hidden}
f.see([]byte(c.event))
f.start = f.start.Add(-time.Second)
f.see([]byte(c.event))
if got := f.reasoningMs(c.proto) > 0; got != c.want {
t.Fatalf("reasoning evidence=%v; want %v", got, c.want)
}
})
}
f := firstToken{start: time.Now().Add(-time.Second)}
f.see([]byte(thought + thought))
if f.reasoningMs(provider.Chat) != 0 {
t.Fatal("a buffered burst acquired reasoning timing")
}
}
func TestReasoningSpeedThroughGateway(t *testing.T) {
events := []string{
`~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":"{}"}}]}}]}`,
`data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":5,"completion_tokens":120,"completion_tokens_details":{"reasoning_tokens":100}}}`,
`data: [DONE]`,
}
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})
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)
}
body := `{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"hi"}],"max_tokens":300}`
if path == "/v1/responses" {
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)
if rec.Code != 200 || u.FirstReasoning <= 0 || u.FirstText != 0 || u.Reasoning != 100 {
t.Fatalf("streamed reasoning missing: %d %+v %s", rec.Code, u, rec.Body)
}
n, w := u.Decode()
if n != 120 || w < usage.MinDecodeMs {
t.Fatalf("visible reasoning has no speed: %d/%d %+v", n, w, u)
}
if r.FirstReasoning != u.FirstReasoning || len(r.Tries) != 1 || r.Tries[0].FirstReasoning <= 0 || s.Recent()[0].FirstReasoning != u.FirstReasoning {
t.Fatalf("trace disagrees: %+v %+v", u, r)
}
})
}
}
// A reply's speed is over the time it took to come, not what magpie
// writes down of it once it has: a busy disk held saving sticks after the
// answer for seconds, and the reply's ms with it, so a reply that streamed
// at its pace read as one held back and let go in a burst, and told no
// speed (#1939, bytecode-x: Millis 3772 for 450 ms of stream). The disk is
// 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":"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":"{}"}}]}}]}`,
`data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":5,"completion_tokens":120,"completion_tokens_details":{"reasoning_tokens":100}}}`,
`data: [DONE]`,
}
const held = 2 * time.Second
fresh(t)
up := httptest.NewServer(&paced{events: events, gap: 150 * time.Millisecond})
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() {
if let.Stop() {
sticksSaving.Unlock()
}
})
began := time.Now()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`)))
if took := time.Since(began); took < held {
t.Fatalf("the request took %v, before the sticks were let go: nothing waited on saving them", took)
}
u := lastUsage(t)
if rec.Code != 200 || u.Reasoning != 100 {
t.Fatalf("%d %+v %s", rec.Code, u, rec.Body)
}
if u.Millis >= held.Milliseconds() {
t.Fatalf("the reply's ms %d count the %v magpie took saving after it", u.Millis, held)
}
if n, w := u.Decode(); n != 120 || w < usage.MinDecodeMs || w > u.Millis {
t.Fatalf("a reply that came at its pace tells no speed: %d/%d %+v", n, w, u)
}
if r := lastRoute(s); len(r.Tries) != 1 || r.Tries[0].Millis > u.Millis {
t.Fatalf("trace: %+v", r.Tries)
}
}
func TestHiddenReasoningThroughTranslation(t *testing.T) {
events := []string{
`data: {"type":"response.created","response":{"id":"r1","status":"in_progress","output":[]}}`,
`~data: {"type":"response.reasoning_summary_text.delta","delta":"first summary"}`,
`~data: {"type":"response.reasoning_summary_text.delta","delta":"second summary"}`,
`~data: {"type":"response.reasoning_summary_text.delta","delta":"third summary"}`,
`data: {"type":"response.output_item.added","item":{"type":"function_call","call_id":"c1","name":"check"}}`,
`data: {"type":"response.function_call_arguments.delta","delta":"{}"}`,
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":5,"output_tokens":1500,"output_tokens_details":{"reasoning_tokens":1400}}}}`,
}
for _, path := range []string{"/v1/responses", "/v1/chat/completions", "/v1/messages"} {
t.Run(path, func(t *testing.T) {
fresh(t)
up := httptest.NewServer(&paced{events: events, gap: 150 * time.Millisecond})
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Responses: up.URL + "/v1", Key: "k", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
body := `{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"hi"}],"max_tokens":300}`
if path == "/v1/responses" {
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)
n, w := u.Decode()
if rec.Code != 200 || u.FirstReasoning != 0 || r.FirstReasoning != 0 || n != 0 || w != 0 || u.Reasoning != 1400 {
t.Fatalf("hidden reasoning acquired speed after translation: %d %+v %s", rec.Code, u, rec.Body)
}
})
}
}
// A visible portion never licenses timing a sealed portion of the same turn.
func TestMixedRedactedReasoning(t *testing.T) {
events := []string{
`data: {"type":"message_start","message":{"id":"m","role":"assistant","model":"m1","content":[],"usage":{"input_tokens":5,"output_tokens":0}}}`,
`data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}`,
`~data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"first thought"}}`,
`~data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"second thought"}}`,
`data: {"type":"content_block_stop","index":0}`,
`data: {"type":"content_block_start","index":1,"content_block":{"type":"redacted_thinking","data":"sealed-test"}}`,
`data: {"type":"content_block_stop","index":1}`,
`data: {"type":"content_block_start","index":2,"content_block":{"type":"tool_use","id":"t1","name":"check","input":{}}}`,
`data: {"type":"content_block_delta","index":2,"delta":{"type":"input_json_delta","partial_json":"{}"}}`,
`data: {"type":"content_block_stop","index":2}`,
`data: {"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":120}}`,
`data: {"type":"message_stop"}`,
}
for _, path := range []string{"/v1/messages", "/v1/chat/completions", "/v1/responses"} {
t.Run(path, func(t *testing.T) {
fresh(t)
up := httptest.NewServer(&paced{events: events, gap: 150 * time.Millisecond})
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Anthropic: up.URL, Key: "k", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
body := `{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"hi"}],"max_tokens":300}`
if path == "/v1/responses" {
body = `{"model":"fake/m1","stream":true,"input":"hi"}`
}
s := New()
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, httptest.NewRequest("POST", path, strings.NewReader(body)))
row := lastUsage(t)
n, ms := row.Decode()
if w.Code != 200 || row.FirstReasoning != 0 || !row.ReasoningHidden || n != 0 || ms != 0 {
t.Fatalf("mixed sealed reasoning timed: %d %+v", w.Code, row)
}
})
}
}
func TestReasoningPacingEdges(t *testing.T) {
thought := []byte(`data: {"choices":[{"delta":{"reasoning_content":"thought"}}]}` + "\n\n")
tool := []byte(`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{}"}}]}}]}` + "\n\n")
for _, c := range []struct {
name string
gap time.Duration
want bool
}{
{"below 100ms", 99 * time.Millisecond, false}, {"100ms", 100 * time.Millisecond, true},
} {
t.Run(c.name, func(t *testing.T) {
f := firstToken{start: time.Now().Add(-time.Second)}
f.see(thought)
f.start = time.Now().Add(-f.reasoning - c.gap)
f.see(thought)
if got := f.reasoningMs(provider.Chat) > 0; got != c.want {
t.Fatalf("paced=%v; want %v", got, c.want)
}
})
}
f := firstToken{start: time.Now().Add(-time.Second)}
f.see(tool)
f.start = f.start.Add(-time.Second)
f.see(thought)
f.start = f.start.Add(-time.Second)
f.see(thought)
if f.reasoningMs(provider.Chat) != 0 {
t.Fatal("a tool-first turn acquired reasoning timing")
}
// Two writes after a long quiet start still form a burst below the decode minimum.
f = firstToken{start: time.Now().Add(-14 * time.Second)}
f.see(thought)
f.see(thought)
if f.reasoningMs(provider.Chat) != 0 {
t.Fatal("buffered reasoning acquired timing")
}
}
func TestSlowReleasedReasoningIsUntimed(t *testing.T) {
f := firstToken{start: time.Now().Add(-time.Second)}
thought := []byte(`data: {"choices":[{"delta":{"reasoning_content":"one visible thought"}}]}` + "\n\n")
f.see(thought)
first := f.reasoning
for i := 0; i < 40; i++ {
f.start = time.Now().Add(-first - 10*time.Second - time.Duration(i)*6*time.Millisecond)
f.see(thought)
}
end := first.Milliseconds() + 10250
n, w := usage.DecodeOf(500, 450, end, first.Milliseconds(), 0, f.flowFor(450), f.reasoningMs(provider.Chat))
if n != 0 || w != 0 {
t.Fatalf("held reasoning inflated speed: %d/%d flow=%d", n, w, f.flowFor(450))
}
}
func TestEncryptedReasoningWithoutTokenDetails(t *testing.T) {
f := firstToken{start: time.Now().Add(-time.Second)}
f.see([]byte(`data: {"type":"response.output_item.done","item":{"type":"reasoning","encrypted_content":"synthetic-seal"}}` + "\n\n"))
if !f.hidden {
t.Fatal("encrypted reasoning discarded its hidden marker")
}
}
func TestGeminiSummaryWithoutThoughtCount(t *testing.T) {
f := firstToken{start: time.Now().Add(-time.Second)}
f.see([]byte(`data: {"candidates":[{"content":{"parts":[{"text":"visible summary","thought":true}]}}]}` + "\n\n"))
if !f.hidesReasoning(provider.Gemini, 0) || !f.hidesReasoning(provider.Gemini, 7) || f.reasoningMs(provider.Gemini) != 0 {
t.Fatal("Gemini summary timed without its hidden thought count")
}
}
func TestCodexBackendHiddenReasoningWithoutCount(t *testing.T) {
setup(t, provider.Chat, &fake{t: t})
p := &paced{events: []string{
`data: {"type":"response.created","response":{"id":"r1","status":"in_progress","output":[]}}`,
`~data: {"type":"response.reasoning_summary_text.delta","delta":"summary"}`,
`~data: {"type":"response.output_item.done","item":{"type":"reasoning","encrypted_content":"synthetic-seal"}}`,
`~data: {"type":"response.function_call_arguments.delta","delta":"{}"}`,
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":5,"output_tokens":120}}}`,
}, gap: 150 * time.Millisecond}
chatgpt(t, func(w http.ResponseWriter, r *http.Request) { 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)
}
r := lastUsage(t)
n, ms := r.Decode()
if !r.ReasoningHidden || n != 0 || ms != 0 {
t.Fatalf("Codex hidden reasoning timed with no count: %+v", r)
}
}
func TestResponsesToolWithoutReasoningStillTimed(t *testing.T) {
events := []string{
`data: {"type":"response.created","response":{"id":"r1","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.added","item":{"type":"function_call","call_id":"c1","name":"check"}}`,
`~data: {"type":"response.function_call_arguments.delta","delta":"{\"value\":"}`,
`~data: {"type":"response.function_call_arguments.delta","delta":"1290}"}`,
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":5,"output_tokens":10}}}`,
}
fresh(t)
up := httptest.NewServer(&paced{events: events, gap: 150 * time.Millisecond})
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Responses: up.URL + "/v1", Key: "k", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
s := New()
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`)))
row := lastUsage(t)
n, ms := row.Decode()
if w.Code != 200 || row.ReasoningHidden || n != 10 || ms < usage.MinDecodeMs {
t.Fatalf("tool-only response lost timing: %d %+v", w.Code, row)
}
}
func TestMultipleVisibleReasoningBlocks(t *testing.T) {
f := firstToken{start: time.Now().Add(-time.Second)}
for i, ev := range []string{
`data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}`,
`data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"first thought"}}`,
`data: {"type":"content_block_stop","index":0}`,
`data: {"type":"content_block_start","index":1,"content_block":{"type":"thinking","thinking":""}}`,
`data: {"type":"content_block_delta","index":1,"delta":{"type":"thinking_delta","thinking":"second thought"}}`,
} {
f.start = time.Now().Add(-time.Second - time.Duration(i)*150*time.Millisecond)
f.see([]byte(ev + "\n\n"))
}
if first := f.reasoningMs(provider.Anthropic); first <= 0 || first != f.first.Milliseconds() {
t.Fatal("multiple visible blocks lost their first reasoning time")
}
}
func TestLiveVisibleReasoningFixture(t *testing.T) {
var sample struct {
First int64 `json:"first_reasoning_ms"`
Events []struct {
At int64 `json:"at_ms"`
Delta string `json:"delta"`
} `json:"events"`
Record struct {
Out, Reasoning int
Ms int64
TTFT int64 `json:"ttft_ms"`
Flow int64 `json:"flow_ms"`
} `json:"record"`
}
b, err := os.ReadFile("testdata/reasoning-speed-visible.json")
if err != nil {
t.Fatal(err)
}
if err = json.Unmarshal(b, &sample); err != nil {
t.Fatal(err)
}
f := firstToken{}
for _, e := range sample.Events {
delta, _ := json.Marshal(e.Delta)
f.start = time.Now().Add(-time.Duration(e.At) * time.Millisecond)
f.see([]byte(`data: {"choices":[{"delta":{"reasoning_content":` + string(delta) + `}}]}` + "\n\n"))
}
if f.reasoningMs(provider.Chat) != sample.Events[0].At {
t.Fatalf("live reasoning fixture lost its start: %d/%d", f.reasoningMs(provider.Chat), sample.Events[0].At)
}
// its 731 ms came after a 6.8 s wait, which tells no speed: under a
// second after a wait over four times longer reads as a reply let go
// whole (HeldWindowMs, pluviophile714 on Discord), though this one was
// streamed. The same turn after a short wait tells it from its first
// reasoning.
rec, d := sample.Record, int64(6500)
if n, ms := usage.DecodeOf(rec.Out, rec.Reasoning, rec.Ms, rec.TTFT, 0, rec.Flow, sample.First); n != 0 || ms != 0 {
t.Fatalf("live tool turn after a 6.8 s wait: speed %d/%d; want none", n, ms)
}
n, ms := usage.DecodeOf(rec.Out, rec.Reasoning, rec.Ms-d, rec.TTFT-d, 0, rec.Flow, sample.First-d)
if n != 76 || ms != 731 {
t.Fatalf("live tool turn speed %d/%d; want 76/731", n, ms)
}
}