Skip to content

fix(storage): import reverted Codex threads along their history base chain - #5450

Merged
Astro-Han merged 4 commits into
apache:mainfrom
sunrioa:codex/fix-5448-codex-handoff-import
Sep 27, 2026
Merged

Astro-Han merged 4 commits into
apache:mainfrom
sunrioa:codex/fix-5448-codex-handoff-import

Conversation

@sunrioa

@sunrioa sunrioa commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When Codex reverts a thread, it keeps the thread id, writes a new rollout rollout-<ts>-<threadId>_<rolloutId>.jsonl, and moves the thread's state row to it (revert_thread.rs). The new file's session_meta.history_base names the rollout it continues and the byte offset where the kept history ends; whatever follows that offset in the older rollout was reverted. Codex rebuilds a thread's history by following these pointers (rollout_lineage.rs). The adapter only accepted names ending in -<threadId>.jsonl, so reverted threads were missing from the import catalog, and importing one by id silently converted only the opening rollout, including what the first revert had discarded.

This PR makes CodexSessionAdapter:

  • recognize _<rolloutId> file names as rollouts of <threadId>;
  • start from the rollout the state row names, or, without a state database, from the newest file named for the thread, as Codex does;
  • follow history_base by rollout id across the active and archived roots, reading each earlier rollout only up to its offset;
  • stop at a fork's base in its parent thread, so a fork still imports only its own history, as on main;
  • require every rollout on the chain to name the thread in its own session_meta before any other record;
  • fail rather than import a shortened thread when a base is missing, loops, names another thread, or ends past or inside a record;
  • keep one maxRolloutBytes budget, counting only the bytes read, and continuous line numbers across the chain, so generated message ids stay unique;
  • list a reverted thread once, from its opening rollout, in the filesystem fallback.

Threads that were never reverted import exactly as before, with the same reads.

An earlier revision of this PR took these files for context hand-offs and joined every file of a thread end to end. That also imported the reverted turns, including messages that had been edited and sent again.

Fixes #5448

Verification

  • packages/storage codex-session-adapter tests: 40/40. Of the 12 rollout-chain tests, 9 fail on the previous revision of this branch (38884be47); the other 3 cover ownership checks that revision already had.
    • Covered: import along the chain with and without a state database, turns a revert discarded, a lookalike a_b thread id, a revert that kept no earlier turn, a row naming the opening rollout, an archived chain, a fork read directly and through a later revert, line numbers across the chain with a torn tail, the shared byte budget, and each refusal listed above.
  • Full packages/storage suite: 1460 passed, 1 failed, 8 skipped. The failure, rejects a second authority for the same storage root in another process (managed-dependency-environment-crash.test.js), fails identically on the base commit 99cfeb7e9.
  • packages/runtime-host external-session tests: 43/43.
  • npm run lint, npm run format:check, and the @maka/storage typecheck: pass.
  • Not run: the full root npm test.
  • Against a real local Codex store (read-only), comparing the base commit 99cfeb7e9 with this branch:
    • the 154 threads already importable on main produce identical imports;
    • the 10 reverted threads become listable and import without errors, with and without the state database;
    • each of the 10 matches, message for message, a single-file import of the byte ranges that Codex's lineage rules select, assembled independently;
    • none of the 14 user messages that a revert discarded appears in any import;
    • the thread from bug(storage): reverted Codex threads are missing from the import catalog #5448 imports 21,951 messages through 2026-09-16, instead of 19,820 messages ending 2026-09-10.

AI use

Select exactly one:

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Claude Code investigated the rollout layout, implemented the adapter change and tests, and ran the verification above. I reviewed the change and the results.

Checklist

  • Tests cover the change and fail without it
  • Lint, format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

🤖 Generated with Claude Code

sunrioa and others added 2 commits September 25, 2026 05:37
Codex writes each context hand-off of a long thread into a new
rollout-<ts>-<threadId>_<childId>.jsonl and points the state row at the
newest one. The adapter accepted only names ending in -<threadId>.jsonl,
so such a thread vanished from the import catalog, and importing it by id
silently converted only the opening rollout.

Recognize hand-off names, collect the thread's rollout family from the
active and archived roots, and stream it oldest-first with one byte
budget and continuous line numbers. Each file's session_meta must name
the thread, so a separate thread whose own id is `a_b` is never merged
into thread `a`. The filesystem fallback lists a thread once, from its
opening rollout.

Fixes apache#5448

Generated-by: Claude Code
Co-Authored-By: Claude Opus 5 <[email protected]>
Every import walked `sessions/` and `archived_sessions/` and applied the
catalog candidate cap to that walk, so a store above the cap could import
nothing and a single-file thread paid for a directory walk it did not
need. Codex moves the state row to each hand-off file, so a row that
still names the opening rollout is read on its own; only a hand-off row,
or an entry the filesystem fallback located, collects the family, and
that walk is no longer capped.

Generated-by: Claude Code
Co-Authored-By: Claude Fable 5.1 <[email protected]>
@sunrioa
sunrioa force-pushed the codex/fix-5448-codex-handoff-import branch from 09d72e7 to c95ee97 Compare September 24, 2026 21:39

@hqhq1025 hqhq1025 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change collects Codex hand-off rollout files across active and archived stores, imports them in order with a shared line/byte cursor, and limits the store walk to hand-off state rows. I found one remaining ownership-validation issue below. The current-head test check passed; I did not run local storage tests or import from a real Codex store. No schema or migration changes are included.

Automated review notice: This comment was posted by an automated review agent operated by hqhq1025. It is not an independent human review and does not replace one.

The row-named rollout went into the family unchecked, and doing so also
hid it from the walk, which skips names already there. It therefore
never faced the `session_meta` ownership check every walked candidate
must pass. `beginRollout` already declared that each file proves itself,
but nothing read `awaitingRolloutMeta`, and `finish` only requires the
family-wide flag the opening file already set. A hand-off that is empty,
torn before its metadata, or carrying records ahead of it was imported
under the opening file's identity.

A walked candidate that cannot prove the thread is simply not part of
the family. The row-named one failing means the store contradicts
itself, so it fails the import rather than quietly yielding a thread
that stops at the opening turn.

Generated-by: Claude Code
Co-Authored-By: Claude Opus 5 <[email protected]>

@hqhq1025 hqhq1025 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I re-reviewed PR #5450 at current head 38884be47d1174d214b92616955c0d9515915946, including the changes since my previous review. The earlier P2 is addressed: collectRolloutFamily() now validates the state-row-named hand-off before adding it, and the converter rejects records before each rollout's own matching session_meta. New regressions cover a hand-off with no metadata and records preceding metadata. I found no new substantiated P0–P3 issue in the inspected paths.

The PR imports Codex threads across opening and hand-off rollout files with shared ordering and read limits. The current-head test check passed (run 36166464965); the full PR diff passes git diff --check and a local merge-tree calculation against current main has no conflict. I did not run the storage suite locally or test against a real Codex store. Repository branch-protection settings were not readable through the API. Final feature and merge decisions remain with human maintainers.

Automated review notice: This comment was posted by an automated review agent operated by hqhq1025. It is not an independent human review and does not replace one.

…lout

A `rollout-<ts>-<threadId>_<rolloutId>.jsonl` file is not a context
hand-off. Codex writes one only on `thread/revert`: its `session_meta`
carries a `history_base` naming the rollout it continues and the byte
offset where the kept history ends, and whatever follows that offset was
reverted. Joining every file of a thread imported those reverted turns,
including messages that were edited and sent again.

Rebuild the thread the way Codex does: start from the rollout the state
row names, follow `history_base` by rollout id, read each earlier rollout
only up to its offset, and stop at a fork's base in its parent thread. A
missing, looping, or foreign base, or one that ends past or inside a
record, fails the import rather than shortening the thread.

Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sunrioa sunrioa changed the title fix(storage): import Codex threads continued across hand-off rollouts fix(storage): import reverted Codex threads along their history base chain Sep 25, 2026
@sunrioa

sunrioa commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor Author

This PR's premise was wrong, and I have pushed a rewrite (c25f10df0) since the re-review above.

The _<rolloutId> rollouts are not context hand-offs. Codex writes one only on thread/revert (revert_thread.rs), as a new rollout for the same thread whose session_meta.history_base keeps the previous rollout only up to end_byte_offset; whatever follows was reverted. In my local store, each of the 20 such files that has a base cuts its predecessor short, and the cut tails hold 14 user messages, each different from the message sent right after the revert. The previous revision joined whole files and imported them: I checked 6 of those 14 against its build, and all 6 were in the import.

The adapter now follows the history_base chain as Codex's rollout_lineage.rs does. The ownership checks from the earlier P2 still apply to every rollout on the chain. The updated description has the details and the verification.

@hqhq1025 hqhq1025 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I re-reviewed PR #5450 at current head c25f10df0813ec4d4a386145dcfe5af2a2171158. This revision substantially changes the mechanism: instead of concatenating files with matching thread IDs, it starts from the state row's current rollout (or the newest named rollout without a state database), follows session_meta.history_base by rollout ID, and reads each ancestor only through the byte offset retained by the revert. It stops at a fork's parent-thread boundary. I compared the lineage and cutoff model with the pinned upstream Codex implementation and inspected the new ownership, missing-base, cycle, offset, archived-store, and fork tests. I found no substantiated P0–P3 issue in the inspected paths.

The current-head test check passed (run 36184813363). The full PR diff passes git diff --check and a local merge-tree calculation against current main has no conflict. I did not independently run the storage suite or verify the author's real-store import comparison; repository branch-protection settings were not readable through the API. This is a behavior change, so final feature and merge decisions remain with human maintainers.

Automated review notice: This comment was posted by an automated review agent operated by hqhq1025. It is not an independent human review and does not replace one.

@github-actions github-actions Bot added effort/L Under 1000 readable lines and removed effort/M Under 500 readable lines labels Sep 26, 2026

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at this head by three independent AI reviewers (different model families). No P0–P2 findings; two P3 notes inline and two observations below.

The root cause is right: a Codex thread/revert starts a new rollout whose session_meta.history_base points at the previous rollout and the byte offset where retained history ends, and the old adapter read only one rollout, so a reverted thread imported the discarded tail or lost its earlier history. Following history_base from the state row's current rollout, checking each segment's own session_meta.id, failing loudly on a missing base or a loop, cutting at the retained offset and stopping at a fork's parent matches upstream Codex's rollout lineage. The history-base index is built only from validated filenames, so rollout content cannot steer paths. Adapter tests pass (40/40 in one reviewer's run), and 12 of the new tests fail when the adapter is swapped back to the base version.

Observations (not blocking):

  • Without a state DB, every import now walks both session folders in full (a stat per file) even for threads that were never reverted, where main stopped at the first match. Speed only.
  • Filename parsing is looser when locating rollouts than when building the history-base index (rolloutFileIds requires the rollout-YYYY-MM-DDTHH-MM-SS- prefix). With today's Codex names this is not reachable; if the naming ever changes, a successor would fail with "history base is missing" rather than a clearer name mismatch. Sharing one parser would avoid that.

Not verified: a real Codex store end to end, and Codex rollouts compressed to .jsonl.zst (not readable on main either; a reverted thread whose base was compressed would fail with "history base is missing").


Automated review notice: This review was posted by an automated review agent operated by Astro-Han. It combines independent reviews from several AI models. It is not an independent human review and does not replace one.


// `hasSessionMeta` is lineage-wide, so the opening file's metadata would
// otherwise vouch for every later file. Each rollout proves its own.
if (this.awaitingRolloutMeta) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 — This makes "a record before session_meta" a hard failure for every rollout, including the opening rollout of threads that were never reverted, which main imported. That contradicts the description's "import exactly as before" for the common case. If the per-segment proof is only needed for later segments of a lineage, consider applying it from the second segment on, or note the stricter behaviour in the description.

path: string,
sessionId: string,
): Promise<RolloutHeadMeta | undefined> {
const head = await readUtf8Prefix(path, CODEX_ROLLOUT_HEAD_BYTES).catch(() => undefined);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 — The head read is capped at CODEX_ROLLOUT_HEAD_BYTES. If a reverted thread's session_meta line is longer than that (over ~512 KB, e.g. large instructions), no id is found and the chain fails with a misleading "Session id mismatch", while the same thread imports fine without a revert when a state DB is present. Reading the first line up to the rollout byte budget, or reporting "session metadata too large", would make this clearer.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at @Astro-Han's explicit request: independent automated reviews of this head from multiple model families found no blocking (P0–P2) issues, and CI is green. Remaining P3 notes are non-blocking.

@Astro-Han
Astro-Han merged commit c3bc163 into apache:main Sep 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/L Under 1000 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(storage): reverted Codex threads are missing from the import catalog

3 participants