Files
magpie/internal/gateway/live_test.go
T
yetoneandCanda15 0f1b40bbc4 gateway: a reply in flight shows on Routing how much has arrived and roughly its tokens (#4951, Canda15)
A long or reasoning reply looked stuck on the Routing page. A request still
out now shows, on its row and beside the account answering it, the bytes
the agent has had, "≈ n tokens" once a rate is learned, and how long ago
content last came; after 15s with no content the account's dot stops
pulsing. Nothing stays once the request is done.

Ported from Canda15's 7f7bdb189 (github.com/Canda15/magpie), the idea and
the per-protocol content extractors kept, the hot path and storage
reworked:
- every write to the agent passes captureResponseWriter.Write, which calls
  liveRow.observe; the row hangs on the request's inflight (an atomic
  pointer), not a context value made per request
- rates are in memory only (256 pairs); the fork wrote a JSON file under
  the estimate lock on every finished request
- a whole JSON reply is counted in bytes only: the fork held and decoded up
  to 8 MiB of it at the end; a stream is cut into lines in place, only a
  line cut by a write's end is copied, a line over 1 MiB passed over, and
  each event decoded once (the fork ran json.Valid then Unmarshal)
- the reply's kind comes from its first byte, not a header read on every
  write; the keepalive check allocates nothing (the fork split each write)
- everything a row counts is under its own lock, and inflight.tr is read
  under f.mu; the estimate is in float64 (the int64 product overflowed)
- the route's Seq is bumped on every write and traceGather batches the
  wake-ups; the fork's global throttle could drop the last update before
  a pause
- learned only from a streamed reply that did not fail and had the
  vendor's output tokens
- left out: the finished row's "Received" metric and the ended caption
  (layout restraint; an ended row has its own metrics), a duplicate
  "Received" i18n key, the receive-monitor doc (the design is in
  gateway-routing.md now), the fork's xmod and Windows CI commits
The Routing plugin and i18n.js are outside TestCoreScriptBudget.

Run on mote at this head unless named: gofmt -l; go vet (darwin),
GOOS=linux -tags nogui and GOOS=windows go vet ./...; go build ./...;
go test -race on the live tests; go test ./internal/gateway ./internal/gui
(gateway ok; gui red only on TestCoreScriptBudget, #5139, 1533907 >
1533802 before this change and untouched by it, and
TestMainWindowCorners/macOS_26_SDK, #3949); routing-live-bytes.test.cjs and i18n-keys in Chromium and
WebKit, en/zh/zh-TW/ja/de, 1100 and 420px (24/24); every routing-* and
panel-routing* GUI file with i18n-keys in WSL Linux (Chromium all green;
WebKit reds in 12 files, the same class at origin/main 7dd2434d3 run
the same way: 25 there, 28 here, the sets differing by timing samples;
routing-live-patch alone red 2 of 3 on main, 1 of 3 here; filed as
#4121, #4520, #5156, #2174).

Rebased onto 38f4f74e6 (gateway.go, trace.go and the Routing plugin
moved): build, gateway vet, the live tests, routing-live-bytes and
i18n-keys run again there, green (24/24).

Broken by hand, each red, then put back:
- no observe in capture.Write: TestLiveCountsARealStream
- commentOnly never true: TestLiveHeartbeatIsBytesNotContent,
  TestCommentOnlyTellsHeartbeatsApart
- learning from failed replies: TestRatesLearnFromRepliesTheVendorFinished
- a whole reply read as a stream: TestContentCounterLeavesAWholeReply
- no Seq bump in liveBytes: TestLiveWritesReachTheRoute
- no quiet class, no per-second repaint: routing-live-bytes 0/20

Not tried: a real vendor's stream in the running app; the ChatGPT backend
chain (codex_backend.go) writes past the capture writer and isn't counted;
the full Go and GUI suites run after the tag.

Co-authored-by: Canda15 <124772373+Canda15@users.noreply.github.com>
2026-11-04 19:39:08 +08:00

307 lines
11 KiB
Go

package gateway
import (
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// freshRates starts a test with no rate learned, and leaves none after.
func freshRates(t *testing.T) {
t.Helper()
resetRates()
t.Cleanup(resetRates)
}
// liveFor is a counter for a request in flight whose route is rt.
func liveFor(rt *Route, proto provider.Protocol) *liveRow {
f := &inflight{tr: &trace{}}
if rt != nil {
f.route.Store(rt)
}
return liveBegin(f, "m", proto)
}
func liveChatEvent(s string) []byte {
return []byte(`data: {"choices":[{"delta":{"content":"` + s + `"}}]}` + "\n\n")
}
// The bytes are every byte the agent got, the content only the words, and
// a finish counted twice (an encoder's last write after serve) changes
// nothing.
func TestLiveCountsBytesAndContent(t *testing.T) {
freshRates(t)
live := liveFor(nil, provider.Chat)
if b, c, at := live.totals(); b != 0 || c != 0 || at != 0 {
t.Fatalf("a request with no byte yet: %d bytes, %d content, content at %d", b, c, at)
}
first := liveChatEvent("hello")
live.observe(first)
b, c, at := live.totals()
if b != int64(len(first)) || c != 5 || at == 0 {
t.Fatalf("after %q: %d bytes, %d content, content at %d", first, b, c, at)
}
live.finish(false, 42)
live.finish(false, 42)
if _, c, _ := live.totals(); c != 5 {
t.Fatalf("content after it ended twice: %d, want 5", c)
}
}
// The gateway's keepalive adds bytes, but neither content nor a newer
// content time: a stuck reply must look still.
func TestLiveHeartbeatIsBytesNotContent(t *testing.T) {
live := liveFor(nil, provider.Chat)
live.observe(liveChatEvent("a"))
b0, c0, at0 := live.totals()
time.Sleep(5 * time.Millisecond)
beat := []byte(": keepalive\n")
live.observe(beat)
b, c, at := live.totals()
if b != b0+int64(len(beat)) || c != c0 || at != at0 {
t.Fatalf("a keepalive: bytes %d→%d, content %d→%d, content at %d→%d", b0, b, c0, c, at0, at)
}
live.observe(liveChatEvent("b"))
if _, _, back := live.totals(); back <= at0 {
t.Fatalf("content didn't move the time forward: %d then %d", at0, back)
}
}
func TestCommentOnlyTellsHeartbeatsApart(t *testing.T) {
for _, c := range []struct {
in string
want bool
}{
{": keepalive\n", true},
{": keepalive\n\n", true},
{"\n: ping\r\n\n", true},
{"", false},
{"\n\n", false},
{"data: {\"a\":1}\n\n", false},
{": keepalive\ndata: {\"a\":1}\n\n", false},
{"data: {\"a\":\":\"}\n", false},
{`{"a":1}`, false},
} {
if got := commentOnly([]byte(c.in)); got != c.want {
t.Errorf("commentOnly(%q) = %v, want %v", c.in, got, c.want)
}
}
}
// What counts as content in each protocol, from events as the vendors
// write them.
func TestContentBytesPerProtocol(t *testing.T) {
for _, c := range []struct {
name string
proto provider.Protocol
body string
want int
}{
{"chat text", provider.Chat, `{"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"hello"},"finish_reason":null}]}`, 5},
{"chat reasoning", provider.Chat, `{"choices":[{"delta":{"reasoning_content":"hm"}}]}`, 2},
{"chat tool name", provider.Chat, `{"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"read","arguments":""}}]}}]}`, 4},
{"chat tool args", provider.Chat, `{"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"a\":1}"}}]}}]}`, 7},
{"chat usage", provider.Chat, `{"choices":[],"usage":{"prompt_tokens":3,"completion_tokens":5}}`, 0},
{"responses delta", provider.Responses, `{"type":"response.output_text.delta","item_id":"msg_1","output_index":0,"content_index":0,"delta":"abc"}`, 3},
{"responses done", provider.Responses, `{"type":"response.output_text.done","text":"abc"}`, 0},
{"responses call", provider.Responses, `{"type":"response.output_item.added","item":{"type":"function_call","name":"shell","arguments":""}}`, 5},
{"anthropic text", provider.Anthropic, `{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"abc"}}`, 3},
{"anthropic thinking", provider.Anthropic, `{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"de"}}`, 2},
{"anthropic tool", provider.Anthropic, `{"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_1","name":"grep","input":{}}}`, 4},
{"anthropic input", provider.Anthropic, `{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"q\":"}}`, 5},
{"gemini text", provider.Gemini, `{"candidates":[{"content":{"parts":[{"text":"abc"}],"role":"model"}}]}`, 3},
{"gemini call", provider.Gemini, `{"candidates":[{"content":{"parts":[{"functionCall":{"name":"ls","args":{}}}]}}]}`, 4},
{"done", provider.Chat, `[DONE]`, 0},
{"empty", provider.Chat, `{}`, 0},
} {
if got := liveContentOf(c.proto, []byte(c.body)); got != c.want {
t.Errorf("%s: %s gave %d, want %d", c.name, c.body, got, c.want)
}
}
}
// A stream cut into writes of any size counts the same as written whole.
func TestContentCounterSurvivesSplitWrites(t *testing.T) {
stream := string(liveChatEvent("hello")) + ": keepalive\n" + string(liveChatEvent(" wörld")) + "data: [DONE]\n\n"
for _, size := range []int{1, 2, 3, 7, 13, len(stream)} {
c := &liveContentCounter{proto: provider.Chat}
for i := 0; i < len(stream); i += size {
c.add([]byte(stream[i:min(i+size, len(stream))]))
}
c.flush()
if c.total != 12 {
t.Errorf("written in %d-byte pieces: %d content bytes, want 12", size, c.total)
}
}
// a stream ending without its blank line still counts its last event
c := &liveContentCounter{proto: provider.Chat}
c.add([]byte(strings.TrimSuffix(string(liveChatEvent("abc")), "\n\n")))
c.flush()
if c.total != 3 {
t.Errorf("a last event without its blank line: %d, want 3", c.total)
}
}
// A whole JSON reply is not counted (nor copied): it arrives at once and
// shows nothing while it is out, and it is not learned from.
func TestContentCounterLeavesAWholeReply(t *testing.T) {
freshRates(t)
for i := 0; i < liveRateMin+1; i++ {
live := liveFor(nil, provider.Chat)
live.routed("vendor", "m")
live.observe([]byte(`{"choices":[{"message":{"content":"pong"}}],"usage":{"completion_tokens":2}}`))
if live.counter.sse || len(live.counter.part) != 0 || len(live.counter.data) != 0 {
t.Fatalf("a whole reply was read as a stream or held: %+v", live.counter)
}
live.finish(false, 2)
}
if got := liveRates.estimate("vendor", "m", 100); got != 0 {
t.Fatalf("whole replies were learned from: estimate %d", got)
}
}
// A line longer than liveLineMost is passed over, not held.
func TestContentCounterDropsAnEndlessLine(t *testing.T) {
c := &liveContentCounter{proto: provider.Chat}
c.add([]byte("data: \""))
chunk := []byte(strings.Repeat("x", 64<<10))
for i := 0; i < 2*liveLineMost/len(chunk); i++ {
c.add(chunk)
}
if len(c.part) > liveLineMost {
t.Fatalf("held %d bytes of one line", len(c.part))
}
c.add([]byte("\"\n\n"))
c.add(liveChatEvent("ok"))
c.flush()
if c.total != 2 {
t.Fatalf("after an endless line: %d content bytes, want 2", c.total)
}
}
// Three replies learned before an estimate, at their summed ratio, per
// provider and model, rounded down.
func TestRatesLearnThenEstimate(t *testing.T) {
freshRates(t)
liveRates.learn("vendor", "m", 100, 10)
if got := liveRates.estimate("vendor", "m", 500); got != 0 {
t.Fatalf("one sample estimated %d", got)
}
liveRates.learn("vendor", "m", 100, 10)
liveRates.learn("vendor", "m", 200, 20)
if got := liveRates.estimate("vendor", "m", 959); got != 95 {
t.Fatalf("959 content bytes at 10:1 is 95 tokens, got %d", got)
}
if got := liveRates.estimate("other", "m", 950); got != 0 {
t.Fatalf("another provider was estimated from this one's samples: %d", got)
}
// sums past what an int64 product holds still estimate
for i := 0; i < 3; i++ {
liveRates.learn("big", "m", 1<<40, 1<<38)
}
if got := liveRates.estimate("big", "m", 1<<30); got != 1<<28 {
t.Fatalf("large sums estimated %d, want %d", got, 1<<28)
}
}
// Only a streamed reply that didn't fail and whose vendor said its output
// is learned from; an agent gone after the last content still is.
func TestRatesLearnFromRepliesTheVendorFinished(t *testing.T) {
freshRates(t)
for _, c := range []struct {
vendor string
failed bool
output int
learn bool
}{
{"v-failed", true, 10, false},
{"v-nousage", false, 0, false},
{"v-ok", false, 10, true},
} {
live := liveFor(nil, provider.Chat)
live.routed(c.vendor, "m")
live.observe(liveChatEvent("hello"))
live.finish(c.failed, c.output)
liveRates.mu.Lock()
got := liveRates.m[c.vendor+"/m"]
liveRates.mu.Unlock()
if c.learn != (got.seen == 1) || c.learn && (got.bytes != 5 || got.output != 10) {
t.Errorf("%s: learned %+v, want learned=%v", c.vendor, got, c.learn)
}
}
}
// The gateway's own requests are not counted, and every method is safe on
// the nil they get.
func TestLiveIsOptional(t *testing.T) {
if l := liveBegin(nil, "m", provider.Chat); l != nil {
t.Fatal("a request not in flight was counted")
}
var live *liveRow
live.observe([]byte("x"))
live.routed("p", "m")
live.finish(true, 0)
if liveOfInflight(nil) != nil || liveOfInflight(&inflight{}) != nil {
t.Fatal("a request with no counter has one")
}
f := &inflight{}
if l := liveBegin(f, "m", provider.Chat); liveOfInflight(f) != l {
t.Fatal("the counter begun isn't the request's")
}
}
// Writes reach the request's route: a keepalive its bytes and not its
// content time, and a route begun after the first write the running total.
func TestLiveWritesReachTheRoute(t *testing.T) {
freshRates(t)
rt := &Route{Agent: "a", Model: "m", Time: time.Now()}
live := liveFor(rt, provider.Chat)
first := liveChatEvent("hello")
live.observe(first)
if rt.Bytes != int64(len(first)) || rt.ContentAt == 0 || rt.Seq == 0 {
t.Fatalf("the route didn't hear the first bytes: %+v", rt)
}
at, seq := rt.ContentAt, rt.Seq
beat := []byte(": keepalive\n")
live.observe(beat)
if rt.Bytes != int64(len(first)+len(beat)) || rt.ContentAt != at || rt.Seq <= seq {
t.Fatalf("after a keepalive the route has %d bytes, content at %d (was %d), seq %d (was %d)", rt.Bytes, rt.ContentAt, at, rt.Seq, seq)
}
f := &inflight{tr: &trace{}}
late := liveBegin(f, "m", provider.Chat)
late.observe(first)
lateRoute := &Route{Agent: "a", Model: "m", Time: time.Now()}
f.route.Store(lateRoute)
late.observe(first)
if want := int64(2 * len(first)); lateRoute.Bytes != want {
t.Fatalf("a route begun late got %d bytes, want the running total %d", lateRoute.Bytes, want)
}
}
// The route gets an estimate once its provider and model learned a rate,
// and none before.
func TestRouteGetsTheEstimateOnlyOnceItIsCalibrated(t *testing.T) {
freshRates(t)
rt := &Route{Agent: "a", Model: "m", Time: time.Now()}
live := liveFor(rt, provider.Chat)
live.routed("vendor", "m")
live.observe(liveChatEvent("hello"))
if rt.EstTok != 0 {
t.Fatalf("estimated %d tokens with no sample", rt.EstTok)
}
for i := 0; i < liveRateMin; i++ {
done := liveFor(nil, provider.Chat)
done.routed("vendor", "m")
done.observe(liveChatEvent("hello"))
done.finish(false, 2)
}
live.observe(liveChatEvent("hello"))
// 10 content bytes at 15 bytes for 6 tokens
if rt.EstTok != 4 {
t.Fatalf("10 content bytes at 2.5 bytes a token is 4; got %d", rt.EstTok)
}
}