magpie icon indicating copy to clipboard operation
magpie copied to clipboard

gateway: a client-written service_tier "flex" is relayed to the ChatGPT backend and 400s every turn of the thread

Open yu06816 opened this issue 1 week ago • 3 comments

What happens

A client that writes service_tier: "flex" into a Codex thread breaks the thread for good: the native /backend-api/codex route relays the tier untouched, the ChatGPT backend answers 400 invalid_request_error: Unsupported service_tier: flex, and every later turn fails the same way, because the client re-applies the stored thread_settings.service_tier on each turn.

Today codexTiered only rewrites the body when a magpie Codex tier is set (provider.CodexServiceTier()), and fastTier only rewrites the word "fast"; on the backend route codexTiered is the only tier step (internal/gateway/codex_backend.go:124), so a client tier that the ChatGPT backend refuses goes up as it came. The same tier on /v1/responses is dropped (TestFastTierWordReachesCodex, the Responses, flex case), so the two ways into the same backend disagree.

This was first filed against the plugins repo (magpie-community/plugins#1558), where L0NE-6 pointed here and named the files below.

Steps to reproduce

Codex CLI 0.172.0 signed in to ChatGPT, magpie as its provider:

# ~/.codex/config.toml
model = "gpt-6.2-sol"
openai_base_url = "http://127.0.0.1:3425/backend-api/codex"
codex exec --skip-git-repo-check -c service_tier=flex "reply with exactly: OK"
# ERROR: {"error":{"type":"invalid_request_error","message":"Unsupported service_tier: flex"},
#         "headers":{"x-codex-websocket-max-message-bytes":"134217728"},"status":400,"type":"error"}   exit=1

codex exec --skip-git-repo-check -c service_tier=standard "reply with exactly: OK"
# OK   exit=0

codex exec --skip-git-repo-check -c service_tier=ultrafast "reply with exactly: OK"
# OK   exit=0

magpie's log for the failing call: gateway: codex's call to gpt-6.2-sol on openai failed: 400 after 1.373s: Unsupported service_tier: flex.

Why flex reaches the thread at all

The client offers it: Paseo's inspect_provider for codex/gpt-6.2-sol lists the Speed control as default | flex | priority, so a user can pick Flex for a model that is served over the ChatGPT-auth lane, where flex (an OpenAI-API billing tier) cannot work. The pick is persisted per agent and survives restarts; models_cache.json for that model offers only priority, so Codex's own tier picker has no flex entry to switch away from, and config.toml's service_tier = "default" is overridden by the stored thread setting. The user's only way out is editing the client's agent config by hand.

Proposed fix

Normalize a client tier on the native ChatGPT-auth route only, where the accepted set is the one Codex itself sends — nothing, priority (Fast), ultrafast — and take anything else out, so one bad tier costs one turn's speed instead of the thread:

// in internal/gateway/codex_tier.go

// codexTierNative are the tiers the ChatGPT backend takes from a turn's
// own body, as Codex sends them: none (standard), "priority" (Fast), and
// "ultrafast" on a plan that lists it. A client's own word for a tier
// that lane has no price for — "flex", an OpenAI-API billing tier — is
// taken out, as Codex's own routes already do (/v1/responses), so the
// backend doesn't answer 400 "Unsupported service_tier: flex" to every
// turn the client's stored setting re-applies.
var codexTierNative = map[string]bool{"": true, "priority": true, "ultrafast": true}

// codexTierTakenOut is body without a top-level service_tier the native
// route's backend refuses, every other byte as it came.
func codexTierTakenOut(body []byte) []byte {
	t := topValue(body, tierField)
	if t.Type != gjson.String || codexTierNative[t.Str] {
		return body
	}
	return withServiceTier(body, "")
}

called from codexUpstreamOn (the choke point every native relay and compaction goes through), or at codex_backend.go:354 for the plain relay plus the compaction branches; whichever the maintainer prefers — the fastTier/codexTiered layering suggests one place, not four.

A test beside TestFastTierWordReachesCodex would keep the two routes honest: a POST /backend-api/codex/responses with "service_tier":"flex" reaches the backend with no service_tier, and "priority"/"ultrafast" still arrive. I can send the patch as a PR if that's the preferred shape — I can't verify the live behaviour against a real ChatGPT account from here, only the body the backend is handed (the 400 above is from this same setup, so the tier is the only difference).

Environment

  • magpie 0.1.4823 (app, Windows, gateway on 127.0.0.1:3425)
  • codex-cli 0.172.0, codex login status → "Logged in using ChatGPT", model gpt-6.2-sol, reasoning low
  • ~/.codex/config.toml: service_tier = "default", openai_base_url = "http://127.0.0.1:3425/backend-api/codex"
  • The affected thread stores service_tier: "flex"; setting it back to default on the client side makes the same thread complete (task_complete, error: null)
  • Related: #3978 (a relay's refusal wording isn't recognised, so the 400 reaches Codex instead of the tier being dropped), #2514 (plugins saying which tiers a model serves, and Codex offering them)

yu06816 avatar Nov 26 '26 04:11 yu06816

Fix branch: https://github.com/qq01-hub/magpie/tree/fix/codex-backend-service-tier (03defe188) Compare: https://github.com/yetone/magpie/compare/main...qq01-hub:magpie:fix/codex-backend-service-tier

Root cause: On the native /backend-api/codex route, requests relayed through codexUpstreamOn to provider.CodexBase bypassed provider.codexBody and forwarded client-written service_tier values such as "flex" unchanged, causing the ChatGPT backend to return 400 Unsupported service_tier: flex on every turn. Separately, withServiceTier(body, "") compared cur against "" and treated an explicit "service_tier":"" as a no-op instead of stripping it.

Changes:

  • Add codexTierTakenOut in internal/gateway/codex_tier.go to strip any top-level service_tier other than "priority" and "ultrafast" (matching provider.codexBody), leaving absent/nested/non-JSON bodies byte-for-byte unchanged.
  • Fix withServiceTier in internal/gateway/codex_tier.go so tier == "" removes "service_tier":"" as documented.
  • Call fastTier(body) before codexTiered/keyTiered in codexBackend (internal/gateway/codex_backend.go, matching /v1/responses) so "fast" normalizes to "priority", and call codexTierTakenOut(body) inside codexUpstreamOn when base == provider.CodexBase (covering /responses, WebSocket turns, and /responses/compact, while preserving "flex" on API-key codexAPIBase relays and running before route-trace bodyTier recording).
  • Add unit tests (TestCodexTierTakenOut, TestWithServiceTier empty-tier case) and end-to-end tests (TestCodexBackendDropsUnsupportedServiceTier) in internal/gateway/codex_tier_test.go and internal/gateway/codex_fast_word_test.go.

Verification (Windows 11 amd64, Go 1.26.8):

  • Before fix: TestCodexBackendDropsUnsupportedServiceTier failed with 400 Unsupported service_tier: flex, and both TestCodexTierTakenOut and TestWithServiceTier failed to strip "service_tier":"".
  • After fix: go test -count=5 ./internal/gateway -run 'TestWithServiceTier|TestCodexTierTakenOut|TestCodexBackendDropsUnsupportedServiceTier|TestFastTier|TestCodexServiceTierSet' (30/30) and go test -count=1 ./internal/gateway -run 'Tier|CodexBackend|CodexRelay|CodexCompactRelay' (92/92) passed; go vet ./internal/gateway passed on Windows, Linux (-tags nogui), and macOS (-tags nogui).

qq01-hub avatar Nov 26 '26 04:11 qq01-hub

Why this surfaced only in the last few days, on the client side — the value was not typeable before, and the client used to correct it:

  • Paseo built its Speed menu from the model catalog's serviceTiers, which never lists flex (Codex leaves it out of model/list), so Flex could not be picked; and a stored service_tier: "flex" was rewritten to default before the first turn. That rewrite is what kept a client flex from ever reaching an upstream that refuses it.
  • getpaseo/paseo#9809 (2026-11-19, shipped in v0.17.4 on 2026-11-23) declares flex itself in codexSpeedTiers() so the menu can offer it, and the pre-turn rewrite went with it. The reasoning there quotes Codex's own "Flex is an API request option, even when the Codex catalog does not advertise it".

So the fix proposed above is still the one that bounds the damage, with one addition worth considering: the refusal wording carries the tier (Unsupported service_tier: flex) — recognising it and dropping the field would let a client that made this mistake degrade one turn instead of losing the thread, and magpie already has code for dropping fields a provider refuses (optionalFields / markUnfit, #3978).

Reported on the client side as getpaseo/paseo#10895.

yu06816 avatar Nov 26 '26 04:11 yu06816

Fixed in v0.1.4922 (8ceeda89d), ported from Q's patch with credit. The native /backend-api/codex route now leaves out a client-set service_tier the ChatGPT backend does not take (flex among them), so a thread that had one written into it goes on instead of answering 400 Unsupported service_tier: flex every turn. priority and magpie's own Fast setting are unchanged. Thanks @yu06816 for the file map and for tracing why it surfaced only now.

yetone avatar Nov 27 '26 07:11 yetone