Files
magpie/internal/gateway/codex_memory.go
T
yetone 6e96903346 codex: auto-review follows the session on a ChatGPT Free plan (cybercaius)
cybercaius on Discord measured codex-auto-review spending a Free
account's allowance ("实测在 free 账号不免费"). magpie left an unset
auto-review to Codex (codex-auto-review), and held such a call by no
usage cap, on #1593's finding that OpenAI counts it in no window; that
was measured on a paid account.

Decision: with nothing picked and Codex signed in to a Free plan (the
ID token's chatgpt_plan_type is "free"), auto-review counts as "Same as
the session", as Codex's other side calls do, so the review stops
spending the free allowance when the session is on one of magpie's
models. On Codex's own model nothing changes (its entries keep OpenAI's
reviewer). A paid plan, or a plan magpie can't read, keeps Codex's pick;
a pick in Settings always wins.

- codexcat.AutoReviewModel is the setting in effect; the plan comes from
  provider.CodexSignInPlan (liveLogin, keychain read cached), read once
  per model list, and the list tag already re-stamps on auth.json.
- freeOnChatGPT no longer exempts a Free plan's codex-auto-review call
  from usage caps (capHeld).
- GUI tooltip says it (en, zh, zh-TW, ja, de); docs updated.

Run at the pushed tree (on origin/main 88ac42843), go test -tags nogui
-count=1 -timeout 45m: internal/gateway ok 447s, internal/codexcat ok,
internal/gui ok, internal/agent/... ok; internal/provider crashes in
TestUnreadLoginsWriteErrorsKeepCredentials ("close of synctest channel
from outside bubble" in filememo.Forget), red the same alone on
origin/main 88ac42843, filed as #3632 with a patch there; with that
test skipped internal/provider ok 152s. gofmt -l clean; GOOS=linux go
vet -tags nogui ./... and GOOS=windows go vet ./... clean.
- GUI (chromium + webkit, every language): codex-auto-review, i18n-keys,
  gui-ja, gui-zh-tw, gui-de, search-keys, agent-prefs: 79 of 79 pass
Broken by hand: with AutoReviewModel returning the raw setting,
TestCodexAutoReviewOnAFreePlan fails; with freeOnChatGPT ignoring the
plan, TestAccountCapHoldsAutoReviewOnAFreePlan fails.
TestCodexSignInPlan reads a Free plan from a sandbox auth.json.
Not tried: a real Free ChatGPT account; whether OpenAI bills
codex-auto-review on Plus/Pro is still #1593's measurement.
2026-10-26 17:19:00 +08:00

106 lines
4.1 KiB
Go

package gateway
import (
"net/http"
"strings"
"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)
}
// 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, counted in none of its usage windows: Codex's reviewer model,
// codex-auto-review, since OpenAI made Auto-review free for ChatGPT
// sign-ins on 2026-10-06 (#1593, hywang1230), but not on a Free plan,
// where cybercaius on Discord measured it spending the allowance. A review
// run on another model, or through an API key, is counted as any call.
func freeOnChatGPT(model, plan string) bool {
return strings.EqualFold(strings.TrimSpace(model), "codex-auto-review") && !strings.EqualFold(strings.TrimSpace(plan), "free")
}
// 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.
func (s *Server) codexMemoryStandIn(r *http.Request, who, agent, kind, asked string) string {
if agent != "codex" || !isMemoryKind(kind) && !isReviewCall(kind, asked) {
return ""
}
if _, _, ok := provider.Resolve(asked); ok {
return ""
}
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
}