The owner liked a tweet's token-speed curve and approved a mockup
(claude.ai/artifact/7mrkMKdVi6jA3HA1Z6pRr5). Each Sessions row now
shows its output speed with a small curve; clicking it opens a popover
with the current tok/s, two minutes of seconds, tool gaps, and the
round's average, fastest second and output so far. A row not live
shows "Avg {n} tok/s".
Gateway: a curveMeter (session_speed.go) on the Server counts every
reply's content bytes per agent and session, from liveRow.observe, the
same count the Live view uses, and turns them into tokens with the
learned per-model rate (tokenRates.rate). A model with no rate yet
shows the curve's shape and no number: a guess would be a wrong
number. counted() opens a session's in-flight span, so a second with
nothing in flight is a gap (a tool ran), not a zero. A pause past 2m
starts a new round; 10m of quiet forgets the session; at most 256
sessions. Codex's own sign-in (codex_backend.go) went past serve and
liveRow before; it now has a liveRow too, so its sessions are counted.
GUI: GET /api/speeds, kept out of /api/sessions* so a once-a-second
poll doesn't set off session reading. The page polls only while
Sessions is shown and visible. managedJSON gains keys, so a row
matches a session the gateway saw under another of its keys. The
curve hides below 620px.
Run (mote's sshd resets every connection today, so Linux and Windows):
- Linux (magpie-desktop, wsl-run): gofmt clean; go test -timeout 45m
./internal/gateway ./internal/sessions ok except
TestGroupFailoverTimesEachTry, red once under load, 5/5 green alone
here and on main (#5948); node --test over every sessions-*.test.cjs
and i18n-*.test.cjs: 546/549, the reds sessions-export en in both
engines, the same on origin/main d657e182a alone (#5947), and
sessions-folder-paging webkit zh, green alone. sessions-speed in
Chromium and WebKit x en, zh, zh-TW, ja, de (row, popover, live
update, narrow width, no poll off the page).
- Windows (xiaomi-gpu, win-run), at this head: go vet ./... clean; the
curve/live/rates/codex tests of ./internal/gateway ok; go test
./internal/gui ./internal/sessions ok but 12 tests, every one red the
same on origin/main 4d95d81ea and filed (#4115, #5928, #4043, #4925,
#5324; TestAntigravityAppFolder new as #5957). (internal/gui needs gtk4 for cgo, which
the WSL distro lacks.)
- At the pushed head (on 6886da1ca, which changed core i18n.js and
gateway.go), Linux: sessions-speed, i18n-keys, i18n-en-no-cjk,
sessions-row-narrow 22/22; the gateway curve/live/rates/codex/CORS
tests ok. The Windows run above was at 4d95d81ea.
Broken by hand: with live.go's curves.add skipped, both
TestSessionCurveFromARealStream and TestSessionCurveFromCodexOwnSignIn
went red; green once restored.
Not tried: a real agent session in the built app; macOS (mote down);
darwin vet; the full go and GUI suites (run after the push, reds
filed).
293 lines
8.5 KiB
Go
293 lines
8.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// curveClock is a meter's clock the test moves by hand.
|
|
type curveClock struct{ at atomic.Int64 }
|
|
|
|
func (c *curveClock) now() time.Time { return time.Unix(c.at.Load(), 0) }
|
|
func (c *curveClock) tick(d int64) { c.at.Add(d) }
|
|
|
|
func meterAt(start int64) (*curveMeter, *curveClock) {
|
|
c := &curveClock{}
|
|
c.at.Store(start)
|
|
return &curveMeter{now: c.now}, c
|
|
}
|
|
|
|
// learnRate teaches liveRates tokens-per-byte for a pair, past liveRateMin.
|
|
func learnRate(provider, model string, bytes int64, output int) {
|
|
for range liveRateMin {
|
|
liveRates.learn(provider, model, bytes, output)
|
|
}
|
|
}
|
|
|
|
func onlySpeed(t *testing.T, m *curveMeter) Speed {
|
|
t.Helper()
|
|
sp := m.speeds()
|
|
if len(sp) != 1 {
|
|
t.Fatalf("speeds %+v, want one session", sp)
|
|
}
|
|
return sp[0]
|
|
}
|
|
|
|
// The curve is tokens a second over whole seconds, a gap where nothing of
|
|
// the session's was in flight (a tool ran), and the round's average over
|
|
// the seconds it wrote in, its peak and its output.
|
|
func TestSessionCurveCountsSecondsAndGaps(t *testing.T) {
|
|
freshRates(t)
|
|
learnRate("p", "m", 400, 100) // a byte is a quarter token
|
|
m, c := meterAt(1_000_000)
|
|
|
|
shut := m.open("claude", "s1")
|
|
for range 5 {
|
|
m.add("claude", "s1", "p", "m", 400)
|
|
c.tick(1)
|
|
}
|
|
shut()
|
|
c.tick(10) // the agent runs a tool
|
|
shut = m.open("claude", "s1")
|
|
for range 3 {
|
|
m.add("claude", "s1", "p", "m", 800)
|
|
c.tick(1)
|
|
}
|
|
|
|
sp := onlySpeed(t, m)
|
|
if !sp.Live || !sp.Rated {
|
|
t.Fatalf("live %v rated %v", sp.Live, sp.Rated)
|
|
}
|
|
if len(sp.Curve) != curveSpan {
|
|
t.Fatalf("curve has %d seconds", len(sp.Curve))
|
|
}
|
|
tail := sp.Curve[curveSpan-19:]
|
|
var got []string
|
|
for _, v := range tail {
|
|
if v == nil {
|
|
got = append(got, "-")
|
|
} else {
|
|
got = append(got, strconv.FormatFloat(*v, 'f', -1, 64))
|
|
}
|
|
}
|
|
// 5 seconds at 100, the second the first request ended in (live, 0),
|
|
// 9 seconds of nothing in flight, then 3 at 200
|
|
want := "100 100 100 100 100 0 - - - - - - - - - 200 200 200"
|
|
if g := strings.Join(got[1:], " "); g != want {
|
|
t.Fatalf("curve tail\n got %s\nwant %s", g, want)
|
|
}
|
|
if sp.Now != 200 {
|
|
t.Errorf("now %v, want 200", sp.Now)
|
|
}
|
|
if want := float64(5*400+3*800) / 8 / 4; sp.Avg != want {
|
|
t.Errorf("avg %v, want %v", sp.Avg, want)
|
|
}
|
|
if sp.Peak != 200 {
|
|
t.Errorf("peak %v, want 200", sp.Peak)
|
|
}
|
|
if sp.Output != 1100 {
|
|
t.Errorf("output %v, want 1100", sp.Output)
|
|
}
|
|
|
|
shut()
|
|
c.tick(4)
|
|
sp = onlySpeed(t, m)
|
|
if sp.Live || sp.Now != 0 || sp.Idle != 4 {
|
|
t.Fatalf("after the turn: live %v now %v idle %v", sp.Live, sp.Now, sp.Idle)
|
|
}
|
|
}
|
|
|
|
// roundBytes is the content bytes a session's round has so far.
|
|
func (m *curveMeter) roundBytes(session string) int64 {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
for k, t := range m.m {
|
|
if k.session == session {
|
|
return t.roundBytes
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// A model with no rate yet has the curve's shape in bytes and no numbers:
|
|
// a guess would be a wrong number on the page.
|
|
func TestSessionCurveUnratedHasNoNumbers(t *testing.T) {
|
|
freshRates(t)
|
|
m, c := meterAt(2_000_000)
|
|
shut := m.open("codex", "s2")
|
|
defer shut()
|
|
m.add("codex", "s2", "openai", "gpt", 300)
|
|
c.tick(1)
|
|
sp := onlySpeed(t, m)
|
|
if sp.Rated || sp.Now != 0 || sp.Avg != 0 || sp.Peak != 0 || sp.Output != 0 {
|
|
t.Fatalf("unrated speed has numbers: %+v", sp)
|
|
}
|
|
if v := sp.Curve[curveSpan-1]; v == nil || *v != 300 {
|
|
t.Fatalf("unrated curve's last second %v, want its 300 bytes", v)
|
|
}
|
|
}
|
|
|
|
// A pause past curveRound with nothing in flight starts a new round; one
|
|
// past curveForget drops the session. A request with no session has no
|
|
// curve.
|
|
func TestSessionCurveRoundsAndForgets(t *testing.T) {
|
|
freshRates(t)
|
|
learnRate("p", "m", 100, 100)
|
|
m, c := meterAt(3_000_000)
|
|
shut := m.open("a", "s")
|
|
m.add("a", "s", "p", "m", 1000)
|
|
c.tick(1)
|
|
shut()
|
|
c.tick(int64(curveRound/time.Second) + 1)
|
|
shut = m.open("a", "s")
|
|
m.add("a", "s", "p", "m", 10)
|
|
c.tick(1)
|
|
shut()
|
|
sp := onlySpeed(t, m)
|
|
if sp.Output != 10 || sp.Peak != 10 || sp.Avg != 10 {
|
|
t.Fatalf("new round kept the old one's numbers: %+v", sp)
|
|
}
|
|
m.open("a", "")()
|
|
m.add("a", "", "p", "m", 10)
|
|
if len(m.speeds()) != 1 {
|
|
t.Fatal("a request with no session got a curve")
|
|
}
|
|
c.tick(int64(curveForget/time.Second) + 1)
|
|
if sp := m.speeds(); len(sp) != 0 {
|
|
t.Fatalf("a session quiet past curveForget is kept: %+v", sp)
|
|
}
|
|
}
|
|
|
|
// A long tool run moves the ring on without going through every second
|
|
// in between, and leaves it all gap.
|
|
func TestSessionCurveLongGap(t *testing.T) {
|
|
freshRates(t)
|
|
learnRate("p", "m", 100, 100)
|
|
m, c := meterAt(4_000_000)
|
|
shut := m.open("a", "s")
|
|
m.add("a", "s", "p", "m", 50)
|
|
shut()
|
|
c.tick(int64(curveRound/time.Second) - 1)
|
|
m.open("a", "s")
|
|
c.tick(1)
|
|
sp := onlySpeed(t, m)
|
|
for i, v := range sp.Curve[:curveSpan-1] {
|
|
if v != nil {
|
|
t.Fatalf("second %d of a long gap is %v, want a gap", i, *v)
|
|
}
|
|
}
|
|
if sp.Peak != 50 || sp.Output != 50 {
|
|
t.Fatalf("the round before the gap is lost: %+v", sp)
|
|
}
|
|
}
|
|
|
|
// Through the whole gateway: a session's streamed reply is on its curve,
|
|
// in flight while it streams and not after.
|
|
func TestSessionCurveFromARealStream(t *testing.T) {
|
|
freshRates(t)
|
|
learnRate("live", "m", 6, 6) // a content byte is a token
|
|
release := make(chan struct{})
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("content-type", "text/event-stream")
|
|
io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"abcd\"}}]}\n\n")
|
|
w.(http.Flusher).Flush()
|
|
<-release
|
|
io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"ef\"}}]}\n\n")
|
|
io.WriteString(w, "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":6,\"total_tokens\":9}}\n\ndata: [DONE]\n\n")
|
|
}))
|
|
defer up.Close()
|
|
var once atomic.Bool
|
|
free := func() {
|
|
if once.CompareAndSwap(false, true) {
|
|
close(release)
|
|
}
|
|
}
|
|
defer free()
|
|
if err := provider.Save(provider.Provider{ID: "live", Name: "Live", Chat: up.URL + "/v1", Key: "k", Models: []string{"m"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
clock := &curveClock{}
|
|
clock.at.Store(5_000_000)
|
|
s.curves.now = clock.now
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"live/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`))
|
|
req.Header.Set("content-type", "application/json")
|
|
req.Header.Set(SessionHeader, "sess-e2e")
|
|
rec := httptest.NewRecorder()
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
s.Handler().ServeHTTP(rec, req)
|
|
}()
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
if sp := s.Speeds(); len(sp) == 1 && sp[0].Live {
|
|
if s.curves.roundBytes("sess-e2e") == 4 {
|
|
break
|
|
}
|
|
}
|
|
if time.Now().After(deadline) {
|
|
free()
|
|
t.Fatalf("the first content never reached the session's curve: %+v", s.Speeds())
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
if sp := s.Speeds()[0]; sp.Session != "sess-e2e" || !sp.Live {
|
|
t.Fatalf("mid-stream %+v", sp)
|
|
}
|
|
free()
|
|
<-done
|
|
clock.tick(1)
|
|
sp := s.Speeds()
|
|
if len(sp) != 1 || sp[0].Live || !sp[0].Rated || sp[0].Output != 6 {
|
|
t.Fatalf("after the reply: %+v", sp)
|
|
}
|
|
if v := sp[0].Curve[curveSpan-1]; v == nil || *v != 6 {
|
|
t.Fatalf("the reply's second on the curve is %v, want 6 tokens", v)
|
|
}
|
|
}
|
|
|
|
// Codex's own sign-in is served apart from serve: its reply is on the
|
|
// session's curve too.
|
|
func TestSessionCurveFromCodexOwnSignIn(t *testing.T) {
|
|
setup(t, provider.Chat, &fake{t: t})
|
|
freshRates(t)
|
|
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, sse(
|
|
`data: {"type":"response.output_text.delta","delta":"hello"}`,
|
|
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":9,"output_tokens":5}}}`))
|
|
})
|
|
s := New()
|
|
clock := &curveClock{}
|
|
clock.at.Store(6_000_000)
|
|
s.curves.now = clock.now
|
|
for range liveRateMin + 1 {
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(`{"model":"gpt-5.5","stream":true,"input":"hi"}`))
|
|
req.Header.Set("Authorization", "Bearer chatgpt-token")
|
|
req.Header.Set("session_id", "codex-sess")
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
clock.tick(1)
|
|
sp := s.Speeds()
|
|
if len(sp) != 1 || sp[0].Session != "codex-sess" || !sp[0].Rated {
|
|
t.Fatalf("speeds %+v", sp)
|
|
}
|
|
// four replies of five content bytes, each five tokens
|
|
if sp[0].Output != 20 {
|
|
t.Fatalf("output %d, want 20", sp[0].Output)
|
|
}
|
|
}
|