Files
magpie/internal/usage/codex_review_free_test.go
T
yetone c201fe2266 usage: Codex's auto-review through a ChatGPT sign-in costs nothing (silverzhou)
silverzhou (Discord): codex-auto-review has been free for a long time,
but magpie still estimates a cost for it. OpenAI's Codex lead
(thsottiaux, "Day 2.1"): "We have made Auto-review free for all users
signed in through a ChatGPT account."

provider.ListPrice now prices codex-auto-review at $0 on the "codex"
provider, the one that holds only ChatGPT sign-ins, unless that sign-in
is on the Free plan: cybercaius measured auto-review spending the Free
allowance, which is why the gateway's freeOnChatGPT already left Free
out. That rule now has one owner, provider.ChatGPTReviewFree, and
freeOnChatGPT calls it. The plan is read from Codex's current sign-in;
an unreadable plan or a Codex signed out since counts as a paid plan
(priceOf's unknown-model branch answers $0 for it too).

Still priced, since nothing says those are free: codex-auto-review sent
with an API key (openai, chatgpt-api), through a relay, or a review
magpie made follow the session's model (the row's served model is that
model). A price the user set for the model wins over the $0.
PricedNameFor names the model itself, so the Requests list no longer
says "priced as gpt-5.6-luna". The usage summary cache key carries the
Codex plan, so a plan change re-prices cached views.

Old rows: prices are applied when the log is read, so every past
codex-auto-review row through a ChatGPT sign-in reads $0 after the
upgrade: Usage's Overview and Requests, the tray, the ledger and key
limits' estimated-cost caps (their in-memory sums are rebuilt at start).
Nothing is rewritten on disk.

Left alone: a Codex window's holds (apiPricer) still count these calls,
filed as #4181. docs/subsystems/providers-accounts.md has the rule.

Run at this tree (rebased on 033b17baf):
- mote-run: TestAutoReviewFreeOnChatGPTSignIn (provider) and
  TestAutoReviewFreeOnChatGPT (usage: ledger, Summarize, ledger sum,
  NewCoster), the KimiCode, CodexSignInPlan and gateway Review tests: ok.
- Red on the old code: both new tests fail with the code changes taken
  out (codex priced at luna, $0.234; summary 0.992 vs 0.758; key limit
  0.134).
- Broken by hand: dropping the plan check or the provider check turns
  the Free-plan and relay/openai assertions red in both tests.
- go vet on mote, and GOOS=linux and GOOS=windows; gofmt -l clean.
- At the rebased head, mote-run go test ./internal/provider
  ./internal/usage ./internal/usageapi ./internal/gateway: all ok (gateway 1883s, provider 846s,
  usage 158s, usageapi 4s); gofmt -l clean.
- Full suite: a mote run at the pre-rebase head reached two packages (ok)
  and then died (exit 144) when the touched-package run re-synced the
  same mote copy, at load ~340 with five other full suites; per the owner's
  ship-at-once rule it is run again after the push.
Not tried: a real auto-review row (none in ~/.codex/sessions or the
real usage log); a multi-account Codex with mixed plans (the live
sign-in's plan decides); the summary cache's plan key was not broken by
hand.
2026-10-29 16:11:26 +08:00

74 lines
3.0 KiB
Go

package usage
import (
"math"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// silverzhou on Discord: OpenAI made Codex's Auto-review free for users
// signed in through a ChatGPT account, and the Usage page still counted
// it at GPT-5.6 Luna's price. A codex-auto-review call through the Codex
// subscription is now priced at nothing, rows logged before the change
// too (they are priced as they are read); the same model through an API
// key, and a review that followed the session to another model, are
// priced as before. The key limits' cost counts the same (NewCoster).
func TestAutoReviewFreeOnChatGPT(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
if err := os.WriteFile(catalog.CachePath(), []byte(`{"openai":{"id":"openai","models":{`+
`"gpt-5.6-luna":{"id":"gpt-5.6-luna","cost":{"input":3,"output":7,"cache_read":1}},`+
`"gpt-5.6":{"id":"gpt-5.6","cost":{"input":5,"output":20,"cache_read":0.5}}}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
in, out, cached := 40_000, 2_000, 100_000
luna := (float64(in)*3 + float64(out)*7 + float64(cached)*1) / 1e6
gpt := (float64(in)*5 + float64(out)*20 + float64(cached)*0.5) / 1e6
now := time.Now()
calls := []struct {
prov, model, asked string
want float64
}{
{"codex", "codex-auto-review", "codex-auto-review", 0},
{"openai", "codex-auto-review", "codex-auto-review", luna},
{"codex", "gpt-5.6", "codex-auto-review", gpt}, // followed the session
{"codex", "gpt-5.6-luna", "gpt-5.6-luna", luna},
}
for i, c := range calls {
Append(Record{Time: now.Add(-time.Duration(len(calls)-i) * time.Minute), Agent: "codex", Provider: c.prov, Model: c.model, Requested: c.asked,
Kind: "guardian", Input: in, Output: out, CacheRead: cached, Status: 200})
}
rows, sum, _ := Ledger(Month, Filter{})
if len(rows) != len(calls) {
t.Fatalf("rows %d", len(rows))
}
var total float64
for i, c := range calls {
r := rows[len(calls)-1-i]
if r.Provider != c.prov || r.Model != c.model || !r.Priced || math.Abs(r.Cost-c.want) > 1e-9 {
t.Errorf("%s/%s: priced=%v cost=%v, want %v", c.prov, c.model, r.Priced, r.Cost, c.want)
}
total += c.want
}
if s := Summarize(Month); s.Unpriced != 0 || math.Abs(s.Cost-total) > 1e-9 || math.Abs(sum.Cost-total) > 1e-9 {
t.Errorf("summary %v (unpriced %d), ledger %v; want %v", s.Cost, s.Unpriced, sum.Cost, total)
}
cost := NewCoster()
if c, ok := cost(Record{Provider: "codex", Model: "codex-auto-review", Input: in, Output: out}); !ok || c != 0 {
t.Errorf("key limit counts a free review at %v (%v)", c, ok)
}
if c, ok := cost(Record{Provider: "openai", Model: "codex-auto-review", Input: in, Output: out}); !ok || c == 0 {
t.Errorf("key limit counts an API key's review at %v (%v)", c, ok)
}
}