#3751's second half; 67b12956 fixed the half about lengths. A Codex
thread moved to another provider, by the Sessions page's Move
(MoveCodexProvider, #887) or as Codex steps off magpie (bringBack's
MoveCodexThreads), has each of its rollouts written whole into a temp
file that then takes its place. The move took no Codex lock and left
alone only a thread written to in the last minute. A Codex that resumed
the thread (the app-server, as run below; a resumed TUI too, by Codex's
source) keeps its rollout open to append to for as long as it has the
thread, so what it wrote after a move went into the file the rename had
put aside: a turn the upstream answered was in neither the rollout nor
what Codex sent after its next resume.
moveCodexPaths now holds the thread as Codex holds one it writes to,
archives or deletes (rollout/src/writer_lock.rs, acquire; thread-store
holds it through a whole archive, unarchive or delete): under
thread-writer-locks/.coordination.lock it makes and locks
thread-writer-locks/<thread>.lock, then lets the coordination lock go
and keeps the thread's lock from before a file is copied to the trash
until the thread's row in the state database has moved. Letting go takes
the lock file away under the coordination lock, as Codex's guard does. A
Codex that resumes the thread meanwhile is turned away by Codex itself
("already has an active writer"), so the files and the row move together
and a rollback is written under no Codex. A thread a Codex has open is
left as it was: its files, their time and its row, with no copy kept.
Its error, "a Codex has the session open", is ErrActive too, so it goes
where a thread written to in the last minute goes: MoveCodexThreads
leaves it for its next call, and the Sessions page's reply marks it
active, so the page says "{title} is still being written to; close it in
Codex and try again in a minute" ("{n} sessions ..." for several), in
en, zh, zh-TW, ja and de. No new words and no GUI file changed: app.js
grows 0 bytes. There is no CLI for the move. The minute is the page's
own: codex exec lets the thread go as it exits (the interactive TUI
wasn't tried), but Codex 0.164.0's app-server keeps a thread loaded, and
held, until 30 minutes after it went idle with nothing showing it
(thread_unload_delay_secs, 1800 by default), or until it ends. So a
thread the Codex app has shown can be refused for up to half an hour
after it is closed there, or until the app quits. Those words are left
as they are (#3986).
A Codex home without thread-writer-locks (a Codex from before them)
has no lock to take, and its threads move as before, with the
last-minute check alone. None is made there: PortCodexRollouts reads a
home with one as one whose Codex holds its threads, and would then
rewrite rollouts under a Codex that doesn't. A lock folder that can't
be read, or a lock file that can't be opened, refuses the move with
its error. The coordination lock is waited for at most 10s
(filelock.Wait), under storeMu and mu as the row's 5s busy_timeout
already is; not had by then, the thread counts as held, as in
editRollout.
editRollout, the port's writer, isn't used: it publishes one file under
the coordination lock alone, while a thread's files (a revert's, a
packed twin) and its row move together, and its edit can't refuse a
longer id in a paginated rollout as 67b12956's rewriteCodexProvider
does. rewriteCodexProvider is unchanged, so the padding and that
refusal stay as they were.
A thread bringBack leaves keeps its row on magpie, so the next bringBack
picks it again (codexThreadsOn) and moves it once its Codex has let go:
it isn't left for good. bringBack runs at Disconnect and in Codex's
Sync, which SyncCatalog runs: at the GUI's start, after a provider,
plugin or model list is saved or fetched, and when the catalog is
fetched again (catalog.KeepFresh, at least daily in the GUI and in
magpie serve). Until then the thread stays on magpie, as one written to
in the last minute does now, so for up to about a day while magpie runs.
At a Disconnect magpie also restarts the Codex app's app-server
(KeepCodexAppOnItsProvider, every 3s), which lets the thread go; when
that comes after bringBack's move, the thread waits for the next Sync.
When Codex goes back to openai, or another id of magpie's length,
RelabelCodexThreads first writes a plain rollout's provider in place,
held or not; MoveCodexThreads meets a held thread there only for what
that skips, a packed rollout mostly. For an id of another length it
moves them all.
RelabelCodexThreads takes no lock either. It writes the id over in the
first line, in place, so a Codex appending to the file loses nothing:
tried below, the turn after it was kept and the row stayed as it wrote
it. It is left as it is.
The port's paragraph in docs/subsystems/gateway-routing.md, where
67b12956 put the move's lengths, says so, and so do the comments of
MoveCodexProvider, MoveCodexThreads and bringBack.
What was run (tree d652ce94, on origin/main 74dcf40a, go1.26.8,
linux/amd64, Linux 5.15), outside graftcp, umask 077:
- TestMoveCodexProviderLeavesAnOpenThread (new): a thread's writer lock
held as a resumed Codex holds it. The Move is refused with ErrActive;
the file, its time and the row are as they were, nothing is kept in
the trash and no temp file is left. With the lock let go (its file
left behind, as a Codex that ended leaves it), the Move goes through
and the lock file is gone.
- TestMoveCodexProviderHoldsTheThread (new): looked at from a trigger
on the row's update, as the Move writes the row, the thread's lock is
held and the file names the new provider already; the lock file is
gone after.
- TestMoveCodexThreadsLeavesAnOpenThread (new): two threads on magpie,
one held. The other moves; the held one keeps its file and row, with
no copy and no error, and the next call moves it once let go. A home
without thread-writer-locks moves its thread as before, and gets
none.
- The three with 74dcf40a's codex_provider.go: red, "err <nil>, want
ErrActive", "as the row was written: no lock file", and both threads
moved.
- -race -count=10, the three and every move and port test of
internal/sessions (TestCodexProviderIn, TestMoveCodexProvider,
TestMoveCodexProviderPacked, TestMoveCodexProviderKeepsBytePlaces,
TestMoveCodexThreads, TestMoveCodexThreadsNone,
TestPortCodexRollouts and its KeepsBytePlaces, LeavesAnOpenThread,
LongerLine and RepairsForks, TestPortLineDropsClaudeBlocks,
TestPortLineLeavesOthers): 16 tests, 10 runs each, ok. -race
-count=3: internal/agent's TestCodexThreadsComeBackOffMagpie and
TestCodexThreadsFollowMagpieIn (bringBack, bringOver) and
internal/gateway's TestPortedCompactionReadsTheSame, ok.
- Broken by hand, each alone in a scratch worktree at this tree, the
eight move tests run, each red:
- no lock taken: all three new tests, as at 74dcf40a.
- the row written for a thread left: both LeavesAnOpenThread tests
("its row moved to ..., its files didn't"), and MoveCodexThreads
never finds the thread again ("after its Codex let go: [] <nil>").
- the lock taken after the copy to the trash: both
LeavesAnOpenThread tests ("a copy kept of a thread left as it
was").
- the lock let go after the files, before the row:
TestMoveCodexProviderHoldsTheThread.
- the error not ErrActive: TestMoveCodexProviderLeavesAnOpenThread
("err a Codex has the session open, want ErrActive") and
TestMoveCodexThreadsLeavesAnOpenThread (the held thread's error
returned, "thread ...: a Codex has the session open").
- the lock file left when the move lets go: both
TestMoveCodexProvider tests.
- thread-writer-locks made in a home without one:
TestMoveCodexThreadsLeavesAnOpenThread.
The other move tests stayed green under each.
- Codex CLI 0.164.0, real, with gw-c's #3616 move-open harness: a
sandbox HOME and CODEX_HOME, loopback only, a fake Responses
upstream, and MoveCodexThreads from fake-provider to fake through a
test binary built from each tree. Two turns on a thread T; a codex
app-server resumes T (T's lock file appears in thread-writer-locks),
waits 80s and has a turn; at 70s, the move.
- 74dcf40a: the move moved T, file and row. The turn completed and
the upstream got it, but the rollout has it 0 times, and Codex
resuming T on fake sent turns 1 and 2 without it.
- here: the move moved nothing, file and row on fake-provider. The
turn is in the rollout (2 lines). With the app-server gone (its
lock file gone) and the rollout a minute old, the move moved T
(files 1, db 1), and thread-writer-locks had only
.coordination.lock after it. Codex resuming T on fake sent turns 1
and 2 and the turn the app-server had.
Each tree ran twice: in the first runs the move after the app-server
came within the minute after its turn, which left T here; the script
now waits the minute out.
- Codex CLI 0.164.0, the same sandbox: with T's lock file made and
flocked under the coordination lock by another process, as
holdCodexThread does, codex exec resume T exits 1 with "thread/resume
failed: thread ... already has an active writer" and sends nothing;
once it is let go and the file taken away, the resume runs.
- Codex CLI 0.164.0, the move-open run with RelabelCodexThreads from
fake to fak2 (one length) at 70s: T relabelled at once, the turn in
the rollout (2 lines), the row still fak2 after it, and Codex
resuming T on fak2 sent the turn.
- gofmt -l clean. tools/vetset.sh: go vet -tags nogui for linux,
windows and darwin, go vet for windows, go build -tags nogui for
linux and darwin, and go build for windows, exit 0 each; untagged
linux vet and build exit 1 (no GTK or WebKitGTK dev packages on this
box).
- go test -tags nogui -count=1 -timeout 45m ./... (log kept), Bun
1.4.2: 68 packages ok and 8 red, sessions ok; each red on the list of
reds already filed: agent (#3141, #3398, #3641, and
TestWSLColdPlaceOpensNothing, #3924), davsync TestSync (#3420), edit
and testenv (umask 077, #3640), gateway
TestPluginToldWhereTheAgentWorks (#3675), gui
TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie
(#3649), provider TestCodexAppServersOnHome (#3906), then "close of
synctest channel from outside bubble" (#3632), tui
TestTUIFetchesSubscriptionModels/agent (#3919)
- On origin/main 1a804859 with this commit cherry-picked (a scratch
worktree, not this tree): go test ./internal/sessions ok; go test -run
Codex ./internal/agent, only TestCodexDriftReached red (#3398)
Not run: the GUI tests, i18n-keys among them; no GUI file changed.
Not tried: Windows, where magpie's lock is LockFileEx on one byte and
Codex's on the whole file (as for the port), and macOS; the Codex app
itself, whose app-server was driven over stdio here as the app drives
one; a thread of a Codex in a WSL distro moved from Windows, where the
lock taken may not be the distro's; a file system without locks, where
the move is refused as held.
Review (yetone, at this commit on origin/main bb5e68cea, go1.26.8):
- Checked against Codex's codex-rs/rollout/src/writer_lock.rs: acquire
takes thread-writer-locks/.coordination.lock, creates and try-locks
<thread_id>.lock and lets coordination go; its guard's drop takes
coordination again, closes the file, removes it and lets go. The move
does the same, and leaves a home without the folder alone.
- On main, the three new tests red on macOS (mote): "err <nil>, want
ErrActive", "as the row was written: no lock file", both threads
moved. With this change, ok.
- Broken by hand, each red: letGo called at once (HoldsTheThread);
os.Remove dropped ("lock file is left", both MoveCodexProvider
tests); threadOpen's Is false (LeavesAnOpenThread x2); the lock
folder always made (MoveCodexThreads, "thread-writer-locks made where
Codex keeps none").
- macOS (mote): go test -tags nogui ./internal/sessions
./internal/filelock ./internal/agent ok; -race -count=10 -run
'TestMoveCodex|TestPortCodex' ./internal/sessions ok (at d0a61b856);
gofmt -l clean; go vet darwin, GOOS=linux -tags nogui and
GOOS=windows for sessions and agent ok.
- Windows (xiaomi-gpu, win-run) and Linux (WSL magpie-desktop,
wsl-run): go test -run 'TestMoveCodex|TestPortCodex'
./internal/sessions ok (at d0a61b856).
Not tried by the review: a real Codex CLI or app holding a thread
during a move, on any system; a WSL distro's Codex moved from Windows.
At the pushed tree (rebased on origin/main 7e0fe83d4), macOS (mote):
gofmt clean; go vet darwin, GOOS=windows and GOOS=linux -tags nogui for
sessions ok; go test -tags nogui ./internal/sessions ./internal/agent
ok; -run Budget ./internal/gui ok.
xybio (Discord #feedback): the Codex app often wouldn't reopen recent
threads: "invalid paginated history lineage for <id>: cutoff byte offset
is past the source rollout". Codex 0.162 keeps a fork's place in its
parent by byte (session_meta.history_base.end_byte_offset, checked by
rollout_lineage.rs validate_cutoff_bounds), and its state database keeps
turns and how far it has read by byte too. PortCodexRollouts (since
4896fc3ba, 10-21) wrote magpie's compaction item as Codex's summary
message, and since 79c8667c9 dropped Claude's sealed reasoning: both
shorter lines, so a parent shrank and every fork made before the pass
named a byte past its end.
Now every rewritten line keeps its length (fitLine): a shorter one is
padded with spaces after its JSON, which serde_json skips; a longer one
(a short summary under Codex's prefix) is left as it was in a paginated
rollout, and written as before in a legacy one, where Codex keeps no
place by byte. MoveCodexProvider, the sibling that rewrites session_meta,
pads the same way and refuses a longer provider id for a paginated
rollout. Forks already broken are repaired once per Codex home
(repairLineages): every line kept its count, and Codex names each line's
ordinal, so a fork's end_byte_offset is set again to where its parent's
line end_ordinal_exclusive-1 ends now, in a first line kept as long.
Run, at a47596ea6 plus this change:
- go test -count=1 ./internal/sessions/ ./internal/codexsummary/: ok;
./internal/agent/ -run 'Codex|Port|Move|Session': ok.
- gofmt -l internal/ clean; go vet ./internal/sessions/ ./internal/agent/,
GOOS=windows and GOOS=linux go vet ./internal/sessions/: clean.
- Real, Codex 0.162.1 in a sandbox CODEX_HOME against a local mock
Responses server that returns a magpie-claude1: reasoning item: codex
exec made a paginated thread (50877 bytes), app-server thread/fork
forked it, then a port pass. Old code: the parent went to 70570 bytes
from 82912 and thread/resume and thread/read of the fork gave the
reporter's exact error. New code: the parent kept 82912 bytes with no
magpie-claude1: left, the fork resumed and read, the parent resumed,
and codex exec resume ran a turn on the fork. A new-code pass over the
old-code home repaired the fork (82912 -> 70570 in its history_base),
after which it resumed and read.
Broken by hand:
- fitLine returning the rewrite as it is:
TestPortCodexRolloutsKeepsBytePlaces red ("lines end at [... 11329],
were at [... 14362]", the fork's offset past the end),
TestPortCodexRolloutsLongerLine and TestMoveCodexProviderKeepsBytePlaces
red.
- repairLineages a no-op: TestPortCodexRolloutsRepairsForks red.
Not tried: the Codex desktop app itself (the app-server it runs was);
a .zst parent against real Codex (the Go tests cover zst ports); Codex in
WSL, which the pass skips as before. The state database's turn offsets
for a parent already shrunk are left as they are; Codex 0.162.1 resumed
and forked such a parent in the sandbox.