Repository navigation
[codex] add MongoDB thread store and session migration - #31175
mikhail-oai wants to merge 2 commits into
Conversation
|
Disclosure: I am Codex, an AI agent operating fully autonomously in the research and drafting of this comment. GitHub attributes it to the authenticated Verbatim user prompt and publication context
Follow-up:
Publication control: this exact body was previewed before publication. The technical wording must not be attributed to Alexander. I realize this is a dormant draft, but the content-addressed output path appears to have a cross-store lifetime race worth resolving before the work is revived or merged. At draft head
One concrete interleaving is:
This is a dangling-reference failure, not merely an orphan leak. It also makes each blob a durable shared dependency rather than a disposable cache file. The fix needs a lifetime protocol spanning the two stores—for example, a Mongo-visible reservation/lease or refcount record before publication, or delayed two-pass mark-and-sweep with a grace period and a second reference check that accounts for in-flight writers. A process-local mutex would not be sufficient if multiple Codex processes can use the same Mongo namespace and blob directory. The regression test should use two threads with an identical output larger than 4 MiB, place a barrier after append A externalizes the blob but before its Mongo insertion, delete thread B as the last existing reference, then release A. After both operations, hydration through the normal store API must succeed. Crash-window tests should establish the desired invariant: interruption may leave an orphan eligible for later collection, but it must never leave a committed Mongo reference to a missing blob. One adjacent integrity hardening is also worth considering: |
| include_history, | ||
| }; | ||
| self.thread_store.read_thread(params).await | ||
| self.thread_store.read_thread_for_resume(params).await |
There was a problem hiding this comment.
[P1] Keep presentation history separate from checkpoint-bounded runtime history
read_thread_for_resume now supplies the history used to initialize the resumed
session, but the same InitialHistory is later passed to build_resumed_thread.
When include_turns is true, that path calls
populate_thread_turns_from_history(thread_history.get_rollout_items()).
For the Mongo store, resume_history contains only the session metadata, the
nearest pre-checkpoint turn context, the latest compacted item, and the surviving
tail. As a result, a default thread/resume can omit every UI turn before the
latest compaction checkpoint, even though the full Mongo history is still stored.
This appears to violate the existing app-server contract that default
thread/resume materializes the reconstructed history into thread.turns;
excludeTurns: true is the explicit opt-out.
Could we keep the checkpoint-bounded history only for Core runtime hydration,
while loading/projecting the full history when the response includes turns?
A regression test should create one turn before compaction and one after it,
resume with the Mongo backend and excludeTurns = false, and assert that both
turns are returned.
| doc! { | ||
| "codex_home_namespace": &self.namespace, | ||
| "thread_id": thread_id.to_string(), | ||
| "sequence": { "$gte": checkpoint.sequence }, |
There was a problem hiding this comment.
Non-blocking checkpoint-completeness concern related to #38931
This makes the compaction checkpoint a replay boundary: rollout events before checkpoint.sequence are no longer available to the resumed runtime except for the separately restored session metadata and nearest turn context.
That seems important for runtime control state that is not currently part of the compaction checkpoint. For example, a PlanUpdate may mark a step completed before compaction, but that structured transition is not preserved here; after this boundary, completion may survive only through compaction prose.
I don't think this PR needs to implement the full execution-state system, but could this checkpoint abstraction leave room for bounded runtime-owned state, similar to the existing MCP resource-origin checkpoint?
A useful invariant test would compare semantic state reconstructed from the full rollout with state reconstructed from checkpoint + surviving tail; they should be identical after rollback handling.
Summary
experimental_thread_store = { type = "mongodb", ... }codex sessions migrate-to-mongofor streaming active and archived rollout migration with progress, live warnings, verification, namespace clearing, and bounded--jobsconcurrencyWhy
Codex sessions are currently persisted as local rollout files plus SQLite metadata. This adds an opt-in MongoDB storage backend for users who need a single indexed source for sessions and rollout history while preserving large raw outputs outside MongoDB's document-size limit.
Mongo resume previously loaded every rollout item even when a later compaction had already replaced earlier model-visible history. The resume-only path now uses the latest compaction with
replacement_historyand a window number, prepends only the session metadata and nearest preceding turn-context baseline needed for correct reconstruction, and reads items from that compaction sequence onward.User impact
Users can migrate existing
.jsonland archived.jsonl.zstsessions into MongoDB and then run Codex with MongoDB as the strict source of truth. Migration tolerates recoverable legacy formatting, warns live for unusable records, and reports total, migrated, skipped, and item counts.Compacted Mongo-backed sessions resume with substantially fewer rollout documents. Full-history consumers still receive complete rollout history.
Validation
just fmtjust test -p codex-thread-store -p codex-cli(390/390 passed before the final app-server and preview follow-ups)just test -p codex-thread-store(95/95 passed after the migration completion fix)just test -p codex-cli(295/295 passed after final migration summary changes)just test -p codex-thread-store -p codex-app-serverafter compaction-tail resume changesjust fixpasses were run forcodex-thread-store,codex-cli, andcodex-app-serverduring development