Commit Graph
5 Commits
Author SHA1 Message Date
yetone a4916aec10 gateway: a Codex websocket turn sent to a Responses relay leaves Lite's client_metadata marker behind (#4853)
A Codex title request through a Responses relay (CPA) failed with
"X-OpenAI-Internal-Codex-Responses-Lite requires reasoning.context to be
all_turns". fromLite took Lite's shape off for a backend that isn't
OpenAI's: it lifted additional_tools, left reasoning.context out and
didn't send the Lite header on. But over a websocket Codex says it is
Lite a second way, in client_metadata
(ws_request_header_x_openai_internal_codex_responses_lite: "true",
codex-rs client.rs build_ws_client_metadata). magpie left that marker
in, so the relay still read the request as Lite and passed it to the
ChatGPT backend without the context it had been told to expect.

fromLite now also drops that one client_metadata key
(withoutLiteMetadata). Every other client_metadata entry, the
instructions and the identity line go as Codex sent them.

Reported by ChangQiu on Discord and filed as #4853 by ZyphrZero. The
withoutLiteMetadata patch and its two tests are ported from
ZyphrZero's fork commit 7f67c732. TestCodexSocketLiteTurnLeavesLiteForARelay
is new: it sends #4853's title request through passthrough to a fake
relay and checks that the marker, the context and the header are all gone.

Run on mote at this head:
- the three new tests plus TestLiteSubagentLeavesLite and the
  additional_tools tests: ok
- go test -timeout 45m ./internal/gateway: one red,
  TestCodexPoolSaysTheClientsVersion (#4807). It passes 3/3 alone here
  and on origin/main.
- go vet ./internal/gateway with GOOS=linux, windows and darwin: clean.
  gofmt -l: clean.
Broken by hand: with withoutLiteMetadata stubbed to return its input,
all three new tests went red.
Not tried: a real CPA, or a real Codex websocket session through magpie.
2026-11-02 14:06:01 +08:00
ggbdpq 1ff928139d gateway: a remote magpie's codex model gets Codex's Responses Lite as it was sent (#4445)
What changed: a Codex Desktop turn on one of its own Lite models (gpt-6.1-sol)
routed through Model Map to a Remote Magpie provider answered 400 "Remote
magpie: Codex: X-OpenAI-Internal-Codex-Responses-Lite requires
`reasoning.context` to be `all_turns`". liteKept treats a Remote Magpie like
any plain Responses upstream, so passthrough ran fromLite (the additional_tools
item lifted into tools, reasoning.context taken off) and forwardOnce dropped
the Lite header. The remote serves codex/<model> through its ChatGPT account,
which passes on what it got, and the ChatGPT backend refuses a request for one
of its Lite models without reasoning context "all_turns" (#534): the request
arrived stripped of exactly what that model demands. Had it gone as Codex sent
it, the remote's account path keeps the Lite shape (liteKept true there) and
its answer is 200, as the reporter's codex=as-sent workaround showed.

liteKept now also keeps the shape, with the model it is asked for: a Remote
Magpie's codex model, named codex/<model> — the ChatGPT account's namespace in
every magpie's catalog, so the remote lists one only when that account is there
to serve it. Its other models still get what Codex sends a model that isn't
Lite (#350), header and reasoning context included in the lift (#350, TangForX).

Run: go test -tags nogui -run 'TestRemoteMagpieCodexModelKeepsLite|
TestRemoteMagpieOtherModelLeavesLite' ./internal/gateway/ — the first failed
with the 400 above before the change and passes after; the second keeps the
old behavior. go test -tags nogui ./internal/gateway/ passes.

Fixes #4445

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 22:44:23 +08:00
yetone 254461829b gateway: a Codex Lite request is no longer decoded whole, nor parsed twice for its facts
XiaoYin_OK on X: after a long run the app's memory climbs toward 2 GB.
The owner's own app (26h up) showed RSS 550 MB, HeapInuse 317 MB but
HeapSys 992 MB: the heap peaks while requests are in flight. Its profile
had 209 GB allocated, ~94 GB of it under codexBackend/serve, of which:
- parseResponses 24.7 GB, liftAdditionalTools 14.9 GB of it;
- factsOf 24.6 GB, reached from turnOf (13.6 GB) and dirOf (11.0 GB),
  the same conversation parsed twice per request.

Three causes, each fixed where it is:
1. liftAdditionalTools decoded the whole Lite request into maps to move
   its additional_tools item, and wrote it all again: ~7.6x the body.
   Now only that item and the tools are decoded; every other item and
   member goes byte for byte as it came, written once into one buffer.
2. affine counted the turn in the masked body while dirOf read the plain
   one, two different slices, so factsOf's memo could never serve both.
   affine now counts it in the body as the agent wrote it.
3. That body is rarely the one readRequestBody held (requestModel,
   spawnAgentModels and the lift each write a new one), so factsOf's
   memo missed for it anyway. serve now holds it (holdBody), and holdBody
   lists a body once when it is held twice.

Tests (internal/gateway/lite_lift_test.go), each red on the old code:
- TestLiteLiftMovesOnlyTheTools: same result as the old whole decode
  (liftWhole, kept in the test) on testdata/context/codex.json, the
  merged-namespace case, a 2,000-word conversation and the no-op shapes;
  an item with its keys unsorted, \u escapes and 2.50 goes as written.
  Old: the kept item came back re-sorted and re-escaped.
- TestLiteLiftAllocatesAboutTheBody: under 2x a 1.35 MB body.
  Old: 14,126,976 B (10.4x).
- TestMaskedRequestParsedOnceForItsFacts: a served masked Lite request
  is parsed once for its turn and folder (factsParsed hook in readFacts).
  Old: 2. With only change 2 it was still 2 (cause 3).

Measured (darwin/arm64, a 4,012,240 B Lite body):
- liftAdditionalTools: 30.46 MB/op, 98,560 allocs -> 5.17 MB/op, 6,609.
- readFacts of it: 54.0 MB/op -> 26.7 MB/op.
- Served through New().Handler() with masking on, 8 concurrent x 5
  rounds of a 4,010,268 B body, two runs each:
  allocated per request 215.6 / 220.7 MB -> 116.2 / 116.2 MB;
  peak HeapInuse 993 / 802 MB -> 612 / 561 MB;
  HeapSys 1001 / 892 MB -> 699 / 674 MB.
- The live app's WebKit processes were ~8-9 MB RSS each, so the webview
  is not where the memory went on macOS; the live pid sat at 476-596 MB
  RSS over a 15-minute sample.

Run at this head: gofmt -l clean; go vet ./...; GOOS=linux go vet -tags
nogui ./...; GOOS=windows go vet ./...; go build (gui and nogui);
go test -tags nogui -timeout 45m ./... in a sandbox HOME at d2e0ea393
plus this change: every package ok (internal/gateway 619s) except two
guard reds already on main and untouched here: internal/proc's
TestNoCommandBypassesProc (#3567) and internal/agent's
TestFolderVarsAreListed (TZ in grouprule.go, filed as #3600). After the
rebase onto 642d430ec (one gateway test file added upstream),
internal/gateway was run again: ok (425s), with gofmt and the three vets of it clean.
Not tried: a 26h run of the new build against real Codex traffic, and
Windows (#3383's 4.2 GB report) beyond go vet.
2026-10-25 20:02:12 +08:00
yetone 9949ad5eb0 gateway: a subagent a Responses Lite lead spawns elsewhere goes without Lite's reasoning context and header (TangForX on X)
TangForX: Codex with gpt-6.1-sol leading spawned a subagent on OpenCode
Go's DeepSeek v4.1 flash, and every turn of it answered 503 "Endpoint is
unavailable"; the same subagent under a DeepSeek lead, and on DeepSeek's
own API, worked.

Cause: gpt-6.1-sol is use_responses_lite, and Codex sends a subagent it
spawns in the lead's Lite shape with only the model name swapped:
reasoning.context "all_turns", tools in an additional_tools input item,
and the X-OpenAI-Internal-Codex-Responses-Lite header. passthrough lifted
additional_tools (#350) but sent the reasoning context on, and
forwardOnce copied the Lite header (an x-openai-* header Codex sends) to
every key+URL provider. OpenCode Go's DeepSeek route refuses that shape.

Fix: fromLite takes the whole Lite shape off (additional_tools lifted,
reasoning.context dropped, effort and summary kept) for every Responses
upstream that isn't OpenAI, a Codex account, or a codexAsSent relay
(liteKept); forwardOnce drops the Lite header on the same rule, for a
translated Chat upstream too. A body with no Lite in it goes byte for
byte as before. codexAsSent relays still get the request and header as
Codex sent them (TestCodexAsSent).

Run at the pushed head:
- TestLiteSubagentLeavesLite: Codex's code-mode request with Lite's
  fields and the collab_spawn subagent headers, to opencode-go
  deepseek-v4.1-flash, deepseek and a custom Responses relay; the fake
  upstream answers 503 "Endpoint is unavailable" to any Lite remnant.
  TestFromLite: a non-Lite body is unchanged byte for byte.
- gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...: clean.
- go test -tags nogui -count=1 ./internal/gateway (Lite, Codex,
  CodexAsSent, DeepSeek, OpenCode tests): ok.
- go test -tags nogui -timeout 45m ./... at ecfab308e (on 40a0b442c),
  one suite on the machine, log kept whole: every package ok, exit 0.
- Rebased on 3133f6343 (gateway, catalog, provider, agent changed): gofmt,
  vet (darwin, linux nogui, windows) of internal/gateway, and go test
  -count=1 of internal/gateway, catalog, provider and agent at the pushed
  head: ok.

Broken by hand: passthrough back to liftAdditionalTools alone, and the
header skip turned off: TestLiteSubagentLeavesLite red (503) on all
three upstreams each time.

Not tried: a real OpenCode Go request (the owner's Go key answers 401
api_key_expired; Zen free is rate limited), so which of the context and
the header Go refuses is not known; both go. Lite's other parts (empty
instructions with the prompt in a developer message, namespace and
custom-grammar tools) go as they did before.
2026-10-25 03:11:28 +08:00
yetone 4ead7c84a3 gateway: Codex's Responses Lite tools reach SuperGrok and every other backend, and a group moves on when a member can't read a request (#350) — Codex sends a Lite model's tools as an additional_tools input item, its own functions in a "functions" namespace; magpie relayed the item to xAI, which 422s on it (unknown item type), and translated routes (Cursor, Anthropic, Chat…) dropped it, so the model had no tools. Outside OpenAI the item's tools now go in tools, the functions namespace opened into plain tools and earlier calls un-namespaced; OpenAI and the ChatGPT account still get Codex's own form. A 400/422 over the request's shape (failed to deserialize, unknown item type/parameter…) now moves to the group's next member, each asked once, the member not resting; the Routing page says so. TestAdditionalToolsRelayed, TestAdditionalToolsTranslated and TestGroupFailsOverARequestShape fail without the change; routing-shape.test.cjs (Chromium, WebKit, en/zh) added. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; click-scroll, old-webkit, routing-wb-refused pass. 2026-10-01 01:05:49 +08:00