Files
magpie/internal/gateway/effort_update_test.go
T
yetone abec0cae1c gateway: a reasoning effort changed mid-thread keeps the upstream's cached prompt on GPT-6 (#617) — an agent switching effort changed the request's reasoning.effort, and with it the prefix OpenAI caches. OpenAI's Responses API takes the change as an input item instead (developers.openai.com/api/docs/guides/reasoning, "Change reasoning mid-conversation": {"type":"configuration_update","reasoning":{"effort":…}} before the next user message, the request-level effort unchanged, GPT-6 family). For a GPT-6 model through a ChatGPT account or OpenAI's own API, asked in Responses, magpie keeps per thread (conversation, prompt_cache_key, account, model) the effort it started at and where each change went: the top stays at the first effort, and the updates are put back at the same places on every later request (before the turn's first user message, else at the input's end; a turn asked again at another effort replaces its own update). A history that isn't the last one with more after it (compaction, edits), previous_response_id, truncation auto, context_management, a request without an effort or an agent sending its own updates goes as the agent sent it and starts the thread again — what every request cost before. An upstream that answers 400 to a request with updates is asked again as the agent sent it (trace: update); if that one goes, the account and model get no updates for a day. Other vendors (forVendor strips the items anyway) and older models are untouched. A preview, off unless MAGPIE_EFFORT_UPDATES=on, until it is seen on the real backends: one that took the item and ignored it would leave an effort change doing nothing, unseen. Tests: TestEffortChangeKeepsTheCache, TestEffortUpdateRefused (both fail with it off), TestTakesEffortUpdates, TestEffortChangeOtherVendor against a fake ChatGPT backend. Not tried against the real ChatGPT backend or OpenAI API: whether the backend honours the item (rather than ignoring it) and cached_tokens across a switch are unverified. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-03 09:58:00 +08:00

218 lines
8.7 KiB
Go

package gateway
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// effortTurns are a thread's requests as an agent sends them: the
// history so far, each turn adding the reply and the next user message.
func effortTurn(key, effort string, users ...string) string {
var items []string
for i, u := range users {
if i > 0 {
items = append(items, fmt.Sprintf(`{"type":"message","role":"assistant","content":[{"type":"output_text","text":"reply %d"}]}`, i))
}
items = append(items, fmt.Sprintf(`{"type":"message","role":"user","content":[{"type":"input_text","text":%q}]}`, u))
}
return fmt.Sprintf(`{"model":"codex/gpt-6-luna","stream":true,"prompt_cache_key":%q,"reasoning":{"effort":%q},"input":[%s]}`, key, effort, strings.Join(items, ","))
}
// sentShape is what the upstream got: the top-level effort, the
// prompt_cache_key, and the input as roles and updates ("u:high").
func sentShape(t *testing.T, b []byte) (effort, key string, input []string) {
t.Helper()
var q struct {
Key string `json:"prompt_cache_key"`
Reasoning struct {
Effort string `json:"effort"`
} `json:"reasoning"`
Input []struct {
Type string `json:"type"`
Role string `json:"role"`
Reasoning struct {
Effort string `json:"effort"`
} `json:"reasoning"`
} `json:"input"`
}
if err := json.Unmarshal(b, &q); err != nil {
t.Fatalf("upstream body: %v: %s", err, b)
}
for _, it := range q.Input {
if it.Type == "configuration_update" {
input = append(input, "u:"+it.Reasoning.Effort)
} else {
input = append(input, it.Role)
}
}
return q.Reasoning.Effort, q.Key, input
}
func effortCodexAccount(t *testing.T, f *fake) {
t.Helper()
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
claims := func(m map[string]any) string {
b, _ := json.Marshal(m)
return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
}
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
auth := fmt.Sprintf(`{"auth_mode":"chatgpt","tokens":{"id_token":%q,"access_token":%q,"refresh_token":"r","account_id":"acct-1"}}`,
claims(map[string]any{"email": "me@example.com"}), claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}))
os.WriteFile(filepath.Join(home, ".codex", "auth.json"), []byte(auth), 0o600)
up := httptest.NewServer(f)
t.Cleanup(up.Close)
old := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = old })
effortThreads.Lock()
effortThreads.m, effortThreads.no = map[string]*effortThread{}, map[string]time.Time{}
effortThreads.Unlock()
}
var effortReply = sse(
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-6-luna"}}`,
`data: {"type":"response.output_text.delta","delta":"pong"}`,
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`)
// #617: an effort changed mid-thread goes as a configuration_update before
// the user message it came with, the thread's first effort staying at the
// top, and is put back in the same place on every later turn; a thread
// that keeps its effort goes as the agent sent it.
func TestEffortChangeKeepsTheCache(t *testing.T) {
t.Setenv("MAGPIE_EFFORT_UPDATES", "on")
f := &fake{t: t, ctype: "none", reply: effortReply}
effortCodexAccount(t, f)
type want struct {
effort string
input string
}
for i, tc := range []struct {
body string
want want
}{
{effortTurn("t1", "low", "a"), want{"low", "user"}},
{effortTurn("t1", "low", "a", "b"), want{"low", "user,assistant,user"}},
{effortTurn("t1", "high", "a", "b", "c"), want{"low", "user,assistant,user,assistant,u:high,user"}},
{effortTurn("t1", "high", "a", "b", "c", "d"), want{"low", "user,assistant,user,assistant,u:high,user,assistant,user"}},
{effortTurn("t1", "medium", "a", "b", "c", "d", "e"), want{"low", "user,assistant,user,assistant,u:high,user,assistant,user,assistant,u:medium,user"}},
// back to the first effort: an update to it, the top unchanged
{effortTurn("t1", "low", "a", "b", "c", "d", "e", "f"), want{"low", "user,assistant,user,assistant,u:high,user,assistant,user,assistant,u:medium,user,assistant,u:low,user"}},
// another thread, at one effort throughout, is left alone
{effortTurn("t2", "high", "x"), want{"high", "user"}},
{effortTurn("t2", "high", "x", "y"), want{"high", "user,assistant,user"}},
// a history that isn't the last one and more (compacted) starts
// the thread again at the effort it asks
{effortTurn("t1", "xhigh", "summary", "g"), want{"xhigh", "user,assistant,user"}},
} {
code, body := post(t, "/v1/responses", tc.body)
if code != 200 || !strings.Contains(body, "pong") {
t.Fatalf("turn %d: status %d: %s", i, code, body)
}
effort, key, input := sentShape(t, f.got)
var thread struct {
Key string `json:"prompt_cache_key"`
}
json.Unmarshal([]byte(tc.body), &thread)
if effort != tc.want.effort || strings.Join(input, ",") != tc.want.input || key != thread.Key {
t.Fatalf("turn %d: sent effort %q, key %q, input %s; want %q, %q, %s", i, effort, key, strings.Join(input, ","), tc.want.effort, thread.Key, tc.want.input)
}
}
}
// #617: an upstream that turns the update away is asked again as the agent
// sent it, and that account and model aren't sent updates again.
func TestEffortUpdateRefused(t *testing.T) {
t.Setenv("MAGPIE_EFFORT_UPDATES", "on")
f := &fake{t: t, ctype: "none", reply: effortReply, refuse: func(b []byte) (int, string) {
if strings.Contains(string(b), "configuration_update") {
return 400, `{"error":{"message":"Invalid value: 'configuration_update'.","type":"invalid_request_error","param":"input[2].type"}}`
}
return 0, ""
}}
effortCodexAccount(t, f)
post(t, "/v1/responses", effortTurn("t1", "low", "a"))
f.calls = 0
code, body := post(t, "/v1/responses", effortTurn("t1", "high", "a", "b"))
effort, _, input := sentShape(t, f.got)
if code != 200 || f.calls != 2 || effort != "high" || strings.Join(input, ",") != "user,assistant,user" {
t.Fatalf("refused: status %d after %d calls, sent %q %v: %s", code, f.calls, effort, input, body)
}
f.calls = 0
code, _ = post(t, "/v1/responses", effortTurn("t1", "medium", "a", "b", "c"))
effort, _, input = sentShape(t, f.got)
if code != 200 || f.calls != 1 || effort != "medium" || strings.Contains(strings.Join(input, ","), "u:") {
t.Fatalf("after: status %d after %d calls, sent %q %v", code, f.calls, effort, input)
}
}
// #617: only a GPT-6 model through a ChatGPT account or OpenAI's API is
// sent updates; another vendor's Responses API, and older models, never.
func TestTakesEffortUpdates(t *testing.T) {
t.Setenv("MAGPIE_EFFORT_UPDATES", "on")
codex := provider.Provider{ID: "codex", Responses: provider.CodexBase, Account: &provider.Account{Agent: "codex"}}
openai := provider.Provider{ID: "openai", Responses: "https://api.openai.com/v1", Key: "k"}
other := provider.Provider{ID: "relay", Responses: "https://relay.example.com/v1", Key: "k"}
xai := provider.Provider{ID: "xai", Responses: "https://api.x.ai/v1", Key: "k"}
chatOnly := provider.Provider{ID: "openai-chat", Chat: "https://api.openai.com/v1", Key: "k"}
for _, tc := range []struct {
p provider.Provider
model string
want bool
}{
{codex, "gpt-6-luna", true},
{codex, "gpt-6.1-sol", true},
{openai, "gpt-6-astra", true},
{openai, "openai/gpt-6-astra", true},
{codex, "gpt-5.5", false},
{other, "gpt-6-astra", false},
{xai, "gpt-6-astra", false},
{chatOnly, "gpt-6-astra", false},
} {
if got := takesEffortUpdates(tc.p, tc.model); got != tc.want {
t.Errorf("%s %s: %v, want %v", tc.p.ID, tc.model, got, tc.want)
}
}
for _, v := range []string{"", "off", "1"} {
t.Setenv("MAGPIE_EFFORT_UPDATES", v)
if takesEffortUpdates(codex, "gpt-6-luna") {
t.Errorf("MAGPIE_EFFORT_UPDATES=%q still sends updates", v)
}
}
}
// #617: a Responses provider not OpenAI's gets a thread's effort change as
// the agent sent it.
func TestEffortChangeOtherVendor(t *testing.T) {
t.Setenv("MAGPIE_EFFORT_UPDATES", "on")
effortThreads.Lock()
effortThreads.m, effortThreads.no = map[string]*effortThread{}, map[string]time.Time{}
effortThreads.Unlock()
f := &fake{t: t, reply: effortReply}
setup(t, provider.Responses, f)
for _, b := range []string{effortTurn("t1", "low", "a"), effortTurn("t1", "high", "a", "b")} {
b = strings.Replace(b, "codex/gpt-6-luna", "fake/gpt-6-luna", 1)
if code, body := post(t, "/v1/responses", b); code != 200 {
t.Fatalf("status %d: %s", code, body)
}
}
effort, _, input := sentShape(t, f.got)
if effort != "high" || strings.Join(input, ",") != "user,assistant,user" {
t.Fatalf("sent %q %v", effort, input)
}
}