Files
magpie/internal/usage/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

90 lines
3.5 KiB
Go

package usage
import (
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// readBack is r as the ledger reads it back: from a chunk as written, priced
// into a block, and packed to its archive and out again.
func readBack(t *testing.T, r Record) []Row {
t.Helper()
made := func() *rowChunk {
var chunk rowChunk
for i := 0; i < 80; i++ {
chunk.add(Row{Record: r}, "", int64(i), false)
}
return &chunk
}
b := newBlockPricer(nil, nil, func(Record) *catalog.Price { return nil })
defer b.release()
raw := made()
return []Row{raw.row(0), b.block(raw).row(0), b.block(made().pack()).row(0)}
}
// A call whose vendor counted no cache (a plugin's magpie_cache_unknown:
// Kiro tells only credits) is read back from the ledger as one, so its
// CacheRead of 0 is never read as a miss (#3464). A vendor that counted a
// miss stays one, and a Kiro call kept before its plugin said so is read
// as uncounted: Kiro never counted its cache.
func TestCacheUnknownKeptInTheLedger(t *testing.T) {
at := time.Now()
for _, c := range []struct {
name string
r Record
want bool
}{
{"said so", Record{Time: at, Provider: "zcode", Model: "glm-5", Input: 148000, Output: 19, CacheUnknown: true, Status: 200}, true},
{"a counted miss", Record{Time: at, Provider: "anthropic", Model: "claude-sonnet-4-5", Input: 148000, Output: 19, Status: 200}, false},
{"Kiro, kept before", Record{Time: at, Provider: "kiro", Model: "claude-sonnet-4.5", Input: 148000, Output: 19, Status: 200}, true},
{"Kiro's plugin id, kept before", Record{Time: at, Provider: "kiro-plugin", Model: "claude-sonnet-4.5", Input: 148000, Output: 19, Status: 200}, true},
{"Kiro with cache counted", Record{Time: at, Provider: "kiro", Model: "claude-sonnet-4.5", Input: 1000, CacheRead: 9000, Output: 19, Status: 200}, false},
} {
t.Run(c.name, func(t *testing.T) {
for i, row := range readBack(t, c.r) {
if row.CacheUnknown != c.want {
t.Fatalf("read %d: cache unknown %v, want %v", i, row.CacheUnknown, c.want)
}
}
})
}
}
// A hit rate leaves out the prompt of calls whose vendor counted no cache:
// a mixed ledger isn't diluted by them, and one of only those has none,
// where it read 0% (zeewell on X, #3464).
func TestHitRateLeavesUncountedOut(t *testing.T) {
at := time.Now()
counted := Record{Time: at, Provider: "anthropic", Model: "claude-sonnet-4-5", Input: 500, CacheRead: 9000, CacheWrite: 500, Output: 10, Status: 200}
kiro := Record{Time: at, Provider: "kiro", Model: "claude-sonnet-4.5", Input: 148000, Output: 19, CacheUnknown: true, Status: 200}
// kept before the plugin said so: Kiro never counts its cache
kiroBefore := Record{Time: at, Provider: "kiro-plugin", Model: "claude-sonnet-4.5", Input: 90000, Output: 19, Status: 200}
for _, c := range []struct {
name string
rs []Record
rate float64
known bool
calls int
}{
{"mixed", []Record{counted, kiro, kiro, kiroBefore}, 0.9, true, 3},
{"only uncounted", []Record{kiro, kiroBefore}, 0, false, 2},
{"only counted", []Record{counted}, 0.9, true, 0},
} {
t.Run(c.name, func(t *testing.T) {
var byRecord, byRow Totals
for _, r := range c.rs {
byRecord.add(r, nil)
byRow.addRow(Row{Record: r})
}
for _, tot := range []Totals{byRecord, byRow} {
rate, known := tot.HitRate()
if known != c.known || rate != c.rate || tot.CacheUnknownCalls != c.calls {
t.Fatalf("hit rate %v (%v), %d left out; want %v (%v), %d: %+v", rate, known, tot.CacheUnknownCalls, c.rate, c.known, c.calls, tot)
}
}
})
}
}