Files
magpie/internal/usage/direct.go
T
yetone 7909de22ac usage: magpie usage and the TUI's Usage page show the agents' own calls, read from their session files, beside the gateway's, and a Codex-based client's calls aren't counted twice (Kumo31 on Discord: Codex used directly, not through magpie, was on the window's Usage tab but not in magpie usage) — magpie usage and the TUI only ever summed the gateway's log (usage.Summarize); the calls the agents' session files record that the gateway never saw (Codex on the user's own ChatGPT sign-in, Claude Code, Claude Desktop…) were merged only into the ledger the window's Requests tab and the tray panel read (#239). usage.Direct sums those rows of the ledger (Source "log", this computer's, rejected ones left out) by agent, model, account and session, priced as the ledger prices them, and magpie usage prints them after the gateway's tables under "not through magpie · the agents' own requests, read from their session files" (a model whose provider the file doesn't tell reads "local session/<model>"); the TUI's Usage page shows the same block under the gateway's, each with its totals and agents' and models' tables. The two parts are separate, so nothing is counted in both; a period with only the agents' own calls shows them under "no calls through magpie". All of magpie usage's output now goes to the writer it is given. Checking it against the owner's real data showed the ledger counted a Codex-based client's calls twice: miyi-helper's requests through the gateway are logged under its own name while its Codex session file names Codex, and the gateway/session-file matcher keyed on the agent, so they never matched; the matcher now keys on the session, tokens and time alone (a session id names one conversation), which also corrects the Requests tab. With read-only copies of the owner's ~/.config/magpie and four days of ~/.codex/sessions in a sandbox HOME: magpie usage week printed 177 calls not through magpie, in 1.3M, out 52.4K, cache read 3.9M, gpt-6-astra 154 and gpt-6-sol 23 — the same rows, to the token, as usage.LedgerOf (what /api/usage/requests serves) gave for the period; before the matcher fix, today's 7 miyi-helper calls showed in both parts. TestDirectCountsOnlySessionFileCalls (the miyi-helper case included), TestUsageShowsCallsNotThroughMagpie and TestUsagePageShowsCallsNotThroughMagpie fail on the old code. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 23:03:15 +08:00

65 lines
2.2 KiB
Go

package usage
import (
"sort"
"time"
)
// Direct is a period's calls the agents made on their own, which the gateway
// never saw: those their session files record (Codex, Claude Code, Claude
// Desktop…), summed up as Summarize sums the gateway's, by agent, model and
// account. The Usage page's Requests tab and the tray panel count them
// beside the gateway's; magpie usage and the TUI show them in a part of
// their own (Kumo31 on Discord: Codex used outside magpie was on the
// window's Usage tab but not in magpie usage).
func Direct(p Period) Summary {
return direct(p, time.Now(), LedgerOf(p, Filter{}).Rows)
}
func direct(p Period, now time.Time, rows []Row) Summary {
s := Summary{Period: p, Since: p.Since(now), Bucket: "day", Agents: []Group{}, Models: []Group{}, ProviderKeys: []Group{}, Accounts: []Group{}, CallerKeys: []Group{}, Sessions: []Group{}, Series: []Point{}}
if p != Today && p != Week && p != Month {
s.Period = All
}
agents, models, accounts, sessions := map[string]*Group{}, map[string]*Group{}, map[string]*Group{}, map[string]*Group{}
put := func(m map[string]*Group, k string, g Group, r Row) {
if m[k] == nil {
m[k] = &g
}
m[k].addRow(r)
}
for _, r := range rows {
if r.Source != "log" || r.IsRejected() || r.Time.Before(s.Since) {
continue
}
s.Totals.addRow(r)
id := AgentOf(r.Agent)
put(agents, id, Group{ID: id}, r)
put(models, r.Provider+"/"+r.Model, Group{ID: r.Provider + "/" + r.Model, Provider: r.Provider, Model: r.Model}, r)
if who := r.Account(); who != "" {
put(accounts, r.Provider+"@"+who, Group{ID: r.Provider + "@" + who, Provider: r.Provider, Account: who}, r)
}
if r.Session != "" {
put(sessions, id+"|"+r.Session, Group{ID: r.Session, Agent: id}, r)
}
}
list := func(m map[string]*Group) []Group {
gs := []Group{}
for _, g := range m {
gs = append(gs, *g)
}
sort.SliceStable(gs, func(i, j int) bool {
if a, b := gs[i].Tokens(), gs[j].Tokens(); a != b {
return a > b
}
if gs[i].Calls != gs[j].Calls {
return gs[i].Calls > gs[j].Calls
}
return gs[i].ID < gs[j].ID
})
return gs
}
s.Agents, s.Models, s.Accounts, s.Sessions = list(agents), list(models), list(accounts), list(sessions)
return s
}