Commit Graph
4 Commits
Author SHA1 Message Date
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