diff --git a/docs/reference.md b/docs/reference.md index 3e7d41db2..9efb834f8 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -261,13 +261,15 @@ first text (`usage.DecodeOf`): hidden reasoning is written before the stream shows anything, so counting it in a window that starts after it read gpt-6.1-sol at hundreds of tok/s. A reply that reasoned and then wrote only tool calls, one not streamed, and a burst (under 100 ms, or over 10,000 tok/s) -tell no speed. The window is no longer than the reply's content took to come -(`flow_ms`, from a tenth of its bytes to nine tenths, scaled to the whole): a -vendor that sends the first words, holds the rest and lets it go in one burst -gives a flow of next to none, so such a reply tells no speed instead of its -tokens over the wait. 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 read -over its whole window. +tell no speed. So does a reply its vendor held back and let go in a burst at +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 +reasoning, the first text and the flow; a record from before the flow was kept +is told by its reasoning's pace alone. It lists **accounts** too: each Codex, Claude or other subscription account's tokens and cost, by the account that actually answered — the one that took diff --git a/internal/gateway/held_speed_test.go b/internal/gateway/held_speed_test.go new file mode 100644 index 000000000..083f53d38 --- /dev/null +++ b/internal/gateway/held_speed_test.go @@ -0,0 +1,271 @@ +package gateway + +import ( + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/usage" +) + +// John on Discord, again after v0.1.1119: Kimi Code (api.kimi.ai, Chat, +// k3-256k) answering OpenCode read 2,237 tok/s for 550 tokens, 22 of them +// reasoning, in 16 s with its first content at 5.2 s, and 2,012 tok/s for +// 395 tokens in 19 s. The burst those replies came in was let go over a +// couple of hundred milliseconds, many events, not in one write, so the +// time it took to come passed for the time it took to write. + +// kimiStep is what a fake Kimi Code writes at a moment after the request. +type kimiStep struct { + at time.Duration + events []string +} + +// kimiTimed is a fake Kimi Code that writes each step at its moment. +type kimiTimed struct { + steps map[string][]kimiStep // by the model asked for +} + +func (k *kimiTimed) ServeHTTP(w http.ResponseWriter, r *http.Request) { + start := time.Now() + body, _ := io.ReadAll(r.Body) + var steps []kimiStep + for m, s := range k.steps { + if strings.Contains(string(body), `"model":"`+m+`"`) { + steps = s + } + } + w.Header().Set("Content-Type", "text/event-stream") + f := w.(http.Flusher) + for _, s := range steps { + time.Sleep(time.Until(start.Add(s.at))) + io.WriteString(w, strings.Join(s.events, "\n\n")+"\n\n") + f.Flush() + } +} + +func kimiChunk(delta string) string { + return `data: {"id":"chatcmpl-1","object":"chat.completion.chunk","created":1791500000,"model":"k3-256k","choices":[{"index":0,"delta":` + delta + `,"finish_reason":null}]}` +} + +func kimiEnd(finish string, out, reasoning int) []string { + return []string{ + `data: {"id":"chatcmpl-1","object":"chat.completion.chunk","created":1791500000,"model":"k3-256k","choices":[{"index":0,"delta":{},"finish_reason":"` + finish + `"}],"usage":{"prompt_tokens":538,"completion_tokens":` + fmt.Sprint(out) + `,"total_tokens":` + fmt.Sprint(538+out) + `,"completion_tokens_details":{"reasoning_tokens":` + fmt.Sprint(reasoning) + `}}}`, + `data: [DONE]`, + } +} + +// kimiShape is one of the ways the reply can have come, each over the same +// times: its first content (reasoning) at first, ended at end. +type kimiShape struct { + name string + // the reply tells this speed, 0 for none + want float64 + // reasoning: its usage's; steps gives its output's too + reasoning int + steps func(first, end time.Duration) (s []kimiStep, out int) +} + +// thought: 22 tokens of reasoning, in four deltas 30 ms apart from first. +func kimiThought(first time.Duration) []kimiStep { + var s []kimiStep + for i := range 4 { + s = append(s, kimiStep{first + time.Duration(i)*30*time.Millisecond, []string{kimiChunk(`{"reasoning_content":"The user wants the file written. "}`)}}) + } + return s +} + +// burst: the tool call the vendor held, let go over the last 236 ms in 40 +// writes of four argument deltas each (John's 550 tokens at 2,237 tok/s: +// 528 answer tokens in 236 ms), optionally with the text before it. +func kimiBurst(end time.Duration, text bool) []kimiStep { + from := end - 236*time.Millisecond + var s []kimiStep + if text { + s = append(s, kimiStep{from, []string{kimiChunk(`{"content":"I'll write the file now."}`)}}) + } + s = append(s, kimiStep{from, []string{kimiChunk(`{"tool_calls":[{"index":0,"id":"write:0","type":"function","function":{"name":"write","arguments":""}}]}`)}}) + for i := range 40 { + var evs []string + for range 4 { + evs = append(evs, kimiChunk(`{"tool_calls":[{"index":0,"function":{"arguments":"line of the file, "}}]}`)) + } + s = append(s, kimiStep{from + time.Duration(i)*6*time.Millisecond, evs}) + } + return s +} + +var kimiShapes = []kimiShape{ + // its text came after the thought, and the tool call it wrote over the + // rest of the time came in a burst at the end: its flow (from its first + // text) is the burst, its window from its first text the whole wait + {"held call", 0, 22, func(first, end time.Duration) ([]kimiStep, int) { + s := kimiThought(first) + s = append(s, kimiStep{first + 200*time.Millisecond, []string{kimiChunk(`{"content":"I'll write the file."}`)}}) + s = append(s, kimiBurst(end, false)...) + return append(s, kimiStep{end, kimiEnd("tool_calls", 550, 22)}), 550 + }}, + // its text came with the burst, long after its 22 reasoning tokens: + // the window from its first text is the burst itself + {"held answer", 0, 22, func(first, end time.Duration) ([]kimiStep, int) { + s := kimiThought(first) + s = append(s, kimiBurst(end, true)...) + return append(s, kimiStep{end, kimiEnd("tool_calls", 550, 22)}), 550 + }}, + // a steady 50 tok/s answer after the same thought: one token every + // 20 ms from 200 ms after the first content to the end + {"steady", 50, 22, func(first, end time.Duration) ([]kimiStep, int) { + s := kimiThought(first) + n := 0 + for at := first + 200*time.Millisecond; at < end; at += 20 * time.Millisecond { + s = append(s, kimiStep{at, []string{kimiChunk(`{"content":"word "}`)}}) + n++ + } + return append(s, kimiStep{end, kimiEnd("stop", n+22, 22)}), n + 22 + }}, +} + +// The reply's speed as each agent's protocol has it, through the real +// gateway from a fake Kimi Code: the held replies tell none, the steady one +// its 50 tok/s. The wait is shorter than John's (first content at 1.3 s, +// the end at 4 s) to keep the test short; the burst is his, 236 ms. +// Before, the held call read 1,790 tok/s and the held answer 2,237. +func TestKimiHeldBurstTellsNoSpeed(t *testing.T) { + fresh(t) + const first, end = 1300 * time.Millisecond, 4000 * time.Millisecond + agents := []struct{ path, body string }{ + {"/v1/chat/completions", `{"model":"kimi-code/%s","stream":true,"stream_options":{"include_usage":true},"messages":[{"role":"user","content":"write the file"}],"tools":[{"type":"function","function":{"name":"write","parameters":{"type":"object","properties":{"content":{"type":"string"}}}}}]}`}, + {"/v1/responses", `{"model":"kimi-code/%s","stream":true,"input":"write the file","tools":[{"type":"function","name":"write","parameters":{"type":"object","properties":{"content":{"type":"string"}}}}]}`}, + {"/v1/messages", `{"model":"kimi-code/%s","stream":true,"max_tokens":4000,"messages":[{"role":"user","content":"write the file"}],"tools":[{"name":"write","input_schema":{"type":"object","properties":{"content":{"type":"string"}}}}]}`}, + } + // a model for each agent and shape, which the usage is told apart by + model := func(a, i int) string { return fmt.Sprintf("k3-%d-%d", a, i) } + k := &kimiTimed{steps: map[string][]kimiStep{}} + var models []string + for a := range agents { + for i, sh := range kimiShapes { + models = append(models, model(a, i)) + k.steps[model(a, i)], _ = sh.steps(first, end) + } + } + up := httptest.NewServer(k) + t.Cleanup(up.Close) + p := provider.Provider{ID: "kimi-code", Name: "Kimi Code", Key: "k", Models: models, Chat: up.URL + "/coding/v1"} + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + gw := httptest.NewServer(New().Handler()) + t.Cleanup(gw.Close) + var wg sync.WaitGroup + for ai, a := range agents { + for i := range kimiShapes { + m := model(ai, i) + wg.Add(1) + go func() { + defer wg.Done() + res, err := http.Post(gw.URL+a.path, "application/json", strings.NewReader(fmt.Sprintf(a.body, m))) + if err != nil { + t.Error(err) + return + } + b, _ := io.ReadAll(res.Body) + res.Body.Close() + if res.StatusCode != 200 { + t.Errorf("%s %s: %d %s", a.path, m, res.StatusCode, b) + } + }() + } + } + wg.Wait() + recs := usage.Load(time.Time{}) + for ai, a := range agents { + for i, sh := range kimiShapes { + var rec *usage.Record + for j := range recs { + if strings.HasSuffix(recs[j].Model, "/"+model(ai, i)) || recs[j].Model == model(ai, i) { + rec = &recs[j] + } + } + if rec == nil { + t.Errorf("%s %s: no usage", a.path, sh.name) + continue + } + n, w := rec.Decode() + got := 0.0 + if w > 0 { + got = float64(n) * 1000 / float64(w) + } + ok := got == 0 + if sh.want > 0 { + ok = got > sh.want*0.8 && got < sh.want*1.2 + } + if !ok { + t.Errorf("%s %s: %d tokens in %d ms read %.0f tok/s (out %d, reasoning %d, ms %d, ttft %d, first text %d, flow %d); want %v", + a.path, sh.name, n, w, got, rec.Output, rec.Reasoning, rec.Millis, rec.TTFT, rec.FirstText, rec.Flow, sh.want) + } + } + } +} + +// The same three at John's own times (first content at 5.2 s, the end at +// 16 s, 550 tokens of which 22 reasoning), read by firstToken as the +// gateway reads a Chat stream, without waiting them out: the held ones +// read 2,237 tok/s before, the held call by its flow and the held answer +// by its window from its first text. +func TestKimiHeldBurstAtJohnsTimes(t *testing.T) { + const first, end = 5200 * time.Millisecond, 16000 * time.Millisecond + for _, sh := range kimiShapes { + t.Run(sh.name, func(t *testing.T) { + f := firstToken{} + steps, out := sh.steps(first, end) + for _, s := range steps { + // as if the request were s.at ago + f.start = time.Now().Add(-s.at) + f.see([]byte(strings.Join(s.events, "\n\n") + "\n\n")) + } + reasoning := sh.reasoning + ttft, text := f.ms() + ms := end.Milliseconds() + flow := f.flowFor(reasoning) + n, w := usage.DecodeOf(out, reasoning, ms, ttft, text, flow) + got := 0.0 + if w > 0 { + got = float64(n) * 1000 / float64(w) + } + ok := got == 0 + if sh.want > 0 { + ok = got > sh.want*0.9 && got < sh.want*1.1 + } + if !ok { + t.Errorf("%d tokens in %d ms read %.0f tok/s (out %d, ttft %d, first text %d, flow %d); want %v", n, w, got, out, ttft, text, flow, sh.want) + } + }) + } +} + +// The rows John posted, as their records hold them: 550 tokens (22 +// reasoning) in 16 s from a first content at 5.2 s, the answer's flow 236 +// ms, whether its text came early or with the burst; and 395 tokens in +// 19 s at 2,012 tok/s, a flow of 196 ms (his row gives no first token; 5.2 +// s is taken). Before, they read 2,237 and 2,015 tok/s. +func TestJohnsRowsTellNoSpeed(t *testing.T) { + for _, c := range []struct { + name string + out, reasoning int + ms, ttft, firstText, flow int64 + }{ + {"23:11:07, text early", 550, 22, 16000, 5200, 5400, 236}, + {"23:11:07, text with the burst", 550, 22, 16000, 5200, 15764, 236}, + {"23:12:56", 395, 0, 19000, 5200, 0, 196}, + } { + if n, w := usage.DecodeOf(c.out, c.reasoning, c.ms, c.ttft, c.firstText, c.flow); w != 0 { + t.Errorf("%s: %d tokens in %d ms read %d tok/s; want none", c.name, n, w, int64(n)*1000/w) + } + } +} diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index a7a84d6cb..5b1318dd3 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -15685,13 +15685,17 @@ const LED_COLS = [ // usage.DecodeOf and routing.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. -// Its window is no longer than its content took to come (flow_ms): a -// reply held back and sent in one burst tells none (John on Discord) +// 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 const ledDecode = (r) => { if (ledFailed_(r) || !(r.ttft_ms > 0)) return null; const think = r.reasoning > 0, n = think ? r.out - r.reasoning : r.out, from = think ? r.first_text_ms : r.ttft_ms; - const w = r.flow_ms > 0 ? Math.min(r.ms - from, r.flow_ms) : r.ms - from; - return n > 0 && from > 0 && w >= 100 && n * 1000 <= 10000 * w ? { n, w } : null; + 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 (think && n * (r.first_text_ms - r.ttft_ms) > 20 * r.reasoning * w) return null; + return { n, w }; }; // how fast a reply wrote, in tokens a second: 0 when it can't tell const ledRowSpeed = (r) => { const d = ledDecode(r); return d ? d.n / (d.w / 1000) : 0; }; diff --git a/internal/gui/assets/routing.js b/internal/gui/assets/routing.js index 2fc93d956..388eb4b96 100644 --- a/internal/gui/assets/routing.js +++ b/internal/gui/assets/routing.js @@ -257,12 +257,17 @@ // the reasoning was written before the stream showed any, OpenAI's // encrypted and its summary sent when done, and counting it read // gpt-6.1-sol at 163 tok/s. The reply's reasoning is its served try's. - // The window is no longer than its content took to come (flow): a reply - // held back and sent in one burst tells no speed (John on Discord). + // 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. function decodeOf(r, ms, ttft, firstText, flow = r.flow) { const think = reasoningOf(r), n = think > 0 ? r.out - think : r.out, from = think > 0 ? firstText : ttft; - const w = flow > 0 ? Math.min(ms - from, flow) : ms - from; - return n > 0 && ttft > 0 && from > 0 && w >= 100 && n * 1000 <= 10000 * w ? { n, w } : null; + 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 (think > 0 && n * (firstText - ttft) > 20 * think * w) return null; + return { n, w }; } const reasoningOf = (r) => r.reasoning ?? (r.usage?.length ? r.usage[r.usage.length - 1].reasoning || 0 : 0); const speedOf = (r, ms = r.ms, ttft = r.ttft, firstText = r.firstText, flow = r.flow) => { diff --git a/internal/gui/tests/routing-burst-speed.test.cjs b/internal/gui/tests/routing-burst-speed.test.cjs index e7dd4b532..8f4c8ead3 100644 --- a/internal/gui/tests/routing-burst-speed.test.cjs +++ b/internal/gui/tests/routing-burst-speed.test.cjs @@ -20,11 +20,18 @@ const key = { id: "antigravity", provider: "antigravity", name: "Antigravity", k // and last, a reply whose first words came at 5.1 s and the rest held back // and sent at once (John on Discord: Kimi Code, 1,367 tok/s): its flow says // its content took 2 ms to come -const timing = [[8264, 24360, 24359], [8264, 1500, 1000], [1200, 15000, 3000], [100, 3000, 1000], [100, 3000, 1000], [100, 3000, 1000], [2187, 6700, 5100, 2]]; -const routes = timing.map(([out, ms, ttft, flow], i) => ({ +// Then John's rows after that fix (Kimi Code again: 2,237 and 2,012 tok/s), +// 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. +// [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]]; +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 }], - done: true, status: 200, ms, ttft, flow, tokens: out + 4087, out, + order: [key], tries: [{ id: key.id, model: key.model, start: at(i), done: true, status: 200, ms, ttft, flow, firstText }], + done: true, status: 200, ms, ttft, flow, firstText, reasoning, tokens: out + 4087, out, })); function serve(lang) { @@ -101,9 +108,18 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.match(await story(2), want[lang].normal); assert.match(await story(3), want[lang].slow); // a burst after its first words: no speed, not 1,367 tok/s - const held = await story(timing.length - 1); + const held = await story(6); assert.doesNotMatch(held, want[lang].speed); assert.doesNotMatch(held, /1367|1,367/); + // a burst let go over 236 ms after its text, or with it, and over + // 196 ms: no speed, not 2,237 or 2,012 tok/s + for (const [i, n] of [[7, /2237|2,237/], [8, /2237|2,237/], [9, /2015|2,015|2012|2,012/]]) { + const s = await story(i); + assert.doesNotMatch(s, want[lang].speed, `row ${i}: ${s}`); + assert.doesNotMatch(s, n); + } + // a steady answer after the same reasoning keeps its 50 tok/s + assert.match(await story(10), want[lang].slow); assert.deepEqual(errors, []); }); } diff --git a/internal/gui/tests/usage-speed.test.cjs b/internal/gui/tests/usage-speed.test.cjs index 1f8e9f7e1..8b78ae580 100644 --- a/internal/gui/tests/usage-speed.test.cjs +++ b/internal/gui/tests/usage-speed.test.cjs @@ -33,6 +33,14 @@ for (let i = 4; i < 30; i++) ROWS.push(row(i, "glm-fast", 135, 600)); // one whose content came in a burst (flow_ms, John on Discord: a Kimi Code // reply read 1,367 tok/s): its tokens over the wait tell no speed ROWS[4].flow_ms = 2; +// and John's after that fix (Kimi Code: 2,237 tok/s): 550 tokens, 22 of +// them reasoning, in 16 s from a first content at 5.2 s, the rest let go +// over 236 ms, its text early or with the burst; then a steady 50 tok/s +// answer after the same reasoning +const kimi = { out: 550, reasoning: 22, ms: 16000, ttft_ms: 5200, flow_ms: 236 }; +Object.assign(ROWS[5], kimi, { first_text_ms: 5400 }); +Object.assign(ROWS[6], kimi, { first_text_ms: 15764 }); +Object.assign(ROWS[7], kimi, { out: 552, first_text_ms: 5400, flow_ms: 10600 }); const FAST = { id: "glm-fast", calls: 28, input: 1, output: 3780, cache_read: 0, cache_write: 0, cost: 0.3, timed: 27, ttft_ms: 27 * 600, decode_ms: 27 * 1800, decode_out: 27 * 135 }; const SLOW = { id: "kimi-slow", calls: 1, input: 1, output: 20, cache_read: 0, cache_write: 0, cost: 0.01, timed: 1, ttft_ms: 600, decode_ms: 2000, decode_out: 20 }; @@ -123,6 +131,9 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.equal((await cell(1).textContent()).trim(), w.slow); assert.equal((await cell(3).textContent()).trim(), "—"); assert.equal((await cell(4).textContent()).trim(), "—"); + assert.equal((await cell(5).textContent()).trim(), "—"); + assert.equal((await cell(6).textContent()).trim(), "—"); + assert.equal((await cell(7).textContent()).trim(), lang === "en" ? "50 tok/s" : "50 token/秒"); // the table fits the window; what it leaves out is in the details const wrap = p.locator("#ledWrap"); diff --git a/internal/usage/usage.go b/internal/usage/usage.go index aa4989618..8cb01cc79 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -352,11 +352,29 @@ func DecodeWindow(out int, ms, ttft int64) int64 { // hold, and counting them read a 2,000-token think and a 200-token answer // in 1.5 s as 1,467 tok/s. Its time goes to the wait before the answer. // One with reasoning and no text (all tool calls) tells no speed. The -// window is DecodeWindow's: 0 for a reply that tells none. flow, when -// known (Record.Flow), is how long its content took to come, and the -// window is no longer than it: a reply whose first words came at once -// and the rest held back and sent in a burst at its end wrote nothing -// in between, and its tokens over the wait read an impossible speed. +// window is DecodeWindow's: 0 for a reply that tells none. +// +// A reply its vendor held back and let go in a burst at its end tells +// none either (John on Discord: Kimi Code answering OpenCode read 1,367, +// then 2,237 and 2,012 tok/s). The burst is told two ways, as the reply's +// text came before it or with it: +// +// - flow, when known (Record.Flow), is how long its content took to come +// from where its window starts. A window HeldShare times longer had +// its content come in a small part of it: the rest was a wait while +// the vendor wrote and held it. The time it took to come is not the +// time it took to write (a burst let go over 236 ms read 528 tokens at +// 2,237 tok/s), and that time is not seen, so it tells no speed. A +// stream that came as it was written has a flow about its window. +// - a reply that reasoned is timed from its first text, its reasoning +// written before. When the answer would have been written over +// ThinkPace times faster than its reasoning came before it, from the +// first content to the first text, its first text came with the burst +// and the wait before it was the answer being written and held (22 +// reasoning tokens over 10.5 s, then 528 answer tokens in 236 ms). A +// 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. func DecodeOf(out, reasoning int, ms, ttft, firstText, flow int64) (tokens int, w int64) { start := ttft if reasoning > 0 { @@ -365,15 +383,27 @@ func DecodeOf(out, reasoning int, ms, ttft, firstText, flow int64) (tokens int, if ttft <= 0 { return 0, 0 } - if flow > 0 && start > 0 && start+flow < ms { - ms = start + flow - } if w = DecodeWindow(out, ms, start); w == 0 { return 0, 0 } + if flow > 0 && flow*HeldShare < w { + return 0, 0 + } + if reasoning > 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.js's +// decodeOf and app.js's ledDecode have the same. +const ( + HeldShare = 4 + ThinkPace = 20 +) + // Decode is the record's DecodeOf. func (r Record) Decode() (tokens int, w int64) { return DecodeOf(r.Output, r.Reasoning, r.Millis, r.TTFT, r.FirstText, r.Flow)