gateway: a Codex conversation's prefix is sent the same on every request

@m0eaki on X: LobeHub's Codex 0.169 on magpie, the prompt cache holding
only through the base instructions, the skills_instructions message
differing from turn to turn. Codex 0.169 puts its skills, permissions,
deferred tools and <model_catalog> in one developer message, and magpie
writes that message again when it widens the catalog with the models a
subagent may be put on (spawnAgentModels), so it is the one place magpie
could change it.

TestCodexPrefixSameOnEveryRequest posts Codex 0.169.0's request with
LobeHub's app-server headers through the whole handler: a turn's request
twice, the next turn, after a restart and 20 times over on fresh servers,
with two providers and a routing group in the catalog, and checks every
input item up to the new ones is the same bytes upstream. It passes:
magpie's widening is already stable, and magpie never writes or reorders
<skills_instructions> itself. Nothing in magpie is changed.

What does vary is Codex's own: run in a sandbox CODEX_HOME
(workspace-write), Codex 0.169.0 writes the working folder into the same
message's writable roots, so a host that starts turns in different
folders changes it. Codex's skills block itself was the same bytes across
fresh processes (0.165.0, 0.169.0).

Broken by hand: shuffling spawnChoices' list turns the test red at "the
same request again: input[1] differs".

Run (mica, mica-run):
- go test -count=1 -timeout 45m ./... at 3869aee3b with this commit: 99
  packages ok, 5 red, none in code this commit touches:
  - internal/agent TestWSLAgentFolderIsALink (#7150)
  - internal/dirent watch tests, five of them (#7031)
  - internal/gateway TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes
    (#7117, red on origin/main)
  - internal/gui TestGatewayModeListsNoOwnSessions (#7064)
  - internal/sessions timed out at 45m (2700.7s), and again run alone
    (2701.5s, TestStatsMidnight at the deadline); noted on #5451
- rebased on origin/main ef997d186: go vet
  ./internal/gateway ok; go test -count=1 -timeout 45m ./internal/gateway
  (1172s): only #7117 red, TestCodexPrefixSameOnEveryRequest and the
  TestSpawnAgent* tests pass.
- rebased again on origin/main ddc78405c (this push's parent, two
  gateway commits on): TestCodexPrefixSameOnEveryRequest and
  TestSpawnAgent* ok.
- gofmt -l over every tracked .go file: clean.

Not tried: a real LobeHub conversation through magpie. The reporter's two
captured item-2 texts would show which line changes.
This commit is contained in:
yetone
2026-11-19 14:21:43 +08:00
parent ddc78405cf
commit 593ea437d8
@@ -0,0 +1,164 @@
package gateway
import (
"bytes"
"encoding/json"
"fmt"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// upItems is a request's input, each item as the upstream got it.
func upItems(t *testing.T, body []byte) []json.RawMessage {
t.Helper()
var q struct {
Input []json.RawMessage `json:"input"`
}
if err := json.Unmarshal(body, &q); err != nil {
t.Fatalf("%v: %.300s", err, body)
}
return q.Input
}
// upText is what the model reads of an input item: its parts' text.
func upText(t *testing.T, item json.RawMessage) string {
t.Helper()
var it struct {
Content []struct {
Text string `json:"text"`
} `json:"content"`
}
json.Unmarshal(item, &it)
var b strings.Builder
for _, c := range it.Content {
b.WriteString(c.Text)
b.WriteString("\x00")
}
return b.String()
}
// A Codex conversation's prefix is the same on every request magpie
// sends of it, so the upstream's prompt cache holds past it (@m0eaki on
// X: LobeHub's Codex 0.169 on magpie, its cache holding only through the
// base instructions). Codex 0.169 puts its skills, permissions, deferred
// tools and, with features.model_catalog_in_context, its <model_catalog>
// in one developer message; magpie widens that catalog with the models a
// subagent may be put on (spawnAgentModels), and so writes the message
// again on every request. It must come out the same on each: a turn's
// first request and its next, the turn after (the conversation grown), and
// after a restart. The catalog has two providers and a routing group,
// whose order has to hold from request to request. What Codex itself
// writes there is its own: the same message names the sandbox's writable
// roots, the working folder among them (Codex 0.169.0 in a sandbox
// CODEX_HOME, workspace-write, two folders), and the skill roots, so a
// host that starts each turn in another folder changes it on Codex's side.
func TestCodexPrefixSameOnEveryRequest(t *testing.T) {
var models []string
for i := 1; i <= 12; i++ {
models = append(models, fmt.Sprintf("m%d", i))
}
f := &fake{t: t, reply: sse(`data: {"type":"response.created","response":{"id":"r1"}}`,
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":9,"output_tokens":2}}}`)}
up := setup(t, provider.Responses, f)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Responses: up.URL + "/v1", Models: models}); err != nil {
t.Fatal(err)
}
if err := provider.Save(provider.Provider{ID: "other", Name: "Other", Key: "k", Responses: up.URL + "/v1", Models: []string{"z9", "a1", "k5"}}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{Name: "Both", Members: []string{"fake/m1", "other/a1"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
// what Codex 0.169.0 sends under LobeHub's app-server
head := map[string]string{
"User-Agent": "lobehub-desktop/0.169.0 (Mac OS 26.5.0; arm64) unknown (lobehub-desktop; 2.1.40)",
"originator": "lobehub-desktop",
"session_id": "019e1a2b-3c4d-7e5f-8a9b-0c1d2e3f4a5b",
"x-codex-window-id": "019e1a2b-3c4d-7e5f-8a9b-0c1d2e3f4a5b:0",
}
post := func(t *testing.T, s *Server, body []byte) []byte {
t.Helper()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/responses", bytes.NewReader(body))
for k, v := range head {
req.Header.Set(k, v)
}
s.Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
return bytes.Clone(f.got)
}
b, err := os.ReadFile("testdata/workdir/codex-0.169.0-model-catalog.json")
if err != nil {
t.Fatal(err)
}
turn1 := withModel(b, "fake/m1")
// the next turn: the conversation so far, Codex's answer and the
// user's next words after it
q, err := rawObject(turn1)
if err != nil {
t.Fatal(err)
}
var in []json.RawMessage
if err := json.Unmarshal(q["input"], &in); err != nil {
t.Fatal(err)
}
in = append(in, json.RawMessage(codexItem("assistant", "Spawned it.")), json.RawMessage(codexItem("user", "and again")))
q["input"], _ = json.Marshal(in)
turn2, err := json.Marshal(q)
if err != nil {
t.Fatal(err)
}
s := New()
first := post(t, s, turn1)
items := upItems(t, first)
// the developer message with the skills and the model catalog
at := -1
for i, it := range items {
if strings.HasPrefix(upText(t, it), "<skills_instructions>") {
at = i
break
}
}
if at < 0 {
t.Fatalf("no skills message upstream: %.500s", first)
}
if txt := upText(t, items[at]); !strings.Contains(txt, spawnMore) || !strings.Contains(txt, "- `fake/m12`") || !strings.Contains(txt, "- `other/z9`") {
t.Fatalf("the catalog isn't widened, so this tests nothing:\n%s", txt)
}
same := func(t *testing.T, name string, got []byte) {
t.Helper()
gi := upItems(t, got)
if len(gi) < len(items) {
t.Fatalf("%s: %d items, the first request had %d", name, len(gi), len(items))
}
for i := range items {
if !bytes.Equal(gi[i], items[i]) {
t.Fatalf("%s: input[%d] differs from the first request's:\n%s\n---\n%s", name, i, items[i], gi[i])
}
}
}
// the same request again, as Codex sends a turn's next request
if again := post(t, s, turn1); !bytes.Equal(again, first) {
same(t, "the same request again", again)
t.Fatalf("the same request went differently:\n%s\n---\n%s", first, again)
}
// the next turn: what was sent before is sent the same, byte for byte
same(t, "the next turn", post(t, s, turn2))
// after a restart
if again := post(t, New(), turn1); !bytes.Equal(again, first) {
same(t, "after a restart", again)
t.Fatalf("after a restart the request went differently:\n%s\n---\n%s", first, again)
}
// and many times over: an order taken from a map would show here
for i := range 20 {
same(t, fmt.Sprintf("request %d", i), post(t, New(), turn2))
}
}