cybercaius on Discord (#help, 10-25): "已经更改供应商 id 的也没有变更过来反而
分开显示了". Reproduced in a sandbox HOME (magpie web on 3582, fake upstream
on 3583): after renaming myrelay to newrelay in the editor, the Requests
page's provider list had both myrelay and newrelay, each named "myrelay",
because the model-list probe row was still under myrelay. The summary was
already folded; the probe chunk, the Sessions tab's vias and the gateway's
loggedAnswers (lead-first routing) were not renamed at all.
What changed
- provider.Renames (UsageRenames): Of(id, time) is the id a call recorded
under id at that time belongs to now. Every usage reader goes through it:
summarizeFrom (and its historical-key set), ledgerMatched, NewPricer,
NewCoster, the Requests page's gateway blocks, probe rows and other
computers' days, window holds and their history, buildVias, and
gateway loggedAnswers. Visit stays as recorded so Of is taken once.
The summary, Requests and vias caches key on Renames.Key.
- An id reused by another provider no longer pulls the new one's calls
into the old one. Rename onto, or Save of a new provider under, an id in
another's Was moves it into that one's Gave with the time (giveID):
calls before then stay the old provider's. freeID treats Was ids as
taken, so adding the vendor again gets "-2". Renamed() (refs) skips ids
a provider or subscription holds now.
- A providers.json that can't be read keeps the last renames read instead
of none (a failed read is unknown), so usage doesn't split meanwhile.
- History: nothing in usage.jsonl is rewritten. Calls from before this
release under a renamed id show under the new id at once. An id that was
reused before this release has no hand-over time, so its calls show under
whoever holds the id now, as before.
Siblings checked: budget keys by caller key; per-key limits, RPM and
concurrency count live requests by the current id; tray totals go through
QueryPage; codex_memory reads Requested refs. None needed a change.
Run at the tree before the rebase; rebased onto 894603bcd (GUI-only,
none of these files) and provider, usage, usageapi, the build and the
Rename/Answer/Lead tests of gateway and gui run again there: ok.
- TestRenamesByWhenTheIDWasTaken (provider), TestRenamedProviderShownOnce
(usage), TestRenamedProviderAnsweredItsLead (gateway): each red with its
piece broken by hand (Save's giveID, freeID's Was, the last-good renames,
probe rename, vias rename, block pricer's timed path, summarize's time,
loggedAnswers' rename), green with it.
- go test -timeout 45m provider usage usageapi budget middleware cli agent
gui gateway sessions profile tui: all ok.
- gofmt -l, go vet ./internal/..., GOOS=linux/windows -tags nogui vet of
provider/usage/gateway and build of the binary: clean.
- Sandbox: rebuilt binary on the same HOME; /api/usage/requests lists only
newrelay, the probe row included.
Not tried: the GUI suite (no JS or asset changed; /api/providers gains an
omitempty "gave"), Windows tests run on the Windows box, a real account,
and the full ./... suite (other sessions' suites were running).
91 lines
3.5 KiB
Go
91 lines
3.5 KiB
Go
package usage
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// readBack is r as the ledger reads it back: from a chunk as written, priced
|
|
// into a block, and packed to its archive and out again.
|
|
func readBack(t *testing.T, r Record) []Row {
|
|
t.Helper()
|
|
made := func() *rowChunk {
|
|
var chunk rowChunk
|
|
for i := 0; i < 80; i++ {
|
|
chunk.add(Row{Record: r}, "", int64(i), false)
|
|
}
|
|
return &chunk
|
|
}
|
|
b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
|
|
defer b.release()
|
|
raw := made()
|
|
return []Row{raw.row(0), b.block(raw).row(0), b.block(made().pack()).row(0)}
|
|
}
|
|
|
|
// A call whose vendor counted no cache (a plugin's magpie_cache_unknown:
|
|
// Kiro tells only credits) is read back from the ledger as one, so its
|
|
// CacheRead of 0 is never read as a miss (#3464). A vendor that counted a
|
|
// miss stays one, and a Kiro call kept before its plugin said so is read
|
|
// as uncounted: Kiro never counted its cache.
|
|
func TestCacheUnknownKeptInTheLedger(t *testing.T) {
|
|
at := time.Now()
|
|
for _, c := range []struct {
|
|
name string
|
|
r Record
|
|
want bool
|
|
}{
|
|
{"said so", Record{Time: at, Provider: "zcode", Model: "glm-5", Input: 148000, Output: 19, CacheUnknown: true, Status: 200}, true},
|
|
{"a counted miss", Record{Time: at, Provider: "anthropic", Model: "claude-sonnet-4-5", Input: 148000, Output: 19, Status: 200}, false},
|
|
{"Kiro, kept before", Record{Time: at, Provider: "kiro", Model: "claude-sonnet-4.5", Input: 148000, Output: 19, Status: 200}, true},
|
|
{"Kiro's plugin id, kept before", Record{Time: at, Provider: "kiro-plugin", Model: "claude-sonnet-4.5", Input: 148000, Output: 19, Status: 200}, true},
|
|
{"Kiro with cache counted", Record{Time: at, Provider: "kiro", Model: "claude-sonnet-4.5", Input: 1000, CacheRead: 9000, Output: 19, Status: 200}, false},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
for i, row := range readBack(t, c.r) {
|
|
if row.CacheUnknown != c.want {
|
|
t.Fatalf("read %d: cache unknown %v, want %v", i, row.CacheUnknown, c.want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A hit rate leaves out the prompt of calls whose vendor counted no cache:
|
|
// a mixed ledger isn't diluted by them, and one of only those has none,
|
|
// where it read 0% (zeewell on X, #3464).
|
|
func TestHitRateLeavesUncountedOut(t *testing.T) {
|
|
at := time.Now()
|
|
counted := Record{Time: at, Provider: "anthropic", Model: "claude-sonnet-4-5", Input: 500, CacheRead: 9000, CacheWrite: 500, Output: 10, Status: 200}
|
|
kiro := Record{Time: at, Provider: "kiro", Model: "claude-sonnet-4.5", Input: 148000, Output: 19, CacheUnknown: true, Status: 200}
|
|
// kept before the plugin said so: Kiro never counts its cache
|
|
kiroBefore := Record{Time: at, Provider: "kiro-plugin", Model: "claude-sonnet-4.5", Input: 90000, Output: 19, Status: 200}
|
|
for _, c := range []struct {
|
|
name string
|
|
rs []Record
|
|
rate float64
|
|
known bool
|
|
calls int
|
|
}{
|
|
{"mixed", []Record{counted, kiro, kiro, kiroBefore}, 0.9, true, 3},
|
|
{"only uncounted", []Record{kiro, kiroBefore}, 0, false, 2},
|
|
{"only counted", []Record{counted}, 0.9, true, 0},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var byRecord, byRow Totals
|
|
for _, r := range c.rs {
|
|
byRecord.add(r, nil)
|
|
byRow.addRow(Row{Record: r})
|
|
}
|
|
for _, tot := range []Totals{byRecord, byRow} {
|
|
rate, known := tot.HitRate()
|
|
if known != c.known || rate != c.rate || tot.CacheUnknownCalls != c.calls {
|
|
t.Fatalf("hit rate %v (%v), %d left out; want %v (%v), %d: %+v", rate, known, tot.CacheUnknownCalls, c.rate, c.known, c.calls, tot)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|