Files
magpie/internal/usage/failed_try_test.go
T
yetone d370bef982 usage: Failed lists the requests a fallback answered, as Routing shows their tries failed (maohy915 on X)
maohy915 runs magpie on a NAS and uses it from another computer's magpie
through Remote magpie. The NAS's Routing page showed failed calls, but
Usage → Requests → Failed found none. Reproduced with two sandbox
instances on mote (B → remote-magpie → A, where A's provider `bad`
answers 429 and falls back to `good`). A's trace had 2 requests and 1
rerouted. Usage had 2 rows, Failed had 0 and Retried had 1. A try that
fails and is handed on writes no row when nothing is billed, and its
request's row is an answer on the second try. Failed kept only
LostRequest, the requests whose agent got the error, so the failed try
was nowhere under Failed. Plain failures, remote or not, were already
listed; only rerouted ones were missing.

Failed now keeps every request a try failed on (Record.FailedTry): the
requests the agent got an error for, the tries handed on, and the
requests a later try answered. Retried still keeps only the answered
ones. The counts are unchanged: each request is counted once, by how it
ended (小一's rule), so a Failed list reads "1 failed · 2 answered on a
retry". The Failed option and the card's "N failed" button now say so in
their tooltip, in every language. Filtered page cells are cached on disk
under the filter, so settledCountVersion goes to 4; the first start after
the upgrade pairs the history again once.

Run (mote):
- go test ./internal/usage ./internal/usageapi ./internal/gui: ok.
- New test TestFailedKeepsRequestsAFallbackAnswered; updated
  TestRequestsCountedOnceWithTheirRetries.
- GUI, both engines: ledger-retried (5 languages, 420/1100), i18n-keys,
  i18n-en-no-cjk, usage-ledger, api-key-usage, purpose-filter,
  usage-ledger-content, usage-phone-head, usage-loops, usage-request-page.
  All pass except known reds: i18n-keys "a plugin's words are its own"
  (#6422/#6423) and usage-ledger "newest live route needs no
  return-to-live button" (#4078/#3104/#4626).
- gofmt; go vet for darwin, linux -tags nogui, and windows.
- Real repro with a binary built from this tree: A's Failed went from 0
  rows to the rerouted request, both in the API and in the real page
  (Chromium).

Broken by hand:
- Filter back to LostRequest: both Go tests red.
- The Failed option's title dropped: ledger-retried red.

Not tried:
- Restoring settledCountVersion to 3 doesn't turn
  TestSettledCountedAsVersioned red: it counts only Filter{}. The bump is
  by reading.
- Not tried on a real NAS or in the desktop app's remote window; the
  page is the NAS's own web page.
2026-11-13 14:17:49 +08:00

51 lines
2.7 KiB
Go

package usage
import (
"testing"
"time"
)
// maohy915 on X: a magpie on a NAS, used from another computer's magpie,
// showed failed calls on Routing while Usage's Failed filter found none.
// The calls had failed on one provider and been answered by its fallback:
// the failed try wrote no row (nothing was billed) and its request's row
// is an answer on the second try. Failed keeps that request, as Routing
// shows its try failed, and a try handed on, beside the requests the
// agent got an error for; Retried still keeps only the answered ones.
func TestFailedKeepsRequestsAFallbackAnswered(t *testing.T) {
pageHome(t)
now := time.Now()
at := func(s int) time.Time { return now.Add(time.Duration(s-30) * time.Second) }
Append(Record{Time: at(1), Agent: "claude", Provider: "good", Model: "m2", Requested: "bad/m1", Input: 5, Output: 2, Status: 200, RouteID: 1, Tries: 2, Via: "desk"})
Append(Record{Time: at(2), Agent: "claude", Provider: "good", Model: "m2", Requested: "bad/m1", Input: 5, Output: 2, Status: 200, RouteID: 2, Via: "desk"})
Append(Record{Time: at(3), Agent: "claude", Provider: "bad", Model: "m1", Input: 5, Status: 429, Error: "rate limited", RouteID: 3, HandedOn: true, Via: "desk"})
Append(Record{Time: at(4), Agent: "claude", Provider: "good", Model: "m2", Input: 5, Output: 2, Status: 200, RouteID: 3, Tries: 2, Via: "desk"})
Append(Record{Time: at(5), Agent: "claude", Provider: "bad", Model: "m1", Status: 429, Error: "rate limited", RouteID: 4, Via: "desk"})
Append(Record{Time: at(6), Agent: "claude", Provider: "good", Model: "m2", Status: StatusCanceled, Error: AgentCanceled, ErrType: CanceledErrType, RouteID: 5, Tries: 2, Via: "desk"})
routes := func(rows []Row) map[int64]int {
m := map[int64]int{}
for _, r := range rows {
m[int64(r.RouteID)]++
}
return m
}
rows, sum, _ := Ledger(All, Filter{Failed: true})
if got := routes(rows); len(rows) != 4 || got[1] != 1 || got[3] != 2 || got[4] != 1 {
t.Fatalf("Failed kept routes %v (%d rows), want 1, 3 (both tries) and 4", got, len(rows))
}
if sum.Calls != 3 || sum.Errors != 1 || sum.Retried != 2 {
t.Fatalf("Failed's sum %+v: want 3 requests, 1 failed, 2 answered on a retry", sum)
}
if p := QueryPage(All, Filter{Failed: true}, 0, 50); p.Total != 4 || p.Sum.Errors != 1 || p.Sum.Retried != 2 {
t.Fatalf("Failed page: %d rows, %+v", p.Total, p.Sum)
}
if p := QueryPage(All, Filter{Retried: true}, 0, 50); p.Total != 2 || p.Sum.Errors != 0 {
t.Fatalf("Retried page: %d rows, %+v", p.Total, p.Sum)
}
// counted once each, by how it ended, as before
if p := QueryPage(All, Filter{}, 0, 50); p.Sum.Calls != 5 || p.Sum.Errors != 1 || p.Sum.Retried != 2 || p.Sum.Canceled != 1 {
t.Fatalf("all: %+v", p.Sum)
}
}