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.
116 lines
4.4 KiB
Go
116 lines
4.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// isMemoryKind: a call Codex makes to write or consolidate its memories,
|
|
// however it names one (usage.PurposeOf's memory_consolidation).
|
|
func isMemoryKind(kind string) bool {
|
|
return usage.PurposeOf(kind) == "kind:memory_consolidation"
|
|
}
|
|
|
|
// codexTurnKey is whose turns codexTurns holds: the agent recorded and the
|
|
// gateway key it came with, so a Codex on another computer (its own key,
|
|
// #742's Codex-BWG and Codex-R4S) is told apart from this one's.
|
|
func codexTurnKey(r *http.Request, who string) string {
|
|
return who + "\x00" + access.Caller(r.Context()).KeyID
|
|
}
|
|
|
|
// rememberCodexTurn keeps the model a turn of Codex's own conversation
|
|
// was asked on, one magpie serves, for its memory calls.
|
|
func (s *Server) rememberCodexTurn(r *http.Request, who, agent, kind, asked string) {
|
|
if agent != "codex" || kind != "" {
|
|
return
|
|
}
|
|
s.codexTurns.Store(codexTurnKey(r, who), asked)
|
|
// and by its conversation, for a memory call that raced it
|
|
// (codexMemoryStandIn)
|
|
s.codexLeads.saw(r, kind, asked)
|
|
}
|
|
|
|
// isReviewCall: Codex's auto-review of an approval (the guardian), which
|
|
// it asks for on codex-auto-review, the reviewer model of OpenAI's, when
|
|
// its list has that or names it as a model's auto_review_model_override.
|
|
func isReviewCall(kind, asked string) bool {
|
|
return kind == "guardian" || asked == "codex-auto-review"
|
|
}
|
|
|
|
// freeOnChatGPT says whether a call on model by a ChatGPT account on plan
|
|
// is free (provider.ChatGPTReviewFree, which the Usage page's prices
|
|
// follow too).
|
|
func freeOnChatGPT(model, plan string) bool { return provider.ChatGPTReviewFree(model, plan) }
|
|
|
|
// codexMemoryStandIn is the model a Codex memory call goes to when magpie
|
|
// knows none by the name it came with (#742): Codex writes and
|
|
// consolidates its memories on models of its own (memories.extract_model
|
|
// and consolidation_model, gpt-5.6-luna and gpt-5.6-terra by default) by
|
|
// their bare names, whatever it is set to, so with magpie as its provider
|
|
// every one was turned away as an unknown model before it was routed. It
|
|
// goes to the model that Codex's own turns go to — the last one it had
|
|
// answered here, else the last in the ledger (a week back) from the same
|
|
// agent with the same key — so it is routed, and in the views, as the
|
|
// memory call it is. Its auto-review (isReviewCall) goes there too when
|
|
// Codex's config names no model magpie serves (StandIn): Codex reviews on
|
|
// the conversation's model when it has no reviewer, and a Codex whose
|
|
// config magpie doesn't read as its own — on magpie through another
|
|
// provider table or a profile, under a CODEX_HOME of its own, or on
|
|
// another computer — was answered 404 "magpie knows no model
|
|
// \"codex-auto-review\"" (Adam on Discord). "" for any other call, a model
|
|
// magpie serves, or a Codex that has had no turn answered here.
|
|
//
|
|
// Codex starts its memories with each turn of a conversation, so a memory
|
|
// call can come before the turn it was started with, the first after the
|
|
// model was switched: it is read after that turn, as the app's titles are
|
|
// (codexLeads.sideOf, beside), not on the model switched away from
|
|
// (cybercaius on Discord).
|
|
func (s *Server) codexMemoryStandIn(r *http.Request, who, agent, kind, asked string, body []byte) string {
|
|
if agent != "codex" || !isMemoryKind(kind) && !isReviewCall(kind, asked) {
|
|
return ""
|
|
}
|
|
if _, _, ok := provider.Resolve(asked); ok {
|
|
return ""
|
|
}
|
|
if isMemoryKind(kind) {
|
|
if m := s.codexLeads.sideOf(r, body, true); m != "" && m != asked {
|
|
if _, _, ok := provider.Resolve(m); ok {
|
|
return m
|
|
}
|
|
}
|
|
}
|
|
key := codexTurnKey(r, who)
|
|
if m, ok := s.codexTurns.Load(key); ok {
|
|
if m, _ := m.(string); m != "" && m != asked {
|
|
if _, _, ok := provider.Resolve(m); ok {
|
|
return m
|
|
}
|
|
}
|
|
}
|
|
keyID := access.Caller(r.Context()).KeyID
|
|
var last usage.Record
|
|
usage.Visit(time.Now().Add(-7*24*time.Hour), func(rec usage.Record) {
|
|
if rec.Agent != who || rec.CallerKeyID != keyID || rec.Kind != "" || rec.Rejected || rec.IsReported() || rec.Failed() || rec.Requested == "" {
|
|
return
|
|
}
|
|
if rec.Time.After(last.Time) {
|
|
last = rec
|
|
}
|
|
})
|
|
m := last.Requested
|
|
if id, ok := provider.GroupFor(m); ok {
|
|
m = id
|
|
}
|
|
if m == "" || m == asked {
|
|
return ""
|
|
}
|
|
if _, _, ok := provider.Resolve(m); !ok {
|
|
return ""
|
|
}
|
|
return m
|
|
}
|