From e50b88d67c5a00e1eef7929cf75c1150178d6b51 Mon Sep 17 00:00:00 2001 From: merefield Date: Mon, 24 Aug 2026 11:54:34 +0100 Subject: [PATCH 1/2] Account for subscription benchmarks in quota estimates --- README.md | 11 ++ internal/codex/benchmark.go | 20 +++ internal/ui/benchmark_quota.go | 161 ++++++++++++++++++++ internal/ui/benchmark_quota_test.go | 219 ++++++++++++++++++++++++++++ internal/ui/model.go | 39 ++++- internal/ui/quota_api_eq.go | 3 + 6 files changed, 446 insertions(+), 7 deletions(-) create mode 100644 internal/ui/benchmark_quota.go create mode 100644 internal/ui/benchmark_quota_test.go diff --git a/README.md b/README.md index 8925cad0..5e6b0616 100644 --- a/README.md +++ b/README.md @@ -463,6 +463,17 @@ cache-write input, and output are priced separately; requests above the published 272,000-input-token threshold use the corresponding long-context rates where OpenAI publishes them. Unknown models or missing price classes fail closed as `UNPRICED MODEL MIX` rather than being guessed or treated as free. +Core, Extended, and DigBench trials use ephemeral threads that intentionally do +not appear in normal persisted session telemetry. While a subscription-funded +benchmark suite is active, Quota views replace the numeric API-equivalent +estimate with `SUBSCRIPTION BENCHMARK ACTIVE` instead of presenting partial +accounting. When each trial finishes, its authoritative benchmark usage is +folded into the same process-local accounting used for quota learning; missing +or unpriceable benchmark usage fails closed and restarts the learning anchor. +Benchmarks funded by `CODEXOMETER_BENCHMARK_API_KEY` are excluded because they +do not consume the displayed subscription quota. Benchmark threads remain +hidden from Monitor; hiding presentation does not exclude their aggregate +subscription impact from Quota views. The embedded rates come from the [official OpenAI API pricing page](https://developers.openai.com/api/docs/pricing) and were retrieved on **2026-08-23**. diff --git a/internal/codex/benchmark.go b/internal/codex/benchmark.go index 1046c82a..7c588819 100644 --- a/internal/codex/benchmark.go +++ b/internal/codex/benchmark.go @@ -89,6 +89,26 @@ const ( BenchmarkUsageRawResponses BenchmarkUsageSource = "raw-responses" ) +// BenchmarkBillingSource identifies whether benchmark model calls consume the +// Codex subscription quota displayed by Codexometer or a separately billed API +// key. Quota accounting must not infer this from identical token telemetry. +type BenchmarkBillingSource string + +const ( + BenchmarkBillingUnknown BenchmarkBillingSource = "unknown" + BenchmarkBillingSubscription BenchmarkBillingSource = "subscription" + BenchmarkBillingAPIKey BenchmarkBillingSource = "api-key" +) + +// BenchmarkBillingSource reports the billing path selected for benchmark +// app-server sessions created by this client. +func (c Client) BenchmarkBillingSource() BenchmarkBillingSource { + if strings.TrimSpace(c.BenchmarkAPIKey) != "" { + return BenchmarkBillingAPIKey + } + return BenchmarkBillingSubscription +} + // BenchmarkResponseUsage is the exact upstream usage for one Responses API // completion when the current Codex app-server exposes experimental raw events. type BenchmarkResponseUsage struct { diff --git a/internal/ui/benchmark_quota.go b/internal/ui/benchmark_quota.go new file mode 100644 index 00000000..512f6729 --- /dev/null +++ b/internal/ui/benchmark_quota.go @@ -0,0 +1,161 @@ +package ui + +import ( + "fmt" + "math" + "strings" + + "github.com/merefield/codexometer/internal/codex" +) + +type benchmarkBillingProvider interface { + BenchmarkBillingSource() codex.BenchmarkBillingSource +} + +// benchmarkQuotaAccounting retains only content-free, process-local totals for +// ephemeral benchmark calls. These calls never enter the persisted rollout +// reader, but subscription-funded calls do move the account quota windows. +type benchmarkQuotaAccounting struct { + billingSource codex.BenchmarkBillingSource + revision uint64 + runSequence uint64 + active bool + settling bool + costUSD float64 + pricedCalls int64 + unpricedCalls int64 + accounted map[string]struct{} +} + +func benchmarkQuotaAccountingFor(runner BenchmarkRunner) benchmarkQuotaAccounting { + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingUnknown} + if provider, ok := runner.(benchmarkBillingProvider); ok { + accounting.billingSource = provider.BenchmarkBillingSource() + } + return accounting +} + +func (a *benchmarkQuotaAccounting) start() { + a.runSequence++ + a.active = a.billingSource == codex.BenchmarkBillingSubscription + a.settling = false + a.accounted = make(map[string]struct{}) + if a.active { + a.revision++ + } +} + +func (a *benchmarkQuotaAccounting) finish() bool { + wasActive := a.active + a.active = false + if wasActive { + a.settling = true + a.revision++ + } + return a.settling +} + +func (a *benchmarkQuotaAccounting) settle() { + a.settling = false +} + +func (a *benchmarkQuotaAccounting) abandonActiveResult() { + if a.billingSource != codex.BenchmarkBillingSubscription || !a.active { + return + } + a.unpricedCalls++ + a.revision++ +} + +func (a *benchmarkQuotaAccounting) observe(result codex.BenchmarkResult) { + if a.billingSource != codex.BenchmarkBillingSubscription { + return + } + key := fmt.Sprintf("%d\x00%s", a.runSequence, benchmarkRunKey(result)) + if _, exists := a.accounted[key]; exists { + return + } + a.accounted[key] = struct{}{} + + cost, known := benchmarkQuotaCost(result) + calls := int64(len(result.ResponseUsage)) + if calls == 0 { + calls = 1 + } + if known { + a.costUSD += cost + a.pricedCalls += calls + a.revision++ + return + } + a.unpricedCalls += calls + a.revision++ +} + +func benchmarkQuotaCost(result codex.BenchmarkResult) (float64, bool) { + if result.CostKnown && validBenchmarkQuotaCost(result.CostUSD) { + return result.CostUSD, true + } + model := strings.TrimSpace(result.ActualModel) + if model == "" { + model = result.Model + } + if len(result.ResponseUsage) > 0 { + var total float64 + for _, response := range result.ResponseUsage { + cost, known, _ := codex.EstimateStandardAPIEqCost(model, response.Usage) + if !known { + return 0, false + } + total += cost + } + return total, validBenchmarkQuotaCost(total) + } + if !result.UsageKnown { + return 0, false + } + cost, known, _ := codex.EstimateStandardAPIEqCost(model, result.Usage) + return cost, known && validBenchmarkQuotaCost(cost) +} + +func validBenchmarkQuotaCost(cost float64) bool { + return cost >= 0 && !math.IsNaN(cost) && !math.IsInf(cost, 0) +} + +func (a benchmarkQuotaAccounting) combine(usage codex.LiveUsageSnapshot) codex.LiveUsageSnapshot { + if a.billingSource != codex.BenchmarkBillingSubscription { + return usage + } + usage.APIEqUSD += a.costUSD + usage.APIEqPricedCalls += a.pricedCalls + usage.APIEqUnpricedCalls += a.unpricedCalls + if a.active { + usage.APIEqPendingCalls++ + } + return usage +} + +func (a benchmarkQuotaAccounting) deferred() bool { + return a.billingSource == codex.BenchmarkBillingSubscription && (a.active || a.settling) +} + +func (a benchmarkQuotaAccounting) deferredLabel(width int) string { + if a.active { + full, compact := "API-EQ DEFERRED // SUBSCRIPTION BENCHMARK ACTIVE", "API-EQ // BENCHMARK ACTIVE" + if width >= len(full) { + return full + } + if width >= len(compact) { + return compact + } + return "EQ // BENCH ACTIVE" + } + full, compact := "API-EQ DEFERRED // BENCHMARK ACCOUNTING SETTLING", "API-EQ // BENCHMARK SETTLING" + if width >= len(full) { + return full + } + if width >= len(compact) { + return compact + } + return "EQ // BENCH SETTLE" +} diff --git a/internal/ui/benchmark_quota_test.go b/internal/ui/benchmark_quota_test.go new file mode 100644 index 00000000..ce89efcc --- /dev/null +++ b/internal/ui/benchmark_quota_test.go @@ -0,0 +1,219 @@ +package ui + +import ( + "math" + "strings" + "testing" + "time" + + "github.com/merefield/codexometer/internal/codex" +) + +func TestSubscriptionBenchmarkAccountingDefersAndIncludesFinalCost(t *testing.T) { + now := time.Now() + reset := now.Add(7 * 24 * time.Hour).Unix() + model := Model{ + snapshot: apiEqSnapshot(10, reset), + quotaAPIAnchors: make(map[string]quotaAPIAnchor), + quotaAPIIssues: make(map[string]string), + quotaAPIAccount: "account-a", + benchmarkQuotaAccounting: benchmarkQuotaAccounting{ + billingSource: codex.BenchmarkBillingSubscription, + }, + } + model.observeQuotaAPIEq(model.snapshot, codex.LiveUsageSnapshot{}, now) + model.benchmarkQuotaAccounting.start() + if line := model.quotaAPILine(model.snapshot.Meters()[0], 100); !strings.Contains(line, "SUBSCRIPTION BENCHMARK ACTIVE") { + t.Fatalf("active benchmark API-EQ line = %q", line) + } + + result := codex.BenchmarkResult{ + TaskID: codex.BenchmarkDigBench, TaskName: "DIGBENCH P-6", + Model: "gpt-5.6-sol", Effort: "high", CostKnown: true, CostUSD: 120, + } + model.benchmarkQuotaAccounting.observe(result) + model.benchmarkQuotaAccounting.observe(result) + model.benchmarkQuotaAccounting.finish() + + accounting := model.benchmarkQuotaAccounting.combine(codex.LiveUsageSnapshot{ + APIEqUSD: 20, APIEqPricedCalls: 1, + }) + if accounting.APIEqPendingCalls != 0 || accounting.APIEqPricedCalls != 2 || math.Abs(accounting.APIEqUSD-140) > 1e-12 { + t.Fatalf("combined accounting = %#v", accounting) + } + advanced := apiEqSnapshot(15, reset) + model.snapshot = advanced + model.observeQuotaAPIEq(advanced, accounting, now.Add(time.Minute)) + estimate, ok := model.quotaAPIEstimate(advanced, advanced.Meters()[0]) + if !ok || math.Abs(estimate.fullLow-140.0*100/6) > 1e-9 || math.Abs(estimate.fullHigh-140.0*100/4) > 1e-9 { + t.Fatalf("benchmark-inclusive estimate = %#v, %v", estimate, ok) + } +} + +func TestSubscriptionBenchmarkAccountingFailsClosedForUnknownCost(t *testing.T) { + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingSubscription} + accounting.start() + accounting.observe(codex.BenchmarkResult{ + TaskID: codex.BenchmarkDigBench, TaskName: "DIGBENCH P-6", Model: "future-model", Effort: "high", + }) + accounting.finish() + combined := accounting.combine(codex.LiveUsageSnapshot{}) + if combined.APIEqUSD != 0 || combined.APIEqPricedCalls != 0 || combined.APIEqUnpricedCalls != 1 || combined.APIEqPendingCalls != 0 { + t.Fatalf("unknown benchmark accounting = %#v", combined) + } +} + +func TestSubscriptionBenchmarkAccountingRejectsInvalidKnownCost(t *testing.T) { + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingSubscription} + accounting.start() + accounting.observe(codex.BenchmarkResult{ + TaskID: codex.BenchmarkDigBench, TaskName: "DIGBENCH P-6", Model: "gpt-5.6-sol", Effort: "high", + CostKnown: true, CostUSD: math.Inf(1), + }) + accounting.finish() + combined := accounting.combine(codex.LiveUsageSnapshot{}) + if combined.APIEqUSD != 0 || combined.APIEqPricedCalls != 0 || combined.APIEqUnpricedCalls != 1 { + t.Fatalf("invalid known benchmark cost was accepted: %#v", combined) + } +} + +func TestSubscriptionBenchmarkAccountingFailsClosedForAbandonedActiveResult(t *testing.T) { + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingSubscription} + accounting.start() + accounting.abandonActiveResult() + accounting.finish() + combined := accounting.combine(codex.LiveUsageSnapshot{}) + if combined.APIEqUnpricedCalls != 1 || combined.APIEqPricedCalls != 0 || combined.APIEqUSD != 0 { + t.Fatalf("abandoned benchmark result was not marked unpriced: %#v", combined) + } +} + +func TestSubscriptionBenchmarkAccountingCanPricePolicyInvalidatedResult(t *testing.T) { + usage := codex.BenchmarkUsage{TotalTokens: 1200, InputTokens: 1000, CachedInputTokens: 800, OutputTokens: 200} + want, known, _ := codex.EstimateStandardAPIEqCost("gpt-5.6-sol", usage) + if !known { + t.Fatal("test usage should be priceable") + } + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingSubscription} + accounting.start() + accounting.observe(codex.BenchmarkResult{ + TaskID: codex.BenchmarkMergeRanges, TaskName: "MERGE RANGES", Model: "gpt-5.6-sol", Effort: "high", + Usage: usage, UsageKnown: true, CostIssue: "tool use prohibited", + }) + accounting.finish() + combined := accounting.combine(codex.LiveUsageSnapshot{}) + if combined.APIEqPricedCalls != 1 || combined.APIEqUnpricedCalls != 0 || math.Abs(combined.APIEqUSD-want) > 1e-12 { + t.Fatalf("policy-invalidated benchmark accounting = %#v, want cost %f", combined, want) + } +} + +func TestAPIKeyBenchmarkDoesNotAffectQuotaAccounting(t *testing.T) { + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingAPIKey} + accounting.start() + accounting.observe(codex.BenchmarkResult{ + TaskID: codex.BenchmarkDigBench, TaskName: "DIGBENCH P-6", + Model: "gpt-5.6-sol", Effort: "high", CostKnown: true, CostUSD: 120, + }) + base := codex.LiveUsageSnapshot{APIEqUSD: 3, APIEqPricedCalls: 2} + combined := accounting.combine(base) + if accounting.deferred() || combined.APIEqUSD != base.APIEqUSD || + combined.APIEqPricedCalls != base.APIEqPricedCalls || combined.APIEqUnpricedCalls != 0 || combined.APIEqPendingCalls != 0 { + t.Fatalf("API-key benchmark changed quota accounting: %#v", combined) + } +} + +func TestBenchmarkQuotaAccountingUsesExplicitClientBillingSource(t *testing.T) { + subscription := benchmarkQuotaAccountingFor(codex.Client{}) + apiKey := benchmarkQuotaAccountingFor(codex.Client{BenchmarkAPIKey: "secret"}) + if subscription.billingSource != codex.BenchmarkBillingSubscription || apiKey.billingSource != codex.BenchmarkBillingAPIKey { + t.Fatalf("billing sources = %q and %q", subscription.billingSource, apiKey.billingSource) + } +} + +func TestBenchmarkQuotaAccountingSettlesOnlyAfterStableRefresh(t *testing.T) { + now := time.Now() + snapshot := apiEqSnapshot(10, now.Add(time.Hour).Unix()) + model := Model{ + usageFetcher: stubLiveFetcher{}, + snapshot: snapshot, + quotaAPIAnchors: make(map[string]quotaAPIAnchor), + quotaAPIIssues: make(map[string]string), + benchmarkQuotaAccounting: benchmarkQuotaAccounting{ + billingSource: codex.BenchmarkBillingSubscription, + settling: true, + }, + } + if line := model.quotaAPILine(snapshot.Meters()[0], 100); !strings.Contains(line, "ACCOUNTING SETTLING") { + t.Fatalf("settling API-EQ line = %q", line) + } + updated, _ := model.Update(fetchedMsg{ + snapshot: snapshot, + usage: codex.LiveUsageSnapshot{}, + at: now, + }) + settled := updated.(Model) + if settled.benchmarkQuotaAccounting.deferred() { + t.Fatal("stable quota/accounting refresh did not settle benchmark accounting") + } +} + +func TestBenchmarkQuotaDeferredLabelsAreResponsive(t *testing.T) { + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingSubscription, active: true} + for _, width := range []int{20, 30, 60} { + if label := accounting.deferredLabel(width); len(label) > width { + t.Errorf("active label %q exceeds width %d", label, width) + } + } + accounting.active, accounting.settling = false, true + for _, width := range []int{20, 35, 60} { + if label := accounting.deferredLabel(width); len(label) > width { + t.Errorf("settling label %q exceeds width %d", label, width) + } + } +} + +func TestStaleQuotaFetchCannotSettleNewBenchmarkAccounting(t *testing.T) { + now := time.Now() + snapshot := apiEqSnapshot(15, now.Add(time.Hour).Unix()) + model := Model{ + usageFetcher: stubLiveFetcher{}, + snapshot: snapshot, + quotaAPIAnchors: make(map[string]quotaAPIAnchor), + quotaAPIIssues: make(map[string]string), + benchmarkQuotaAccounting: benchmarkQuotaAccounting{ + billingSource: codex.BenchmarkBillingSubscription, + revision: 2, + settling: true, + costUSD: 120, + pricedCalls: 1, + }, + } + updated, _ := model.Update(fetchedMsg{ + snapshot: snapshot, + usage: codex.LiveUsageSnapshot{APIEqUSD: 1, APIEqPricedCalls: 1}, + at: now, + benchmarkQuotaRevision: 1, + }) + deferred := updated.(Model) + if !deferred.benchmarkQuotaAccounting.deferred() || deferred.quotaAPITelemetryIssue != "OBSERVATION DEFERRED" || len(deferred.quotaAPIEvidence) != 0 { + t.Fatalf("stale fetch changed benchmark quota learning: accounting=%#v issue=%q evidence=%#v", + deferred.benchmarkQuotaAccounting, deferred.quotaAPITelemetryIssue, deferred.quotaAPIEvidence) + } +} + +func TestSubscriptionBenchmarkRerunIsAccountedAgain(t *testing.T) { + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingSubscription} + result := codex.BenchmarkResult{ + TaskID: codex.BenchmarkDigBench, TaskName: "DIGBENCH P-6", + Model: "gpt-5.6-sol", Effort: "high", CostKnown: true, CostUSD: 2, + } + for range 2 { + accounting.start() + accounting.observe(result) + accounting.finish() + } + combined := accounting.combine(codex.LiveUsageSnapshot{}) + if combined.APIEqUSD != 4 || combined.APIEqPricedCalls != 2 { + t.Fatalf("rerun accounting = %#v", combined) + } +} diff --git a/internal/ui/model.go b/internal/ui/model.go index 3926c6e0..8aa321ec 100644 --- a/internal/ui/model.go +++ b/internal/ui/model.go @@ -116,6 +116,7 @@ type Model struct { benchmarkDetailActive bool benchmarkDetailScroll int benchmarkDetailCache benchmarkDetailTranscriptCache + benchmarkQuotaAccounting benchmarkQuotaAccounting monitorState monitorState monitorAutoStart bool @@ -155,11 +156,12 @@ type Model struct { } type fetchedMsg struct { - snapshot codex.Snapshot - err error - usage codex.LiveUsageSnapshot - usageErr error - at time.Time + snapshot codex.Snapshot + err error + usage codex.LiveUsageSnapshot + usageErr error + at time.Time + benchmarkQuotaRevision uint64 } type secondMsg time.Time @@ -379,6 +381,7 @@ func New(fetcher Fetcher, refreshEvery time.Duration) Model { } if runner, ok := fetcher.(BenchmarkRunner); ok { model.benchmarkRunner = runner + model.benchmarkQuotaAccounting = benchmarkQuotaAccountingFor(runner) } if runner, ok := fetcher.(ScopedBenchmarkRunner); ok { model.benchmarkScopedRunner = runner @@ -703,9 +706,12 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) { if message.err == nil { m.snapshot = message.snapshot m.lastRefresh = time.Now() - if message.usageErr == nil && m.usageFetcher != nil { + if message.benchmarkQuotaRevision != m.benchmarkQuotaAccounting.revision || m.benchmarkQuotaAccounting.active { + m.quotaAPITelemetryIssue = "OBSERVATION DEFERRED" + } else if message.usageErr == nil && m.usageFetcher != nil { m.quotaAPITelemetryIssue = "" m.observeQuotaAPIEq(message.snapshot, message.usage, message.at) + m.benchmarkQuotaAccounting.settle() } else if message.usageErr != nil { if errors.Is(message.usageErr, errQuotaObservationChanged) { m.quotaAPITelemetryIssue = "OBSERVATION DEFERRED" @@ -799,6 +805,9 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) { } case benchmarkEventMsg: if !message.ok { + if m.benchmarkActive != nil { + m.benchmarkQuotaAccounting.abandonActiveResult() + } if m.benchmarkState == benchmarkStopping { m.benchmarkState = benchmarkStopped } else { @@ -808,6 +817,9 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) { m.benchmarkActiveSince = time.Time{} m.benchmarkEvents = nil m.benchmarkCancel = nil + if m.benchmarkQuotaAccounting.finish() { + return m, m.fetch() + } return m, nil } event := message.event @@ -842,6 +854,7 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) { m.benchmarkScroll++ } result := *event.Result + m.benchmarkQuotaAccounting.observe(result) m.benchmarkResults, m.benchmarkResultIndexes = upsertBenchmarkResult(m.benchmarkResults, m.benchmarkResultIndexes, result) m.benchmarkRunResults, m.benchmarkRunIndexes = upsertBenchmarkResult(m.benchmarkRunResults, m.benchmarkRunIndexes, result) if m.benchmarkDetailActive && m.benchmarkDetail != nil && benchmarkRunKey(*m.benchmarkDetail) == benchmarkRunKey(result) { @@ -856,6 +869,9 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) { m.benchmarkError = event.Err.Error() } if event.Done { + if m.benchmarkActive != nil && event.Result == nil { + m.benchmarkQuotaAccounting.abandonActiveResult() + } if event.Stopped { m.benchmarkState = benchmarkStopped } else { @@ -865,6 +881,9 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) { m.benchmarkActiveSince = time.Time{} m.benchmarkEvents = nil m.benchmarkCancel = nil + if m.benchmarkQuotaAccounting.finish() { + return m, m.fetch() + } return m, nil } return m, waitBenchmarkEvent(message.events) @@ -1281,6 +1300,7 @@ func (m Model) startBenchmark(tasks []codex.BenchmarkTaskID, scope codex.Benchma m.benchmarkScopeOpen = false m.benchmarkScopeKeyboard = false m.benchmarkState = benchmarkRunning + m.benchmarkQuotaAccounting.start() m.benchmarkAllArmed = false m.benchmarkSelectedArmed = false ctx, cancel := context.WithCancel(context.Background()) @@ -1763,11 +1783,16 @@ func (m Model) fetch() tea.Cmd { var beforeErr error if m.usageFetcher != nil { before, beforeErr = m.usageFetcher.FetchTokenUsage(context.Background()) + before = m.benchmarkQuotaAccounting.combine(before) } snapshot, err := m.fetcher.Fetch(context.Background()) - message := fetchedMsg{snapshot: snapshot, err: err, at: time.Now()} + message := fetchedMsg{ + snapshot: snapshot, err: err, at: time.Now(), + benchmarkQuotaRevision: m.benchmarkQuotaAccounting.revision, + } if err == nil && m.usageFetcher != nil { after, afterErr := m.usageFetcher.FetchTokenUsage(context.Background()) + after = m.benchmarkQuotaAccounting.combine(after) switch { case beforeErr != nil: message.usageErr = beforeErr diff --git a/internal/ui/quota_api_eq.go b/internal/ui/quota_api_eq.go index f080080f..40c61ac8 100644 --- a/internal/ui/quota_api_eq.go +++ b/internal/ui/quota_api_eq.go @@ -345,6 +345,9 @@ func (m Model) quotaMetersWithInsights(width int) []codex.Meter { } func (m Model) quotaAPILine(meter codex.Meter, width int) string { + if m.benchmarkQuotaAccounting.deferred() { + return m.benchmarkQuotaAccounting.deferredLabel(width) + } if m.quotaAPITelemetryIssue != "" { if width < 44 { return "API-EQ // " + m.quotaAPITelemetryIssue From a1b57f5f23d4c22e210e505986881fe5816533cf Mon Sep 17 00:00:00 2001 From: merefield Date: Mon, 24 Aug 2026 12:07:32 +0100 Subject: [PATCH 2/2] FIX: validate benchmark fallback pricing --- internal/codex/benchmark.go | 7 +++++++ internal/ui/benchmark_quota.go | 10 ++++----- internal/ui/benchmark_quota_test.go | 32 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/internal/codex/benchmark.go b/internal/codex/benchmark.go index 7c588819..ac4dacee 100644 --- a/internal/codex/benchmark.go +++ b/internal/codex/benchmark.go @@ -1663,6 +1663,13 @@ func EstimateStandardAPIEqCost(model string, usage BenchmarkUsage) (float64, boo return estimateSingleResponseAPICostWithIssue(model, usage) } +// EstimateStandardAPIEqAggregateCost prices aggregate usage only when its +// standard API cost can be reconstructed without response boundaries. Long +// context usage therefore fails closed because its surcharge is per response. +func EstimateStandardAPIEqAggregateCost(model string, usage BenchmarkUsage) (float64, bool, string) { + return estimateAPICostWithIssue(model, usage) +} + func priceForModel(model string) (apiPrice, bool) { model = strings.ToLower(strings.TrimSpace(model)) if price, ok := standardAPIPrices[model]; ok { diff --git a/internal/ui/benchmark_quota.go b/internal/ui/benchmark_quota.go index 512f6729..c67fb127 100644 --- a/internal/ui/benchmark_quota.go +++ b/internal/ui/benchmark_quota.go @@ -100,7 +100,10 @@ func benchmarkQuotaCost(result codex.BenchmarkResult) (float64, bool) { if model == "" { model = result.Model } - if len(result.ResponseUsage) > 0 { + if !result.UsageKnown { + return 0, false + } + if result.UsageSource == codex.BenchmarkUsageRawResponses && len(result.ResponseUsage) > 0 { var total float64 for _, response := range result.ResponseUsage { cost, known, _ := codex.EstimateStandardAPIEqCost(model, response.Usage) @@ -111,10 +114,7 @@ func benchmarkQuotaCost(result codex.BenchmarkResult) (float64, bool) { } return total, validBenchmarkQuotaCost(total) } - if !result.UsageKnown { - return 0, false - } - cost, known, _ := codex.EstimateStandardAPIEqCost(model, result.Usage) + cost, known, _ := codex.EstimateStandardAPIEqAggregateCost(model, result.Usage) return cost, known && validBenchmarkQuotaCost(cost) } diff --git a/internal/ui/benchmark_quota_test.go b/internal/ui/benchmark_quota_test.go index ce89efcc..5075de77 100644 --- a/internal/ui/benchmark_quota_test.go +++ b/internal/ui/benchmark_quota_test.go @@ -77,6 +77,38 @@ func TestSubscriptionBenchmarkAccountingRejectsInvalidKnownCost(t *testing.T) { } } +func TestSubscriptionBenchmarkAccountingRejectsUnvalidatedResponseUsage(t *testing.T) { + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingSubscription} + accounting.start() + accounting.observe(codex.BenchmarkResult{ + TaskID: codex.BenchmarkDigBench, TaskName: "DIGBENCH P-6", Model: "gpt-5.6-sol", Effort: "high", + ResponseUsage: []codex.BenchmarkResponseUsage{{ + ResponseID: "partial-response", + Usage: codex.BenchmarkUsage{TotalTokens: 1200, InputTokens: 1000, CachedInputTokens: 800, OutputTokens: 200}, + }}, + }) + accounting.finish() + combined := accounting.combine(codex.LiveUsageSnapshot{}) + if combined.APIEqUSD != 0 || combined.APIEqPricedCalls != 0 || combined.APIEqUnpricedCalls != 1 { + t.Fatalf("unvalidated response usage was accepted: %#v", combined) + } +} + +func TestSubscriptionBenchmarkAccountingRejectsAggregateLongContextUsage(t *testing.T) { + accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingSubscription} + accounting.start() + accounting.observe(codex.BenchmarkResult{ + TaskID: codex.BenchmarkDigBench, TaskName: "DIGBENCH P-6", Model: "gpt-5.6-sol", Effort: "high", + UsageKnown: true, UsageSource: codex.BenchmarkUsageCumulative, + Usage: codex.BenchmarkUsage{TotalTokens: 300_100, InputTokens: 300_000, OutputTokens: 100}, + }) + accounting.finish() + combined := accounting.combine(codex.LiveUsageSnapshot{}) + if combined.APIEqUSD != 0 || combined.APIEqPricedCalls != 0 || combined.APIEqUnpricedCalls != 1 { + t.Fatalf("aggregate long-context usage was accepted: %#v", combined) + } +} + func TestSubscriptionBenchmarkAccountingFailsClosedForAbandonedActiveResult(t *testing.T) { accounting := benchmarkQuotaAccounting{billingSource: codex.BenchmarkBillingSubscription} accounting.start()