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).
188 lines
5.7 KiB
Go
188 lines
5.7 KiB
Go
package usage
|
|
|
|
import (
|
|
"slices"
|
|
"sync"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// swapKnown marks a row whose Swapped flag was settled when it was priced,
|
|
// so reading it back needs no second look at the two names.
|
|
const swapKnown = 64
|
|
|
|
// pricedAs is what a gateway call is priced as, the same for every call of
|
|
// one provider, model, served model and effort.
|
|
type pricedAs struct {
|
|
provider string
|
|
flags uint16
|
|
price *catalog.Price
|
|
}
|
|
|
|
// blockPricer prices the gateway's raw blocks as a query reads them. What a
|
|
// row's price, provider id, swap and routing are depends on a few of its
|
|
// names only, so they are worked out once for each set of names, not once
|
|
// per call: 750k calls name a few hundred.
|
|
type blockPricer struct {
|
|
renames provider.Renames
|
|
wires map[string]string
|
|
price func(Record) *catalog.Price
|
|
by map[[4]string]pricedAs
|
|
agents map[string][2]string
|
|
pooled []*[]packedRow
|
|
}
|
|
|
|
// pricedRows are the rows of the blocks a query priced, handed back when
|
|
// it is done with them: a query over the whole history decodes every block,
|
|
// and allocating and clearing their rows again each time was a third of it.
|
|
var pricedRows = sync.Pool{New: func() any { s := make([]packedRow, 0, logBlockRows); return &s }}
|
|
|
|
// release hands the rows of the blocks it priced back, once the query no
|
|
// longer reads them (a page's rows are copies).
|
|
func (b *blockPricer) release() {
|
|
for _, rows := range b.pooled {
|
|
pricedRows.Put(rows)
|
|
}
|
|
b.pooled = nil
|
|
}
|
|
|
|
func newBlockPricer(renames provider.Renames, wires map[string]string, price func(Record) *catalog.Price) *blockPricer {
|
|
return &blockPricer{renames: renames, wires: wires, price: price, by: map[[4]string]pricedAs{}, agents: map[string][2]string{}}
|
|
}
|
|
|
|
// as is priceRow's answer for a gateway call of these names, prov the
|
|
// id the provider has now (Renames).
|
|
func (b *blockPricer) as(prov, model, served, effort string) pricedAs {
|
|
k := [4]string{prov, model, served, effort}
|
|
if a, ok := b.by[k]; ok {
|
|
return a
|
|
}
|
|
a := pricedAs{provider: prov, flags: swapKnown}
|
|
sent := provider.SentNameIn(b.wires, prov, model, effort)
|
|
if served != "" && Swapped(sent, served) && !(SameSpelled(model, served) || ServingName(model, served) || RelayRenamed(model, served)) {
|
|
a.flags |= 2
|
|
}
|
|
if GroupRouted(sent, served) {
|
|
a.flags |= 32
|
|
}
|
|
a.price = b.price(Record{Provider: prov, Model: model, Served: served, Effort: effort})
|
|
b.by[k] = a
|
|
return a
|
|
}
|
|
|
|
// agent is reportedAs's answer for a client, the agent counted and the
|
|
// client's own name when the user's counting changed it.
|
|
func (b *blockPricer) agent(ua string) (string, string) {
|
|
a, ok := b.agents[ua]
|
|
if !ok {
|
|
a[0], a[1] = reportedAs(ua)
|
|
b.agents[ua] = a
|
|
}
|
|
return a[0], a[1]
|
|
}
|
|
|
|
// block is the priced copy of a raw block, made for one query and not kept.
|
|
func (b *blockPricer) block(packed *rowChunk) *rowChunk {
|
|
buf := pricedRows.Get().(*[]packedRow)
|
|
raw := packed.unpackInto(*buf)
|
|
// unpackInto decodes into the buffer whenever it is large enough
|
|
if raw != nil && raw != packed && cap(*buf) >= len(raw.Rows) {
|
|
b.pooled = append(b.pooled, buf)
|
|
} else {
|
|
pricedRows.Put(buf)
|
|
}
|
|
if raw == nil {
|
|
return nil
|
|
}
|
|
c := *raw
|
|
if raw == packed {
|
|
c.Rows = slices.Clone(raw.Rows) // a block too small to compress is shared
|
|
}
|
|
c.Strings = slices.Clip(c.Strings)
|
|
added := map[string]uint32{}
|
|
intern := func(s string, was uint32) uint32 {
|
|
if c.Strings[was] == s {
|
|
return was
|
|
}
|
|
if id, ok := added[s]; ok {
|
|
return id
|
|
}
|
|
id := uint32(len(c.Strings))
|
|
c.Strings = append(c.Strings, s)
|
|
added[s] = id
|
|
return id
|
|
}
|
|
// a shape's names are priced once, and the row takes the shape they
|
|
// make: the agent, the client and the provider are all a shape's
|
|
c.Shapes, c.shapeIDs = slices.Clip(c.Shapes), nil
|
|
type ids struct {
|
|
id uint32 // the provider's string, or the row's shape
|
|
flags uint16
|
|
price *catalog.Price
|
|
}
|
|
names := map[[5]uint32]ids{}
|
|
agents := map[uint32][2]uint32{}
|
|
shapes := map[[2]uint32]ids{}
|
|
// the provider each shape names, as it is now: one an id was given
|
|
// away from since has its calls told by when each was made, so its
|
|
// rows take the shape per provider they go to (Renames.Timed)
|
|
type renamedTo struct {
|
|
to uint32
|
|
timed bool
|
|
}
|
|
provs := map[uint32]renamedTo{}
|
|
for i := range c.Rows {
|
|
p := &c.Rows[i]
|
|
raw := c.Shapes[p.Shape][1]
|
|
to, ok := provs[raw]
|
|
if !ok {
|
|
id := c.Strings[raw]
|
|
to = renamedTo{timed: b.renames.Timed(id)}
|
|
if !to.timed {
|
|
to.to = intern(b.renames.Of(id, p.Time), raw)
|
|
}
|
|
provs[raw] = to
|
|
}
|
|
if to.timed {
|
|
to.to = intern(b.renames.Of(c.Strings[raw], p.Time), raw)
|
|
}
|
|
sk := [2]uint32{p.Shape, to.to}
|
|
got, ok := shapes[sk]
|
|
if !ok {
|
|
t := c.Shapes[p.Shape]
|
|
k := [5]uint32{t[1], t[5], t[7], t[8], to.to}
|
|
named, ok := names[k]
|
|
if !ok {
|
|
a := b.as(c.Strings[k[4]], c.Strings[k[1]], c.Strings[k[2]], c.Strings[k[3]])
|
|
named = ids{k[4], a.flags, a.price}
|
|
names[k] = named
|
|
}
|
|
agent, ok := agents[t[0]]
|
|
if !ok {
|
|
a, client := b.agent(c.Strings[t[0]])
|
|
agent = [2]uint32{intern(a, t[0]), intern(client, t[rowClient])}
|
|
agents[t[0]] = agent
|
|
}
|
|
t[0], t[1], t[rowClient] = agent[0], named.id, agent[1]
|
|
got = ids{c.shape(t), named.flags, named.price}
|
|
shapes[sk] = got
|
|
}
|
|
p.Shape = got.id
|
|
p.Flags = p.Flags&^(1|2|32) | got.flags
|
|
if p.Flags&rowOwnCost != 0 {
|
|
// a reported call that said what it cost costs that (#1874)
|
|
if p.Tokens[0]+p.Tokens[1] > 0 {
|
|
p.Flags |= 1
|
|
}
|
|
continue
|
|
}
|
|
p.Cost = 0
|
|
if got.price != nil && p.Tokens[0]+p.Tokens[1] > 0 {
|
|
p.Flags |= 1
|
|
p.Cost = got.price.CostSplit(int(p.Tokens[0]), int(p.Tokens[1]), int(p.Tokens[2]), int(p.Tokens[3]), int(p.Tokens[5]))
|
|
}
|
|
}
|
|
return &c
|
|
}
|