Files
magpie/internal/usage/retried_requests_test.go
T
yetone 47269f448f usage, gateway, gui: the Requests card counts requests once, a try handed on to the next isn't a failure, and says how many were answered on a retry (小一 on Discord)
小一's Codex-on-credits ledger read "5,482 · 1,094 failed" while many rows
said "2 tries/3 tries" and had been answered. "Failed" counted usage rows,
not requests: besides each request's final row, the gateway writes a row
for every try a vendor refused or turned away (hw.refused/hw.turnedAway),
and each of those was counted as a call and a failure even when a later
member answered. (Ordinary failovers write no row; 499 cancels and the
agent's own client-side retries are separate requests.) In the owner's own
usage.jsonl, 24 of the 71 "failed" rows were such refusals a later member
answered.

Now a request counts once, by how it ended:
- usage.Record gains `tries` (final row, when the route took more than one)
  and `handed_on` (the row of a try that failed and handed the request on).
  Totals.count skips handed-on rows; a failure is a request whose agent got
  an error (LostRequest); one answered after a failed try is Totals.Retried
  (RetriedRequest), with a `retried=1` filter.
- Logs written before this are read the same way: the log index marks a
  failed, non-rejected row followed by another of the same route id as
  handed on and raises the later row's tries (routeRows); the packed row
  and its snapshot carry both.
- Panel card, Usage overview tile, ledger card, rankings and group rows,
  `magpie usage` and the TUI say "N answered on a retry"; the CSV adds
  `tries` and `handed_on` at the end. A row says "· handed on" (with why
  in its title) or "· N tries".
- On the ledger's Requests card "N failed" and "N answered on a retry" are
  buttons that switch the list to the Failed / new Retried filter, and back
  when pressed again. The lines wrap rather than cut a button off (German).
  zh, zh-TW, ja, de strings added.
- Docs: reference.md (counting, filter, CSV columns), gateway-routing.md.

Run:
- go test -tags nogui ./internal/usage ./internal/usageapi ./internal/tui
  ./internal/gateway . : ok. New TestRequestsCountedOnceWithTheirRetries
  (JSONL fixture: legacy refused row + 200, handed_on + 200 tries 3, 502
  tries 2, plain 200, routeless 429 → 5 calls, 2 failed, 2 retried, across
  Ledger, QueryPage, Summarize, heatmap, filters, CSV) and
  TestRetriedRequestCountsOnce (group a refuses, b 500s, c answers → 1
  call, 0 errors, 1 retried; a lone 400 → 1 error).
- Broke each by hand (count without the HandedOn skip, gateway without
  HandedOn, log index without routeRows): each test went red; restored.
- go test -tags nogui -timeout 40m ./... under the gotest lock, rebased on origin/main de1ba9f12: all ok
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on ., the
  usage, usageapi, tui, gateway and gui packages: clean.
- Playwright ledger-retried.test.cjs, Chromium and WebKit, en zh zh-TW ja
  de, 420 and 1100 wide: 20/20 (German first failed on the cut-off line,
  fixed by letting the button lines wrap). usage-*, api-key-usage,
  purpose-filter (Failed is now the 2nd of 3 options), panel-usage,
  click-scroll, request-archive-usage, both engines: all green except
  purpose-filter's Routing case (1px focus scroll), which fails the same
  on 909d76747 and de1ba9f12, in Chromium too; it is #1291 (commented)
2026-10-11 12:58:03 +08:00

142 lines
4.9 KiB
Go

package usage
import (
"bytes"
"encoding/csv"
"os"
"path/filepath"
"testing"
"time"
)
// The Requests card counts requests, not tries (小一 on Discord): a try
// that failed and handed its request on to another account is neither a
// request nor a failure of its own, and a request answered after such a
// try is counted as answered on a retry. Failed is what the agent got an
// error for. A log written before the gateway marked those tries is told
// by its route: a failed row whose route a later row ends.
func TestRequestsCountedOnceWithTheirRetries(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
now := time.Now()
at := func(m int) string { return now.Add(time.Duration(m-10) * time.Second).Format(time.RFC3339Nano) }
lines := []string{
// written before handed_on: a safety refusal that a group's next
// member answered, the two sharing the route
`{"t":"` + at(1) + `","agent":"claude","provider":"yylx","model":"m","in":10,"ms":5,"status":400,"error":"content blocked by the safety filter","route_id":101}`,
`{"t":"` + at(2) + `","agent":"claude","provider":"b","model":"m","in":10,"out":5,"ms":9,"status":200,"route_id":101}`,
// today's: a refused try handed on, a 429 that wrote no row, then
// the answer on the third try
`{"t":"` + at(3) + `","agent":"codex","provider":"a","model":"m","in":20,"ms":5,"status":400,"error":"refused","route_id":102,"handed_on":true}`,
`{"t":"` + at(4) + `","agent":"codex","provider":"c","model":"m","in":20,"out":7,"ms":9,"status":200,"route_id":102,"tries":3}`,
// a request that failed outright, after two tries
`{"t":"` + at(5) + `","agent":"codex","provider":"a","model":"m","ms":5,"status":502,"error":"bad gateway","route_id":103,"tries":2}`,
// one answered at once, and one that failed with no route at all
`{"t":"` + at(6) + `","agent":"codex","provider":"a","model":"m","in":1,"out":1,"ms":5,"status":200,"route_id":104}`,
`{"t":"` + at(7) + `","agent":"codex","provider":"a","model":"m","ms":5,"status":429}`,
}
var b bytes.Buffer
for _, l := range lines {
b.WriteString(l + "\n")
}
os.MkdirAll(filepath.Dir(Path()), 0o755)
if err := os.WriteFile(Path(), b.Bytes(), 0o600); err != nil {
t.Fatal(err)
}
rows, sum, _ := Ledger(Today, Filter{})
// five requests: 101 and 102 answered on a retry, 103 and the routeless
// 429 failed, 104 answered; all seven rows are still listed, and the
// handed-on tries' tokens are still in the sums
if len(rows) != 7 || sum.Calls != 5 || sum.Errors != 2 || sum.Retried != 2 || sum.Input != 61 {
t.Fatalf("rows %d, calls %d, errors %d, retried %d, in %d", len(rows), sum.Calls, sum.Errors, sum.Retried, sum.Input)
}
handed := 0
for _, r := range rows {
if r.HandedOn {
handed++
if r.RouteID != 101 && r.RouteID != 102 {
t.Fatalf("handed on: %+v", r)
}
}
if r.RouteID == 101 && r.Status == 200 && r.Tries != 2 {
t.Fatalf("legacy answer's tries %d", r.Tries)
}
}
if handed != 2 {
t.Fatalf("%d rows handed on, want 2", handed)
}
failed, fsum, _ := Ledger(Today, Filter{Failed: true})
if len(failed) != 2 || fsum.Errors != 2 || fsum.Calls != 2 {
t.Fatalf("failed filter: %d rows, %+v", len(failed), fsum)
}
for _, r := range failed {
if r.HandedOn || !r.Failed() {
t.Fatalf("failed filter kept %+v", r)
}
}
retried, rsum, _ := Ledger(Today, Filter{Retried: true})
if len(retried) != 2 || rsum.Retried != 2 || rsum.Errors != 0 {
t.Fatalf("retried filter: %d rows, %+v", len(retried), rsum)
}
for _, r := range retried {
if r.Status != 200 || r.Tries < 2 {
t.Fatalf("retried filter kept %+v", r)
}
}
page := QueryPage(Today, Filter{}, 0, 100)
if page.Sum.Calls != 5 || page.Sum.Errors != 2 || page.Sum.Retried != 2 {
t.Fatalf("page sum %+v", page.Sum)
}
if p := QueryPage(Today, Filter{Failed: true}, 0, 100); p.Total != 2 {
t.Fatalf("failed page has %d rows", p.Total)
}
if p := QueryPage(Today, Filter{Retried: true}, 0, 100); p.Total != 2 {
t.Fatalf("retried page has %d rows", p.Total)
}
if s := Summarize(Today); s.Calls != 5 || s.Errors != 2 || s.Retried != 2 {
t.Fatalf("summary %d calls, %d errors, %d retried", s.Calls, s.Errors, s.Retried)
}
calls := 0
for _, d := range HeatmapOf(Filter{}).Days {
calls += d.Calls
}
if calls != 5 {
t.Fatalf("heatmap counts %d calls", calls)
}
var out bytes.Buffer
if err := WriteCSV(&out, rows); err != nil {
t.Fatal(err)
}
recs, err := csv.NewReader(&out).ReadAll()
if err != nil {
t.Fatal(err)
}
head := recs[0]
ti, hi := -1, -1
for i, h := range head {
switch h {
case "tries":
ti = i
case "handed_on":
hi = i
}
}
if ti < 0 || hi < 0 {
t.Fatalf("CSV header %v", head)
}
seen := map[string]int{}
for _, r := range recs[1:] {
seen[r[ti]+"/"+r[hi]]++
}
if seen["/true"] != 2 || seen["3/false"] != 1 || seen["2/false"] != 2 {
t.Fatalf("CSV tries/handed_on %v", seen)
}
}