diff --git a/docs/reference.md b/docs/reference.md index d048458d9..4bef0ab59 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -447,8 +447,10 @@ its end, however long the burst took to come: one whose content came in under a quarter of its window (`flow_ms`, from a tenth of its bytes to nine tenths, scaled to the whole), or that reasoned and whose answer would have come over 20 times faster than its reasoning came before its first text (its text came -with the burst). The time a burst takes to come is not the time it took to -write, and that time isn't seen. History is read the same way, as it keeps the +with the burst), or whose window was under a second after a wait over four +times longer (`usage.HeldWindowMs`: 392 tokens 0.25 s after an 8.2 s first +token read 1,539 tok/s from gpt-6.1-sol). The time a burst takes to come is +not the time it took to write, and that time isn't seen. History is read the same way, as it keeps the reasoning, the first text and the flow; a record from before the flow was kept is told by its reasoning's pace alone. @@ -2459,7 +2461,8 @@ curl -s http://127.0.0.1:3425/v1/magpie/usage/requests \ be before `ttft_ms` or after `ms`. `flow_ms` is how long the content took to come, from where the speed is timed: the time from a tenth of it to nine tenths, times 10/8 (at most `ms` × 10/8). A reply whose - content came in under a quarter of its window was held and let go in a + content came in under a quarter of its window, or whose window was under + a second after a wait over four times longer, was held and let go in a burst, and tells no speed. Leave out what you don't know. - **Pricing:** a record without `cost` is priced at magpie's prices for its provider and model. diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index bf6a84b70..f9fd88734 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -665,7 +665,9 @@ however old, so a client's history can be backfilled. speed is `usage.DecodeOf`'s, as a gateway row's is, on the server (`Row.Decode`, the totals' `DecodeN`) and in `page.js`'s `ledDecode`. A row that reasoned tells none without `first_text_ms`; `ThinkPace` and, - with `flow_ms`, `HeldShare` tell a reply let go in a burst. `first_text_ms` + with `flow_ms`, `HeldShare` tell a reply let go in a burst, as does + `HeldWindowMs`: a window under a second after a wait over four times + longer. `first_text_ms` may not be before `ttft_ms` (the first text is content too) or after `ms`; `flow_ms` is the middle 80% of the content's time scaled by 10/8, as `firstToken.flowMs` measures it, so it is bounded by `ms` × 10/8, not diff --git a/internal/gateway/reasoning_speed_test.go b/internal/gateway/reasoning_speed_test.go index f1eafec08..2f9292dc5 100644 --- a/internal/gateway/reasoning_speed_test.go +++ b/internal/gateway/reasoning_speed_test.go @@ -373,7 +373,16 @@ func TestLiveVisibleReasoningFixture(t *testing.T) { 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) + // 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) } diff --git a/internal/gateway/short_window_speed_test.go b/internal/gateway/short_window_speed_test.go new file mode 100644 index 000000000..ff6de0210 --- /dev/null +++ b/internal/gateway/short_window_speed_test.go @@ -0,0 +1,28 @@ +package gateway + +import ( + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" +) + +// Speed first weighs a seat by the pace usage.DecodeOf tells: a reply that +// came whole 255 ms after an 8.2 s first token (pluviophile714 on Discord, +// 1,539 tok/s from gpt-6.1-sol) gives its seat a first token, not a pace, +// so a relay that holds replies doesn't pass for the fastest. +func TestShortWindowAfterLongWaitNotesNoPace(t *testing.T) { + forgetSpeeds() + t.Cleanup(forgetSpeeds) + c := candidate{p: provider.Provider{ID: "v"}, rest: "v", model: "gpt-6.1-sol"} + t0 := time.Date(2026, 11, 3, 12, 0, 0, 0, time.UTC) + noteTrySpeed(c, Try{Millis: 8455, TTFT: 8200, FirstText: 8200, Flow: 230}, 392, 0, t0) + if ttft, tps := speedOf(c, t0); ttft != 8200 || tps != 0 { + t.Fatalf("burst after a long wait: ttft %v, tps %v; want 8200, 0", ttft, tps) + } + // a streamed answer keeps its pace: 392 tokens over 4 s + noteTrySpeed(c, Try{Millis: 12200, TTFT: 8200, FirstText: 8200, Flow: 4000}, 392, 0, t0) + if _, tps := speedOf(c, t0); tps != 98 { + t.Fatalf("streamed answer: tps %v; want 98", tps) + } +} diff --git a/internal/gateway/usage_ingest_test.go b/internal/gateway/usage_ingest_test.go index 5bfb20e9f..a5e34ef31 100644 --- a/internal/gateway/usage_ingest_test.go +++ b/internal/gateway/usage_ingest_test.go @@ -348,7 +348,10 @@ func TestUsageIngestConcurrent(t *testing.T) { // grok-4.7 reply that reasoned, tells its answer's speed from its first // text; without first_text_ms it told none. A reply let go in a burst // tells none, whether its flow_ms says so (HeldShare) or its answer came -// over 20 times faster than its reasoning before it (ThinkPace). +// over 20 times faster than its reasoning before it (ThinkPace). The +// reporter's own record, all of it 403 ms after a 4.6 s first token, now +// tells none either (HeldWindowMs, pluviophile714 on Discord); the same +// record after a 0.3 s first token tells its answer's. func TestUsageIngestTellsSpeed(t *testing.T) { do := ingestServer(t) repro := `{ @@ -357,13 +360,14 @@ func TestUsageIngestTellsSpeed(t *testing.T) { at := time.Now().Add(-time.Hour).UTC().Format(time.RFC3339) // a reply that didn't reason, its 601 tokens let go in the last 109 ms // (its flow, scaled as the gateway scales it, 136 ms) of a 4 s window + quick := strings.NewReplacer(`"ttft_ms":4632,"first_text_ms":4700`, `"ttft_ms":232,"first_text_ms":300`, `"ms":5035`, `"ms":635`, "repro-1", "quick-1").Replace(repro) burst := `{"t":"` + at + `","agent":"my-app","provider":"xai","model":"grok-4.7","rid":"burst-1","in":1000,"out":601,"ms":5035,"ttft_ms":1000,"flow_ms":136,"status":200}` // the same reply, streamed as it was written even := strings.Replace(strings.Replace(burst, `"flow_ms":136`, `"flow_ms":4000`, 1), "burst-1", "even-1", 1) // 22 reasoning tokens over 4.4 s, then 528 answer tokens in 335 ms held := `{"t":"` + at + `","agent":"my-app","provider":"xai","model":"grok-4.7","rid":"held-1","in":1000,"out":550,"reasoning":22,"ms":5035,"ttft_ms":300,"first_text_ms":4700,"status":200}` - c, out, b := do("127.0.0.1:5000", "["+repro+","+burst+","+even+","+held+"]") - if c != 200 || out.Accepted != 4 { + c, out, b := do("127.0.0.1:5000", "["+repro+","+quick+","+burst+","+even+","+held+"]") + if c != 200 || out.Accepted != 5 { t.Fatal(c, b) } @@ -382,8 +386,9 @@ func TestUsageIngestTellsSpeed(t *testing.T) { speed[row.RequestID] = [2]int64{int64(n), w} } want := map[string][2]int64{ - "repro-1": {307 - 129, 5035 - 4700}, // its answer, from its first text - "burst-1": {0, 0}, // held: its content came in under a quarter of its window + "repro-1": {0, 0}, // held: all of it under half a second after 4.6 s + "quick-1": {307 - 129, 635 - 300}, // its answer, from its first text + "burst-1": {0, 0}, // held: its content came in under a quarter of its window "even-1": {601, 5035 - 1000}, "held-1": {0, 0}, // held: its answer came over 20 times its reasoning's pace } diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index a48d39979..14dfc0ce8 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -410,7 +410,10 @@ function routingPage(part) { // A reply held back and let go in a burst at its end tells no speed // (usage.DecodeOf, John on Discord: Kimi Code at 2,237 tok/s): its // content came in under a quarter of its window (flow), or its answer - // at over 20 times the pace its reasoning came before its first text. + // at over 20 times the pace its reasoning came before its first text, or + // it came in under a second after a wait four times longer + // (pluviophile714 on Discord: gpt-6.1-sol, 392 tokens 0.25 s after an + // 8.2 s first token, read 1,539 tok/s; usage.HeldWindowMs). function decodeOf(r, ms, ttft, firstText, flow = r.flow, firstReasoning = r.firstReasoning, reasoningHidden = r.reasoningHidden) { if (reasoningHidden && !(firstText > 0)) return null; const think = reasoningOf(r), visible = think > 0 && !(firstText > 0) && firstReasoning >= ttft && flow > 0 && think <= r.out; @@ -418,6 +421,7 @@ function routingPage(part) { const w = ms - from; if (!(n > 0 && ttft > 0 && from > 0 && w >= 100 && n * 1000 <= 10000 * w)) return null; if (flow > 0 && flow * 4 < w) return null; + if (w < 1000 && from > 4 * w) return null; if (think > 0 && firstText > 0 && n * (firstText - ttft) > 20 * think * w) return null; return { n, w }; } diff --git a/internal/gui/assets/plugins/usage/i18n.js b/internal/gui/assets/plugins/usage/i18n.js index 804ec3277..03be48301 100644 --- a/internal/gui/assets/plugins/usage/i18n.js +++ b/internal/gui/assets/plugins/usage/i18n.js @@ -235,6 +235,7 @@ magpie.strings({ "All its output was reasoning: no answer after it to time a speed from": "输出全是推理,之后没有正文,算不出速度", "Too little came after the first token to time a speed by: {n} tokens in {ms}": "首字后内容太少,测不出速度:{ms} 输出 {n} 个 token", "It came in one burst at its end, not as it was written, so how fast it was written isn't seen": "内容在最后一次性到达,不是边写边发,看不出写得多快", + "It came in under a second after a wait of {wait}: it may have been written during the wait and let go at once, so how fast it was written isn't seen": "等了 {wait} 后不到一秒就全部到达:可能是在等待中写完再一次性发出,看不出写得多快", "At least: {n} answer tokens in {ms} after the first token, the {r} reasoning tokens left out. No first text was recorded, so reasoning after the first token is counted too": "下限:首字后 {ms} 输出 {n} 个正文 token,{r} 个推理 token 不计。没有记录首个正文的时间,首字后的推理时间也算在里面", "At least: {n} output tokens in {ms}, from the request to its last token, the wait for the first one counted too": "下限:从收到请求到最后一个 token,{ms} 输出 {n} 个 token,等首字的时间也算在里面", "{i} in · {o} out · {r} cache read · {w} cache creation": "输入 {i} · 输出 {o} · 缓存读取 {r} · 缓存创建 {w}", @@ -476,6 +477,7 @@ magpie.strings({ "All its output was reasoning: no answer after it to time a speed from": "輸出全是推理,之後沒有正文,算不出速度", "Too little came after the first token to time a speed by: {n} tokens in {ms}": "首字後內容太少,測不出速度:{ms} 輸出 {n} 個 token", "It came in one burst at its end, not as it was written, so how fast it was written isn't seen": "內容在最後一次性到達,不是邊寫邊發,看不出寫得多快", + "It came in under a second after a wait of {wait}: it may have been written during the wait and let go at once, so how fast it was written isn't seen": "等了 {wait} 後不到一秒就全部到達:可能是在等待中寫完再一次性送出,看不出寫得多快", "At least: {n} answer tokens in {ms} after the first token, the {r} reasoning tokens left out. No first text was recorded, so reasoning after the first token is counted too": "下限:首字後 {ms} 輸出 {n} 個正文 token,{r} 個推理 token 不計。沒有記錄首個正文的時間,首字後的推理時間也算在裡面", "At least: {n} output tokens in {ms}, from the request to its last token, the wait for the first one counted too": "下限:從收到請求到最後一個 token,{ms} 輸出 {n} 個 token,等首字的時間也算在裡面", "{i} in · {o} out · {r} cache read · {w} cache creation": "輸入 {i} · 輸出 {o} · 快取讀取 {r} · 快取建立 {w}", @@ -717,6 +719,7 @@ magpie.strings({ "All its output was reasoning: no answer after it to time a speed from": "出力はすべて推論で、その後に回答がないため、速度を計れません", "Too little came after the first token to time a speed by: {n} tokens in {ms}": "最初のトークンの後が少なすぎて速度を計れません:{ms} で {n} トークン", "It came in one burst at its end, not as it was written, so how fast it was written isn't seen": "最後に一度に届き、書かれたとおりに届いていないため、書く速さはわかりません", + "It came in under a second after a wait of {wait}: it may have been written during the wait and let go at once, so how fast it was written isn't seen": "{wait} 待った後、1 秒足らずで全部届きました。待つ間に書かれて一度に送られたかもしれないため、書く速さはわかりません", "At least: {n} answer tokens in {ms} after the first token, the {r} reasoning tokens left out. No first text was recorded, so reasoning after the first token is counted too": "下限:最初のトークン後 {ms} で回答 {n} トークン(推論 {r} トークンは含めず)。最初の本文の時刻が記録されていないため、最初のトークン後の推論の時間も含まれます", "At least: {n} output tokens in {ms}, from the request to its last token, the wait for the first one counted too": "下限:リクエストを受けてから最後のトークンまで {ms} で {n} トークンを出力。最初のトークンを待つ時間も含まれます", "{i} in · {o} out · {r} cache read · {w} cache creation": "入力 {i} · 出力 {o} · キャッシュ読み取り {r} · キャッシュ作成 {w}", @@ -958,6 +961,7 @@ magpie.strings({ "All its output was reasoning: no answer after it to time a speed from": "Die ganze Ausgabe war Reasoning: keine Antwort danach, an der sich ein Tempo messen ließe", "Too little came after the first token to time a speed by: {n} tokens in {ms}": "Nach dem ersten Token kam zu wenig für ein Tempo: {n} Token in {ms}", "It came in one burst at its end, not as it was written, so how fast it was written isn't seen": "Sie kam am Ende in einem Schwung, nicht so, wie sie geschrieben wurde, also ist nicht zu sehen, wie schnell sie geschrieben wurde", + "It came in under a second after a wait of {wait}: it may have been written during the wait and let go at once, so how fast it was written isn't seen": "Sie kam nach einer Wartezeit von {wait} in weniger als einer Sekunde: Sie wurde womöglich während des Wartens geschrieben und auf einmal losgelassen, also ist nicht zu sehen, wie schnell sie geschrieben wurde", "At least: {n} answer tokens in {ms} after the first token, the {r} reasoning tokens left out. No first text was recorded, so reasoning after the first token is counted too": "Mindestens: {n} Antwort-Token in {ms} nach dem ersten Token, ohne die {r} Reasoning-Token. Kein erster Text aufgezeichnet, also zählt Reasoning nach dem ersten Token mit", "At least: {n} output tokens in {ms}, from the request to its last token, the wait for the first one counted too": "Mindestens: {n} Ausgabe-Token in {ms}, von der Anfrage bis zu ihrem letzten Token, das Warten auf das erste mitgezählt", "{i} in · {o} out · {r} cache read · {w} cache creation": "{i} Eingabe · {o} Ausgabe · {r} Cache-Lesen · {w} Cache-Erstellung", diff --git a/internal/gui/assets/plugins/usage/page.js b/internal/gui/assets/plugins/usage/page.js index d34365786..0dd0adb5c 100644 --- a/internal/gui/assets/plugins/usage/page.js +++ b/internal/gui/assets/plugins/usage/page.js @@ -2028,13 +2028,19 @@ const LED_COLS = [ ["In", "n"], ["Out", "n"], ["Cache write", "n", true], ["Cache read", "n", true], ["Cost", "n"], ["TTFT", "n", 2, "Time to first token"], ["Duration", "n"], ["Speed", "n"], ["Status"], ]; +// a window under a second after a wait over four times longer: the reply came +// whole in a moment after a long silence, and what it was written over isn't +// seen (pluviophile714 on Discord: gpt-6.1-sol, 392 tokens 0.25 s after an +// 8.2 s first token, read 1,539 tok/s; usage.HeldWindowMs) +const ledHeldShort_ = (w, from) => w < 1000 && from > 4 * w; // the tokens a reply was seen to write and the ms it took, as // usage.DecodeOf and routing/client.js's decodeOf tell them: one that reasoned // counts its answer from its first text, its reasoning written before // the stream showed any (tony on Discord); null when it tells no speed. // A reply held back and let go in a burst at its end tells none (John on // Discord): its content came in under a quarter of its window (flow_ms), -// or its answer at over 20 times the pace its reasoning came before it +// or its answer at over 20 times the pace its reasoning came before it, +// or it came in under a second after a wait four times longer (ledHeldShort_) const ledDecode = (r) => { if (ledFailed_(r) || r.reasoning_hidden && !(r.first_text_ms > 0) || !(r.ttft_ms > 0)) return null; const think = r.reasoning > 0, visible = think && !(r.first_text_ms > 0) && r.first_reasoning_ms >= r.ttft_ms && r.flow_ms > 0 && r.reasoning <= r.out; @@ -2042,6 +2048,7 @@ const ledDecode = (r) => { const w = r.ms - from; if (!(n > 0 && from > 0 && w >= 100 && n * 1000 <= 10000 * w)) return null; if (r.flow_ms > 0 && r.flow_ms * 4 < w) return null; + if (ledHeldShort_(w, from)) return null; if (think && r.first_text_ms > 0 && n * (r.first_text_ms - r.ttft_ms) > 20 * r.reasoning * w) return null; if (ledCodexTurn(r) && (w < LED_CODEX_WINDOW_MS || n < LED_CODEX_BODY)) return null; return { n, w }; @@ -2114,6 +2121,7 @@ const ledNoSpeed_ = (r) => { const from = visible ? r.first_reasoning_ms : think && r.first_text_ms > 0 ? r.first_text_ms : r.ttft_ms, w = r.ms - from; const little = t("Too little came after the first token to time a speed by: {n} tokens in {ms}", { n: ledNum(n), ms: ledTook(Math.max(0, w)) }); if (w < 100) return little; + if (ledHeldShort_(w, from) && !(n * 1000 > 10000 * w || r.flow_ms > 0 && r.flow_ms * 4 < w)) return t("It came in under a second after a wait of {wait}: it may have been written during the wait and let go at once, so how fast it was written isn't seen", { wait: ledTook(from) }); if (n * 1000 > 10000 * w || r.flow_ms > 0 && r.flow_ms * 4 < w || think && r.first_text_ms > 0 && n * (r.first_text_ms - r.ttft_ms) > 20 * r.reasoning * w) return t("It came in one burst at its end, not as it was written, so how fast it was written isn't seen"); return little; }; diff --git a/internal/gui/tests/routing-burst-speed.test.cjs b/internal/gui/tests/routing-burst-speed.test.cjs index d73452018..f8843aef1 100644 --- a/internal/gui/tests/routing-burst-speed.test.cjs +++ b/internal/gui/tests/routing-burst-speed.test.cjs @@ -24,10 +24,14 @@ const key = { id: "antigravity", provider: "antigravity", name: "Antigravity", k // a burst let go over 236 ms and 196 ms: 550 tokens, 22 of them reasoning, // in 16 s with the first content at 5.2 s, its text early (5.4 s) and // with the burst (15.764 s); 395 tokens in 19 s. And a steady 50 tok/s -// answer after the same reasoning, which keeps its speed. +// answer after the same reasoning, which keeps its speed. Last, +// pluviophile714's row (gpt-6.1-sol on Discord: 1,539 tok/s): 392 tokens +// 255 ms after an 8.2 s first token, spread over most of that (flow 230 ms). +// Under a second after a wait over four times longer tells no speed. // [out, ms, ttft, flow, reasoning, firstText] const timing = [[8264, 24360, 24359], [8264, 1500, 1000], [1200, 15000, 3000], [100, 3000, 1000], [100, 3000, 1000], [100, 3000, 1000], [2187, 6700, 5100, 2], - [550, 16000, 5200, 236, 22, 5400], [550, 16000, 5200, 236, 22, 15764], [395, 19000, 5200, 196], [552, 16000, 5200, 10600, 22, 5400]]; + [550, 16000, 5200, 236, 22, 5400], [550, 16000, 5200, 236, 22, 15764], [395, 19000, 5200, 196], [552, 16000, 5200, 10600, 22, 5400], + [392, 8455, 8200, 230, 0, 8200]]; const routes = timing.map(([out, ms, ttft, flow, reasoning, firstText], i) => ({ id: 100 - i, seq: 100 - i, time: at(i), agent: "codex", model: "antigravity/gemini-3.8-flash", provider: "antigravity", order: [key], tries: [{ id: key.id, model: key.model, start: at(i), done: true, status: 200, ms, ttft, flow, firstText }], @@ -60,8 +64,8 @@ function serve(lang) { } const want = { - en: { first: /first token in 24 s/, speed: /tok\/s/, normal: /· 100 tok\/s/, slow: /· 50 tok\/s/ }, - zh: { first: /24 秒 后出首字/, speed: /token\/秒/, normal: /· 100 token\/秒/, slow: /· 50 token\/秒/ }, + en: { first: /first token in 24 s/, late: /first token in 8\.2 s/, speed: /tok\/s/, normal: /· 100 tok\/s/, slow: /· 50 tok\/s/ }, + zh: { first: /24 秒 后出首字/, late: /8\.2 秒 后出首字/, speed: /token\/秒/, normal: /· 100 token\/秒/, slow: /· 50 token\/秒/ }, }; for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { @@ -144,6 +148,11 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", } // a steady answer after the same reasoning keeps its 50 tok/s assert.match(await story(10), want[lang].slow); + // the report: its first token, and no 1,539 tok/s + const late = await story(11); + assert.match(late, want[lang].late); + assert.doesNotMatch(late, want[lang].speed); + assert.doesNotMatch(late, /1539|1,539/); assert.deepEqual(errors, []); }); } diff --git a/internal/gui/tests/usage-speed-every-row.test.cjs b/internal/gui/tests/usage-speed-every-row.test.cjs index ea5e7e181..a7d0c2a94 100644 --- a/internal/gui/tests/usage-speed-every-row.test.cjs +++ b/internal/gui/tests/usage-speed-every-row.test.cjs @@ -43,7 +43,19 @@ const ROWS = [ { ...GW, route_id: 8, t: at(7), out: 41, reasoning: 41, ms: 6000, ttft_ms: 5000, reasoning_hidden: true, flow_ms: 1 }, // 8: measured: 500 tokens in the 5 s after its first { ...GW, route_id: 9, t: at(8), out: 500, ms: 7000, ttft_ms: 2000, first_text_ms: 2000, flow_ms: 5000 }, + // 9: pluviophile714 on Discord: gpt-6.1-sol, 392 tokens 255 ms after an + // 8.2 s first token, spread over most of that (flow 230 ms), read 1,539 + // tok/s: under a second after a wait over four times longer tells none + { ...GW, route_id: 10, t: at(9), req: "codex/gpt-6.1-sol", model: "gpt-6.1-sol", served: "gpt-6.1-sol", in: 21617, cache_read: 0, out: 392, ms: 8455, ttft_ms: 8200, first_text_ms: 8200, flow_ms: 230 }, ]; +// row 9's tooltip in every language its words are in +const HELD = { + en: "It came in under a second after a wait of 8.2 s: it may have been written during the wait and let go at once, so how fast it was written isn't seen", + zh: "等了 8.2 秒 后不到一秒就全部到达:可能是在等待中写完再一次性发出,看不出写得多快", + "zh-TW": "等了 8.2 秒 後不到一秒就全部到達:可能是在等待中寫完再一次性送出,看不出寫得多快", + ja: "8.2 秒 待った後、1 秒足らずで全部届きました。待つ間に書かれて一度に送られたかもしれないため、書く速さはわかりません", + de: "Sie kam nach einer Wartezeit von 8.2 s in weniger als einer Sekunde: Sie wurde womöglich während des Wartens geschrieben und auf einmal losgelassen, also ist nicht zu sehen, wie schnell sie geschrieben wurde", +}; function ledger(period) { return { @@ -111,7 +123,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit"); const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); t.after(() => browser.close()); - for (const lang of ["en", "zh"]) { + for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { const w = L[lang]; await t.test(lang, async () => { const errors = []; @@ -137,6 +149,10 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert(s, "row " + i + " has a speed or a dash"); if (s === "—") assert((await tip(i)).length > 0, "row " + i + "'s dash says why"); } + // the report: no 1,539 tok/s, and why + assert.equal(await text(9), "—"); + assert.equal(await tip(9), HELD[lang]); + if (!w) { assert.deepEqual(errors, []); await ctx.close(); return; } // not streamed, long enough: the least it wrote at, and why it's a floor assert.equal(await text(0), w.whole); diff --git a/internal/usage/reasoning_stream_speed_test.go b/internal/usage/reasoning_stream_speed_test.go index 6d9047399..6ebd20360 100644 --- a/internal/usage/reasoning_stream_speed_test.go +++ b/internal/usage/reasoning_stream_speed_test.go @@ -24,10 +24,10 @@ func TestVisibleReasoningSpeed(t *testing.T) { {"held reasoning just below quarter", func() Record { r := visible; r.Flow = 3499; return r }(), 0, 0}, {"held reasoning", func() Record { r := visible; r.Flow = 1000; return r }(), 0, 0}, {"invalid count", func() Record { r := visible; r.Reasoning = 1600; return r }(), 0, 0}, - {"below decode minimum", Record{Output: 10, Reasoning: 5, Millis: 1099, TTFT: 1000, FirstReasoning: 1000, Flow: 99}, 0, 0}, - {"at decode minimum", Record{Output: 10, Reasoning: 5, Millis: 1100, TTFT: 1000, FirstReasoning: 1000, Flow: 100}, 10, 100}, - {"above max speed", Record{Output: 1001, Reasoning: 5, Millis: 1100, TTFT: 1000, FirstReasoning: 1000, Flow: 100}, 0, 0}, - {"at max speed", Record{Output: 1000, Reasoning: 5, Millis: 1100, TTFT: 1000, FirstReasoning: 1000, Flow: 100}, 1000, 100}, + {"below decode minimum", Record{Output: 10, Reasoning: 5, Millis: 119, TTFT: 20, FirstReasoning: 20, Flow: 99}, 0, 0}, + {"at decode minimum", Record{Output: 10, Reasoning: 5, Millis: 120, TTFT: 20, FirstReasoning: 20, Flow: 100}, 10, 100}, + {"above max speed", Record{Output: 1001, Reasoning: 5, Millis: 120, TTFT: 20, FirstReasoning: 20, Flow: 100}, 0, 0}, + {"at max speed", Record{Output: 1000, Reasoning: 5, Millis: 120, TTFT: 20, FirstReasoning: 20, Flow: 100}, 1000, 100}, {"redacted with no reasoning count", Record{Output: 120, Millis: 9000, TTFT: 1000, Flow: 7000, ReasoningHidden: true}, 0, 0}, {"late hidden think", Record{Output: 1500, Reasoning: 1400, Millis: 9000, TTFT: 8000, Flow: 900}, 0, 0}, } { diff --git a/internal/usage/short_window_speed_test.go b/internal/usage/short_window_speed_test.go new file mode 100644 index 000000000..b9a9263d6 --- /dev/null +++ b/internal/usage/short_window_speed_test.go @@ -0,0 +1,47 @@ +package usage + +import "testing" + +// pluviophile714 on Discord: a gpt-6.1-sol row on the Routing page read +// 1,539 tok/s, where other monitors read ~70: 392 output tokens, its first +// token at 8.2 s and its end at 8.3 s. The reply came whole in a moment after +// a long wait, spread over ~250 ms, so neither its flow nor the 100 ms floor +// told it from a stream: a window under HeldWindowMs after a wait HeldShare +// times longer tells no speed. A fast vendor's short reply after a short +// wait, and a long window after a long wait, keep theirs. +func TestShortWindowAfterLongWaitTellsNoSpeed(t *testing.T) { + for _, c := range []struct { + name string + r Record + n int + w int64 + }{ + // the report: 392 tokens 255 ms after an 8.2 s first token, the + // content spread over most of the window (flow 230 ms) + {"the report", Record{Output: 392, Millis: 8455, TTFT: 8200, FirstText: 8200, Flow: 230, Status: 200}, 0, 0}, + // a row of the owner's own ledger: gpt-6-sol, 42 reasoning tokens, + // 376 answer tokens 315 ms after its first text at 10 s (1,194 tok/s) + {"reasoned, answer after a long think", Record{Output: 418, Reasoning: 42, Millis: 10325, TTFT: 9949, FirstText: 10010, Flow: 172, ReasoningHidden: true, Status: 200}, 0, 0}, + // the flow unknown (a record from before it was kept) + {"no flow kept", Record{Output: 392, Millis: 8455, TTFT: 8200, Status: 200}, 0, 0}, + // a fast vendor: 500 tokens in 250 ms after a 300 ms first token + {"fast vendor, short wait", Record{Output: 500, Millis: 550, TTFT: 300, Flow: 250, Status: 200}, 500, 250}, + // at the boundary: a wait of four windows still tells, one more ms doesn't + {"wait four windows", Record{Output: 200, Millis: 4995, TTFT: 3996, Flow: 999, Status: 200}, 200, 999}, + {"wait past four windows", Record{Output: 200, Millis: 4996, TTFT: 3997, Flow: 999, Status: 200}, 0, 0}, + // a second or more is timed however long the wait + {"a second after a long wait", Record{Output: 100, Millis: 9200, TTFT: 8200, Flow: 1000, Status: 200}, 100, 1000}, + } { + t.Run(c.name, func(t *testing.T) { + if n, w := c.r.Decode(); n != c.n || w != c.w { + t.Fatalf("speed counted %d tokens/%d ms; want %d/%d", n, w, c.n, c.w) + } + }) + } + // the report's row adds its first token to a period's totals, not a speed + var tot Totals + tot.addRow(Row{Record: Record{Output: 392, Millis: 8455, TTFT: 8200, FirstText: 8200, Flow: 230, Status: 200}}) + if tot.Timed != 1 || tot.DecodeN != 0 || tot.Speed() != 0 { + t.Fatalf("totals: timed %d, decode_n %d, speed %v", tot.Timed, tot.DecodeN, tot.Speed()) + } +} diff --git a/internal/usage/speed_count_test.go b/internal/usage/speed_count_test.go index 9d29928a9..af5083a65 100644 --- a/internal/usage/speed_count_test.go +++ b/internal/usage/speed_count_test.go @@ -152,7 +152,8 @@ func TestCodexWholeTurnFloorNeedsAThousandTokens(t *testing.T) { // body 100 tokens or more; else it tells the least it wrote at over the // whole turn, when that is 1,000 tokens or more over a second or more, as // Claude Code's whole-time floor is. The gateway's own rows, and other -// agents', keep their speed. +// agents', keep their speed after a short wait, and tell none 0.2 s after a +// long one (HeldWindowMs). func TestCodexTurnMeasuredOnlyOverASecondAndAHundredTokens(t *testing.T) { row := func(out int, ms, ttft int64) Row { return Row{Source: "log", Record: Record{Agent: "codex", Model: "gpt-6.1-sol", Output: out, Millis: ms, TTFT: ttft}} @@ -181,13 +182,20 @@ func TestCodexTurnMeasuredOnlyOverASecondAndAHundredTokens(t *testing.T) { t.Errorf("%s: floor %d/%d ms, want %d/%d", c.name, n, ms, c.floorN, c.floorMs) } } - // the same reply through the gateway, or Claude Code's, keeps its speed + // the same reply through the gateway, or Claude Code's, tells none + // either: 0.2 s after a 6.8 s wait is a reply let go whole + // (HeldWindowMs, pluviophile714 on Discord); after a 0.3 s first token, + // they keep their speed for _, r := range []Row{ {Record: Record{Agent: "codex", Output: 380, Millis: 7000, TTFT: 6800}}, {Source: "log", Record: Record{Agent: "claude", Output: 380, Millis: 7000, TTFT: 6800}}, } { + if n, w := r.Decode(); n != 0 || w != 0 { + t.Errorf("%s/%q: measured %d/%d ms, want none", r.Agent, r.Source, n, w) + } + r.Millis, r.TTFT = 500, 300 if n, w := r.Decode(); n != 380 || w != 200 { - t.Errorf("%s/%q: measured %d/%d ms, want 380/200", r.Agent, r.Source, n, w) + t.Errorf("%s/%q after a short wait: measured %d/%d ms, want 380/200", r.Agent, r.Source, n, w) } } // a group and the ledger's sum count it as a floor, not 1,900 tok/s diff --git a/internal/usage/usage.go b/internal/usage/usage.go index d94ae74f0..71daf4f73 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -665,6 +665,16 @@ func DecodeWindow(out int, ms, ttft int64) int64 { // reasoning hidden from the stream (OpenAI's, Claude's omitted) comes // before the first content and leaves this time short; one that // streams comes at the pace its answer does. +// - a window under HeldWindowMs after a wait HeldShare times longer: the +// reply came whole in a moment after a long silence, and what it was +// written over is not seen (pluviophile714 on Discord: gpt-6.1-sol, +// 392 tokens 0.25 s after an 8.2 s first token, read 1,539 tok/s where +// the vendor writes ~70). Its content can come spread over that moment +// (a relay replaying a held reply, OpenAI's answer let go after its +// hidden reasoning), so flow doesn't tell it. A stream as it is written +// takes a second or more to write a few hundred tokens; one that takes +// under a second is too short a sample to tell a speed by against such +// a wait, so a short answer after a long think tells none either. func DecodeOf(out, reasoning int, ms, ttft, firstText, flow int64, firstReasoning ...int64) (tokens int, w int64) { start := ttft if reasoning > 0 { @@ -685,19 +695,24 @@ func DecodeOf(out, reasoning int, ms, ttft, firstText, flow int64, firstReasonin if flow > 0 && flow*HeldShare < w { return 0, 0 } + if w < HeldWindowMs && start > HeldShare*w { + return 0, 0 + } if reasoning > 0 && firstText > 0 && int64(out)*(firstText-ttft) > ThinkPace*int64(reasoning)*w { return 0, 0 } return out, w } -// HeldShare and ThinkPace tell a reply held back and let go in a burst -// (DecodeOf): its content came in under a quarter of its window, or its -// answer at over 20 times the pace its reasoning came. routing/client.js's -// decodeOf and app.js's ledDecode have the same. +// HeldShare, ThinkPace and HeldWindowMs tell a reply held back and let go +// in a burst (DecodeOf): its content came in under a quarter of its window, +// its answer at over 20 times the pace its reasoning came, or its window +// was under a second and under a quarter of the wait before it. +// routing/client.js's decodeOf and usage/page.js's ledDecode have the same. const ( - HeldShare = 4 - ThinkPace = 20 + HeldShare = 4 + ThinkPace = 20 + HeldWindowMs = 1000 ) // Decode is the record's DecodeOf.