Files
magpie/internal/gateway/retried_usage_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

66 lines
2.8 KiB
Go

package gateway
import (
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// A request a group answered on its third member, after the first refused
// (its row kept for what it billed) and the second failed, is one request
// answered on a retry, not a failure (小一 on Discord): the refused try's
// row is marked handed on, and the answer's row says how many tries it
// took. A request that fails outright is the one failure.
func TestRetriedRequestCountsOnce(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{200, "text/event-stream", anthropicRefusal}}}
b := &scripted{replies: []reply{{500, "application/json", `{"error":{"message":"overloaded"}}`}}}
c := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
scriptedOn(t, "a", provider.Anthropic, a)
scriptedOn(t, "b", provider.Anthropic, b)
scriptedOn(t, "c", provider.Anthropic, c)
refusalGroup(t, "a/m", "b/m", "c/m")
s := New()
code, body := sendTo(s, "/v1/responses", codexAsk)
if code != 200 || a.n != 1 || b.n == 0 || c.n != 1 {
t.Fatalf("%d %s (a %d, b %d, c %d)", code, body, a.n, b.n, c.n)
}
route := s.trace.routes[len(s.trace.routes)-1]
if len(route.Tries) < 3 {
t.Fatalf("tries: %+v", route.Tries)
}
recs := usage.Load(time.Time{})
if len(recs) != 2 || recs[0].Provider != "a" || !recs[0].HandedOn || recs[0].Status != 400 ||
recs[1].Provider != "c" || recs[1].HandedOn || recs[1].Status != 200 || recs[1].Tries != len(route.Tries) {
t.Fatalf("usage: %+v", recs)
}
_, sum, _ := usage.Ledger(usage.All, usage.Filter{})
if sum.Calls != 1 || sum.Errors != 0 || sum.Retried != 1 {
t.Fatalf("ledger counts %d calls, %d failed, %d retried", sum.Calls, sum.Errors, sum.Retried)
}
if rows, _, _ := usage.Ledger(usage.All, usage.Filter{Failed: true}); len(rows) != 0 {
t.Fatalf("failed filter keeps a request that was answered: %+v", rows)
}
if rows, _, _ := usage.Ledger(usage.All, usage.Filter{Retried: true}); len(rows) != 1 || rows[0].Provider != "c" {
t.Fatalf("retried filter: %+v", rows)
}
// one provider, failing outright: one request, one failure
fresh(t)
x := &scripted{replies: []reply{{400, "application/json", `{"type":"error","error":{"type":"invalid_request_error","message":"bad"}}`}}}
scriptedOn(t, "x", provider.Anthropic, x)
code, body = sendTo(New(), "/v1/messages", `{"model":"x/m","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`)
if code < 400 {
t.Fatalf("%d %s", code, body)
}
_, sum, _ = usage.Ledger(usage.All, usage.Filter{})
if sum.Calls != 1 || sum.Errors != 1 || sum.Retried != 0 {
t.Fatalf("outright failure counts %d calls, %d failed, %d retried", sum.Calls, sum.Errors, sum.Retried)
}
if rows, _, _ := usage.Ledger(usage.All, usage.Filter{Failed: true}); len(rows) != 1 || rows[0].HandedOn || rows[0].Tries > 1 {
t.Fatalf("failed filter: %+v", rows)
}
}