Skip to content

Unify rollout reconstruction with resume/fork TurnContext hydration#12612

Open
charley-oai wants to merge 42 commits intomainfrom
cc/unify-rollout-reconstruction-resume-hydration
Open

Unify rollout reconstruction with resume/fork TurnContext hydration#12612
charley-oai wants to merge 42 commits intomainfrom
cc/unify-rollout-reconstruction-resume-hydration

Conversation

@charley-oai
Copy link
Collaborator

@charley-oai charley-oai commented Feb 23, 2026

Summary

This PR unifies rollout history reconstruction and resume/fork metadata hydration under a single reconstruct_history_from_rollout implementation.

The current shape is intentionally eager and simple:

  • rebuild history by replaying rollout forward
  • compute previous_model and reference_context_item in the same reconstruction pass
  • delete the separate last_rollout_regular_turn_context_lookup scan
  • keep resume/fork startup consuming a single RolloutReconstruction

The replay code now stays focused on the part we want to keep eager long-term:

  • previous_model
  • reference_context_item

It does not try to introduce lazy/reverse history loading yet.

What Changed

Unified replay output

reconstruct_history_from_rollout now returns:

  • reconstructed history
  • previous_model
  • reference_context_item

Resume and fork use that output directly instead of doing a second bespoke rollout traversal.

Simpler replay shape

The reconstruction logic in codex-rs/core/src/codex/rollout_reconstruction.rs was simplified back to an eager forward replay.

That removed the temporary reverse/tail-scan scaffolding and keeps the implementation closer to mainline behavior while still unifying metadata accounting with history replay.

TurnContext persistence contract

TurnContextItem persistence is now treated as the replay source of truth for eager resume metadata.

The important behavior is:

  • first real user turn persists TurnContextItem so resume can recover previous_model
  • steady-state turns that emit explicit context diffs persist TurnContextItem
  • if the baseline was cleared and the next user turn reinjects full context, that turn also persists TurnContextItem

That last case matters for resume after compaction: otherwise replay can keep an older previous_model, which causes bad model-mismatch warnings and duplicate <model_switch> injections.

Behavior Preserved

  • rollback replay stays aligned with drop_last_n_user_turns
  • rollback skips only user turns
  • incomplete active user turns are dropped before older finalized turns when rollback applies
  • unmatched aborts do not consume the current active turn
  • missing abort IDs still conservatively clear stale compaction state
  • trailing incomplete turns are finalized conservatively for replay
  • compaction clears reference_context_item but still allows previous_model to come from the last surviving user turn

Tests

Targeted validation run:

  • cargo test -p codex-core reconstruct_history_rollback_ --lib
  • cargo test -p codex-core record_initial_history_resumed_ --lib
  • cargo test -p codex-core record_context_updates_and_set_reference_context_item_persists_full_reinjection_to_rollout --lib
  • cargo test -p codex-core record_context_updates_and_set_reference_context_item_updates_baseline_without_emitting_diffs --lib
  • cargo test -p codex-core --test all suite::resume::resume_switches_models_preserves_base_instructions -- --nocapture
  • cargo test -p codex-core --test all suite::resume::resume_model_switch_is_not_duplicated_after_pre_turn_override -- --nocapture
  • cargo test -p codex-core
    • only unrelated existing failure: tools::js_repl::tests::js_repl_can_attach_image_via_view_image_tool
  • just fmt
  • just fix -p codex-core

Per repo workflow, tests were not rerun after the final fmt / fix step.

@charley-oai
Copy link
Collaborator Author

@codex review this

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 859ea16a31

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector
Copy link
Contributor

Security review completed. No security issues were found in this pull request.

View security finding report

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@charley-oai
Copy link
Collaborator Author

@codex review this

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d4efe495b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@charley-oai
Copy link
Collaborator Author

@codex review this

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d1ab8c7d01

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@charley-oai
Copy link
Collaborator Author

@codex review this

@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. Nice work!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@charley-oai
Copy link
Collaborator Author

@codex review this

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9506d61000

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@charley-oai
Copy link
Collaborator Author

@codex review this

@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. Swish!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

}
RolloutItem::EventMsg(EventMsg::ThreadRolledBack(rollback)) => {
history.drop_last_n_user_turns(rollback.num_turns);
let mut turns_to_drop =
Copy link
Collaborator

Choose a reason for hiding this comment

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

a bit strange to run two version of this logic in parallel, one to drop user messages, another to drop turns

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we can't unify them because history only contains model-visible data whereas our stack of user turns contains metadata needed to fetch previous_model and reference_context_item

let push_replayed_turn = |replayed_segments: &mut Vec<RolloutReplayMetaSegment>,
active_turn: ActiveRolloutTurn| {
replayed_segments.push(RolloutReplayMetaSegment::Turn(Box::new(
ReplayedRolloutTurn {
Copy link
Collaborator

Choose a reason for hiding this comment

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

we are copying the struct into almost the same struct ActiveRolloutTurn -- >ReplayedRolloutTurn.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The conversion serves to eliminate dead/unused fields

  • dropping turn_id, which is only needed while matching lifecycle events
  • dropping saw_user_message, which is only needed to decide whether the span counts as a user turn at all

@charley-oai
Copy link
Collaborator Author

@codex review this

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: acec761d88

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@charley-oai
Copy link
Collaborator Author

@codex review this

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 06f8e6b316

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@charley-oai
Copy link
Collaborator Author

@codex review this

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 37cacb2dab

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@charley-oai
Copy link
Collaborator Author

@codex review this

@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. Delightful!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@charley-oai charley-oai force-pushed the cc/unify-rollout-reconstruction-resume-hydration branch from 258153b to ff322b4 Compare February 26, 2026 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants