sessions: a Codex thread left magpie keeps no reasoning OpenAI didn't seal
hentulong on X: after routing Codex through WorkBuddy and switching back,
Codex couldn't connect, and a fork said "Persisted response contains
hosted-tool, compaction, or unverifiable hidden reasoning state that
Rustponses cannot replay".
Cause: while Codex is routed through magpie to another vendor, the
reasoning items magpie hands it stay in the thread's rollout. After the
switch back Codex talks to chatgpt.com itself, and OpenAI refuses every
reasoning item it didn't seal. On the owner's real account:
- summary only, no encrypted_content: unsupported_persisted_item_context
(the reporter's Rustponses error) on a previous_response_id
continuation;
- with content (reasoning_text, what Codex 0.165 writes for DeepSeek via
magpie): 400 array_above_max_length "Invalid 'input[5].content'";
- foreign encrypted_content: invalid_encrypted_content.
Codex treats each as fatal, so that thread and its forks fail while new
threads work. No stale config: Unwire/EndFailover drop openai_base_url.
Fix: when Codex is no longer routed or failing over (Unwire, quit,
uninstall and the periodic pass), PortCodexRolloutsLeft rewrites, in
threads begun on openai/magpie, every reasoning item (and every one in a
compacted line's replacement_history) whose encrypted_content isn't an
OpenAI Fernet token ("gAAAA") into type "magpie_reasoning", without id
and encrypted_content. Codex reads an unknown type as Other and never
sends it; magpie's sessions readers still show it. The edit keeps member
order and line length (same fitLine/lock path as the existing port),
has its own state entry, and starts at 2026-09-21. A survey of 150 live
rollouts found 340 of 340 OpenAI reasoning items in the gAAAA shape.
Run (mote, at f1d0c0bfd + this): go vet ./... for darwin, linux and
windows; go build for the three; go test -tags nogui -timeout 45m ./...:
every package ok but internal/gui, whose one red is
TestSettingsSaveKeepsWhatItDoesNotSend (SearchAll), open on main as #4514; gofmt -l clean.
Broken by hand: with the retire step off, and with Unwire passing false,
TestPortCodexRolloutsLeftRetiresUnsealedReasoning,
TestPortCodexRolloutsLeftKeepsItsOwnState and
TestCodexDisconnectRetiresMagpieReasoning go red.
Real Codex 0.165, sandbox CODEX_HOME, owner's account: a turn via magpie
with auto/deepseek-flash, then `codex exec resume` on gpt-6-sol direct
failed with array_above_max_length; after the left port the same resume
answered. The summary-only Rustponses refusal was reproduced with a
websocket probe against chatgpt.com and is retired by the same rule.
Not tried: the reporter's exact case in the interactive TUI (exec
resume doesn't warm up, so it never sends previous_response_id). A
thread Codex holds open keeps its in-memory history until reopened; a
switch not made through Unwire is caught by the periodic pass within
10 minutes.
Rebased after the full run onto 9551fadcb (5174c0970 GUI assets,
9551fadcb gateway/usage; no file here). At the pushed head:
go test -tags nogui -run Codex ./internal/sessions ./internal/agent ok,
go vet ./internal/sessions ./internal/agent ok.
This commit is contained in:
@@ -1485,7 +1485,7 @@ func codexIn(at place) *Agent {
|
||||
// in them written as Codex's own (a distro's Codex locks its
|
||||
// files where this machine's locks can't reach)
|
||||
if at.spell == nil {
|
||||
portCodexHome(dir, codexPortUnwire)
|
||||
portCodexHome(dir, codexPortUnwire, true)
|
||||
}
|
||||
// and are listed where Codex now is
|
||||
bringBack()
|
||||
|
||||
@@ -13,7 +13,10 @@ import (
|
||||
// OpenAI itself, unless its rollout is written portable
|
||||
// (sessions.PortCodexRollouts, LukeMa on X): done while magpie runs, for
|
||||
// the threads no Codex has open, and again as magpie lets Codex go (quit,
|
||||
// Disconnect), for those it has just let go.
|
||||
// Disconnect), for those it has just let go. Once Codex no longer goes
|
||||
// through magpie, magpie's reasoning items are written so too
|
||||
// (sessions.PortCodexRolloutsLeft): OpenAI won't take a thread holding
|
||||
// one as the base of its next turn (hentulong on X).
|
||||
|
||||
// codexPortEvery is how often a running magpie makes the rollouts written
|
||||
// since portable; codexPortAfter how long after it starts it first does.
|
||||
@@ -24,11 +27,15 @@ var codexPortEvery, codexPortAfter = 10 * time.Minute, 2 * time.Minute
|
||||
const codexPortUnwire = 3 * time.Second
|
||||
|
||||
// portCodexHome makes the rollouts of the Codex home dir portable, for
|
||||
// up to d.
|
||||
func portCodexHome(dir string, d time.Duration) {
|
||||
// up to d; left says Codex no longer goes through magpie.
|
||||
func portCodexHome(dir string, d time.Duration, left bool) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), d)
|
||||
defer cancel()
|
||||
res, err := sessions.PortCodexRollouts(ctx, dir)
|
||||
port := sessions.PortCodexRollouts
|
||||
if left {
|
||||
port = sessions.PortCodexRolloutsLeft
|
||||
}
|
||||
res, err := port(ctx, dir)
|
||||
if err != nil {
|
||||
log.Println("codex rollouts:", err)
|
||||
}
|
||||
@@ -41,10 +48,17 @@ func portCodexHome(dir string, d time.Duration) {
|
||||
// to d: as magpie quits.
|
||||
func PortCodexRollouts(d time.Duration) {
|
||||
if home, err := os.UserHomeDir(); err == nil {
|
||||
portCodexHome(here(home).codexHome(), d)
|
||||
portCodexHome(here(home).codexHome(), d, codexLeft(home))
|
||||
}
|
||||
}
|
||||
|
||||
// codexLeft says this machine's Codex doesn't go through magpie: neither
|
||||
// routed to it nor failing over by it.
|
||||
func codexLeft(home string) bool {
|
||||
a := codex(home)
|
||||
return !(a.Routed != nil && a.Routed()) && !(a.FailingOver != nil && a.FailingOver())
|
||||
}
|
||||
|
||||
// KeepCodexRolloutsPortable makes this machine's Codex rollouts portable
|
||||
// every so often until ctx ends.
|
||||
func KeepCodexRolloutsPortable(ctx context.Context) {
|
||||
@@ -62,6 +76,6 @@ func KeepCodexRolloutsPortable(ctx context.Context) {
|
||||
}
|
||||
wait = codexPortEvery
|
||||
// a pass that is cut short goes on from where it was next time
|
||||
portCodexHome(dir, codexPortEvery/2)
|
||||
portCodexHome(dir, codexPortEvery/2, codexLeft(home))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/gateway"
|
||||
)
|
||||
|
||||
// hentulong (X): Codex routed to WorkBuddy through magpie and back on its
|
||||
// own ChatGPT sign-in couldn't go on with the thread ("… unverifiable
|
||||
// hidden reasoning state that Rustponses cannot replay"). Disconnect lets
|
||||
// Codex go to OpenAI itself, so the reasoning items magpie gave it are
|
||||
// written as magpie_reasoning there (sessions.PortCodexRolloutsLeft); a
|
||||
// Codex still going through magpie (codexLeft false) keeps them for the
|
||||
// models it gets their reasoning back.
|
||||
func TestCodexDisconnectRetiresMagpieReasoning(t *testing.T) {
|
||||
base := gateway.URL() + gateway.CodexPath
|
||||
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`,
|
||||
"model = \"gpt-5.5\"\nopenai_base_url = \""+base+"\"\n")
|
||||
if codexLeft(home) {
|
||||
t.Fatalf("Codex through magpie read as let go:\n%s", read())
|
||||
}
|
||||
dir := filepath.Join(home, ".codex")
|
||||
if err := os.MkdirAll(filepath.Join(dir, "thread-writer-locks"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
day := filepath.Join(dir, "sessions", "2026", "10", "30")
|
||||
os.MkdirAll(day, 0o755)
|
||||
path := filepath.Join(day, "rollout-2026-10-30T15-05-20-01a15820-ff81-7840-bb3f-e18771c53aa8.jsonl")
|
||||
line := `{"timestamp":"2026-10-30T07:05:40.551Z","type":"response_item","payload":{"type":"reasoning","id":"rs_18b3f2a9c1d2e3f40000002a","summary":[{"type":"summary_text","text":"先看 go.mod"}],"encrypted_content":null}}` + "\n"
|
||||
if err := os.WriteFile(path, []byte(line), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Hour)
|
||||
os.Chtimes(path, old, old)
|
||||
|
||||
if err := codex(home).Unwire(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !codexLeft(home) {
|
||||
t.Fatalf("Codex after Disconnect still read as through magpie:\n%s", read())
|
||||
}
|
||||
b, _ := os.ReadFile(path)
|
||||
if !strings.Contains(string(b), `"type":"magpie_reasoning"`) || len(b) != len(line) {
|
||||
t.Fatalf("after Disconnect:\n%s", b)
|
||||
}
|
||||
}
|
||||
@@ -454,7 +454,9 @@ func cxAnswerItem(b []byte) bool {
|
||||
return false
|
||||
}
|
||||
switch typeAfter(h, cxPayload) {
|
||||
case "reasoning", "function_call", "custom_tool_call", "web_search_call", "local_shell_call", "tool_search_call", "image_generation_call":
|
||||
// magpie_reasoning: a reasoning item of magpie's, written so as Codex let
|
||||
// magpie go (PortCodexRolloutsLeft)
|
||||
case "reasoning", "magpie_reasoning", "function_call", "custom_tool_call", "web_search_call", "local_shell_call", "tool_search_call", "image_generation_call":
|
||||
return true
|
||||
case "message":
|
||||
return typeAfter(h, cxRole) == "assistant"
|
||||
|
||||
@@ -39,6 +39,30 @@ import (
|
||||
// encrypted_content (claudeseal) is refused the same way; it is written
|
||||
// as its summary, as the gateway sends it to OpenAI.
|
||||
//
|
||||
// A reasoning item magpie gave Codex for a reply another vendor made
|
||||
// (WorkBuddy, DeepSeek, Kimi …) isn't one OpenAI sealed, and once Codex
|
||||
// talks to OpenAI itself every turn of the thread is refused, which
|
||||
// Codex doesn't retry (hentulong on X, after routing Codex to WorkBuddy
|
||||
// and back; each tried on a real ChatGPT account):
|
||||
// - a summary and no encrypted_content: the history is taken as a
|
||||
// warmup, and the turn sent on it with previous_response_id is
|
||||
// refused, 400 unsupported_persisted_item_context, "Persisted response
|
||||
// contains hosted-tool, compaction, or unverifiable hidden reasoning
|
||||
// state that Rustponses cannot replay";
|
||||
// - its reasoning_text in content: 400 array_above_max_length
|
||||
// ("Invalid 'input[5].content': array too long");
|
||||
// - another API's encrypted_content: invalid_encrypted_content.
|
||||
//
|
||||
// Every reasoning item OpenAI didn't seal (its encrypted_content is a
|
||||
// Fernet token, "gAAAA…"), one with Claude's block too, is written as
|
||||
// magpie_reasoning, a type Codex reads as one it doesn't know and never
|
||||
// sends (ResponseItem::Other), its summary and content kept for magpie's
|
||||
// own reading. That is done only as magpie lets Codex go
|
||||
// (PortCodexRolloutsLeft), as through magpie the gateway sends each as
|
||||
// the model it goes to takes it (a DeepSeek or Kimi one gets its
|
||||
// reasoning back from them), and only in a thread begun on OpenAI's or
|
||||
// magpie's provider: one on another provider of the user's own is theirs.
|
||||
//
|
||||
// Only magpie's compaction items and those reasoning items change, in
|
||||
// whichever line holds them (a response_item, a compacted line's
|
||||
// replacement_history); every other line keeps its bytes, and the file
|
||||
@@ -70,6 +94,15 @@ type codexPortHome struct {
|
||||
// own (c3931c91d, 2026-09-25): no rollout last written before then has one.
|
||||
var codexPortFirst = time.Date(2026, 9, 24, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
// codexLeftFirst is the day before magpie's first commit (2026-09-22): no
|
||||
// rollout last written before then has a reasoning item of magpie's.
|
||||
var codexLeftFirst = time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
// codexLeftKey is the state a PortCodexRolloutsLeft pass keeps for a
|
||||
// home, apart from the other passes': one of those, while Codex went
|
||||
// through magpie, left magpie's reasoning items as they were.
|
||||
const codexLeftKey = " (left)"
|
||||
|
||||
// codexPortSlack is how far before a pass began the next one looks again:
|
||||
// a file's time can be coarser than the clock.
|
||||
const codexPortSlack = 2 * time.Minute
|
||||
@@ -89,6 +122,17 @@ type CodexPort struct {
|
||||
// ctx ends. The first pass reads every rollout written since magpie began
|
||||
// to compact; later ones only what changed since.
|
||||
func PortCodexRollouts(ctx context.Context, home string) (CodexPort, error) {
|
||||
return portCodexRollouts(ctx, home, false)
|
||||
}
|
||||
|
||||
// PortCodexRolloutsLeft is PortCodexRollouts for a Codex that no longer
|
||||
// goes through magpie: magpie's reasoning items are written as
|
||||
// magpie_reasoning too, as OpenAI can't take them as a base.
|
||||
func PortCodexRolloutsLeft(ctx context.Context, home string) (CodexPort, error) {
|
||||
return portCodexRollouts(ctx, home, true)
|
||||
}
|
||||
|
||||
func portCodexRollouts(ctx context.Context, home string, left bool) (CodexPort, error) {
|
||||
var res CodexPort
|
||||
locks := filepath.Join(home, "thread-writer-locks")
|
||||
if fi, err := os.Stat(locks); err != nil || !fi.IsDir() {
|
||||
@@ -111,9 +155,12 @@ func PortCodexRollouts(ctx context.Context, home string) (CodexPort, error) {
|
||||
if st.Homes == nil {
|
||||
st.Homes = map[string]codexPortHome{}
|
||||
}
|
||||
key := filepath.Clean(home)
|
||||
key, first := filepath.Clean(home), codexPortFirst
|
||||
if left {
|
||||
key, first = key+codexLeftKey, codexLeftFirst
|
||||
}
|
||||
was := st.Homes[key]
|
||||
since := codexPortFirst
|
||||
since := first
|
||||
if !was.Since.IsZero() {
|
||||
since = was.Since.Add(-codexPortSlack)
|
||||
}
|
||||
@@ -152,7 +199,7 @@ func PortCodexRollouts(ctx context.Context, home string) (CodexPort, error) {
|
||||
pending = append(pending, due[i:]...)
|
||||
break
|
||||
}
|
||||
changed, err := portRollout(ctx, home, p)
|
||||
changed, err := portRollout(ctx, home, p, left)
|
||||
switch {
|
||||
case errors.Is(err, errRolloutHeld):
|
||||
res.Held = append(res.Held, p)
|
||||
@@ -225,11 +272,33 @@ var magpieMark = []byte(codexsummary.Mark)
|
||||
// block (claudeseal), which only magpie reads.
|
||||
var claudeMark = []byte(claudeseal.Mark)
|
||||
|
||||
// reasoningMark is in a line with a reasoning item, as Codex writes it;
|
||||
// openAISealed begins the encrypted_content of one OpenAI sealed (a
|
||||
// Fernet token).
|
||||
var reasoningMark, openAISealed = []byte(`"type":"reasoning"`), []byte(`"encrypted_content":"gAAAA`)
|
||||
|
||||
// unsealedReasoning says a line may hold a reasoning item OpenAI didn't
|
||||
// seal, which a line of OpenAI's own reasoning (most of them) doesn't; a
|
||||
// compacted line, with other items' seals beside, is always looked at.
|
||||
func unsealedReasoning(line []byte) bool {
|
||||
n := bytes.Count(line, reasoningMark)
|
||||
return n > 0 && (n > bytes.Count(line, openAISealed) || bytes.Contains(line[:min(len(line), 256)], []byte(`"type":"compacted"`)))
|
||||
}
|
||||
|
||||
// portRollout writes the rollout at path portable, if it has magpie's
|
||||
// compaction item, and says whether it did.
|
||||
func portRollout(ctx context.Context, home, path string) (bool, error) {
|
||||
// compaction item (or, left, a reasoning item OpenAI didn't seal), and
|
||||
// says whether it did.
|
||||
func portRollout(ctx context.Context, home, path string, left bool) (bool, error) {
|
||||
ours := true // begun on OpenAI's provider or magpie's
|
||||
return editRollout(ctx, home, path, func(line []byte, paged bool) ([]byte, bool) {
|
||||
out, ok := portLine(line)
|
||||
if isMetaLine(line) {
|
||||
switch gjson.GetBytes(line, "payload.model_provider").String() {
|
||||
case "", "openai", "magpie":
|
||||
default:
|
||||
ours = false
|
||||
}
|
||||
}
|
||||
out, ok := portLineAs(line, left && ours)
|
||||
if ok {
|
||||
out, ok = fitLine(line, out, paged)
|
||||
}
|
||||
@@ -520,6 +589,90 @@ func ordinalEnds(path string) map[int64]int64 {
|
||||
}
|
||||
}
|
||||
|
||||
// portLineAs is portLine, with magpie's reasoning items written as
|
||||
// magpie_reasoning first when left.
|
||||
func portLineAs(line []byte, left bool) ([]byte, bool) {
|
||||
retired := false
|
||||
if left && unsealedReasoning(line) {
|
||||
line, retired = retireReasoning(line)
|
||||
}
|
||||
out, ok := portLine(line)
|
||||
return out, ok || retired
|
||||
}
|
||||
|
||||
// retireReasoning is a rollout line with each reasoning item in it that
|
||||
// OpenAI didn't seal (a response_item's, those in a compacted line's
|
||||
// replacement_history) written as magpie_reasoning: a type Codex reads
|
||||
// as one it doesn't know (ResponseItem::Other) and never sends. The item
|
||||
// keeps its summary, content and keys' order, without its id and
|
||||
// encrypted_content, so the line is no longer; the rest of the line keeps
|
||||
// its bytes, as magpie's own readers read a line's head by them.
|
||||
func retireReasoning(line []byte) ([]byte, bool) {
|
||||
body := bytes.TrimRight(line, "\r\n")
|
||||
if !gjson.ValidBytes(body) {
|
||||
return line, false
|
||||
}
|
||||
var at []gjson.Result // items, in the order they are in body, with their place
|
||||
switch gjson.GetBytes(body, "type").String() {
|
||||
case "response_item":
|
||||
at = append(at, gjson.GetBytes(body, "payload"))
|
||||
case "compacted":
|
||||
h := gjson.GetBytes(body, "payload.replacement_history")
|
||||
if !h.IsArray() || h.Index <= 0 {
|
||||
break
|
||||
}
|
||||
from := 0
|
||||
h.ForEach(func(_, v gjson.Result) bool {
|
||||
i := strings.Index(h.Raw[from:], v.Raw)
|
||||
if i < 0 {
|
||||
return false
|
||||
}
|
||||
v.Index = h.Index + from + i
|
||||
from += i + len(v.Raw)
|
||||
at = append(at, v)
|
||||
return true
|
||||
})
|
||||
}
|
||||
var out []byte
|
||||
done := 0
|
||||
for _, it := range at {
|
||||
if it.Index <= 0 || !it.IsObject() || it.Get("type").String() != "reasoning" {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(it.Get("encrypted_content").String(), "gAAAA") {
|
||||
continue // OpenAI's own
|
||||
}
|
||||
var obj []byte
|
||||
it.ForEach(func(k, v gjson.Result) bool {
|
||||
switch k.String() {
|
||||
case "id", "encrypted_content":
|
||||
return true
|
||||
}
|
||||
if len(obj) > 0 {
|
||||
obj = append(obj, ',')
|
||||
}
|
||||
obj = append(obj, k.Raw...)
|
||||
obj = append(obj, ':')
|
||||
if k.String() == "type" {
|
||||
obj = append(obj, `"magpie_reasoning"`...)
|
||||
} else {
|
||||
obj = append(obj, v.Raw...)
|
||||
}
|
||||
return true
|
||||
})
|
||||
out = append(out, body[done:it.Index]...)
|
||||
out = append(out, '{')
|
||||
out = append(out, obj...)
|
||||
out = append(out, '}')
|
||||
done = it.Index + len(it.Raw)
|
||||
}
|
||||
if out == nil {
|
||||
return line, false
|
||||
}
|
||||
out = append(out, line[done:]...)
|
||||
return out, true
|
||||
}
|
||||
|
||||
// portLine is a rollout line with each of magpie's compaction items in it
|
||||
// written as the message it stands for; any other line as it is, byte for
|
||||
// byte. One of magpie's items whose summary can't be read is left out of
|
||||
|
||||
@@ -415,3 +415,130 @@ func TestPortCodexRolloutsRepairsForks(t *testing.T) {
|
||||
t.Fatalf("second pass: %+v, %v", res, err)
|
||||
}
|
||||
}
|
||||
|
||||
// hentulong (X): Codex routed through magpie to WorkBuddy, then back to
|
||||
// its own ChatGPT sign-in, couldn't go on with the thread or a fork of it:
|
||||
//
|
||||
// {"type":"error","error":{"message":"Persisted response contains hosted-tool, compaction, or unverifiable hidden reasoning state that Rustponses cannot replay. Start a new response or use the Python Responses service for this continuation.","type":"invalid_request_error","param":"previous_response_id","code":"unsupported_persisted_item_context"},"status":400}
|
||||
//
|
||||
// The thread holds the reasoning items magpie gave Codex for WorkBuddy's
|
||||
// translated replies: a summary, magpie's id, encrypted_content null, as
|
||||
// Codex writes them. Codex's websocket sends the thread as a warmup and
|
||||
// the turn with previous_response_id; OpenAI takes the warmup and refuses
|
||||
// the turn with that error (tried on a real ChatGPT account, gpt-6-sol;
|
||||
// the same history without the item goes on). Line 6 is the one a real
|
||||
// Codex 0.165 wrote for auto/deepseek-flash through magpie, which, on
|
||||
// Codex's own sign-in after, was refused with array_above_max_length
|
||||
// ("Invalid 'input[5].content': array too long"). A pass made as magpie
|
||||
// lets Codex go writes each reasoning item OpenAI didn't seal as
|
||||
// magpie_reasoning, a type Codex reads as Other and never sends, in a
|
||||
// response_item and in replacement_history (its metadata kept beside
|
||||
// it), each line its length; OpenAI's own sealed reasoning and every
|
||||
// other line keep their bytes. A pass while Codex goes through magpie
|
||||
// leaves them: there a DeepSeek or Kimi model gets its reasoning back
|
||||
// from them. A thread begun on a provider of the user's own is left be.
|
||||
func TestPortCodexRolloutsLeftRetiresUnsealedReasoning(t *testing.T) {
|
||||
sealed := claudeseal.Seal([]byte(`{"type":"thinking","thinking":"hm","signature":"sig"}`))
|
||||
lines := []string{
|
||||
`{"timestamp":"2026-10-30T07:05:20.118Z","type":"session_meta","payload":{"id":"` + portThread + `","timestamp":"2026-10-30T07:05:20.101Z","cwd":"/Users/me/w","originator":"codex_cli_rs","cli_version":"0.170.0","source":"cli","model_provider":"openai","history_mode":"paginated"}}`,
|
||||
`{"timestamp":"2026-10-30T07:05:31.002Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"修一下构建 \"type\":\"reasoning\""}]}}`,
|
||||
`{"timestamp":"2026-10-30T07:05:40.551Z","type":"response_item","payload":{"type":"reasoning","id":"rs_18b3f2a9c1d2e3f40000002a","summary":[{"type":"summary_text","text":"先看 go.mod,再跑测试。"}],"encrypted_content":null}}`,
|
||||
`{"timestamp":"2026-10-30T07:05:41.000Z","type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"好了"}]}}`,
|
||||
`{"timestamp":"2026-10-30T07:40:02.553Z","type":"compacted","payload":{"message":"","replacement_history":[{"type":"message","role":"user","content":[{"type":"input_text","text":"修一下构建"}]},{"type":"reasoning","id":"rs_18b3f2a9c1d2e3f40000002b","summary":[],"encrypted_content":null},{"type":"reasoning","id":"rs_0a1b2c3d4e5f60718293a4b5c6d7e8f9","summary":[],"encrypted_content":"gAAAAABo_OpenAIsOwn=="}],"replacement_history_metadata":[{"client_authored":true},{},{}]}}`,
|
||||
`{"timestamp":"2026-10-30T08:00:00.000Z","type":"response_item","payload":{"type":"reasoning","id":"rs_0a1b2c3d4e5f60718293a4b5c6d7e8f9","summary":[{"type":"summary_text","text":"x"}],"encrypted_content":"gAAAAABo_OpenAIsOwn=="}}`,
|
||||
`{"timestamp":"2026-10-31T09:26:38.975Z","ordinal":12,"type":"response_item","payload":{"type":"reasoning","id":"f353ba26-5235-430b-87ee-76a8ad936ef2","summary":[],"content":[{"type":"reasoning_text","text":"We need answer just number. 17*23=391."}],"encrypted_content":"f1ec9b47-8fb3-4f29-8115-90aa9bb862b0-0","internal_chat_message_metadata_passthrough":{"turn_id":"01a19148-3e68-7bc2-82be-0c4681743dab"}}}`,
|
||||
`{"timestamp":"2026-10-30T08:00:02.000Z","type":"response_item","payload":{"type":"reasoning","id":"rs_18b3f2a9c1d2e3f40000002c","summary":[],"encrypted_content":"` + sealed + `"}}`,
|
||||
}
|
||||
for _, zst := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "plain", true: "zst"}[zst], func(t *testing.T) {
|
||||
home, path := portHome(t, lines, zst)
|
||||
ends := lineEnds(t, path)
|
||||
|
||||
// through magpie: magpie's unsealed reasoning stays; Claude's
|
||||
// block goes as before
|
||||
if _, err := PortCodexRollouts(context.Background(), home); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := readRollout(t, path)
|
||||
if got[2] != lines[2] || got[4] != lines[4] {
|
||||
t.Fatalf("written while Codex goes through magpie:\n%s\n%s", got[2], got[4])
|
||||
}
|
||||
|
||||
res, err := PortCodexRolloutsLeft(context.Background(), home)
|
||||
if err != nil || !res.Done || len(res.Rewritten) != 1 {
|
||||
t.Fatalf("left pass: %+v, %v", res, err)
|
||||
}
|
||||
got = readRollout(t, path)
|
||||
if len(got) != len(lines) || !reflect.DeepEqual(lineEnds(t, path), ends) {
|
||||
t.Fatalf("lines moved: %v, were %v", lineEnds(t, path), ends)
|
||||
}
|
||||
for _, i := range []int{0, 1, 3, 5} {
|
||||
if got[i] != lines[i] {
|
||||
t.Errorf("line %d changed:\n%s\nwas\n%s", i, got[i], lines[i])
|
||||
}
|
||||
}
|
||||
p2 := decode(t, got[2])["payload"].(map[string]any)
|
||||
if p2["type"] != "magpie_reasoning" || p2["encrypted_content"] != nil || p2["id"] != nil ||
|
||||
!reflect.DeepEqual(p2["summary"], []any{map[string]any{"type": "summary_text", "text": "先看 go.mod,再跑测试。"}}) {
|
||||
t.Errorf("magpie's reasoning item: %s", got[2])
|
||||
}
|
||||
p4 := decode(t, got[4])["payload"].(map[string]any)
|
||||
hist := p4["replacement_history"].([]any)
|
||||
if len(hist) != 3 || hist[1].(map[string]any)["type"] != "magpie_reasoning" ||
|
||||
hist[2].(map[string]any)["type"] != "reasoning" || hist[2].(map[string]any)["encrypted_content"] != "gAAAAABo_OpenAIsOwn==" {
|
||||
t.Errorf("replacement_history %v", hist)
|
||||
}
|
||||
if m := p4["replacement_history_metadata"].([]any); len(m) != 3 {
|
||||
t.Errorf("metadata %v", m)
|
||||
}
|
||||
if p7 := decode(t, got[7])["payload"].(map[string]any); p7["type"] != "magpie_reasoning" || strings.Contains(got[7], claudeseal.Mark) {
|
||||
t.Errorf("Claude's block: %s", got[7])
|
||||
}
|
||||
if !strings.HasPrefix(got[6], `{"timestamp":"2026-10-31T09:26:38.975Z","ordinal":12,"type":"response_item","payload":{"type":"magpie_reasoning","summary":[],"content":[{"type":"reasoning_text","text":"We need answer just number. 17*23=391."}],"internal_chat_message_metadata_passthrough":{"turn_id":"01a19148-3e68-7bc2-82be-0c4681743dab"}}} `) {
|
||||
t.Errorf("another API's reasoning: %s", got[6])
|
||||
}
|
||||
// nothing OpenAI can't take as a base is left
|
||||
for i, l := range got {
|
||||
if i != 1 && strings.Contains(l, `"type":"reasoning"`) && !strings.Contains(l, "gAAAA") {
|
||||
t.Errorf("line %d still has magpie's reasoning: %s", i, l)
|
||||
}
|
||||
}
|
||||
// magpie still reads it as the model's output
|
||||
if !cxAnswerItem([]byte(got[2])) {
|
||||
t.Error("cxAnswerItem doesn't take magpie_reasoning")
|
||||
}
|
||||
if res, err := PortCodexRolloutsLeft(context.Background(), home); err != nil || len(res.Rewritten) != 0 {
|
||||
t.Errorf("second left pass: %+v, %v", res, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run("own provider", func(t *testing.T) {
|
||||
own := append([]string{strings.Replace(lines[0], `"model_provider":"openai"`, `"model_provider":"openrouter"`, 1)}, lines[1:7]...)
|
||||
home, path := portHome(t, own, false)
|
||||
orig, _ := os.ReadFile(path)
|
||||
if res, err := PortCodexRolloutsLeft(context.Background(), home); err != nil || len(res.Rewritten) != 0 {
|
||||
t.Fatalf("left pass: %+v, %v", res, err)
|
||||
}
|
||||
if b, _ := os.ReadFile(path); !bytes.Equal(b, orig) {
|
||||
t.Fatal("a thread on the user's own provider was written")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A pass through magpie that ran first doesn't keep a left pass from
|
||||
// looking at what it looked at: each keeps its own state.
|
||||
func TestPortCodexRolloutsLeftKeepsItsOwnState(t *testing.T) {
|
||||
line := `{"timestamp":"2026-10-30T07:05:40.551Z","type":"response_item","payload":{"type":"reasoning","id":"rs_18b3f2a9c1d2e3f40000002a","summary":[],"encrypted_content":null}}`
|
||||
home, path := portHome(t, []string{line}, false)
|
||||
for i := 0; i < 2; i++ {
|
||||
if _, err := PortCodexRollouts(context.Background(), home); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if res, err := PortCodexRolloutsLeft(context.Background(), home); err != nil || len(res.Rewritten) != 1 {
|
||||
t.Fatalf("left pass after two others: %+v, %v", res, err)
|
||||
}
|
||||
if got := readRollout(t, path); !strings.Contains(got[0], `"magpie_reasoning"`) {
|
||||
t.Fatalf("not written: %s", got[0])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,7 +354,7 @@ func codexContentOf(c Call, keep func([]byte) bool, add func(input bool, p Part)
|
||||
case "assistant":
|
||||
return add(false, textPart("assistant", words()))
|
||||
}
|
||||
case "reasoning":
|
||||
case "reasoning", "magpie_reasoning": // magpie_reasoning: PortCodexRolloutsLeft
|
||||
var s []string
|
||||
for _, x := range p.Summary {
|
||||
s = append(s, x.Text)
|
||||
|
||||
Reference in New Issue
Block a user