wwcraft_ on Discord asked for the WorkBuddy credits spent on the Usage page, by model, agent and session. WorkBuddy's chat stream ends with usage.credit, the per-request figure its own app shows as "Credits Consumed". The built-in and the moved plugin both hand that chunk to the gateway's chat decoder (the plugin's renamed()/kept() pass usage on as it came), so cUsage reads `credit` into Usage.Credits and the three record sites keep it on the row. A reply that says nothing has no credits, not 0. - usage: Record.Credits; Totals/Via sum Credits and count Credited; the packed row keeps it under its own flag bit (4096, in TestRowFlagsApart); the ledger CSV gains a trailing "credits" column. - GUI: the Usage ranking's line says "0.7 credits" (积分, 積分, クレジット, Credits) beside the cost, its title how many requests said it; the Sessions page's Routed line says it per route. TUI usage page and `magpie usage` tables say it muted on the same line. No new layout. Real replies on workbuddy-ai, 10-16: gemini-3.5-flash "credit":0.01; deepseek-v4.1-flash, minimax-m3, kimi-k2.6 "credit":0; glm-5.3-flash refused (14003). The tests use those bytes. Run (at b07b518e7 on origin/main63be47717, with #2511's commit): - gofmt -l clean; go vet ./... (darwin), GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean; go build: ok. - go test -timeout 45m ./... with pipefail, sandbox HOME: 63 packages ok, usage (200s), cli, tui, gui among them; agent, gateway and library timed out at load ~270 (see the #2511 commit); library ok alone, agent and gateway ok with -timeout 150m: agent ok (5529s); gateway red only in TestIntentClassifierGroup (#1813), TestMaxRPMWaitsForRoomInTheMinute (#1791) and TestMaxRPMGroupPrefersMemberWithRoom (#2520), known timing reds under load, each passing alone here -count=3 (19s). - New: TestPlanCreditsReachTheLedger (chat stream, whole reply, translated /v1/messages, free 0, none), TestPlanCreditsSummed, TestPlanCreditsPacked, TestFormatCredits, TestUsageSaysCredits, and plan-credits.test.cjs (en, zh, zh-TW, ja, de; Chromium and WebKit; 420px fit) 12/12. - Broken by hand: dropping Credits at the record site turns the gateway test red; skipping it in unpack turns the usage tests red; dropping the ranking's ledCredits turns plan-credits red in every language. - GUI reds elsewhere: see the #2511 commit (#2536, #2552, a provider-detect-draft load timeout that passes 3/3 alone). - Rebased onto origin/mainba132d68b(13 commits; chat.go, gateway.go, app.js, i18n.js among theirs): gofmt and go vet ./internal/... clean; Credits/DeepSeek/Balance/Ledger/RowFlags tests in usage, gateway, provider, cli, tui ok; plan-credits 12/12, balance-parts 4/4, i18n-keys 3/3 in both engines.
161 lines
6.2 KiB
Go
161 lines
6.2 KiB
Go
package usage
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
func TestVisibleReasoningSpeed(t *testing.T) {
|
|
visible := Record{Time: time.Now(), Output: 1500, Reasoning: 1400, Millis: 15000, TTFT: 1000, FirstReasoning: 1000, Flow: 12000, Status: 200}
|
|
for _, c := range []struct {
|
|
name string
|
|
r Record
|
|
n int
|
|
w int64
|
|
}{
|
|
{"visible tool turn", visible, 1500, 14000},
|
|
{"answer remains separate", func() Record { r := visible; r.FirstText = 14000; r.Flow = 1000; return r }(), 100, 1000},
|
|
{"hidden and old records", func() Record { r := visible; r.FirstReasoning = 0; return r }(), 0, 0},
|
|
{"unknown flow", func() Record { r := visible; r.Flow = 0; return r }(), 0, 0},
|
|
{"held reasoning at quarter boundary", func() Record { r := visible; r.Flow = 3500; return r }(), 1500, 14000},
|
|
{"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},
|
|
{"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},
|
|
} {
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
var chunk rowChunk
|
|
for i := 0; i < 80; i++ {
|
|
chunk.add(Row{Record: visible}, "", int64(i), false)
|
|
}
|
|
packed := chunk.pack()
|
|
if len(packed.Archive) == 0 {
|
|
t.Fatal("snapshot not compressed")
|
|
}
|
|
for _, c := range []*rowChunk{&chunk, packed.unpack()} {
|
|
row := c.row(0)
|
|
if n, w := row.Decode(); row.FirstReasoning != 1000 || n != 1500 || w != 14000 {
|
|
t.Fatalf("cache lost streamed reasoning: %+v", row)
|
|
}
|
|
}
|
|
sealed := visible
|
|
sealed.ReasoningHidden = true
|
|
var hiddenChunk rowChunk
|
|
for i := 0; i < 80; i++ {
|
|
hiddenChunk.add(Row{Record: sealed}, "", int64(i), false)
|
|
}
|
|
for _, c := range []*rowChunk{&hiddenChunk, hiddenChunk.pack().unpack()} {
|
|
row := c.row(0)
|
|
n, w := row.Decode()
|
|
if !row.ReasoningHidden || n != 0 || w != 0 {
|
|
t.Fatal("cache lost hidden reasoning flag")
|
|
}
|
|
}
|
|
var sum Totals
|
|
sum.addRow(Row{Record: visible})
|
|
hidden := visible
|
|
hidden.FirstReasoning = 0
|
|
sum.addRow(Row{Record: hidden})
|
|
if sum.DecodeOut != 1500 || sum.DecodeMs != 14000 {
|
|
t.Fatalf("hidden reasoning lifted aggregate: %+v", sum)
|
|
}
|
|
}
|
|
|
|
// A row the Requests page reads back has been priced, and pricing sets
|
|
// swapKnown on it: that bit is not ReasoningHidden's, or every priced reply
|
|
// with no answer text would lose its speed (#1463).
|
|
func TestPricedRowKeepsReasoningEvidence(t *testing.T) {
|
|
visible := Record{Time: time.Now(), Provider: "deepseek", Model: "deepseek-chat", Output: 1500, Reasoning: 1400, Millis: 15000, TTFT: 1000, FirstReasoning: 1000, Flow: 12000, Status: 200}
|
|
toolOnly := Record{Time: time.Now(), Provider: "deepseek", Model: "deepseek-chat", Output: 120, Millis: 9000, TTFT: 1000, Flow: 7000, Status: 200}
|
|
sealed := visible
|
|
sealed.ReasoningHidden = true
|
|
for _, c := range []struct {
|
|
name string
|
|
r Record
|
|
hidden bool
|
|
n int
|
|
w int64
|
|
}{
|
|
{"visible reasoning", visible, false, 1500, 14000},
|
|
{"tool call without reasoning", toolOnly, false, 120, 8000},
|
|
{"hidden reasoning", sealed, true, 0, 0},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var chunk rowChunk
|
|
for i := 0; i < 80; i++ {
|
|
chunk.add(Row{Record: c.r}, "", int64(i), false)
|
|
}
|
|
b := newBlockPricer(nil, nil, func(Record) *catalog.Price { return nil })
|
|
defer b.release()
|
|
for _, from := range []*rowChunk{&chunk, chunk.pack()} {
|
|
priced := b.block(from)
|
|
if priced.Rows[0].Flags&swapKnown == 0 {
|
|
t.Fatal("the row wasn't priced")
|
|
}
|
|
row := priced.row(0)
|
|
if n, w := row.Decode(); row.ReasoningHidden != c.hidden || n != c.n || w != c.w {
|
|
t.Fatalf("priced row: hidden %v, %d tokens/%d ms; want %v, %d/%d", row.ReasoningHidden, n, w, c.hidden, c.n, c.w)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A row's Flags bits are each its own: a reported call's own cost isn't
|
|
// read back as hidden reasoning, nor hidden reasoning as an own cost
|
|
// (rowOwnCost and rowReasoningHidden both took 256 when #1463 and #1874
|
|
// landed apart).
|
|
func TestRowFlagsApart(t *testing.T) {
|
|
cost := 0.5
|
|
own := Record{Time: time.Now(), Source: SourceReported, OwnCost: &cost, Provider: "deepseek", Model: "deepseek-chat", Output: 1500, Reasoning: 1400, Millis: 15000, TTFT: 1000, FirstReasoning: 1000, Flow: 12000, Status: 200}
|
|
hidden := own
|
|
hidden.OwnCost, hidden.ReasoningHidden = nil, true
|
|
for _, c := range []struct {
|
|
name string
|
|
r Record
|
|
hidden bool
|
|
own bool
|
|
}{
|
|
{"own cost", own, false, true},
|
|
{"hidden reasoning", hidden, true, false},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
made := func() *rowChunk {
|
|
var chunk rowChunk
|
|
for i := 0; i < 80; i++ {
|
|
chunk.add(Row{Record: c.r, Source: SourceReported}, "", int64(i), false)
|
|
}
|
|
return &chunk
|
|
}
|
|
b := newBlockPricer(nil, nil, func(Record) *catalog.Price { return nil })
|
|
defer b.release()
|
|
raw := made()
|
|
for _, row := range []Row{raw.row(0), b.block(raw).row(0), b.block(made().pack()).row(0)} {
|
|
if row.ReasoningHidden != c.hidden || (row.OwnCost != nil) != c.own {
|
|
t.Fatalf("hidden %v, own cost %v; want %v, %v", row.ReasoningHidden, row.OwnCost, c.hidden, c.own)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
bits := []uint16{1, 2, 4, 8, 16, 32, swapKnown, rowHandedOn, rowReasoningHidden, rowOwnCost, rowCompactionKnown, rowCompaction, rowCredits}
|
|
var all uint16
|
|
for _, b := range bits {
|
|
if all&b != 0 {
|
|
t.Fatalf("Flags bit %d is taken twice", b)
|
|
}
|
|
all |= b
|
|
}
|
|
}
|