Revises #1463 after the hidden-reasoning inflation review. Keep answers separate, retain evidence in routes and cached ledger rows, and exclude sealed or summary-only reasoning even without a token breakdown. Sync the backend and both GUI formulas. Checked real WorkBuddy DeepSeek, Codex and Claude bridge responses in an isolated HOME; sanitized timings and failures are in docs/verification. Chromium/WebKit speed regressions pass (20 tests, five languages). New edge regressions and loop-order check pass under race repetition. Restoring the old formula or removing the hidden guard fails TestVisibleReasoningSpeed. macOS/Linux/Windows vet/build pass. Full-suite verification is recorded in the issue at this head; inherited proxy/Bun 1.3.14 failures are retained, with clean Bun 1.4.2 rerun in progress. No installed app replacement. Rebased onto main by yetone (#1463). Conflicts with main's Own/Retried/ Queued/Tries row fields, HandedOn and the Codex speed floors were resolved by keeping both. The packed row's ReasoningHidden bit was 64, which main has since given to block_price.go's swapKnown, and main's rowHandedOn took 128, filling the uint8: Flags is now uint16 and ReasoningHidden is bit 512 (rowReasoningHidden; #1874's rowOwnCost took 256), read back in row(). The docs/verification notes and their sample JSON are left out of the tree; they stay on the contributor's branch and the issue. Run after the rebase: go test -tags nogui ./internal/usage/ and the gateway's reasoning/speed/TTFT tests, ok.
330 lines
14 KiB
Go
330 lines
14 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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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) || 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)
|
|
}
|
|
n, ms := usage.DecodeOf(sample.Record.Out, sample.Record.Reasoning, sample.Record.Ms, sample.Record.TTFT, 0, sample.Record.Flow, sample.First)
|
|
if n != 76 || ms != 731 {
|
|
t.Fatalf("live tool turn speed %d/%d; want 76/731", n, ms)
|
|
}
|
|
}
|