Files
magpie/internal/provider/instructions_after_input_test.go
T
yetone f7e0f68ca2 codex: GPT-6.2's instructions go first in the input, where its cache keeps them
From GPT-6.2 on, OpenAI reads a Responses request's top-level
instructions after its input, not before it (prompt caching guide:
"instructions ... appended to the end of the input" for gpt-6.2 and
later), so they sit past the cached prefix and are billed uncached on
every turn. Codex itself sends no top-level instructions: client.rs puts
its base instructions first in the input as a developer message
(BaseInstructionsFragment, codex-rs/core/src/client.rs).

magpie sent Codex's instructions on top for every ChatGPT-account
request that is not Responses Lite: Claude Code, OpenCode, Pi and other
translated agents through a codex account, and Codex CLIs older than
Lite. Measured on a real ChatGPT account, third turn of a growing
conversation: gpt-6.2-sol cached 1,920 of 8,271 input tokens with the
instructions on top, 6,016 with them first in the input; gpt-6-sol
cached 5,632 of 6,843 with them on top (the old order still works
there). Reported on X by Anotiaww (Codex hit rate looked low).

- codexBody: for a GPT-6.2+ model Codex's instructions, and a client's
  own, go first in the input as developer messages, and the request
  carries no instructions field. Older models are unchanged, and Lite
  turns (no instructions at all) are unchanged.
- siwcBody (Sign in with ChatGPT API path): the client's instructions
  go first in the input for GPT-6.2+.
- buildResponses (Chat/Anthropic translated to Responses, any host):
  the system prompt goes first in the input for GPT-6.2+.
- InstructionsAfterInput names the models: gpt-N.M with N.M >= 6.2,
  gpt-7 and later, after a provider prefix; gpt-6, gpt-6.1, gpt-5.x,
  o-series and other vendors' models keep top-level instructions.

The instruction text itself is unchanged; only where it sits moves.

Run (mote, at this tree on origin/main 48fffb60c):
- go test -count=1 -timeout 45m ./...: all 103 packages ok
  (internal/gateway 1602s, internal/provider 463s).
- go vet ./internal/provider ./internal/gateway on darwin, linux and
  windows: ok. gofmt -l: clean.

Broken by hand, then put back: InstructionsAfterInput returning false
turns TestInstructionsAfterInput, TestCodexBodyInstructionsFirstIn-
InputOnGPT62, TestSiwcBodyInstructionsFirstInInputOnGPT62 and
TestResponsesSystemFirstInInputOnGPT62 red.

Not tried: a real Claude Code or OpenCode session through magpie on
gpt-6.2 (the live app lists no gpt-6.2 model here); the cache numbers
above are from direct requests to the ChatGPT backend in both orders.
2026-11-18 16:04:56 +08:00

105 lines
3.9 KiB
Go

package provider
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
func TestInstructionsAfterInput(t *testing.T) {
for model, want := range map[string]bool{
"gpt-6.2-sol": true, "gpt-6.2-astra": true, "codex/gpt-6.2-sol": true, "GPT-6.3": true, "gpt-7": true, "gpt-7.1-luna": true, "relay/gpt-6.10": true,
"gpt-6-sol": false, "gpt-6.1-sol": false, "gpt-6": false, "gpt-5.6-sol": false, "gpt-5.5": false, "gpt-4.1": false, "o3": false,
"claude-opus-5-5": false, "chatgpt-6.2": false, "": false,
} {
if got := InstructionsAfterInput(model); got != want {
t.Errorf("InstructionsAfterInput(%q) = %v, want %v", model, got, want)
}
}
}
// A ChatGPT account's GPT-6.2 turn carries Codex's instructions first in
// its input, not on top, where the model reads them after the input and
// the cache never keeps them; an older model's stay on top.
func TestCodexBodyInstructionsFirstInInputOnGPT62(t *testing.T) {
dir := t.TempDir()
t.Setenv("CODEX_HOME", dir)
t.Setenv("XDG_CACHE_HOME", t.TempDir())
os.WriteFile(filepath.Join(dir, "models_cache.json"), []byte(`{"models":[
{"slug":"gpt-6.2-sol","model_messages":{"instructions_template":"You are Codex, based on GPT-6.2.\nMore."}},
{"slug":"gpt-6-sol","model_messages":{"instructions_template":"You are Codex, based on GPT-6.\nMore."}}]}`), 0o644)
type body struct {
Instructions *string `json:"instructions"`
Input []map[string]any `json:"input"`
}
text := func(it map[string]any) string {
c, _ := it["content"].([]any)
if len(c) != 1 {
return ""
}
p, _ := c[0].(map[string]any)
s, _ := p["text"].(string)
return s
}
for name, tc := range map[string]struct {
in string
want []string // the developer texts first in the input
}{
"translated": {`{"model":"gpt-6.2-sol","instructions":"You are Pi.","input":[{"type":"message","role":"user","content":"hi"}]}`,
[]string{"You are Codex, based on GPT-6.2.\nMore.", "You are Pi."}},
"codex's own": {`{"model":"gpt-6.2-sol","instructions":"You are Codex, based on GPT-6.2.\nMore.","input":[{"type":"message","role":"user","content":"hi"}]}`,
[]string{"You are Codex, based on GPT-6.2.\nMore."}},
"none": {`{"model":"gpt-6.2-sol","input":[{"type":"message","role":"user","content":"hi"}]}`,
[]string{"You are Codex, based on GPT-6.2.\nMore."}},
} {
var m body
out := codexBody([]byte(tc.in))
if err := json.Unmarshal(out, &m); err != nil {
t.Fatal(err)
}
if m.Instructions != nil {
t.Errorf("%s: instructions on top: %s", name, out)
}
if len(m.Input) != len(tc.want)+1 {
t.Fatalf("%s: %s", name, out)
}
for i, w := range tc.want {
if m.Input[i]["role"] != "developer" || text(m.Input[i]) != w {
t.Errorf("%s: input[%d] = %v, want developer %q", name, i, m.Input[i], w)
}
}
if m.Input[len(tc.want)]["role"] != "user" {
t.Errorf("%s: %s", name, out)
}
}
var m body
if err := json.Unmarshal(codexBody([]byte(`{"model":"gpt-6-sol","instructions":"You are Pi.","input":[{"type":"message","role":"user","content":"hi"}]}`)), &m); err != nil {
t.Fatal(err)
}
if m.Instructions == nil || *m.Instructions != "You are Codex, based on GPT-6.\nMore." || len(m.Input) != 2 || text(m.Input[0]) != "You are Pi." {
t.Fatalf("gpt-6-sol: %+v", m)
}
}
func TestSiwcBodyInstructionsFirstInInputOnGPT62(t *testing.T) {
var m map[string]any
if err := json.Unmarshal(siwcBody([]byte(`{"model":"gpt-6.2-astra","instructions":"Be brief.","input":"hello"}`)), &m); err != nil {
t.Fatal(err)
}
in, _ := m["input"].([]any)
first, _ := in[0].(map[string]any)
if _, ok := m["instructions"]; ok || len(in) != 2 || first["role"] != "developer" {
t.Fatalf("%v", m)
}
if k, _ := m["prompt_cache_key"].(string); k == "" {
t.Fatalf("no prompt_cache_key: %v", m)
}
m = nil
json.Unmarshal(siwcBody([]byte(`{"model":"gpt-6-astra","instructions":"Be brief.","input":"hello"}`)), &m)
if m["instructions"] != "Be brief." {
t.Fatalf("gpt-6-astra: %v", m)
}
}