Files
magpie/internal/gui/cache_unknown_test.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

65 lines
2.2 KiB
Go

package gui
import (
"slices"
"testing"
"github.com/yetone/magpie/internal/gateway"
)
// A Kiro request's prompt is counted as Kiro counts it, prompt tokens but
// no cache, so the Context page scores its agent neither up nor down for
// the cache and doesn't call it cache-misses, and each point says its cache
// is unknown (#3464): the plugin said so (CacheUnknown), or it was kept
// before the plugin did and Kiro never counts it.
func TestContextKiroCacheNotAMiss(t *testing.T) {
for _, c := range []struct {
name string
u gateway.RouteUsage
}{
{"said so", gateway.RouteUsage{Provider: "kiro", Input: 9000, CacheUnknown: true}},
{"kept before", gateway.RouteUsage{Provider: "kiro", Input: 9000}},
} {
t.Run(c.name, func(t *testing.T) {
var routes []gateway.Route
for i := range 6 {
r := ctxRoute(int64(i+1), "claude", "s1", 8000, 1000*i, 1000000, 0)
r.Usage = []gateway.RouteUsage{c.u}
routes = append(routes, r)
}
out := contextOf(routes, 1)
a := out.Agents[0]
if a.Scores[0].Key != "cache" || a.Scores[0].Points != 18 || a.Cache != 0 {
t.Fatalf("cache %v, score %+v", a.Cache, a.Scores[0])
}
if slices.Contains(tagKeys(a), "cache-misses") {
t.Fatalf("tags %v", tagKeys(a))
}
for _, p := range out.Sessions[0].Points {
if !p.CacheUnknown {
t.Fatalf("point %+v", p)
}
}
})
}
}
// The Routing page is sent a Kiro try kept before its plugin said Kiro
// counts no cache as one that doesn't, so it shows no hit rate rather than
// 0% (zeewell on X, #3464). A vendor that counts its cache keeps its 0%,
// and the route read is left as it was.
func TestRoutingKiroCacheKeptBefore(t *testing.T) {
kept := []gateway.RouteUsage{{Provider: "kiro", Input: 148000, Output: 19}, {Provider: "anthropic", Input: 1200, Output: 1}}
got := cacheCounted(kept)
if !got[0].CacheUnknown || got[1].CacheUnknown {
t.Fatalf("%+v", got)
}
if kept[0].CacheUnknown {
t.Fatal("the route read was changed")
}
routes := pricedRoutes([]gateway.Route{{ID: 1, Agent: "claude", Provider: "kiro", Model: "claude-sonnet-4.5", Done: true, Status: 200, Usage: kept[:1]}})
if len(routes) != 1 || !routes[0].Usage[0].CacheUnknown {
t.Fatalf("%+v", routes)
}
}