ac4af582301a4e8a29245dbba767eba31334ddf9
4
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
58cfca2f77 |
gateway: a Codex turn's rewrites look at its items in place and decode it only when one has something to change (#1532, #2354)
A long Codex session grew slower each turn through magpie because every
request went through up to eight rewrites (callItemIDs, plainItemIDs,
boundCallIDs, codexInputAs, forVendor, withoutBareReasoning,
withoutReasoningText, withModel), and each one json.Unmarshal'ed the
whole conversation into maps, even though on most turns none of them had
anything to change. When one did change something, encoding/json
re-scanned every raw item one byte per call to write the body back. In
the live heap, withModel's json.Marshal held 28.8 MB, and checkValid and
stateInString were ~25% of CPU.
- Before decoding anything, each rewrite now asks whether any input item
is one it changes (anyInputItem, over longBody's remembered split). It
reads the item's members where they lie, matching keys as the old
decode did: exact and last-wins for map reads, EqualFold for struct
reads (reasoningOf). An escaped key, or a null in a field, falls back
to decodeJSON. The change path is still the old code.
- forVendor and withModel split the body with rawObject/rawArray. When a
rewrite writes the body back, marshalRaw is used (marshalPlain without
HTML escaping, marshalJSON with it). It copies each raw value with its
strings whole, into one buffer sized up front, and its bytes are
encoding/json's own. Where encoding/json would refuse a value, the call
falls back to encoding/json and gets its error.
- withModel now passes a body of null through unchanged. It used to panic
writing into a nil map.
Measured with a sandbox gateway and a fake upstream. The bodies were
shaped from local Codex rollouts and never leave the machine. The machine
was at load ~160-180, so wall times are noisy; the CPU profiles are the
steadier number.
5 MB body, CPU per mode (17 requests, pprof):
relay 4.36s -> 1.38s (~256 -> ~81 ms a request)
accounts 10.29s -> 3.78s (~605 -> ~222 ms)
magpie 7.00s -> 3.40s (~412 -> ~200 ms)
wall time to upstream, medians before -> after:
relay 100k 8.1 -> 7.2ms, 1m 66.7 -> 47.9ms, 5m 1069 -> 280-661ms
accounts 100k 32.3 -> 17.3ms, 1m 1016 -> 373ms, 5m 4852 -> 2118-2567ms
magpie 100k 13.3 -> 10.1ms, 1m 345 -> 139ms, 5m 2713 -> 960-1805ms
The remaining cost at 5 MB is parse/promptOf/codexBody decoding the
body for their own needs (decodeJSON, longBody), not these rewrites.
SSE: the relay flushes each read and the masking writer passes Flush
on; TTFB stayed within ~1-10ms of to-upstream before and after.
Tests:
- TestCodexRewritesAsBefore compares each rewrite's output byte for byte
against its old code, which is kept in the test. It covers marked items
(bad ids, magpie compactions, triggers, reasoning with text or nothing
sealed, configuration_update, Codex metadata), duplicate and escaped
keys, non-object items, an input that is null or an object, and 300
random mixes.
- FuzzCodexRewritesAsBefore ran 150s: 22,796 execs, PASS.
- TestMarshalRawIsEncodingJSON and FuzzMarshalRaw (90s, 46,537 execs,
PASS) check marshalRaw against encoding/json, with and without HTML
escaping.
- Broken by hand to see the new tests fail:
- With anyInputItem always true, TestCodexRewritesLeaveTurnUndecoded
went red: "callItemIDs: 11086 allocations for a turn it leaves as it
came (243 KB)", and every rewrite, forVendor 9172.
- With marshalPlain's marshalRaw path off, TestMarshalRawAllocatesOnce
went red: "marshalPlain of 420 items: 4 allocations".
- gofmt -l and go vet -tags nogui ./internal/gateway are clean.
- GOOS=linux and GOOS=windows go vet -tags nogui ./... are clean.
- go test -tags nogui ./internal/gateway -count=1 -timeout 45m:
ok github.com/yetone/magpie/internal/gateway 2579.125s
|
||
|
|
9492e5db8a |
gateway: a conversation that searched its tools compacts and goes on after the search tool is gone, on a Responses vendor that doesn't know tool_search_call (#1171, tcxp001; #1755, FivePink)
What changed: searchAsFunction turned Codex's tool_search_call and tool_search_output into a function's call and result only while the request offered the tool_search tool. Codex leaves the tool out once nothing deferred is left to find, and its compaction of a magpie model sends no tools at all, but the conversation keeps the earlier searches. Those went as they came, and a vendor that doesn't know the items refused the whole request: Anyrouter's "invalid codex request" on the compaction, Xiaomi MiMo's "input item type 'tool_search_call' is not supported by this gateway phase" on a later turn. They now go as the function's call and result whether or not the search is offered, which is how the same upstream saw them in the turns that offered it. With no search offered, the tools a search found aren't added to the request's tools and the reply isn't read as a search (searchTidy stays off). The ChatGPT backend (a Codex account) still gets the items as Codex sent them. The call-id tests for OpenAI and Responses relays (Lullaby) expected a history-only search sent as tool_search_call with a tsc_ id; it now goes as a function_call, which has no item id to get wrong. Their ctc_ checks are unchanged, and the ChatGPT-backend test keeps the tsc_ expectation (sentToChatGPT). Run: TestSearchHistoryWithoutTheSearch (compaction through CodexPath and a later /v1 turn, each upstream refusing with the reporter's own words) and TestSearchAsFunctionHistoryOnly fail on the old toolsearch.go, the gateway answering 400 with each reporter's error; they pass with the fix, with TestToolSearch*, *CallIDs, TestBareReasoning*, TestAdditionalTools*, TestCodexAsSent*, TestSearchedConversation* and TestSpawnAgentNamesEveryModelAfterToolSearch. Full go test ./internal/gateway/ below in the push record. gofmt -l clean; go vet -tags nogui on darwin, linux and windows. Not tried: Anyrouter or MiMo themselves. |
||
|
|
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. | ||
|
|
224304ab08 |
codex: third-party and group models get Codex's tool search, so the first request no longer carries every MCP and ChatGPT-app tool's schema (#258: 190K tokens before the first word on a magpie model, ~26K on Codex's own)
Codex sends every MCP tool's full schema inline in `tools` unless the model's catalog entry says supports_search_tool (core/src/tools/spec_plan.rs search_tool_enabled; mcp_tool_exposure.rs Deferred vs Direct); its own gpt-5.5/5.6 entries say so, magpie's generic entries didn't, so with a ChatGPT sign-in's apps a turn began at ~190K tokens. The generic entries now set supports_search_tool: true; base_instructions, the context window and the rest are as they were, and code mode and Responses Lite stay off. The gateway serves the search to models that don't know it: for a model it translates for (Chat, Anthropic…), the client-run tool_search is offered as a function, earlier tool_search_call/tool_search_output become a call and its result ("These tools are now available to call: …"), the tools found are offered after those sent (once, namespaces merged), and a call to it goes back to Codex as the tool_search_call Codex runs, streamed or not. A request relayed to another Responses API (not the ChatGPT backend, which runs it as sent) is rewritten the same way, the found tools' defer_loading dropped, and its stream line by line (toolsearch.go). Measured with codex exec in a sandbox HOME against a fake Responses backend and a fake MCP server with 150 tools: the first request's body went from 307,198 to 23,580 bytes. Tested with toolsearch_test.go (translated, streamed and rendered, end to end through a Chat fake and a relayed Responses fake, merging, the stream split mid-line) and TestCodexCatalogToolSearch (search on, prompt, context window and the other fields unchanged). go vet, go build -tags nogui (and GOOS=linux, GOOS=windows) and go test -tags nogui ./... pass. Not tried with real Gemini/antigravity calling the search, real ChatGPT apps, or real third-party Responses providers; a non-streamed relayed reply isn't rewritten (Codex always streams).
|