Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: a Codex thread switched to another effort is shown and sent at it

yetonefc43c69e5349
diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go
index 2d66ae051..c32f6c15d 100644
--- a/internal/gateway/codex_backend.go
+++ b/internal/gateway/codex_backend.go
@@ -2779,7 +2779,8 @@ var keepsSeal = map[string]bool{`"reasoning"`: true, `"compaction"`: true, `"com
// 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,
-// configuration_update items, output.encrypted_content in its include,
+// configuration_update items (the last one's effort at the top instead),
+// 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
@@ -2819,6 +2820,7 @@ func forVendor(p provider.Provider, body []byte) []byte {
}
}
if items, err := rawArray(q["input"]); err == nil {
+ updated := "" // the effort the agent changed to mid-thread
out := items[:0]
for _, raw := range items {
it, err := rawObject(raw)
@@ -2828,6 +2830,9 @@ func forVendor(p provider.Provider, body []byte) []byte {
}
if string(it["type"]) == `"configuration_update"` {
changed = true
+ if e := updateEffort(raw); e != "" {
+ updated = e
+ }
continue
}
_, meta := it["internal_chat_message_metadata_passthrough"]
@@ -2848,6 +2853,12 @@ func forVendor(p provider.Provider, body []byte) []byte {
if changed {
q["input"], _ = marshalPlain(out)
}
+ // the items go, the change they made stays: at the top, which is
+ // all this vendor reads
+ if reasoning, err := rawObject(q["reasoning"]); updated != "" && err == nil && reasoning != nil {
+ reasoning["effort"], _ = json.Marshal(updated)
+ q["reasoning"], _ = marshalPlain(reasoning)
+ }
}
if !changed {
return body
diff --git a/internal/gateway/codex_effort_change_test.go b/internal/gateway/codex_effort_change_test.go
new file mode 100644
index 000000000..d3aca1c19
--- /dev/null
+++ b/internal/gateway/codex_effort_change_test.go
@@ -0,0 +1,122 @@
+package gateway
+
+import (
+ "encoding/json"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// codexEffortChanged is a turn as Codex 0.162.1 sends it with its
+// reasoning_effort_override feature on, after the thread, started at
+// medium, was switched to xhigh (/model, Extra high, "s"): the top stays
+// at medium and the change goes as a configuration_update after the
+// user's message (captured from the real binary, its long developer
+// messages and metadata left out; zzzkl3994 on Discord).
+const codexEffortChanged = `{"model":"%s","stream":true,"tool_choice":"auto","parallel_tool_calls":false,"reasoning":{"effort":"medium","context":"all_turns"},"store":false,"include":["reasoning.encrypted_content"],"input":[` +
+ `{"type":"message","role":"user","content":[{"type":"input_text","text":"say ok"}]},` +
+ `{"type":"configuration_update","reasoning":{"effort":"medium"}},` +
+ `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"ok"}]},` +
+ `{"type":"message","role":"user","content":[{"type":"input_text","text":"again"}]},` +
+ `{"type":"configuration_update","reasoning":{"effort":"xhigh"}}]}`
+
+func codexTurnAt(model string) string {
+ return strings.Replace(codexEffortChanged, "%s", model, 1)
+}
+
+// The effort a thread was switched to is the one Routing shows, and the
+// one a Responses vendor that doesn't read the items is asked: it was
+// shown, and sent, at the thread's first.
+func TestCodexEffortChangeShownAndSent(t *testing.T) {
+ f := &fake{t: t, reply: sse(
+ `data: {"type":"response.created","response":{"id":"r1","model":"m1"}}`,
+ `data: {"type":"response.completed","response":{"id":"r1","output":[],"usage":{"input_tokens":5,"output_tokens":2}}}`)}
+ setup(t, provider.Responses, f)
+ s := New()
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(codexTurnAt("fake/m1")))
+ s.Handler().ServeHTTP(rec, req)
+ if rec.Code != 200 {
+ t.Fatalf("%d %s", rec.Code, rec.Body)
+ }
+ r := lastRoute(s)
+ if r.Effort != "xhigh" || len(r.Tries) != 1 || r.Tries[0].Effort != "xhigh" {
+ t.Errorf("route at %q, tries %+v; want xhigh", r.Effort, r.Tries)
+ }
+ var got struct {
+ Reasoning map[string]any `json:"reasoning"`
+ Input []map[string]any `json:"input"`
+ }
+ json.Unmarshal(f.got, &got)
+ if got.Reasoning["effort"] != "xhigh" {
+ t.Errorf("sent reasoning %v, want xhigh", got.Reasoning)
+ }
+ for _, it := range got.Input {
+ if it["type"] == "configuration_update" {
+ t.Errorf("an update reached the vendor: %s", f.got)
+ }
+ }
+}
+
+// Translated to Chat, the turn is asked at the effort it was switched to.
+func TestCodexEffortChangeReachesChatVendor(t *testing.T) {
+ f := &fake{t: t, reply: sse(
+ `data: {"id":"c1","model":"m1","choices":[{"delta":{"role":"assistant","content":"hi"}}]}`,
+ `data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}]}`,
+ `data: [DONE]`)}
+ setup(t, provider.Chat, f)
+ if code, body := post(t, "/v1/responses", codexTurnAt("fake/m1")); code != 200 {
+ t.Fatalf("status %d: %s", code, body)
+ }
+ var got map[string]any
+ json.Unmarshal(f.got, &got)
+ if got["reasoning_effort"] != "xhigh" {
+ t.Errorf("sent %s", f.got)
+ }
+}
+
+// What reads and rewrites a Responses request's effort reads and rewrites
+// the last update; OpenAI's own API and the ChatGPT backend get the items
+// as Codex sent them.
+func TestCodexEffortChangeReaders(t *testing.T) {
+ body := []byte(codexTurnAt("gpt-6.1-sol"))
+ if e := requestEffort(provider.Responses, body); e != "xhigh" {
+ t.Errorf("requestEffort %q", e)
+ }
+ if e := requestEffort(provider.Responses, []byte(`{"reasoning":{"effort":"low"},"input":[{"type":"message","role":"user","content":"configuration_update"}]}`)); e != "low" {
+ t.Errorf("a message naming the item: %q", e)
+ }
+ if q, err := parseResponses(body); err != nil || q.Effort != "xhigh" {
+ t.Errorf("parsed at %q (%v)", q.Effort, err)
+ }
+ for _, p := range []provider.Provider{
+ {Responses: "https://api.openai.com/v1"},
+ {Responses: "https://chatgpt.com/backend-api/codex", Account: &provider.Account{Agent: "codex"}},
+ } {
+ if got := forVendor(p, body); string(got) != string(body) {
+ t.Errorf("%s: %s", p.Responses, got)
+ }
+ }
+
+ // magpie's own pick goes where the last change went; the top stays
+ picked := withEffort(provider.Responses, body, "high")
+ var q struct {
+ Reasoning map[string]any `json:"reasoning"`
+ Input []map[string]any `json:"input"`
+ }
+ json.Unmarshal(picked, &q)
+ last, _ := q.Input[len(q.Input)-1]["reasoning"].(map[string]any)
+ first, _ := q.Input[1]["reasoning"].(map[string]any)
+ if q.Reasoning["effort"] != "medium" || last["effort"] != "high" || first["effort"] != "medium" || bodyEffort(provider.Responses, picked) != "high" {
+ t.Errorf("picked high: %s", picked)
+ }
+ fixed := withFixedEffort(provider.Responses, body, "low")
+ if e := requestEffort(provider.Responses, fixed); e != "low" {
+ t.Errorf("fixed at low reads %q: %s", e, fixed)
+ }
+ if b := withoutAnyReasoning(provider.Responses, body); strings.Contains(string(b), "configuration_update") || strings.Contains(string(b), `"reasoning"`) {
+ t.Errorf("reasonless: %s", b)
+ }
+}
diff --git a/internal/gateway/decide.go b/internal/gateway/decide.go
index fc1fb91fe..30c6d49db 100644
--- a/internal/gateway/decide.go
+++ b/internal/gateway/decide.go
@@ -734,7 +734,9 @@ func withFixedEffort(proto provider.Protocol, body []byte, effort string) []byte
v.Reasoning = map[string]any{}
}
v.Reasoning["effort"] = effort
- return withFields(body, map[string]any{"reasoning": v.Reasoning})
+ // and the agent's last change of effort mid-thread, which an
+ // upstream reading the items would take over the top's
+ return withEffortUpdated(withFields(body, map[string]any{"reasoning": v.Reasoning}), effort)
case provider.Anthropic:
var v struct {
Thinking *struct {
diff --git a/internal/gateway/effort_update.go b/internal/gateway/effort_update.go
index 4c798e200..ad4cbe229 100644
--- a/internal/gateway/effort_update.go
+++ b/internal/gateway/effort_update.go
@@ -232,3 +232,88 @@ func isUserItem(raw json.RawMessage) bool {
}
return json.Unmarshal(raw, &it) == nil && it.Role == "user" && (it.Type == "" || it.Type == "message")
}
+
+// Codex sends such updates itself with its reasoning_effort_override
+// feature on (0.162.1): an effort switched mid-thread (/model, then "s")
+// leaves reasoning.effort at the thread's first and goes as a
+// configuration_update after the turn's user message. The last one is the
+// effort the request is asked at; read from the top alone, Routing showed
+// medium for a thread switched to xhigh, and a vendor the items are taken
+// out for (forVendor) or a translation was asked at medium (zzzkl3994 on
+// Discord).
+
+// effortUpdated is the effort the last configuration_update in a Responses
+// request's input asks for: "" with none.
+func effortUpdated(body []byte) string {
+ if !bytes.Contains(body, []byte(`"configuration_update"`)) {
+ return ""
+ }
+ e := ""
+ inputItems(body, func(it []byte) bool {
+ if string(memberOrNil(it, "type")) == `"configuration_update"` {
+ if u := updateEffort(it); u != "" {
+ e = u
+ }
+ }
+ return true
+ })
+ return e
+}
+
+// updateEffort is the effort a configuration_update item asks for.
+func updateEffort(it []byte) string {
+ var u struct {
+ Reasoning *struct {
+ Effort string `json:"effort"`
+ } `json:"reasoning"`
+ }
+ if decodeJSON(it, &u) != nil || u.Reasoning == nil {
+ return ""
+ }
+ return u.Reasoning.Effort
+}
+
+// withEffortUpdated is a Responses request whose last configuration_update
+// asks for effort, the rest as it came: magpie's own pick for the request
+// goes where the agent's last change of effort went, so the upstream
+// that reads the items isn't asked another by them, and the thread's
+// prefix stays as it was. The body as it is when it has none.
+func withEffortUpdated(body []byte, effort string) []byte {
+ if !bytes.Contains(body, []byte(`"configuration_update"`)) {
+ return body
+ }
+ q, err := rawObject(body)
+ if err != nil || q == nil {
+ return body
+ }
+ items, err := rawArray(q["input"])
+ if err != nil {
+ return body
+ }
+ last := -1
+ for i, raw := range items {
+ if string(memberOrNil(raw, "type")) == `"configuration_update"` && updateEffort(raw) != "" {
+ last = i
+ }
+ }
+ if last < 0 {
+ return body
+ }
+ it, err := rawObject(items[last])
+ if err != nil || it == nil {
+ return body
+ }
+ reasoning, err := rawObject(it["reasoning"])
+ if err != nil || reasoning == nil {
+ return body
+ }
+ reasoning["effort"], _ = json.Marshal(effort)
+ it["reasoning"], _ = marshalPlain(reasoning)
+ items[last], _ = marshalPlain(it)
+ q["input"], _ = marshalPlain(items)
+ b, err := marshalPlain(q)
+ if err != nil {
+ return body
+ }
+ return b
+}
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index 80d8ae86a..a939215cd 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -7500,7 +7500,9 @@ var otherOffNamed = map[string]*regexp.Regexp{
var noneQuoted = regexp.MustCompile(`(?i)['"]none\\?['"]`)
// bodyEffort is the reasoning effort a Chat or Responses request asks for,
-// or an Anthropic one in its output_config.
+// or an Anthropic one in its output_config. A Responses request that
+// changed it mid-thread asks for its last configuration_update's
+// (effortUpdated).
func bodyEffort(proto provider.Protocol, body []byte) string {
var v struct {
ReasoningEffort string `json:"reasoning_effort"`
@@ -7518,6 +7520,9 @@ func bodyEffort(proto provider.Protocol, body []byte) string {
case proto == provider.Chat:
return v.ReasoningEffort
case proto == provider.Responses && v.Reasoning != nil:
+ if e := effortUpdated(body); e != "" {
+ return e
+ }
return v.Reasoning.Effort
case proto == provider.Anthropic && v.OutputConfig != nil:
return v.OutputConfig.Effort
@@ -7549,6 +7554,10 @@ func withBodyEffort(proto provider.Protocol, body []byte, effort string) []byte
if decodeJSON(body, &v) != nil || v.Reasoning == nil {
return body
}
+ if effortUpdated(body) != "" {
+ // changed mid-thread: where the agent's last change went
+ return withEffortUpdated(body, effort)
+ }
v.Reasoning["effort"] = effort
return withFields(body, map[string]any{"reasoning": v.Reasoning})
}
diff --git a/internal/gateway/quiet.go b/internal/gateway/quiet.go
index 48aea62ef..9c24c066a 100644
--- a/internal/gateway/quiet.go
+++ b/internal/gateway/quiet.go
@@ -92,6 +92,18 @@ func withoutReasoningFields(proto provider.Protocol, body []byte, all bool) []by
}
}
}
+ // Responses' changes of effort mid-thread ask for reasoning too
+ if in, ok := m["input"].([]any); ok && proto == provider.Responses {
+ kept := in[:0:0]
+ for _, it := range in {
+ if o, ok := it.(map[string]any); !ok || o["type"] != "configuration_update" {
+ kept = append(kept, it)
+ }
+ }
+ if len(kept) < len(in) {
+ m["input"], changed = kept, true
+ }
+ }
case provider.Anthropic:
if t, ok := m["thinking"].(map[string]any); ok {
if s, _ := t["type"].(string); all || s == "enabled" || s == "adaptive" {
diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go
index b056cd80e..41ac8cf1f 100644
--- a/internal/gateway/responses.go
+++ b/internal/gateway/responses.go
@@ -33,6 +33,10 @@ type rItem struct {
// tool_search_output: the tools Codex's search found, which the model
// may call from then on
Tools []rTool `json:"tools,omitempty"`
+ // configuration_update's: an effort changed mid-thread
+ Reasoning *struct {
+ Effort string `json:"effort"`
+ } `json:"reasoning,omitempty"`
// reasoning
Summary []rText `json:"summary,omitempty"`
EncryptedContent string `json:"encrypted_content,omitempty"`
@@ -389,6 +393,13 @@ func readResponses(body []byte, facts bool) (*Request, error) {
continue
}
r.Messages = append(r.Messages, Message{Role: role, Parts: parts})
+ case it.Type == "configuration_update":
+ // the agent changed its effort mid-thread: the last
+ // change is the one the turn is asked at (effortUpdated)
+ if q.Reasoning != nil && it.Reasoning != nil && it.Reasoning.Effort != "" {
+ r.Effort = effortOf(it.Reasoning.Effort)
+ r.ThinkOff = strings.EqualFold(strings.TrimSpace(it.Reasoning.Effort), "none")
+ }
case it.Type == "agent_message":
// MultiAgentV2 hands a subagent its task, and agents their
// messages to each other, as this item: what it says is the