From abdc4b113dc8bf2e1e9951d5b1ee0a4bfee784e9 Mon Sep 17 00:00:00 2001 From: appointat <65004114+Appointat@users.noreply.github.com> Date: Sun, 11 Oct 2026 20:14:40 -0700 Subject: [PATCH] netproxy, provider, gateway: every request to the ChatGPT backend goes over HTTP/1.1, magpie's own too, not only the gateway's turns (#1700) f6e1e98e put the gateway's turns to chatgpt.com on HTTP/1.1 through a wrapper on the gateway's client. magpie's own requests there go through http.DefaultClient and still spoke HTTP/2: the models list (codexModels), the warm-up's POST to /backend-api/codex/responses (warmCodex), the allowance (codexWindows via accountJSON), the reset credits (codexResets) and spending one (consumeCodexReset). #1700's reporter, on 0.1.1164, had Codex's /responses fail with PROTOCOL_ERROR through a local proxy until GODEBUG=http2client=0, set by editing the app bundle. The rule now lives where every magpie Go client goes through. netproxy.Dispatch sends a request netproxy.HTTP1 names through a clone of its transport kept for HTTP/1.1, beside a provider's own proxy if any, and the provider package sets HTTP1 to codexBackend (CodexBase's host). The clone's TLS hello offers no ALPN, as codex-cli's: a transport that has spoken HTTP/2 carries h2 in its TLS config, which a clone copies, and a server that took it would be sent HTTP/1.1 on an HTTP/2 connection. The gateway's codexHTTP1 wrapper is folded into this; its transport file is now upstream_transport.go. Every other vendor is still offered HTTP/2, and GODEBUG=http2client=0 still turns HTTP/2 off for all. What was run (Go 1.26.8, darwin/arm64, a sandbox HOME): - TestCodexBackendOwnRequestsOverHTTP1 (provider): http.DefaultClient as netproxy.Install leaves it, a TLS server offering HTTP/2, the five requests directly, through Settings' proxy and through an account's own CONNECT proxy. On origin/main 6c1c0a74 it fails: all five "HTTP/2.0 (h2 offered)" each way. Another vendor's accountJSON read stays HTTP/2.0. - TestDispatchHTTP1 (netproxy): after the transport has spoken HTTP/2 to the server, a named request arrives HTTP/1.1 with no h2 offered, with a proxy choice too. Without the NextProtos reset it fails with "malformed HTTP response"; with HTTP1 ignored, with "HTTP/2.0". - With the provider's hook off, the gateway's TestCodexBackendOverHTTP1 fails too ("asked over HTTP/2.0, not HTTP/1.1"). TestAccountProxy, which stubs http.DefaultClient with a Dispatch of its own, passes; a separate Codex client in provider, tried first, failed it. - go test -race -count=20: ./internal/netproxy TestDispatchHTTP1 and TestWith, ./internal/provider TestCodex* (this one included), ./internal/gateway TestCodexBackendOverHTTP1 and TestAccountProxy. gofmt -l; go vet (darwin, GOOS=linux -tags nogui, GOOS=windows); go build (the same three); go test -tags nogui ./... (49 packages) pass. - Real use, behind Clash on a Mac: Codex's own sign-in copied into a sandbox HOME (days left on the token, nothing refreshed), and a CONNECT proxy in front of Clash logging each TLS hello's ALPN. origin/main: `magpie provider models codex` and one request at the gateway's start offered h2 to chatgpt.com, the turn none. This branch: no hello to chatgpt.com offered any, models.dev still h2. #1700's minimal request on codex/gpt-6-astra answered 200 with MAGPIE_CODEX_OK on both. Not reproduced: PROTOCOL_ERROR itself; chatgpt.com answered HTTP/2 on this Mac, so whether HTTP/1.1 is what the reporter's path needs is for their use to say. The warm-up and spending a reset weren't sent for real. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XSjhwVsPdGd6QUTrq4KvE4 --- docs/subsystems/providers-accounts.md | 2 + internal/gateway/codex_http1.go | 53 ------ internal/gateway/gateway.go | 2 +- internal/gateway/upstream_transport.go | 28 +++ internal/netproxy/netproxy.go | 64 +++++-- internal/netproxy/netproxy_test.go | 58 ++++++- internal/provider/codex_http1.go | 23 +++ internal/provider/codex_http1_test.go | 232 +++++++++++++++++++++++++ 8 files changed, 390 insertions(+), 72 deletions(-) delete mode 100644 internal/gateway/codex_http1.go create mode 100644 internal/gateway/upstream_transport.go create mode 100644 internal/provider/codex_http1.go create mode 100644 internal/provider/codex_http1_test.go diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index e89661285..9d894e659 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -119,6 +119,7 @@ A signed-in account's model list exists only at its vendor (Cursor's `cursor-age - Claude Code's `/usage` prints a reset with no year when it falls this year ("Oct 9, 2:59pm (UTC)"), or, in older versions, as a time alone ("3pm"). `claudeResetTime` reads it as the end of its window, so never further ahead than the window's length (5 hours for the session, 7 days for a week) and `claudeResetSlack`: a date is in the first of last year, this year and next that is less than a day past and no further ahead, a time alone the next one no further ahead. Where none is, as for a reading days old, it is the latest one already past, so the window reads as renewed rather than full for a year. Where the clocks go back and show a time twice, the reading not yet past is taken. A week's time alone read just after its reset still reads as tomorrow's. - A Copilot account is known by its GitHub login and host (`CopilotAccountName`, #1220): `mona` on github.com, `mona@acme.ghe.com` on an enterprise's GHE.com. That name is its `logins.json` user, its row in every list, its usage card and its per-account routing settings, and it is what `SwitchLogin`, `SetLoginOn` and `ForgetLogin` take, so one login on two hosts is two accounts, neither written over by the other nor dropped as the editors' own. One saved before on an enterprise's host under its login alone is read under its name with the host (`copilotSavedName`), keeping its id; the editors' own remembered so is renamed in place, still hidden if it was (`copilotRenamed`). Either rename takes the account's per-account settings in `providers.json` (proxy, models, cap, concurrency) from the bare login to the new name before the new name is written to `logins.json` (`renameSettled`): a setting the new name has already stays, and while `providers.json` can't be read or written the account stays saved under its old name, its settings with it. `magpie accounts switch|forget copilot` takes the full name, the login with `--host .ghe.com`, or the login alone when it names one account. - A Codex account is known by its email, with the plan for a workspace seat (`codexUser`), and a second seat of one email in another workspace by that name with its workspace after it (`codexName`): `me@example.com · Team · ws-two`. The account Codex is signed in to goes by the name it is saved under (`codexLiveName`) everywhere: its provider's `Account.User` (`codexAccount`), `liveLogin`, and its session identity (`SessionIdentities`). Its allowance, rests and sticks (`restKey`), cap, proxy, model list, a gateway key's account list (`LoginID`) and the Routing page all go by that name, so two seats of one email are never read as one (#1424). +- Every request to the ChatGPT backend (`CodexBase`'s host) goes over HTTP/1.1, as Codex itself speaks to it: each transport [`netproxy.Dispatch`](../../internal/netproxy/netproxy.go) makes (`http.DefaultClient`'s, the gateway's) sends a request `netproxy.HTTP1` names through an HTTP/1.1 copy of itself whose TLS hello offers no h2, and the provider package sets `HTTP1` to `codexBackend` ([codex_http1.go](../../internal/provider/codex_http1.go)). That covers the gateway's turns, the models list, the warm-up, the allowance, its resets and spending one, through the proxy in force, an account's own included. Every other vendor is still offered HTTP/2. - The Copilot account the editors and CLI keep is read by `copilotLogin` ([account.go](../../internal/provider/account.go)): an editor's github.com sign-in in `~/.config/github-copilot/apps.json` or `hosts.json` first, then the Copilot CLI's, then an editor's on GHE.com. When GitHub refuses the editor token's trade for a session token (401 or 403, `copilotRefusals`, retried after an hour), the CLI's sign-in of the same login on the same host takes its place, sent as the CLI sends it, from that request on and on the Usage card (`copilotStandIn`); a CLI signed in to another account is never used for it, and the error names the editors' file (#1238). A sign-in from magpie of the account whose editor token is refused is kept and stands for it (`copilotProbeEditor`, `addCopilotLogin`); while the editor token works, it is let go as the agent's own, as before. - An unreadable `providers.json` is an error, never an empty list. Saving over an empty list would lose every provider. - When `logins.json` cannot be decoded as saved accounts, reads keep the accounts read before. Before saving a change, `lastgood.Keep` ([lastgood.go](../../internal/lastgood/lastgood.go)) keeps the file there now: as `logins.json.bak`, its last good generation, when it reads, or copied aside as `logins.json.bad-