When magpie answers Codex's compaction itself (a magpie model, Codex's
own models through magpie's accounts, a CC Switch table), the item it
returns carries encrypted_content "magpie1:"+base64(summary). Codex keeps
it in the rollout (a response_item, and the compacted checkpoint's
replacement_history) and replays it with every later turn. Sent to the
ChatGPT backend directly, it is refused: "The encrypted content for item
cmp_magpie_1 could not be verified ... invalid_encrypted_content"
(probed with a real request on gpt-5.6-luna; the same summary as a user
message was answered).
Codex's remote compaction v2 keeps exactly one Compaction item, so magpie
can't answer with a plain message instead. sessions.PortCodexRollouts
writes each of magpie's items in a rollout as the message it stands for
(codexsummary.Message: Codex's summary prefix, a newline and the summary,
as a user message), which is what Codex's own local compaction stores and
what the gateway already sends in its place, so it reads the same through
magpie and goes on without it.
- Only magpie's compaction items change; other lines keep their bytes,
the file its line count, mode and time. An unreadable one is left out
of a list with its replacement_history_metadata entry, as Codex's own
ghost-snapshot strip does.
- Published as Codex publishes a compressed rollout: temp file and
rename, under thread-writer-locks/.coordination.lock and only with the
thread's lock free. A thread a Codex has open is left for a later pass.
A Codex home without thread-writer-locks is left alone.
- .jsonl.zst rollouts are read and written packed.
- Passes: 2 minutes after the GUI or magpie serve starts and every 10
minutes; on Codex Disconnect (3s); as the GUI quits (3s). The first
pass reads rollouts written since 2026-09-24; later ones only what
changed since the last whole pass, plus what was left held
(codex-portable.json in magpie's folder).
Run at this series' head (on origin/main 0fe039f14):
- Real codex 0.162.1, sandbox HOME/CODEX_HOME, exec </dev/null, against a
fake upstream. In magpie mode it auto-compacted and the rollout's
compacted line held magpie1:. In OpenAI mode, `exec resume --last`
failed with "Error running remote compact task: ...
invalid_encrypted_content". After PortCodexRollouts on that home, only
that line changed (42 lines before and after). The same resume exited
0, and the upstream saw the summary message.
- Broken by hand. With the thread-lock TryLock check off,
TestPortCodexRolloutsLeavesAnOpenThread went red. With the replacement
off, TestPortCodexRollouts went red. Both green with the fix restored.
- gofmt -l clean. go vet ./... (darwin) clean, GOOS=windows go vet
./internal/... clean, and GOOS=linux go vet -tags nogui ./... clean.
- go test ./... -count=1 -timeout 45m: 64 packages ok, two reds.
- internal/agent ran out of time at 2706s (load average 25 to 57), the
known #2723/#2544. Alone, go test ./internal/agent -count=1 is ok in
901s.
- internal/provider TestHoldReadsMigrationsOnce: "migrations.json read
again while held" at 2.70s. It passes alone 5 of 5. I didn't touch
that package. Filed #3095: heldFor (1s) can run out between the
test's two reads.
Not tried:
- api.openai.com itself after a port. The refusal was probed on the
ChatGPT backend, and the resume ran against a fake.
- A Codex in a WSL distro. Disconnect skips it, because its locks can't
be taken from Windows.
- The Codex app still holding a thread when magpie quits. Its lock is
held, so the file is left for the next pass, and the thread in that
app's memory keeps the magpie1 item until it is loaded again.
68 lines
2.0 KiB
Go
68 lines
2.0 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// A Codex conversation magpie compacted can't go on once Codex talks to
|
|
// 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.
|
|
|
|
// codexPortEvery is how often a running magpie makes the rollouts written
|
|
// since portable; codexPortAfter how long after it starts it first does.
|
|
var codexPortEvery, codexPortAfter = 10 * time.Minute, 2 * time.Minute
|
|
|
|
// codexPortUnwire is how long Disconnect spends on it; what is left is
|
|
// done by the next pass.
|
|
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) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), d)
|
|
defer cancel()
|
|
res, err := sessions.PortCodexRollouts(ctx, dir)
|
|
if err != nil {
|
|
log.Println("codex rollouts:", err)
|
|
}
|
|
if len(res.Rewritten) > 0 || len(res.Held) > 0 {
|
|
log.Printf("codex rollouts: %d written portable, %d left while a Codex has them open", len(res.Rewritten), len(res.Held))
|
|
}
|
|
}
|
|
|
|
// PortCodexRollouts makes this machine's Codex rollouts portable, for up
|
|
// to d: as magpie quits.
|
|
func PortCodexRollouts(d time.Duration) {
|
|
if home, err := os.UserHomeDir(); err == nil {
|
|
portCodexHome(here(home).codexHome(), d)
|
|
}
|
|
}
|
|
|
|
// KeepCodexRolloutsPortable makes this machine's Codex rollouts portable
|
|
// every so often until ctx ends.
|
|
func KeepCodexRolloutsPortable(ctx context.Context) {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return
|
|
}
|
|
dir := here(home).codexHome()
|
|
wait := codexPortAfter
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-time.After(wait):
|
|
}
|
|
wait = codexPortEvery
|
|
// a pass that is cut short goes on from where it was next time
|
|
portCodexHome(dir, codexPortEvery/2)
|
|
}
|
|
}
|