Preserve fork runtime versions without loading full model context (#43545)

## Why

A fork cutoff can exclude the `TurnContext` that records the source's multi-agent runtime version. Recovering that version should not require loading the source's full model context.

## What changed

- Preserve `multi_agent_version` in fork session metadata before applying the cutoff. If absent, scan backward for the newest version-bearing `TurnContext`, respecting inherited segment boundaries and stopping once resolved.
- Load the latest model context for paginated forks only when approval or permission settings need to be restored.

## Testing

Add regression tests covering stored-version precedence, early termination without reading older segments, and version recovery within inherited segment cutoffs.

GitOrigin-RevId: 00e66bcce6f4092805e273cde99bd50f304fb39e
This commit is contained in:
jif
2026-09-07 18:34:49 +00:00
committed by copyberry
parent 7d8e2dd6c5
commit 4f1a2bb5ff
3 changed files with 162 additions and 1 deletions
@@ -1,3 +1,5 @@
//! Reconstructs model context and preserves source runtime metadata across fork cutoffs.
use std::io;
use codex_protocol::protocol::HistoryPosition;
@@ -89,7 +91,7 @@ pub(super) async fn load_for_fork(
.ok_or_else(|| ThreadStoreError::Internal {
message: "fork lineage has no source segment".to_string(),
})?;
let session_meta = codex_rollout::read_session_meta_line(source_path)
let mut session_meta = codex_rollout::read_session_meta_line(source_path)
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!(
@@ -97,6 +99,43 @@ pub(super) async fn load_for_fork(
source_path.display()
),
})?;
if session_meta.meta.multi_agent_version.is_none() {
// Recover only the runtime version before applying the fork cutoff. Stop at the
// newest version-bearing context instead of retaining the source's full replay.
let source_lineage = lineage.clone();
session_meta.meta.multi_agent_version = tokio::task::spawn_blocking(move || {
for segment in source_lineage.segments().iter().rev() {
let file =
codex_rollout::open_rollout_seekable_reader(segment.rollout_path.as_path())?;
let mut scanner = match segment.end.map(|end| end.end_byte_offset) {
Some(end_byte_offset) => ReverseJsonlScanner::new_at(file, end_byte_offset)?,
None => ReverseJsonlScanner::new(file)?,
};
while let Some(outcome) = scanner.scan_next_rollout_line()? {
let ScanOutcome::Parsed(line) = outcome else {
continue;
};
if let RolloutItem::TurnContext(context) = &line.item
&& let Some(version) = context.multi_agent_version
{
return Ok(Some(version));
}
// Ancestor metadata does not describe the immediate source's runtime.
if matches!(line.item, RolloutItem::SessionMeta(_)) {
break;
}
}
}
Ok::<_, io::Error>(None)
})
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to join fork runtime version scan: {err}"),
})?
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to read fork runtime version: {err}"),
})?;
}
match history_base {
Some(history_base) => {
let lineage = lineage.truncate_at(history_base).await?;