Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): rewound Codex threads import their history from before the rewind #30652

MergedMarcoDotIO(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/30652/headmerged by maria-rcks View on GitHub

Problem

Codex reverts a paginated thread (Codex Desktop rewind, thread/revert) by writing a new, immutable rollout named rollout-<ts>-<thread id>_<rollout id>.jsonl. Its session_meta.history_base points at the rollout it replaces, { thread_id: <rollout id>, end_byte_offset }, and the new file inherits that file's first end_byte_offset bytes (see revert_thread.rs and paginated_fork.rs in codex-rs/thread-store/src/local/). Repeated rewinds form a chain of rollouts.

Onboarding import (/welcome → agentSessions.import → AgentSessionScanner.recentThreads) reads each rollout on its own. The newest one sorts first and is imported. Every earlier rollout of the thread has the same session ID, so it is then dropped as a duplicate. As a result, the imported thread starts at the last rewind and loses everything before it, without the "earlier messages were not imported" notice. On a real Codex Desktop home, one thread was split across five rollouts after four rewinds. Import kept only the last one.

Change

When a Codex rollout has a history_base, recentThreads follows the chain through the earlier rollouts it found during the scan. It reads each inherited prefix, keeps the shared 32 MiB history budget and the import byte budget, and parses those records ahead of the new ones. It drops the inherited session_meta records, so the rollout's own metadata still names the thread. readTranscript gains an optional byte limit for these prefix reads. If any rollout in the chain is missing or over budget, the newest rollout is imported on its own, as it is today.

Scope and approval

This is a small fix for an obvious bug in the existing session import: a rewound Codex thread imported with most of its history missing. It adds no settings or new import sources and is limited to AgentSessionScanner.ts and its test.

Related, not overlapping:

  • #25923 changes which Codex user records count as canonical.
  • #23214 adds compressed rollouts. If both land, prefix reads would go through its decompressing reader.

Verification

New test in apps/server/src/project/AgentSessionScanner.test.ts: recentThreads › imports a rewound Codex thread with the history its new rollout inherits. It writes an original rollout containing a kept turn and a rewound turn, plus a replacement rollout whose history_base keeps only the first turn.

Before the fix (vp test run src/project/AgentSessionScanner.test.ts -t "rewound"): 1 failed. The thread started at the replacement rollout:

      "messages": [
-       "Keep the first request",
-       "First reply",
        "Ask again",
        "Final reply",
      ],
-     "title": "Keep the first request",
+     "title": "Ask again",

After the fix: 1 passed. Running AgentSessionScanner.test.ts and AgentSessionImporter.test.ts together gives 68 passed and 2 failed. The two failures (excludes sandboxes reached through a symlink into the worktrees dir and still reads the git identity of repositories next to a blocked one) also fail on unchanged main on macOS, because of /var → /private/var and the timing of FIFO reads. Targeted vp fmt, vp lint, tsc --noEmit in apps/server, and knip passed.

Real-data run: I copied the five rollouts of that real rewound Codex Desktop thread into a scratch Codex home and ran recentThreads on them through the scanner's test harness, printing only counts. main imported 12 messages (3 prompts). This branch imported 35 messages (11 prompts) as one thread, and the four earlier rollouts were still reported as duplicates.

The format was checked against the same Codex home, structure only:

  • The replacement file names match the pattern above.
  • history_base.thread_id names the replaced rollout.
  • Each end_byte_offset lands just after a newline.

Not checked:

  • Paginated forks (thread/fork) use the same history_base field. They should now import the parent history they inherit, but I did not test them.
  • Picking the "current" rollout. That still follows file mtime, not Codex's SQLite pointer.
  • Running clients. The change is server-only, and every client renders the same imported messages.

This does not change visible UI controls; it changes which imported messages a thread contains.

Proved with a test that fails before the fix and passes after, plus the before/after run on a real rewound Codex thread.

Created with Claude Opus 5.5 in the Claude Code desktop app, and reviewed by GPT 6.1 Sol in Codex CLI.

🤖 Generated with Claude Code

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    2. Approvability

      Verdict: Approved at aa39d17

      Macroscope's review found this PR approvable — Rewound Codex threads now import their inherited history alongside the current rollout. Provider-scoped lookup, bounded reads, and fallback behavior limit the impact without modifying original transcripts.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      Codex recent-thread imports now follow history_base metadata to read earlier rollout prefixes. The scanner combines inherited records with the newer rollout, subject to the transcript byte budget. If inheritance cannot be read, it parses the newer rollout alone.

      Changes: Codex rollout history inheritance

      Layer / File(s)Summary
      Decode history metadata and read transcript prefixes
      apps/server/src/project/AgentSessionScanner.ts
      The transcript schema and decoder accept history_base metadata. Transcript reads can stop at a byte offset and carry selected-history byte accounting.
      Resolve and read inherited rollout history
      apps/server/src/project/AgentSessionScanner.ts
      The scanner resolves rollout bases within a provider instance and reads the chain oldest-first. Cycles and unresolved or unreadable bases prevent inheritance.
      Combine inherited records during recent-thread import
      apps/server/src/project/AgentSessionScanner.ts, apps/server/src/project/AgentSessionScanner.test.ts
      Recent-thread imports prepend successfully inherited records to Codex rollout records. The regression test checks the combined conversation and title.

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Bug fix

      Sequence Diagram(s)

      sequenceDiagram
        participant AgentSessionScanner
        participant CodexRolloutFiles
        participant readTranscript
        AgentSessionScanner->>CodexRolloutFiles: Resolve history_base rollout
        AgentSessionScanner->>readTranscript: Read base prefix through end_byte_offset
        readTranscript-->>AgentSessionScanner: Return inherited records and byte count
        AgentSessionScanner->>readTranscript: Read current rollout transcript
        readTranscript-->>AgentSessionScanner: Return current rollout records
        AgentSessionScanner->>AgentSessionScanner: Prepend inherited records for import

      Possibly related issues

      • Issue 21755 concerns discovery of compressed .jsonl.zst rollouts. This change follows history-base chains but does not add compressed rollout discovery.

      Suggested labels: bugfix, size:M

      Suggested reviewers: juliusmarminge, macroscopeapp, maria-rcks

      Merge Risk: Minimal · up to aa39d

      No confirmed issue blocks merging this change after normal checks.

      Pre-merge checks: Passed 4
      Check nameStatusExplanation
      Title checkPassedThe title is concise, conventional, and accurately describes the main change: importing pre-rewind history for rewound Codex threads.
      Description checkPassedThe description includes all required sections and provides clear problem context, implementation details, scope rationale, focused verification results, known failures, and untested cases. It is suff…
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Full details: Description check

      Explanation

      The description includes all required sections and provides clear problem context, implementation details, scope rationale, focused verification results, known failures, and untested cases. It is sufficiently complete for review.

      Finishing Touches
      Generate unit tests (beta)
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Recent review info
      Run configuration
      Commits

      Reviewing files that changed from the base of the PR and between e68e19d605485db6ef31b2033f67a9d07634fc2b and aa39d17803651276d8c850edae0b4eef7c200f0b.

      Files selected for processing: 2
      • apps/server/src/project/AgentSessionScanner.test.ts
      • apps/server/src/project/AgentSessionScanner.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

    4. maria-rckscommented

      Note

      Written by gpt-6.1-sol on behalf of Maria

      Verified in a real dev environment

      verified unchanged head aa39d17803651276d8c850edae0b4eef7c200f0b using linux chrome, repo playwright, the real dev server, and codex gpt-6.1-sol through the local proxy.

      created a real git project in the web app, completed two codex turns, used "edit from here" on the second, and sent a replacement. codex produced a paginated replacement rollout inheriting 138706 bytes from the original. no fabricated transcripts or database fixtures.

      on github/main (8b44f42b14), fresh onboarding import lost the first turn and showed only REPLACEMENT_REPLY, without a truncation notice. importing the same rollouts on this head retained KEPT_FIRST_REPLY and REPLACEMENT_REPLY, and excluded DISCARDED_SECOND_REPLY.

      main imports only the replacement turn

      pr imports the kept turn and replacement turn

      neighboring checks passed: repeated onboarding import kept one imported thread; un-settling and resuming it through the composer returned KEPT_FIRST_REPLY REPLACEMENT_REPLY RESUME_OK. another real resumed turn returned RECORDING_OK in the recording. uploaded playback and seeking were checked.

      real codex turn in the imported conversation

      main agentSessions.import -> importedCount=1, skippedCount=0; first turn missing
      pr agentSessions.import   -> importedCount=1, skippedCount=0; kept history present
      pr repeat import          -> importedCount=1, skippedCount=0; no duplicate thread
      pr resumed codex reply    -> KEPT_FIRST_REPLY REPLACEMENT_REPLY RESUME_OK

      blacksmith: scanner and importer tests passed, 70/70. vp run check:changed against the pr base passed format, lint, knip, graphql, affected typechecks, and scoped tests. no code changes or pushes were needed.

      Not verified

      native desktop, ios/android, remote/relay/tunnel connections, repeated-rewind chains, paginated forks, and missing/over-budget history fallback were not exercised in the real client. the server-only fix was exercised through web onboarding and real codex; existing automated coverage ran separately.

    5. maria-rcks merged thisd0da47a

    6. maria-rcks closed this

Sign in to join this conversation.