Files
magpie/internal/gateway/codex_resume_test.go
T
yetone 25bf394a72 gateway: a resumed Codex subagent goes back on the model it was spawned on
HoGee_xxl on X: a Codex subagent brought back with resume_agent ran on
the lead's model, not the one the lead spawned it on.

Codex's multi-agent V1 (the one magpie's own models ask for) builds the
resumed subagent's config from the lead's current turn:
codex-rs core/src/agent/child_config.rs build_agent_resume_config ->
build_agent_shared_config sets config.model to turn.model_info().slug,
and core/src/agent/control/spawn.rs resume_single_agent_from_rollout
starts the thread on that config without reading back the thread's
stored model. Neither the spawn_agent model nor [agents]
default_subagent_model is asked again. V2's ensure_v2_agent_loaded does
put a thread back on its stored model, so this follows Codex's own rule.

The gateway now notes the model each Codex subagent thread (thread_id in
x-codex-turn-metadata / client_metadata; session_id only on a Codex old
enough not to share it) first asked for, and puts a later request of
that thread asking for another back on it. Kept in codex-subagents.json
beside providers.json (0600, temp file and rename, the 512 latest, 30
days), so a resume after a restart is known. A model set for every
subagent (codexSubagentPick) still wins. No prompt or base_instructions
is touched.

Run (mote-run): go test ./internal/gateway -run 'TestCodexResumed|
TestCodexSubagentThreadOf|TestCodexSubagent|TestCodexFollow|
SessionMetadata|TestCodexSide' -count=1: ok.
Broken by hand: skipping the restore in codex_backend.go turns
TestCodexResumedSubagentKeepsItsSpawnModel red ("worker-1 asking gpt-5.5
went on gpt-5.5, want gpt-6-astra"); skipping the save turns its
after-restart step red.
Not tried: a real Codex resume_agent run against a real ChatGPT account.
2026-10-29 03:45:57 +08:00

101 lines
4.2 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
const workerAsk = `{"type":"message","role":"user","content":[{"type":"input_text","text":"go on"}]}`
// codexThreadAsks is a request of Codex's as a recent Codex sends it: the
// session_id shared by the root thread and every subagent under it, the
// thread's own id in its turn metadata; a subagent's when parent is set.
func codexThreadAsks(s *Server, model, thread, parent string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
body := `{"model":"` + model + `","stream":true,"input":[` + workerAsk + `]}`
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer chatgpt-token")
req.Header.Set("chatgpt-account-id", "acct-1")
req.Header.Set("User-Agent", "codex_cli_rs/0.162.1 (Mac OS 26.2.0; arm64)")
req.Header.Set("session_id", "root-1")
meta := `{"session_id":"root-1","thread_id":"` + thread + `"`
if parent != "" {
req.Header.Set("x-openai-subagent", "collab_spawn")
req.Header.Set("x-codex-parent-thread-id", parent)
meta += `,"parent_thread_id":"` + parent + `"`
}
req.Header.Set("x-codex-turn-metadata", meta+"}")
s.Handler().ServeHTTP(rec, req)
return rec
}
// HoGee_xxl on X: Codex's resume_agent builds the resumed subagent's
// config from its lead's turn (codex-rs core/src/agent/child_config.rs,
// build_agent_resume_config), so a subagent spawned on gpt-6-astra came
// back on the lead's gpt-5.5. It goes back on gpt-6-astra; another
// subagent, and the lead, go on as asked. Remembered across a restart.
func TestCodexResumedSubagentKeepsItsSpawnModel(t *testing.T) {
up := codexSubagentSetup(t)
s := New()
steps := []struct{ model, thread, parent, want string }{
{"gpt-5.5", "root-1", "", "gpt-5.5"}, // the lead
{"gpt-6-astra", "worker-1", "root-1", "gpt-6-astra"}, // spawned on astra
{"gpt-5.5", "worker-2", "root-1", "gpt-5.5"}, // spawned on the lead's
{"gpt-5.5", "worker-1", "root-1", "gpt-6-astra"}, // resumed on the lead's
{"gpt-5.5", "root-1", "", "gpt-5.5"}, // the lead's own, untouched
}
for i, st := range steps {
if rec := codexThreadAsks(s, st.model, st.thread, st.parent); rec.Code != 200 {
t.Fatalf("step %d: %d %s", i, rec.Code, rec.Body.String())
}
if got := up.asked(); got[len(got)-1] != st.want {
t.Fatalf("step %d (%s asking %s) went on %s, want %s", i, st.thread, st.model, got[len(got)-1], st.want)
}
}
// a magpie started afresh still knows worker-1
if rec := codexThreadAsks(New(), "gpt-5.5", "worker-1", "root-1"); rec.Code != 200 {
t.Fatalf("after a restart: %d %s", rec.Code, rec.Body.String())
}
if got := up.asked(); got[len(got)-1] != "gpt-6-astra" {
t.Fatalf("after a restart worker-1 went on %s", got[len(got)-1])
}
}
// A model set for every subagent still holds a resumed one to it.
func TestCodexResumedSubagentHeldToTheModelSet(t *testing.T) {
up := codexSubagentSetup(t)
s := New()
codexThreadAsks(s, "gpt-5.5", "worker-1", "root-1")
if err := provider.SetCodexSubagentModel("codex/gpt-6-astra"); err != nil {
t.Fatal(err)
}
if rec := codexThreadAsks(s, "gpt-5.5", "worker-1", "root-1"); rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
if got := up.asked(); got[len(got)-1] != "gpt-6-astra" {
t.Fatalf("held subagent went on %s", got[len(got)-1])
}
}
// A Codex from before session_id was shared names a subagent's thread by
// its session_id; a recent one whose metadata lacks thread_id shares the
// lead's session_id, and is left as asked rather than tied to the lead.
func TestCodexSubagentThreadOf(t *testing.T) {
h := httptest.NewRequest("POST", "/", nil).Header
h.Set("session_id", "worker-9")
h.Set("x-codex-parent-thread-id", "lead-9")
if got := codexSubagentThread(h, sessionMetadata{}); got != "worker-9" {
t.Fatalf("older Codex: %q", got)
}
h.Set("session_id", "lead-9")
if got := codexSubagentThread(h, sessionMetadata{}); got != "" {
t.Fatalf("shared session id taken as the thread: %q", got)
}
if got := codexSubagentThread(h, sessionMetadata{Thread: "worker-10"}); got != "worker-10" {
t.Fatalf("thread_id: %q", got)
}
}