Repository navigation
fix(storage): import reverted Codex threads along their history base chain - #5450
Conversation
6bd1e51 to
09d72e7
Compare
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]>
09d72e7 to
c95ee97
Compare
hqhq1025
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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]>
|
This PR's premise was wrong, and I have pushed a rewrite ( The The adapter now follows the |
hqhq1025
left a comment
There was a problem hiding this comment.
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.
Astro-Han
left a comment
There was a problem hiding this comment.
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
statper file) even for threads that were never reverted, wheremainstopped at the first match. Speed only. - Filename parsing is looser when locating rollouts than when building the history-base index (
rolloutFileIdsrequires therollout-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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
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'ssession_meta.history_basenames 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:_<rolloutId>file names as rollouts of<threadId>;history_baseby rollout id across the active and archived roots, reading each earlier rollout only up to its offset;main;session_metabefore any other record;maxRolloutBytesbudget, counting only the bytes read, and continuous line numbers across the chain, so generated message ids stay unique;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/storagecodex-session-adaptertests: 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.a_bthread 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.packages/storagesuite: 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 commit99cfeb7e9.packages/runtime-hostexternal-session tests: 43/43.npm run lint,npm run format:check, and the@maka/storagetypecheck: pass.npm test.99cfeb7e9with this branch:mainproduce identical imports;AI use
Select exactly one:
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
Does this PR entail a change in behavior?
🤖 Generated with Claude Code