f2eb56c6bbde1300dddc402861b59cd4e45e0215
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
2a19245cec |
gateway: Codex's output token replay goes to OpenAI only (Copilot refused it)
echo_ts on Discord (#feedback): a Codex conversation begun on gpt-6-astra
and carried on on a Copilot model failed with "Copilot: Invalid value:
'output.encrypted_content'. Supported values are: ...".
Cause: Codex with output token replay on (codex-rs core/src/client.rs)
adds "output.encrypted_content" to include, and keeps each message's and
call's sealed copy (encrypted_content) in its history, only for a
provider named "OpenAI". Signed in, Codex reaches magpie as that
provider, so a magpie model got both as they came: the passthrough to
Copilot's /responses (forVendor) and the rebuilt request (buildResponses)
both kept the include, and Copilot refuses it.
Now magpie does what Codex does under any other provider name
(!is_openai): forVendor drops output.encrypted_content from include and
encrypted_content from every input item but reasoning and compactions
(ResponseItem::clear_encrypted_content keeps those). buildResponses asks
for it only of api.openai.com and chatgpt.com. OpenAI's API and the
ChatGPT backend get the request as before. Astra's sealed reasoning
still goes along; one Copilot can't read is taken out once refused, as
before (foreignReasoning).
Tests: TestCodexOutputReplayStaysWithOpenAI (the fake refuses with the
reporter's literal error; signed-in and /v1/responses routes) and
TestResponsesOutputReplayOnlyToOpenAI. The rewrite equivalence test's
reference copy (wasForVendor) gets the same rule, with new marks.
Run: go test ./internal/gateway -count=1 -timeout 45m: every test ok but TestHandedOverGatewayFinishesItsStream
("request 0 during the handover: ... EOF"), which this change doesn't
reach; it passes 5 of 5 alone (-count=5) and is open as #3568.
gofmt -l clean; go vet, GOOS=windows and GOOS=linux go vet of
internal/gateway clean.
Broken by hand: with codex_backend.go and responses.go as on main, both
new tests are red (400 with the reporter's error; include kept for
api.githubcopilot.com, relay.example and api.x.ai).
Not tried: a real Codex with output token replay against a real Copilot
account, nor Copilot's answer to a message's encrypted_content alone
(left out because Codex leaves it out for any provider not OpenAI's).
|
||
|
|
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
|