Files
magpie/internal/gui/trace.go
T
yetone 3a47e377e0 usage: a call whose vendor counts no cache is left out of every hit rate (#3464)
#3464 (zeewell on X): Kiro caches on its side, but its replies give only
credits and a context share, never cache tokens. Every hit rate outside
the Routing page read those calls' CacheRead of 0 as a miss. A Kiro-heavy
day showed "hit rate 6%" when the counted calls hit 90%.

The plugin already says so (opencode-kiro-auth 0.1.16 sends
usage.magpie_cache_unknown), and the gateway already kept it as
RouteUsage.CacheUnknown for Routing. This carries it the rest of the way:

- gateway: Usage.cacheNotCounted() (the vendor said so and no try
  reported a cache token) goes into usage.Record.CacheUnknown on every
  path that records a call: the gateway's own record, the OTel attempt
  path in observe.go, and trace's routeUsage.
- usage: Record.CacheUnknown is kept as ledger row flag 8192
  (rowCacheUnknown; grepped, no other flag uses that bit). Totals sums
  such calls' prompt into cache_unknown and counts them in
  cache_unknown_calls. Totals.HitRate leaves that prompt out of its
  denominator, and gives no rate when nothing is left.
- Old records: calls kept before the plugin said so carry no flag. A
  Kiro call (provider kiro or kiro-plugin) with no cache counted is read
  as uncounted (usage.CountsNoCache): in the ledger's row read, in
  Totals, and for Routing's history in internal/gui/trace.go
  (cacheCounted). The rule is scoped to Kiro's two ids, so another
  vendor's 0 stays a miss (tested).
- GUI: cacheHitOf in app.js drives the tray panel's card, the Usage
  page's agent rows and cache tile, the requests' Cache hit rate card and
  each ranking row. When only uncounted calls are left it shows "—" or
  nothing instead of 0%. Otherwise it says "{n} requests left out: their
  vendor doesn't count the cache". A request row's cache cells show "—",
  and its details say the vendor doesn't count the cache. The Context
  page (context.go, context.js) shows no cache for such a call and doesn't
  score it as a miss.
- No budget raised. This adds 625 bytes to app.js (1,320,224 to
  1,320,849, under its 1,321,336); d08b6118b had already moved the
  masking page's maskWho, maskProv and stillMotion out to
  plugins/redact, which an earlier version of this change did too.
- Strings in zh, zh-TW, ja and de. docs/subsystems/provider-plugins.md
  now describes the ledger side and what is not covered.

Not covered, filed: hit rates read from the agents' own session files
(the TUI and CLI session lists, the Sessions tab's totals) still count
Kiro's 0 as a miss (#3470). Found on the way: System One's calls are
recorded without their cache tokens (#3471).

Run at this tree (on origin/main e57e78563, fetched just before; the
54-file GUI run was at a84d2ec80, and the 14 files that main's
redact page change since could reach were run again here):
- gofmt -l . prints nothing. go build ./... ok. go vet ./... ok,
  GOOS=linux go vet -tags nogui ./... ok, GOOS=windows go vet ./... ok.
- go test -count=1 -timeout 45m ./internal/usage ./internal/gui
  ./internal/gateway: all ok (gateway 388s), TestCoreScriptBudget
  included.
- GUI, 54 files (ledger-*, panel-*, i18n-*, context-*, request*,
  usage-card*, usage-ledger and the masking/redact files),
  concurrency 2: WebKit 439/452, Chromium 444/457. The new
  ledger-cache-unknown test passed in both. Again here (ledger-*,
  masking-*, redact-*, mask-*, usage-ledger): WebKit 104/117,
  Chromium 109/122, the same reds. The reds, both already
  red alone on main before this change:
  - redact-words-any-case, 10/10 in each engine (#3500)
  - usage-ledger "newest live route needs no return-to-live button",
    en and zh in each engine (#3104)
- At earlier bases, with the same change: usage-agents-hide (#3414),
  usage-checkin's WebKit Qoder tab (#3091), usage-fold (#3415),
  usage-focus-steady (#3501) were red, each red alone on main too;
  usage-card-refresh read a row before it was laid out, under load
  (#3509); context-picks (#3483) and
  context-map's tile overlap (#3488) in Chromium. Filed while doing
  this: TestUsageReadAfterAppendHashesNothingOnAFineClock goes red
  about 1 in 20 on main aa3112f4e (#3511); it passed in this run.
- Red on the old code: the new Go tests in usage, gateway and gui, and
  ledger-cache-unknown.test.cjs (the old page shows "hit rate 6%"
  where this shows 90%). Each changed piece was broken by hand and
  turned a test red. The exception is the OTel attempt path in
  observe.go, which no test reaches.

Not tried: a real Kiro account through the GUI.
2026-10-25 01:23:42 +08:00

402 lines
13 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/url"
"regexp"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/usage"
)
// traceJSON is the gateway's routing trace since the page last asked.
type traceJSON struct {
gateway.TraceState
Mine bool `json:"mine"` // the trace here is live: this magpie serves the gateway, or this console reads the magpie that does (#2606)
Now time.Time `json:"now"`
Routes []routeJSON `json:"routes"`
}
type routeJSON struct {
gateway.Route
SessionTitle string `json:"sessionTitle,omitempty"`
Cost float64 `json:"cost"`
Priced bool `json:"priced"`
Unpriced int `json:"unpriced"`
}
// Name polling reuses the history evidence already loaded by this view. Never
// reread a day's routing log every 15 seconds just to notice a title write.
var titleHistory struct {
sync.Mutex
days map[string][]gateway.Route
}
func rememberTitleHistory(day string, rows []gateway.Route) {
if day == "" {
return
}
evidence := make([]gateway.Route, 0, len(rows))
for _, r := range rows {
if r.Agent != "codex" || (r.TitleLink == nil && r.ParentSession == "") {
continue
}
failed := ""
if r.Error != "" {
failed = "failed"
}
evidence = append(evidence, gateway.Route{ID: r.ID, Agent: r.Agent, Session: r.Session,
ParentSession: r.ParentSession, Kind: r.Kind, Time: r.Time, Millis: r.Millis,
Done: r.Done, Status: r.Status, Error: failed, TitleLink: r.TitleLink})
}
titleHistory.Lock()
defer titleHistory.Unlock()
if titleHistory.days == nil {
titleHistory.days = map[string][]gateway.Route{}
}
if _, exists := titleHistory.days[day]; !exists && len(titleHistory.days) >= 4 {
for key := range titleHistory.days {
delete(titleHistory.days, key)
break
}
}
titleHistory.days[day] = evidence
}
func recalledTitleHistory(day string) []gateway.Route {
titleHistory.Lock()
defer titleHistory.Unlock()
return titleHistory.days[day] // immutable after publication
}
func pricedRoutes(routes []gateway.Route) []routeJSON {
gw := served.Load()
if gw == nil {
gw = &gateway.Server{}
}
routes = gw.ResolveTitleParents(routes)
out := make([]routeJSON, 0, len(routes))
priceOf := usage.NewPricer()
ids := make([]string, 0, len(routes))
for _, r := range routes {
if r.Agent == "codex" {
id := r.Session
if r.ParentSession != "" {
id = r.ParentSession
}
ids = append(ids, id)
}
}
titles := sessions.CodexTitles(ids)
pub := publicRefs()
for _, r := range routes {
r = publicRoute(r, pub)
r.Usage = cacheCounted(r.Usage)
recs := make([]usage.Record, len(r.Usage))
for i, u := range r.Usage {
recs[i] = u.PricingRecord()
}
sum := priceOf(recs)
// Old history and calls without token counts stay unknown, not free.
priced := sum.Calls > sum.Unpriced && sum.Input+sum.Output > 0
name := ""
if r.Agent == "codex" {
id := r.Session
if r.ParentSession != "" {
id = r.ParentSession
}
name = titles[id]
}
out = append(out, routeJSON{Route: r, SessionTitle: name, Cost: sum.Cost, Priced: priced, Unpriced: sum.Unpriced})
}
return out
}
// cacheCounted marks the usage of a vendor that counts no cache as such,
// kept before its plugin said so (usage.CountsNoCache): its hit rate is
// none rather than 0% (#3464). us is copied before it is changed.
func cacheCounted(us []gateway.RouteUsage) []gateway.RouteUsage {
var out []gateway.RouteUsage
for i, u := range us {
if u.CacheUnknown || !cacheNotCounted(u) {
continue
}
if out == nil {
out = slices.Clone(us)
}
out[i].CacheUnknown = true
}
if out == nil {
return us
}
return out
}
// cacheNotCounted is whether u's vendor counted no cache for it: it said
// so, or it never does and none was recorded.
func cacheNotCounted(u gateway.RouteUsage) bool {
return u.CacheUnknown || u.CacheRead+u.CacheWrite == 0 && usage.CountsNoCache(u.Provider)
}
// publicRefs is provider.PublicRef, each ref looked up once: a page of
// routes names a few models many times over.
func publicRefs() func(string) string {
seen := map[string]string{}
return func(ref string) string {
if ref == "" {
return ref
}
out, ok := seen[ref]
if !ok {
out = provider.PublicRef(ref)
seen[ref] = out
}
return out
}
}
// publicRoute is r as the Routing page names it: a group magpie found by
// the id agents pick it by, auto/<model>, wherever r names one — the
// model asked for, the request a search or an image was for, a subagent's
// pick, the group and the groups in it (未寒 on Discord: the live view
// still said group/auto-<model>). The gateway keeps group/auto-<slug> in
// the trace and its history (provider.CanonicalRef); r's own pointers are
// the trace's, so what changes is copied first.
func publicRoute(r gateway.Route, pub func(string) string) gateway.Route {
r.Model = pub(r.Model)
if r.For != nil {
f := *r.For
f.Model = pub(f.Model)
r.For = &f
}
if r.Subagent != nil {
s := *r.Subagent
s.Asked, s.To = pub(s.Asked), pub(s.To)
r.Subagent = &s
}
if g := r.Group; g != nil {
c := *g
if c.Ref == "" { // history written before the trace kept it
c.Ref = pub(provider.GroupPrefix + c.ID)
}
c.Subs = slices.Clone(c.Subs)
for i, sub := range c.Subs {
if sub.Ref == "" {
c.Subs[i].Ref = pub(provider.GroupPrefix + sub.ID)
}
}
r.Group = &c
}
return r
}
// mainView is the tab the window is asked to open on, as the page's view
// parameter: the Routing page on one request (req, its id) when the tray
// panel's Routing tab asks for it, Masking on one request's record (key)
// when a masked call's badge there does, or the Usage page's Requests on one
// provider or agent when its Usage tab does, its Overview (the allowances)
// when a menu-bar cell's click does. Anything but an id, or a name of the
// kind a provider or an agent has, is dropped.
func mainView(q url.Values) string {
view := q.Get("view")
switch view {
case "routing":
if id, err := strconv.ParseInt(q.Get("req"), 10, 64); err == nil && id > 0 {
view += "&req=" + strconv.FormatInt(id, 10)
}
case "masking":
if key, err := strconv.ParseInt(q.Get("key"), 10, 64); err == nil && key > 0 {
view += "&key=" + strconv.FormatInt(key, 10)
}
case "usage":
if tab := q.Get("tab"); tab == "requests" || tab == "usage" {
view += "&tab=" + tab
}
if card := q.Get("card"); q.Get("tab") == "usage" && card != "" {
view += "&card=" + url.QueryEscape(card)
}
for _, k := range []string{"provider", "agent"} {
if v := q.Get(k); mainName.MatchString(v) {
view += "&" + k + "=" + url.QueryEscape(v)
}
}
}
// the panel's picker making a routing group of a model kept for groups
// and in none: the window opens on a new group of it
if m := q.Get("newgroup"); view == "routing" && m != "" {
view += "&newgroup=" + url.QueryEscape(m)
}
return view
}
// mainName is the id of a provider or an agent.
var mainName = regexp.MustCompile(`^[A-Za-z0-9._@:/ -]{1,80}$`)
// mainURL is the window's page for a view as mainView gives it, the
// request's id kept as its own parameter (the panel's link to one request,
// which escaping the whole of it lost).
func mainURL(view, query string) string { return "/?view=" + view + query }
// argView is a tab named on the command line (`magpie gui settings`, a
// restart to update) as ShowMain takes it: a name, never parameters.
func argView(s string) string { return url.QueryEscape(s) }
// quotaView opens the Usage overview at a provider/account card, or at
// the period of one of magpie's own totals (trayTotal). Invalid provider
// names fall back to the overview without a target.
func quotaView(id string) string {
view := "usage&tab=usage"
if p, ok := trayTotalPeriod(id); ok {
// magpie's own total: the Requests tab at its period, which sums
// what the total does (trayTotalSum)
return "usage&tab=requests&period=" + string(p)
}
provider, _, hasAccount := strings.Cut(id, "|")
if mainName.MatchString(provider) {
view += "&provider=" + url.QueryEscape(provider)
if hasAccount {
view += "&card=" + url.QueryEscape(id)
}
}
return view
}
// traceRoutes serves the routing trace for the Gateway view to play: it
// waits up to 25 s for something to change after the seq it is given, so
// the page hears of a request as it happens.
func traceRoutes(mux *http.ServeMux) {
// Names can arrive or change after a route finishes, independently of the
// trace sequence. Read only the IDs the page currently lists.
mux.HandleFunc("POST /api/gateway/session-titles", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
IDs []string `json:"ids"`
Groups bool `json:"groups"`
RouteIDs []int64 `json:"routeIds"`
Day string `json:"day"`
// Sessions are other agents' sessions as agent:id, named
// from their own files (sessions.Titles) rather than Codex's
// index (#1293)
Sessions []string `json:"sessions"`
}
r.Body = http.MaxBytesReader(rw, r.Body, 256<<10)
if json.NewDecoder(r.Body).Decode(&in) != nil || len(in.IDs) > 2000 || len(in.RouteIDs) > 2000 || len(in.Sessions) > 2000 {
http.Error(rw, "invalid session IDs", http.StatusBadRequest)
return
}
if len(in.Sessions) > 0 && len(in.IDs) == 0 {
writeJSON(rw, map[string]any{"titles": sessions.Titles(in.Sessions)})
return
}
if !in.Groups {
writeJSON(rw, sessions.CodexTitles(in.IDs))
return
}
var rows []gateway.Route
gw := served.Load()
if in.Day != "" {
if _, err := time.Parse("2006-01-02", in.Day); err != nil {
http.Error(rw, "invalid route day", http.StatusBadRequest)
return
}
rows = recalledTitleHistory(in.Day)
} else if gw != nil {
rows = gw.Trace(r.Context(), 0, 0).Routes
}
if gw == nil {
gw = &gateway.Server{}
}
rows = gw.ResolveTitleParents(rows)
wanted := make(map[int64]bool, len(in.RouteIDs))
for _, id := range in.RouteIDs {
wanted[id] = true
}
parents := map[int64]string{}
matched := map[int64]bool{}
for _, row := range rows {
if !wanted[row.ID] || row.Agent != "codex" {
continue
}
parents[row.ID], matched[row.ID] = row.ParentSession, row.ParentMatched
if row.ParentSession != "" {
in.IDs = append(in.IDs, row.ParentSession)
}
}
writeJSON(rw, map[string]any{"names": sessions.CodexTitles(in.IDs), "parents": parents, "matched": matched,
"resetInferred": in.Day != "" && rows == nil})
})
mux.HandleFunc("GET /api/gateway/route", func(rw http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.URL.Query().Get("id"), 10, 64)
if err != nil || id <= 0 {
http.Error(rw, "invalid route id", http.StatusBadRequest)
return
}
if gw := served.Load(); gw != nil {
for _, route := range gw.Trace(r.Context(), 0, 0).Routes {
if route.ID == id {
writeJSON(rw, pricedRoutes([]gateway.Route{route})[0])
return
}
}
}
day, err := time.Parse("2006-01-02", r.URL.Query().Get("day"))
if err != nil {
http.Error(rw, "invalid route day", http.StatusBadRequest)
return
}
if route, ok := gateway.HistoryRoute(id, day); ok {
writeJSON(rw, pricedRoutes([]gateway.Route{route})[0])
return
}
http.Error(rw, "route history is no longer available", http.StatusNotFound)
})
mux.HandleFunc("GET /api/gateway/trace", func(rw http.ResponseWriter, r *http.Request) {
gw := served.Load()
out := traceJSON{TraceState: gateway.TraceState{Routes: []gateway.Route{}}, Mine: gw != nil}
after, _ := strconv.ParseInt(r.URL.Query().Get("after"), 10, 64)
var wait time.Duration
if r.URL.Query().Get("wait") != "" {
wait = 25 * time.Second
}
if gw != nil {
out.TraceState = gw.Trace(r.Context(), after, wait)
} else if st, ok := gateway.OwnerTrace(r.Context(), after, wait); ok {
// another magpie on this computer serves the gateway: show the
// routing it decides here too, live, and let the page name the
// process that is routing (#2606)
out.TraceState = st
out.Mine = true
}
out.Now = time.Now()
out.Routes = pricedRoutes(out.TraceState.Routes)
writeJSON(rw, out)
})
// the routes of a day gone by, from the history the gateway keeps on
// disk — read whichever magpie serves the gateway
mux.HandleFunc("GET /api/gateway/history", func(rw http.ResponseWriter, r *http.Request) {
day := r.URL.Query().Get("day")
days, routes, cut := gateway.History(day)
rememberTitleHistory(day, routes)
writeJSON(rw, map[string]any{"days": days, "routes": pricedRoutes(routes), "cut": cut})
})
// an account's rest lifted by hand: verified with its vendor, say
mux.HandleFunc("POST /api/gateway/unrest", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
Key string `json:"key"`
}
gw := served.Load()
if json.NewDecoder(r.Body).Decode(&in) != nil || in.Key == "" || gw == nil {
http.Error(rw, "nothing to lift", http.StatusBadRequest)
return
}
writeJSON(rw, map[string]bool{"lifted": gw.Unrest(in.Key)})
})
}