Files
magpie/internal/usage/cursor_window_cost_test.go
T
yetone 7b5e773873 provider: a model at a window (claude-opus-5-5@300k) is priced as its model, so Cursor's sized ids have a cost
RyuJeXu on X (tweet 2117971061103100279), building an iOS board on magpie's
usage data: "cursor的用量好像不计费呀". Cursor calls showed no cost.

Cause: the Cursor plugin lists each model once per context size
(claude-opus-5-5@300k, @1m, gpt-5.5@272k, ...), lists them all at cost 0
(no price, pluginPrice), and its reply echoes the asked id, so usage rows
carry the sized id. pricedNames only took a window off grok- ids, so every
other sized id fell through the maker catalogs with no price. The plain ids
(claude-opus-5-5) were priced at the maker's list price all along.

Fix: pricedNames also tries the id with an "@<digits>k|m" window taken off
(modelWindow), for every provider. The window names the context size, not
a different model; long-context tiers still follow the call's own prompt
tokens through Price.At. Dated ids (claude-opus-4-5@20251101) and ids with
anything else after the @ are left alone. The regexp runs only for an id
with an @, since every listed model is priced on each Providers poll.

Tests:
- internal/provider/cursor_price_test.go: a moved Cursor provider prices
  @300k, @1m, upper case, a maker-prefixed id and gpt-5.5@272k/@1m as the
  plain model, through ListPrice, MakerPrice, PricedName(For), and its
  tier at 300K prompt tokens; a dated id, "@272", "@", "@272kb", an
  unknown model and auto stay as they were.
- internal/usage/cursor_window_cost_test.go: a Cursor row for
  claude-opus-5-5@300k costs what the plain id does ($6 each), auto stays
  unpriced, the page total is $12.
- grok_price_test.go: gpt-6-astra@500k under grok is now priced ($10/$50);
  grok-4.7-build@500k still isn't.

Run (all through mote-run, go1.26.8, base 823e7c250, the committed tree):
- gofmt -l internal/provider internal/usage: clean.
- go test -tags nogui -count=1 -timeout 45m ./internal/provider
  ./internal/usage ./internal/usageapi: ok 239s / 75s / 1s.
- GOOS=windows go vet and GOOS=linux go vet -tags nogui of provider,
  usage, usageapi: ok.
- Real catalog (a copy of the owner's models.json) before the change: every
  non-Grok sized Cursor id was unpriced; plain ids priced.

Broken by hand: with the old models.go put back, the two new tests fail on
every sized id (usage total 6 instead of 12) and TestGrokEffortListPrice
fails on gpt-6-astra@500k.

Not tried / not changed:
- No real Cursor call; the owner's ledger copy has only auto rows.
- Cursor's own auto and composer-2.5, and its own spellings (glm-5p3,
  deepseek-v41-flash-1017) and gemini-3.1-pro have no catalog price and
  stay unpriced; the plugin would have to give them a cost.
- Not ./... and no GUI suite: the change is in provider's price lookup
  only.
2026-11-04 22:45:40 +08:00

46 lines
1.7 KiB
Go

package usage
import (
"os"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// A call the Cursor plugin served on a model listed at a context size
// (claude-opus-5-5@300k) is counted at its model's price on the request
// log, as a call on the plain id is, not at no cost (RyuJeXu on X, whose
// dashboard reads magpie's usage). A call on a model no catalog prices
// (Cursor's auto) stays unpriced.
func TestCursorWindowCallIsPriced(t *testing.T) {
pageHome(t)
os.WriteFile(catalog.CachePath(), []byte(`{"anthropic":{"id":"anthropic","models":{"claude-opus-5-5":{"id":"claude-opus-5-5","cost":{"input":4,"output":20,"cache_read":0.2,"cache_write":5}}}}}`), 0600)
now := holdClock(t, time.Date(2026, 11, 3, 12, 0, 0, 0, time.Local))
at := Today.Since(now).Add(time.Second)
for i, m := range []string{"claude-opus-5-5@300k", "claude-opus-5-5", "auto"} {
Append(Record{Time: at.Add(time.Duration(i) * time.Second), Agent: "claude-code", Provider: "cursor", Model: m, Requested: "cursor/" + m,
Served: m, Input: 1_000_000, Output: 100_000, Status: 200})
}
p := QueryPage(Today, Filter{}, 0, 10)
if len(p.Rows) != 3 {
t.Fatalf("rows = %+v", p.Rows)
}
cost := map[string]Row{}
for _, r := range p.Rows {
cost[r.Model] = r
}
// $4 for 1M in, $2 for 100K out
for _, m := range []string{"claude-opus-5-5@300k", "claude-opus-5-5"} {
if r := cost[m]; !r.Priced || r.Cost < 5.999 || r.Cost > 6.001 {
t.Errorf("%s: priced %v, cost %v; want $6", m, r.Priced, r.Cost)
}
}
if r := cost["auto"]; r.Priced {
t.Errorf("auto: priced at %v; want unpriced", r.Cost)
}
if p.Sum.Cost < 11.999 || p.Sum.Cost > 12.001 {
t.Errorf("total cost = %v; want $12", p.Sum.Cost)
}
}