Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Limit unarchive scans to the requested thread's archived rollouts (#54060)

ben zhang9d3ae38e13ae
diff --git a/codex-rs/rollout/src/rollout_reference_index.rs b/codex-rs/rollout/src/rollout_reference_index.rs
index a55d2344f9..2b21b82487 100644
--- a/codex-rs/rollout/src/rollout_reference_index.rs
+++ b/codex-rs/rollout/src/rollout_reference_index.rs
@@ -71,6 +71,22 @@ impl RolloutReferenceIndex {
Self::scan_paths(vec![codex_home.join(SESSIONS_SUBDIR)], Some(&thread_ids)).await
}
+ /// Scans archived files whose canonical filenames belong to the requested threads.
+ ///
+ /// Metadata still determines ownership among the candidates. Reference counts are partial
+ /// and must not be used to decide whether a rollout can be deleted or compressed.
+ pub async fn scan_archived_threads(
+ codex_home: &Path,
+ thread_ids: &[ThreadId],
+ ) -> io::Result<Self> {
+ let thread_ids = thread_ids.iter().copied().collect();
+ Self::scan_paths(
+ vec![codex_home.join(ARCHIVED_SESSIONS_SUBDIR)],
+ Some(&thread_ids),
+ )
+ .await
+ }
+
async fn scan_paths(
mut stack: Vec<PathBuf>,
thread_ids: Option<&HashSet<ThreadId>>,
diff --git a/codex-rs/rollout/src/rollout_reference_index_tests.rs b/codex-rs/rollout/src/rollout_reference_index_tests.rs
index 036e870724..9d61db09c4 100644
--- a/codex-rs/rollout/src/rollout_reference_index_tests.rs
+++ b/codex-rs/rollout/src/rollout_reference_index_tests.rs
@@ -247,6 +247,61 @@ async fn filtered_unarchived_scan_validates_candidate_ownership() -> anyhow::Res
Ok(())
}
+/// Restoring a thread must include its compressed/reverted files without scanning other history.
+#[tokio::test]
+async fn filtered_archived_scan_includes_only_owned_archived_candidates() -> anyhow::Result<()> {
+ let home = TempDir::new()?;
+ let owner_uuid = Uuid::from_u128(60);
+ let owner_id = thread_id(owner_uuid)?;
+ let replacement_id = thread_id(Uuid::from_u128(61))?;
+ let other_uuid = Uuid::from_u128(62);
+ let other_id = thread_id(other_uuid)?;
+ let mismatched_id = thread_id(Uuid::from_u128(63))?;
+ let original_path = archived_rollout_path(home.path(), owner_uuid);
+ let replacement_path = original_path.with_file_name(format!(
+ "rollout-2025-01-03T12-00-00-{owner_id}_{replacement_id}.jsonl"
+ ));
+ write_rollout(original_path.clone(), owner_id, /*history_base*/ None)?;
+ compress_now(&original_path)?;
+ write_rollout(
+ replacement_path.clone(),
+ owner_id,
+ Some(history_position(owner_id)),
+ )?;
+
+ // A matching filename is only a candidate: metadata must confirm ownership.
+ let mismatched_path = original_path.with_file_name(format!(
+ "rollout-2025-01-03T12-00-00-{owner_id}_{mismatched_id}.jsonl"
+ ));
+ write_rollout(mismatched_path, other_id, /*history_base*/ None)?;
+ // Unrelated filenames are excluded before reading their metadata.
+ write_rollout(
+ archived_rollout_path(home.path(), other_uuid),
+ owner_id,
+ /*history_base*/ None,
+ )?;
+ write_rollout(
+ active_rollout_path(home.path(), owner_uuid),
+ owner_id,
+ /*history_base*/ None,
+ )?;
+
+ let index = RolloutReferenceIndex::scan_archived_threads(home.path(), &[owner_id]).await?;
+ let mut owned: Vec<_> = index
+ .rollouts_for_thread(owner_id)
+ .map(|(id, path)| (id, path.to_path_buf()))
+ .collect();
+ owned.sort_by_key(|(id, _)| id.to_string());
+ assert_eq!(
+ owned,
+ vec![
+ (owner_id, original_path.with_extension("jsonl.zst")),
+ (replacement_id, replacement_path),
+ ]
+ );
+ Ok(())
+}
+
#[tokio::test]
async fn self_history_base_does_not_count_as_reference() -> anyhow::Result<()> {
let home = TempDir::new()?;
diff --git a/codex-rs/thread-store/src/local/helpers.rs b/codex-rs/thread-store/src/local/helpers.rs
index 3c8433dcc5..b9de0906c4 100644
--- a/codex-rs/thread-store/src/local/helpers.rs
+++ b/codex-rs/thread-store/src/local/helpers.rs
@@ -68,19 +68,6 @@ pub(super) fn rollout_path_is_archived(codex_home: &Path, path: &Path) -> bool {
.any(|component| component.as_os_str() == OsStr::new(ARCHIVED_SESSIONS_SUBDIR))
}
-/// Returns rollout files whose session metadata belongs to `thread_id`.
-pub(super) async fn owned_rollout_paths(
- store: &LocalThreadStore,
- thread_id: ThreadId,
-) -> ThreadStoreResult<Vec<PathBuf>> {
- RolloutReferenceIndex::scan(store.config.codex_home.as_path())
- .await
- .map_err(|err| ThreadStoreError::Internal {
- message: format!("failed to scan thread rollout files: {err}"),
- })
- .map(|index| owned_rollout_paths_from_index(&index, thread_id))
-}
-
pub(super) fn owned_rollout_paths_from_index(
index: &RolloutReferenceIndex,
thread_id: ThreadId,
diff --git a/codex-rs/thread-store/src/local/unarchive_thread.rs b/codex-rs/thread-store/src/local/unarchive_thread.rs
index 017d18ad00..8bf975c269 100644
--- a/codex-rs/thread-store/src/local/unarchive_thread.rs
+++ b/codex-rs/thread-store/src/local/unarchive_thread.rs
@@ -1,5 +1,5 @@
use super::LocalThreadStore;
-use super::helpers::owned_rollout_paths;
+use super::helpers::owned_rollout_paths_from_index;
use super::helpers::restore_rollout_moves;
use super::helpers::rollout_path_is_archived;
use super::helpers::scoped_rollout_path;
@@ -10,6 +10,7 @@ use crate::ReadThreadParams;
use crate::StoredThread;
use crate::ThreadStoreError;
use crate::ThreadStoreResult;
+use codex_rollout::RolloutReferenceIndex;
use codex_rollout::rollout_date_parts;
use super::thread_rollout_resolver;
@@ -34,7 +35,15 @@ pub(super) async fn unarchive_thread(
message: format!("no archived rollout found for thread id {thread_id}"),
})?;
- let mut rollout_paths = owned_rollout_paths(store, thread_id).await?;
+ let reference_index = RolloutReferenceIndex::scan_archived_threads(
+ store.config.codex_home.as_path(),
+ &[thread_id],
+ )
+ .await
+ .map_err(|err| ThreadStoreError::Internal {
+ message: format!("failed to scan thread rollout files: {err}"),
+ })?;
+ let mut rollout_paths = owned_rollout_paths_from_index(&reference_index, thread_id);
if !rollout_paths.contains(&selected_archived_path) {
rollout_paths.push(selected_archived_path.clone());
}