Files
magpie/internal/usage/cleared.go
T
yetoneandNorethSea 4921bf5a97 usage, sessions: one model option for a found routing group's two ids (#5892, Norethcy on X)
A found routing group is called by its public id (auto/<model>) and is
kept as group/auto-<slug>; calls recorded under either were counted as two
models, so Usage › Sessions' model filter listed the same group twice and
the ledger, summaries and session lists split its calls.

Ported from NorethSea's 30d6261cd on their fork: UsageAlias names a found
group by its public id before the user's alias (both spellings, old
session files too), sessions' lists, the ledger keyer, the summary cache's
key and Codex's model/effort read through it, and a cache keyed on the
group names (UsageNamesKey) is read again when a group is renamed. A user's
own provider named "auto" keeps auto/<model> (autoRefsOn).

Changed in review: the patch's ledger filter compared every record with
provider.SameRef, which reads the providers (customIDs, a stat of
providers.json) for each record a filter passes over. FilterOf, the one
place a Filter is made from a query, now canonicalizes the model once, and
keepsRecord asks the providers only for an auto/ record under a group/
filter (sameGroupModel).

Run (mote's sshd reset every connection all afternoon, so on the Linux box
through wsl-run; the package run at base 720200092, and the -run tests,
quota-holds and i18n-keys again at this head on 208b89422; rebased since onto 44732325e,
a gateway test only):
- go test -count=1 -tags nogui ./internal/gui ./internal/provider
  ./internal/sessions ./internal/usage ./internal/usageapi: ok
- go vet -tags nogui (linux), GOOS=darwin CGO_ENABLED=0 go vet (provider,
  usage, usageapi, sessions), and go vet on Windows (win-run, those and
  internal/gui): clean. gofmt -l: clean.
- GUI: usage-ledger-model-pick, usage-ledger, quota-fit-batched,
  i18n-keys in chromium and webkit. usage-ledger's "newest live route
  needs no return-to-live button" (and zh "request routing links") is red
  the same on origin/main 987eac0aa on that box: #3104, #4078.

Broken by hand:
- keepsRecord back to r.Model != f.Model: TestUsageFoundGroupNames and
  TestLedgerFoundGroupNames red.
- usageGroupNames returning nothing: TestUsageFoundGroupNames (provider,
  usage), TestSessionsFoundGroupNames, TestCodexEffortFoundGroupNames,
  TestLedgerFoundGroupNames red.

Not tried: a macOS run (mote unreachable), the darwin gui package's vet,
and the reporter's own usage log.

Co-authored-by: NorethSea <963979204@qq.com>
2026-11-10 18:01:13 +08:00

111 lines
3.2 KiB
Go

package usage
import (
"encoding/json"
"sync/atomic"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// Clearing the Usage page's figures (CrazyMouse_TT on X: a local model
// deployed again and benchmarked, its requests, speed and cache wanted from
// nothing). A clear is kept in settings (settings.UsageCleared) and read
// over whatever the pages count, never written into usage.jsonl: the log
// keeps every call, so a key's spending limit (package budget), the
// allowances' window holds and the gateway's own reads of it (Visit) still
// count the calls cleared, and the agents' session files and other
// computers' shared days, which magpie can't rewrite, are cleared the same
// way as its own log.
// clearSet is the user's clears as last read, the usage aliases a model's
// clear takes in, and a key for the caches built on what they hide.
type clearSet struct {
marks []settings.UsageClear
alias func(string) string
// all is the latest clear of everything, zero for none
all time.Time
key string
}
var clearedAs atomic.Pointer[clearSet]
// readCleared reads the user's clears again, for a pass over the usage:
// each record of the pass is then judged by what was read here
// (clearedNow). It reads settings.json, so it is called before any lock a
// reader of the usage waits on.
func readCleared() *clearSet {
marks := settings.UsageClearedNow()
names := ""
if len(marks) > 0 {
names = provider.UsageNamesKey()
}
b, _ := json.Marshal(struct {
M []settings.UsageClear
N string
}{marks, names})
key := string(b)
if cur := clearedAs.Load(); cur != nil && cur.key == key {
return cur
}
c := &clearSet{marks: marks, key: key}
if len(marks) > 0 {
c.alias = provider.UsageAlias()
}
for _, m := range marks {
if m.Provider == "" && m.Model == "" && m.Time().After(c.all) {
c.all = m.Time()
}
}
clearedAs.Store(c)
return c
}
// clearedKey is what the caches of counted records key on.
func clearedKey() string { return readCleared().key }
// clearedNow is the clears the last readCleared read; nil before any.
func clearedNow() *clearSet { return clearedAs.Load() }
// hides is whether the user cleared r from the usage pages: a clear of
// everything, or of its provider's model, made at or after its time. r's
// provider is the one it is shown under (renameBy) and its model is taken
// by its own id or by the usage alias it is counted under, as the
// Overview's Models list names it.
func (c *clearSet) hides(r *Record) bool {
if c == nil || len(c.marks) == 0 {
return false
}
if !c.all.IsZero() && !r.Time.After(c.all) {
return true
}
model := ""
for _, m := range c.marks {
if m.Model == "" || r.Time.After(m.Time()) || m.Provider != r.Provider {
continue
}
if r.Model == m.Model {
return true
}
if c.alias != nil {
if model == "" {
model = c.alias(r.Model)
}
if model == c.alias(m.Model) {
return true
}
}
}
return false
}
// clearedSince is when the usage counted begins after a clear of
// everything: first, or the clear's time when it is later.
func (c *clearSet) clearedSince(first time.Time) time.Time {
if c == nil || c.all.IsZero() || !first.IsZero() && first.After(c.all) {
return first
}
return c.all
}