diff --git a/internal/gateway/codex_developer_cache_test.go b/internal/gateway/codex_developer_cache_test.go
new file mode 100644
index 000000000..58cbde4be
--- /dev/null
+++ b/internal/gateway/codex_developer_cache_test.go
@@ -0,0 +1,108 @@
+package gateway
+
+import (
+ "encoding/json"
+ "net/http/httptest"
+ "regexp"
+ "strconv"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// codexItem is a Responses input message, as Codex sends one.
+func codexItem(role, text string) string {
+ kind := "input_text"
+ if role == "assistant" {
+ kind = "output_text"
+ }
+ return `{"type":"message","role":"` + role + `","content":[{"type":"` + kind + `","text":` + strconv.Quote(text) + `}]}`
+}
+
+// A developer message Codex adds once its conversation has gone on (its
+// context changed: world state, settings) stays where it is: the system
+// prompt, and so Anthropic's cached prefix, is the one the turns before
+// had (#502: the turn after it wrote the whole conversation to the cache).
+func TestCodexLaterDeveloperMessageKeepsTheSystem(t *testing.T) {
+ head := codexItem("developer", "sandboxed") + `,` +
+ codexItem("user", "cwd") + `,` + codexItem("user", "fix the bug")
+ turn1 := `{"model":"claude-opus-5-5","instructions":"You are Codex.","input":[` + head + `]}`
+ turn2 := `{"model":"claude-opus-5-5","instructions":"You are Codex.","input":[` + head + `,` +
+ codexItem("assistant", "done") + `,` + codexItem("developer", "a new skill") + `,` +
+ codexItem("user", "thanks, and the test?") + `]}`
+ r1, err := parseResponses([]byte(turn1))
+ if err != nil {
+ t.Fatal(err)
+ }
+ r2, err := parseResponses([]byte(turn2))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if r1.System != r2.System || !strings.Contains(r1.System, "permissions instructions") {
+ t.Fatalf("system changed:\n%q\n%q", r1.System, r2.System)
+ }
+ var q1, q2 struct {
+ System json.RawMessage
+ Messages []json.RawMessage
+ }
+ json.Unmarshal(buildAnthropic(r1, "claude-opus-5-5"), &q1)
+ json.Unmarshal(buildAnthropic(r2, "claude-opus-5-5"), &q2)
+ if string(q1.System) != string(q2.System) {
+ t.Fatalf("Anthropic's system changed:\n%s\n%s", q1.System, q2.System)
+ }
+ last := r2.Messages[len(r2.Messages)-1]
+ if len(r2.Messages) != 3 || last.Role != "user" || !strings.Contains(text(last.Parts), "\n") ||
+ !strings.HasSuffix(text(last.Parts), "thanks, and the test?") {
+ t.Fatalf("messages: %+v", r2.Messages)
+ }
+ // the user's words are what the router's classifier is asked about
+ if got := userText(r2); got != "thanks, and the test?" {
+ t.Fatalf("user text %q", got)
+ }
+ if turn, within := turnIn(r2); turn != 2 || within {
+ t.Fatalf("turn %d within %v", turn, within)
+ }
+}
+
+// A Codex thread on a Claude subscription goes on in the Claude Code that
+// had its turn before, when a memory request of Codex's (another model,
+// its own instructions, the same session) came in between and Codex then
+// added a developer message to the thread.
+func TestCodexThreadKeepsItsClaudeRunAcrossAMemoryCall(t *testing.T) {
+ fakeClaude(t)
+ s := New()
+ p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "u"}}
+ said := regexp.MustCompile(`pid \d+ turn \d+`)
+ ask := func(body string) string {
+ t.Helper()
+ req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body))
+ req.Header.Set("session_id", "019a-codex-thread")
+ rec := httptest.NewRecorder()
+ var u Usage
+ if code, msg := s.serveClaudeSubscription(rec, req, provider.Responses, p, "claude-sonnet-5", []byte(body), &u); code != 200 {
+ t.Fatalf("%d %s", code, msg)
+ }
+ got := said.FindString(rec.Body.String())
+ if got == "" {
+ t.Fatalf("no answer: %s", rec.Body)
+ }
+ return got
+ }
+ tools := `"tools":[{"type":"function","name":"shell","parameters":{"type":"object"}}]`
+ head := codexItem("developer", "sandboxed") + `,` + codexItem("user", "fix the bug")
+ first := ask(`{"model":"claude-sonnet-5","instructions":"You are Codex.",` + tools + `,"input":[` + head + `]}`)
+ pid, _, _ := strings.Cut(strings.TrimPrefix(first, "pid "), " ")
+
+ // Codex's memory extraction: the same session, a prompt of its own
+ if mem := ask(`{"model":"claude-sonnet-5","instructions":"Extract memories.","input":[` + codexItem("user", "rollout: …") + `]}`); strings.Contains(mem, "pid "+pid+" ") {
+ t.Fatalf("the memory call took the thread's run: %q", mem)
+ }
+
+ second := ask(`{"model":"claude-sonnet-5","instructions":"You are Codex.",` + tools + `,"input":[` + head + `,` +
+ codexItem("assistant", first) + `,` + codexItem("developer", "memories updated") + `,` +
+ codexItem("user", "and the test?") + `]}`)
+ if second != "pid "+pid+" turn 2" {
+ t.Fatalf("the thread's next turn: %q, first %q", second, first)
+ }
+}
diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go
index ee3a20a6c..03461e582 100644
--- a/internal/gateway/responses.go
+++ b/internal/gateway/responses.go
@@ -159,7 +159,17 @@ func parseResponses(body []byte) (*Request, error) {
if err := json.Unmarshal(q.Input, &items); err != nil {
return nil, fmt.Errorf("invalid input: %v", err)
}
+ // replied: the conversation has had a turn answered. Codex adds a
+ // developer message where its context changed (world state, settings,
+ // a model switch) and keeps it there; lifted into the system prompt it
+ // changed the prompt's very start, so the whole conversation was
+ // written to the vendor's cache again (Anthropic's system comes
+ // first; a Claude subscription's waiting run is found by it) (#502)
+ replied := false
for _, it := range items {
+ if it.Role == "assistant" || strings.HasPrefix(it.Type, "function_call") || strings.HasPrefix(it.Type, "tool_search") || it.Type == "reasoning" {
+ replied = true
+ }
switch {
case it.Type == "message" || (it.Type == "" && it.Role != ""):
role := "user"
@@ -167,6 +177,13 @@ func parseResponses(body []byte) (*Request, error) {
role = "assistant"
}
parts := responsesParts(it.Content)
+ if (it.Role == "system" || it.Role == "developer") && replied {
+ // in place, told as the system's, not the user's words
+ if t := text(parts); t != "" {
+ r.Messages = append(r.Messages, Message{Role: "user", Parts: []Part{{Kind: Text, Text: "\n" + t + "\n"}}})
+ }
+ continue
+ }
if it.Role == "system" || it.Role == "developer" {
if t := text(parts); t != "" {
if r.System != "" {