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).
This commit is contained in:
yetone
2026-10-27 23:38:47 +08:00
parent 766356cd3f
commit 2a19245cec
4 changed files with 215 additions and 11 deletions
+49 -9
View File
@@ -2692,17 +2692,36 @@ func boundCallIDs(body []byte) []byte {
// openaiOnly are the parts of a Responses request Codex sends only to a
// provider named "OpenAI": its built-in one (signed in, through
// openai_base_url), or CC Switch's table so named for remote compaction.
// Under any other name Codex leaves them out itself (client.rs, !is_openai).
// Under any other name Codex leaves them out itself (client.rs, !is_openai):
// output token replay's include and the sealed output it carries back on a
// message or call (encrypted_content on any item but reasoning and a
// compaction).
var openaiOnly = [][]byte{[]byte(`"internal_chat_message_metadata_passthrough"`),
[]byte(`"encrypted_function_args"`), []byte(`"stream_options"`), []byte(`"configuration_update"`)}
[]byte(`"encrypted_function_args"`), []byte(`"stream_options"`), []byte(`"configuration_update"`),
[]byte(`"` + outputReplay + `"`), []byte(`"encrypted_content"`)}
// outputReplay is what Codex asks OpenAI for with output token replay on
// (client.rs): each message's and call's own sealed copy. Copilot's
// /responses refuses it ("Invalid value: 'output.encrypted_content'",
// echo_ts), as an API that isn't OpenAI's may.
const outputReplay = "output.encrypted_content"
// keepsSeal are the input items whose encrypted_content Codex sends any
// provider (ResponseItem::clear_encrypted_content leaves only these): a
// reasoning item's, which foreignReasoning takes out once refused, and a
// compaction's.
var keepsSeal = map[string]bool{`"reasoning"`: true, `"compaction"`: true, `"compaction_summary"`: true, `"context_compaction"`: true}
// forVendor is a Responses request as Codex sends it to a provider not
// OpenAI's: without its messages' internal metadata, a call's
// encrypted_function_args, stream_options' reasoning_summary_delivery and
// configuration_update items.
// encrypted_function_args, stream_options' reasoning_summary_delivery,
// configuration_update items, output.encrypted_content in its include,
// and the sealed output (encrypted_content) on a message or call that an
// OpenAI model wrote earlier in the conversation.
// A relay that checks it is Codex's turned the lot away ("invalid codex
// request", #292). OpenAI's API and the ChatGPT backend get it as it came;
// so does anything else, byte for byte, when none of them is in it.
// request", #292), and Copilot the include (echo_ts). OpenAI's API and the
// ChatGPT backend get it as it came; so does anything else, byte for byte,
// when none of them is in it.
func forVendor(p provider.Provider, body []byte) []byte {
if p.Account != nil && p.Account.Agent == "codex" || strings.HasSuffix(p.Host(), "openai.com") {
return body
@@ -2726,6 +2745,15 @@ func forVendor(p provider.Provider, body []byte) []byte {
}
}
}
var include []string
if decodeJSON(q["include"], &include) == nil && slices.Contains(include, outputReplay) {
changed = true
if include = slices.DeleteFunc(include, func(v string) bool { return v == outputReplay }); len(include) == 0 {
delete(q, "include")
} else {
q["include"], _ = marshalPlain(include)
}
}
if items, err := rawArray(q["input"]); err == nil {
out := items[:0]
for _, raw := range items {
@@ -2740,10 +2768,15 @@ func forVendor(p provider.Provider, body []byte) []byte {
}
_, meta := it["internal_chat_message_metadata_passthrough"]
_, sealed := it["encrypted_function_args"]
if meta || sealed {
_, replay := it["encrypted_content"]
replay = replay && !keepsSeal[string(it["type"])]
if meta || sealed || replay {
changed = true
delete(it, "internal_chat_message_metadata_passthrough")
delete(it, "encrypted_function_args")
if replay {
delete(it, "encrypted_content")
}
raw, _ = marshalPlain(it)
}
out = append(out, raw)
@@ -2803,16 +2836,23 @@ func forVendorChanges(body []byte) bool {
return true
}
}
if inc, found, ok := topMember(body, "include"); !ok {
return false
} else if found && bytes.Contains(inc, []byte(`"`+outputReplay+`"`)) {
return true
}
return anyInputItem(body, func(it []byte) bool {
if len(it) == 0 || it[0] != '{' {
return false
}
if string(memberOrNil(it, "type")) == `"configuration_update"` {
typ := string(memberOrNil(it, "type"))
if typ == `"configuration_update"` {
return true
}
_, meta := memberOf(it, "internal_chat_message_metadata_passthrough")
_, sealed := memberOf(it, "encrypted_function_args")
return meta || sealed
_, replay := memberOf(it, "encrypted_content")
return meta || sealed || replay && !keepsSeal[typ]
})
}
@@ -0,0 +1,138 @@
package gateway
import (
"encoding/json"
"net/http"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// copilotRefusesOutputReplay is Copilot's /responses turning away output
// token replay's include, word for word as echo_ts met it on Discord: a
// Codex conversation begun on gpt-6-astra and carried on on a Copilot
// model.
const copilotRefusesOutputReplay = `{"error":{"message":"Invalid value: 'output.encrypted_content'. Supported values are: 'file_search_call.results', 'web_search_call.results', 'web_search_call.action.sources', 'message.input_image.image_url', 'computer_call_output.output.image_url', 'code_interpreter_call.outputs', 'reasoning.encrypted_content', and 'message.output_text.logprobs'.","type":"invalid_request_error","param":"include","code":"invalid_value"}}`
// Codex with output token replay on asks a provider named "OpenAI" (its
// built-in one, which reaches magpie signed in) for
// output.encrypted_content, and sends back each earlier message's and
// call's sealed copy in its encrypted_content. Under any other name it
// leaves both out (client.rs: include only when is_openai, and
// clear_encrypted_content on every item but reasoning and compactions). A
// vendor's model gets the request as Codex would have sent it there;
// OpenAI's own API and the ChatGPT backend get it as it came.
func TestCodexOutputReplayStaysWithOpenAI(t *testing.T) {
const sent = `{"model":"fake/m1","instructions":"You are Codex","stream":true,"store":false,
"include":["reasoning.encrypted_content","output.encrypted_content"],"prompt_cache_key":"thread-1","reasoning":{"effort":"medium"},
"tools":[{"type":"function","name":"shell","parameters":{"type":"object"}}],
"input":[
{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the bug"}]},
{"type":"reasoning","id":"rs_1","summary":[],"encrypted_content":"gAAAA-astra-reasoning"},
{"type":"message","role":"assistant","phase":"commentary","content":[{"type":"output_text","text":"looking"}],"encrypted_content":"gAAAA-astra-commentary"},
{"type":"function_call","name":"shell","arguments":"{\"cmd\":\"ls\"}","call_id":"c1","encrypted_content":"gAAAA-astra-call"},
{"type":"function_call_output","call_id":"c1","output":"a.go"},
{"type":"message","role":"user","content":[{"type":"input_text","text":"go on"}]}
]}`
f := &fake{t: t, reply: sse(
`data: {"type":"response.created","response":{"id":"resp_r1","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"OK"}]}}`,
`data: {"type":"response.completed","response":{"id":"resp_r1","status":"completed","output":[]}}`)}
f.refuse = func(body []byte) (int, string) {
if strings.Contains(string(body), "output.encrypted_content") {
return 400, copilotRefusesOutputReplay
}
return 0, ""
}
setup(t, provider.Responses, f)
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
t.Error("a vendor's model reached OpenAI")
w.WriteHeader(400)
})
for _, c := range []struct {
name string
send func() (int, string)
}{
{"signed in", func() (int, string) { return codexPost(t, sent) }},
{"its own provider", func() (int, string) { return post(t, "/v1/responses", sent) }},
} {
code, body := c.send()
if code != 200 {
t.Fatalf("%s: %d %s\nupstream got %s", c.name, code, body, f.got)
}
var q struct {
Include []string `json:"include"`
Input []map[string]any `json:"input"`
}
if err := json.Unmarshal(f.got, &q); err != nil {
t.Fatal(err)
}
if !slices.Equal(q.Include, []string{"reasoning.encrypted_content"}) {
t.Errorf("%s: include %q", c.name, q.Include)
}
var calls int
for _, it := range q.Input {
switch it["type"] {
case "reasoning":
// sealed reasoning still goes; one the vendor can't read is
// taken out once refused (foreignReasoning)
if it["encrypted_content"] != "gAAAA-astra-reasoning" {
t.Errorf("%s: reasoning %v", c.name, it)
}
case "function_call":
calls++
if _, ok := it["encrypted_content"]; ok || it["arguments"] != `{"cmd":"ls"}` {
t.Errorf("%s: call %v", c.name, it)
}
case "message":
if _, ok := it["encrypted_content"]; ok {
t.Errorf("%s: message %v", c.name, it)
}
}
}
// what was said and done stays
if calls != 1 || !strings.Contains(string(f.got), `"text":"looking"`) {
t.Errorf("%s: upstream got %s", c.name, f.got)
}
}
// OpenAI's own API and the ChatGPT backend take them
for _, p := range []provider.Provider{
{Responses: "https://api.openai.com/v1"},
{Account: &provider.Account{Agent: "codex"}},
} {
if got := forVendor(p, []byte(sent)); string(got) != sent {
t.Errorf("%+v: %s", p, got)
}
}
// include holding it alone goes without include
if got := forVendor(provider.Provider{Responses: "https://api.githubcopilot.com"}, []byte(`{"include":["output.encrypted_content"],"input":[]}`)); string(got) != `{"input":[]}` {
t.Errorf("alone: %s", got)
}
}
// A request magpie builds again for a Responses upstream asks for output
// token replay only of OpenAI's API and the ChatGPT backend.
func TestResponsesOutputReplayOnlyToOpenAI(t *testing.T) {
r, err := parseResponses([]byte(`{"model":"m","input":"hi","reasoning":{"effort":"low"},"include":["reasoning.encrypted_content","output.encrypted_content"]}`))
if err != nil {
t.Fatal(err)
}
for host, want := range map[string][]string{
"api.githubcopilot.com": {"reasoning.encrypted_content"},
"relay.example": {"reasoning.encrypted_content"},
"api.x.ai": {"reasoning.encrypted_content"},
"api.openai.com": {"reasoning.encrypted_content", "output.encrypted_content"},
"chatgpt.com": {"reasoning.encrypted_content", "output.encrypted_content"},
} {
var q struct {
Include []string `json:"include"`
}
json.Unmarshal(buildResponses(r, "m", host, false), &q)
if !slices.Equal(q.Include, want) {
t.Errorf("%s: include %q, want %q", host, q.Include, want)
}
}
}
+24 -1
View File
@@ -156,6 +156,9 @@ var codexMarks = []func(it map[string]any){
func(it map[string]any) { it["type"] = "custom_tool_call"; it["id"] = "fc_9" },
func(it map[string]any) { it["type"] = "custom_tool_call"; it["id"] = "ctc" },
func(it map[string]any) { it["encrypted_function_args"] = "sealed" },
func(it map[string]any) { it["encrypted_content"] = "gAAAA-output" },
func(it map[string]any) { it["encrypted_content"] = nil },
func(it map[string]any) { it["type"] = "context_compaction"; it["encrypted_content"] = "gAAAA" },
func(it map[string]any) { it["internal_chat_message_metadata_passthrough"] = nil },
}
@@ -171,6 +174,9 @@ var codexRawMarks = [][2]string{
{`"type":"function_call_output"`, `"type":"configuration_update"`},
{`"internal_chat_message_metadata_passthrough"`, `"internal_chat_message_metadata_passthrough"`},
{`"input":[`, `"input":[null,`},
{`"include":["reasoning.encrypted_content"]`, `"include":["reasoning.encrypted_content","output.encrypted_content"]`},
{`"include":["reasoning.encrypted_content"]`, `"include":["output.encrypted_content"]`},
{`"include":["reasoning.encrypted_content"]`, `"include":"output.encrypted_content"`},
{`"input":[`, `"input":[1,`},
{`"input":[`, `"input":["x",[],`},
{`"input":[`, `"input":null,"x":[`},
@@ -539,6 +545,15 @@ func wasForVendor(p provider.Provider, body []byte) []byte {
}
}
}
var include []string
if json.Unmarshal(q["include"], &include) == nil && slices.Contains(include, "output.encrypted_content") {
changed = true
if include = slices.DeleteFunc(include, func(v string) bool { return v == "output.encrypted_content" }); len(include) == 0 {
delete(q, "include")
} else {
q["include"], _ = wasMarshalPlain(include)
}
}
var items []json.RawMessage
if json.Unmarshal(q["input"], &items) == nil {
out := items[:0]
@@ -554,10 +569,18 @@ func wasForVendor(p provider.Provider, body []byte) []byte {
}
_, meta := it["internal_chat_message_metadata_passthrough"]
_, sealed := it["encrypted_function_args"]
if meta || sealed {
_, replay := it["encrypted_content"]
switch string(it["type"]) {
case `"reasoning"`, `"compaction"`, `"compaction_summary"`, `"context_compaction"`:
replay = false
}
if meta || sealed || replay {
changed = true
delete(it, "internal_chat_message_metadata_passthrough")
delete(it, "encrypted_function_args")
if replay {
delete(it, "encrypted_content")
}
raw, _ = wasMarshalPlain(it)
}
out = append(out, raw)
+4 -1
View File
@@ -1301,9 +1301,12 @@ func buildResponses(r *Request, model, host string, rejectTemp bool) []byte {
// input's goes back to a Responses upstream, as it would without magpie
// (#1445); an account or vendor that can't read another's turns it away,
// and is asked again without it (foreignReasoning, withoutRefused).
// Output token replay's include is OpenAI's alone, as Codex sends it
// (forVendor): Copilot refuses it (echo_ts).
var include []string
for _, v := range r.Include {
if v == "" || slices.Contains(include, v) || (v == "reasoning.encrypted_content" && out["reasoning"] == nil) {
if v == "" || slices.Contains(include, v) || (v == "reasoning.encrypted_content" && out["reasoning"] == nil) ||
v == outputReplay && host != "chatgpt.com" && host != "api.openai.com" {
continue
}
include = append(include, v)