Commit Graph
7 Commits
Author SHA1 Message Date
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
yetone b7d5a97fec gateway: Codex's memories after a model switch wait for the new turn, as titles do
cybercaius (Discord) asked whether Codex's side calls (title, thread
description, composer prediction, memory consolidation, auto-review)
still go to the model the conversation was switched away from, and
whether the first of each after a switch follows it as the title does
since 81a73c1c0.

Titles, descriptions and predictions already followed: untied ones wait
in codexLeads.sideOf for the next turn when the last is stale, and tied
ones (a forked prediction) are asked on the thread's model. The
auto-review runs inside its turn, on that turn's model ("session") or on
codex-auto-review/the set model, so it follows at once.

Memories did not. Codex starts them with every turn that has the user's
input (codex-rs app-server start_memories_startup_task), concurrently
with that turn. Extraction is tied to the conversation's thread, so
codexSessionOf took that thread's last model at once: the one before the
switch. Consolidation, on its own thread, took the key's last model
without waiting. With magpie as Codex's provider, codexMemoryStandIn read
the last turn per agent and key, which the racing turn hadn't written.

Fix, at the two places a memory call is routed:
- codexLeads.sideOf takes beside: a call started with a turn waits for
  the next turn even when tied (codexMemorySession); codexFollowTo uses
  it for memories, and reads CodexSideSet first so a model set in
  Codex's config never waits.
- rememberCodexTurn also records the turn in codexLeads, and
  codexMemoryStandIn reads codexLeads.sideOf(beside) before its last-turn
  record.
docs/subsystems/gateway-routing.md says so.

Tests (fake upstreams, a session switched from one relay to another):
TestCodexSideCallsBeforeTheFirstTurnFollowTheSwitch (title, description,
prediction, memory extraction, memory consolidation, each sent before
the first turn after the switch), TestCodexMemoryOfASwitchedConversation
FollowsTheSwitch (an ongoing conversation switched), and
TestCodexMemoryStandInFollowsTheSwitch (magpie as Codex's provider).

Run: go vet ./... (darwin), GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., gofmt -l clean, go test -timeout 45m
./internal/gateway/... ok (424.7s), at the pushed head
rebased on 94a70f1a0.

Broken by hand: with the old codex_follow.go, codex_memory.go and
gateway.go, both memory subtests, the switched-conversation test and the
stand-in test fail (the old relay got 2 requests); title, description
and prediction pass on the old code, as they already followed. With
beside forced false for memories, the switched-conversation test fails.
With saw() dropped from rememberCodexTurn, the stand-in test fails.

Not tried: a real Codex app or account. The app's ambient_suggestions
calls are not routed by magpie (they go to ChatGPT) and are left alone.
A tied call that isn't started with a turn (a prediction on a known
thread) asked after a switch but before the new turn still takes the
thread's last model, by design: waiting there would add latency to every
prediction.

Reported by cybercaius on Discord.
2026-10-27 08:37:47 +08:00
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
yetone d4c2512d2d usage: apps report their own calls with POST /v1/magpie/usage/requests, backfill included (#1874)
What changed
- POST /v1/magpie/usage/requests (internal/gateway/usage_ingest.go) takes
  one record or an array, in usage.jsonl's shape:
  - the post may carry up to 5000 records and 32 MiB;
  - t, agent, provider, model and rid are required.
  Each field is checked: unknown fields, quoted numbers, negative or absurd
  counts, a status outside 100-599, a cost over 10000 USD, and a t more
  than an hour ahead or before 2000-01-01T00:00:00Z. Any fault refuses the
  whole post with a 400 that names error.index and error.param, and nothing
  is written. A past t is otherwise taken as given, so history can be
  backfilled. The reply gives accepted and skipped counts and each
  record's index, rid and status.
- Access: lanGuard's rule (this machine, or another with a key while
  shared, behind the Host check). A browser page also needs a key, since
  corsGuard lets localhost pages and Origin null through. A held key is
  refused.
- usage.IngestReported writes under usage.mu to the same file as
  usage.Append. Rows carry source "reported" and no caller key. A rid
  already reported is skipped, as is one repeated in the post. The set of
  reported rids is read back from the log: one scan after a restart (lines
  without the reported mark aren't parsed), then only what was appended.
- Pricing: a record with cost keeps it (Record.OwnCost, the rowOwnCost
  flag; packed flags widened to uint16). Otherwise it is priced at
  magpie's prices.
- Display:
  - SourceReported is a source of its own: RequestPage.Reported,
    ?via=reported, and Ledger now honours via.
  - The Requests tab gains a fourth cell "reported by apps", shown once
    there are some, and a "Reported" badge (en, zh, zh-TW, ja, de).
- Nothing live: Record.IsReported keeps reported rows out of:
  - a key's budget/spend limit (budget.counts);
  - window holds, and the log start that decides whether a window can be
    reckoned (logSnapshot.firstOwn);
  - gateway sessions and Vias;
  - Codex's sealed answers and remembered model.
  There is no OTel span, rate limit, rest or alert for them.
- Docs: gateway-sessions.md, reference.md (with a curl), integrating.md,
  site integrate.html (en, zh).

Why: the reporter's apps call vendors directly, and their usage (and
history) should show on the Usage page without becoming live traffic.

Run, in a worktree with sandbox HOME and a private GOCACHE
- gofmt -l: clean.
- go vet ./...: darwin, GOOS=linux -tags nogui, and GOOS=windows.
- go test internal/usage, usageapi, budget, gateway, gui: ok.
- -race -count=3 of the new usage/gateway tests: ok.
- GUI with playwright 1.62.0:
  - usage-ledger-reported.test.cjs, 26/26 in chromium and webkit (5
    languages, 1180 and 420 wide, light and dark);
  - usage-ledger*.test.cjs, ledger-*.test.cjs, usage-sum-whole,
    usage-unpriced-sum and i18n-keys: all pass.
- Timings with 200k reported rows (47 MB log, darwin):
  - 40 posts of 5000 took 0.40 s (worst batch 18 ms);
  - the first post after a restart took 0.45 s (rid rescan), a later one
    1.6 ms;
  - the cold index build took 2.1 s (as for gateway rows);
  - page all took 27 ms warm, page 30d 39 ms, all via=reported 0.53 s;
  - summary all took 215 ms, 30d 20 ms.

Broken by hand, each went red, then restored
- Go: dedupe against stored rids; the restart scan; CostAt ignoring own
  cost; the block pricer overwriting own cost; the Reported split;
  fromBrowser's key rule; skipping bad records instead of refusing; the
  max-records and body limits; the ingest lock (race); both locks;
  firstOwn -> first; the IsReported guards in windowHolds,
  buildGatewaySessions, buildVias, budget.counts and loggedAnswers.
- GUI: the fourth cell, the --vias column count and the badge class.

Not shown red on its own: the remote-no-key and evil.example refusals come
from lanGuard and corsGuard before the handler, so breaking only the
handler's own check stays green. The codex_memory guard is not tested
alone: a reported row has no Requested there anyway. Not tried: a real
client against a live gateway, Windows/Linux runs of the tests, the full
GUI suite.
2026-10-13 06:32:03 +08:00
yetone 0e89b090c1 gateway: Codex's auto-review on codex-auto-review goes through a ChatGPT account past its usage cap, since OpenAI made it free (#1593, hywang1230)
An account past the usage cap set on it in magpie was held for every
model, so Codex's auto-review on codex-auto-review was refused with
"usage cap reached" once the account reached its cap. OpenAI made
Auto-review free for every ChatGPT sign-in on 2026-10-06: the reviewer's
calls are counted in no usage window, so the cap, which keeps a share of
a window unspent, has nothing to keep for them.

capHeld now leaves the user's caps out for a Codex (ChatGPT) account's
call on codex-auto-review (freeOnChatGPT). Credits set not to be spent
still hold it at 100% of a window, as nothing says the backend answers it
there. A review run on another model, or any other model, is held as
before.

Verified:
- TestAccountCapLetsAutoReviewThrough: an account at 92% with a 90% cap
  takes codex-auto-review on Codex's backend path and as
  codex/codex-auto-review, and still refuses gpt-5.5 on both. With the
  exemption disabled it fails with the reporter's exact 429 ("usage cap
  reached: every account that serves "codex/codex-auto-review" ...").
- go test -run 'TestAccountCap|TestCodexNoCredits|TestWindowCaps'
  ./internal/gateway, gofmt -l, GOOS=linux/windows go vet -tags nogui.
- Not tried against the real ChatGPT backend with a capped account.
2026-10-11 21:00:28 +08:00
yetone f1fc8c65d3 gateway: Codex's auto-review reaches a model when its config names a group bare or Codex is on magpie through another provider table, instead of 404 "magpie knows no model codex-auto-review" (Adam on Discord)
Codex asks for its approval review on codex-auto-review whenever its model
list has that name or a model's entry names it as auto_review_model_override
(as a ChatGPT account's entries do). magpie stood that in with
config.toml's `model` (agent.StandIn, e31384b1), but only:
- when the top-level model_provider is "magpie": a Codex reaching magpie
  through another table pointed at it (CC Switch's "custom", the thread
  tables magpie writes), a profile, its own CODEX_HOME or another computer
  got no stand-in at all;
- and the stand-in went straight to provider.Resolve, so a config naming a
  routing group bare (its id or name, or Codex's spelling of a group's
  model, #750), which the conversation's own turns reach through GroupFor,
  was a 404 for the review.
Nothing in v0.1.1158..1163 changed this path; these cases were never
covered.

What changed:
- serve: the stand-in is taken through GroupFor and AutoStandIn, as a name
  asked for is (also for Claude Code's tier stand-ins, the same shape).
- codexMemoryStandIn also takes Codex's auto-review (x-openai-subagent
  guardian, or the name codex-auto-review): one that still names no served
  model goes to the model Codex's last turn was answered on (per agent and
  gateway key, else the ledger), as Codex itself reviews on the
  conversation's model. Before any turn it is still a 404; any other
  unknown name still is.
- docs/subsystems/gateway-routing.md says so.
Every Codex route (/v1/responses, /v1/chat/completions, compaction on
/v1/responses) resolves in serve, the one place this runs.

What was run:
- Real Codex CLI 0.162.0 (codex exec, approvals_reviewer=auto_review,
  approval_policy=on-request, a catalog entry with
  auto_review_model_override=codex-auto-review) against a sandbox
  `magpie serve` (temp HOME, port 3591) with a fake chat upstream that asks
  for an escalated exec_command:
  - origin/main: model_provider=magpie + model="my-group" or "Mine" (a
    group), and model_provider="custom" pointed at magpie: Codex printed
    "Automatic approval review failed: unexpected status 404 Not Found:
    magpie knows no model \"codex-auto-review\"", Adam's text; the ledger
    has the guardian calls 404.
  - this build: all three, and model_provider=magpie + fake/m1: guardian
    calls 200 on fake/m1, the command ran.
- TestCodexAutoReviewReachesItsTurnsModel: with gateway.go reverted it
  fails ("my-group: the review 200 ... upstream m1", went to the last turn,
  not the group's m2); with codex_memory.go reverted it fails ("the review
  after a turn: 404 magpie knows no model \"codex-auto-review\"").
- gofmt -l clean; go vet, GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/gateway; go test -tags nogui ./... under a temp HOME with the
  Go caches pinned.
2026-10-10 21:38:51 +08:00
yetone 987ce3d1b0 codex: a memory call on a model of Codex's own goes where Codex's turns go, rather than a 404 (#742, congee949) — Codex writes and consolidates its memories on models of its own by their bare names (memories.extract_model and consolidation_model, gpt-5.6-luna and gpt-5.6-terra by default) whatever it is set to, so with magpie as its provider every memory_consolidation call was turned away as "magpie knows no model" before it was routed (28 in a week in the report, from Codex on two other computers with their own gateway keys, where the stand-in read from this computer's ~/.codex/config.toml can't reach). A Codex memory call (memory_consolidation, memgen, memory) on a model magpie can't resolve now goes to the model the same Codex (the agent and the gateway key it came with) last had a turn of its own answered on here, else the last such turn in the ledger a week back (so after a restart too), and is routed and recorded as the memory call it is. Any other call on an unknown model, and a memory call from a Codex with no turn answered, is turned away as before. Base instructions are untouched. TestCodexMemoryCallsGoWhereItsTurnsGo (terra and luna after a turn, after a restart from the ledger, unknown turn and review still 404) fails on the old code. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. 2026-10-05 00:25:10 +08:00