Avoid scanning archived rollouts when archiving threads (#41908)

## Why

Archiving a thread only needs to locate rollout files that have not already
been moved, so reading the entire rollout archive on every request is
unnecessary.

## What changed

- Add `RolloutReferenceIndex::scan_unarchived` to scan only the active
  `sessions` directory.
- Use the active-only index when archiving threads while retaining full scans
  for operations that require archived-history reference counts.
- Cover discovery of compressed and uncompressed active rollouts and exclusion
  of archived and unrelated rollouts.

GitOrigin-RevId: 46367127578f20bfa46a8db5c6f2b7f59d29f7a9
This commit is contained in:
Eric Traut authored and copyberry committed 2026-08-31 20:40:05 +00:00
1 parent ae0ecd7484
commit 2f0a5d5516
3 files changed
+73 -8

No files matched your search

@@ -36,7 +36,14 @@ struct IndexedRollout {
impl RolloutReferenceIndex {
/// Scans active and archived local rollout metadata without a deadline.
pub async fn scan(codex_home: &Path) -> io::Result<Self> {
let Some(index) = Self::scan_with_deadline(codex_home, ScanDeadline::Unlimited).await?
let Some(index) = Self::scan_with_deadline(
vec![
codex_home.join(ARCHIVED_SESSIONS_SUBDIR),
codex_home.join(SESSIONS_SUBDIR),
],
ScanDeadline::Unlimited,
)
.await?
else {
return Err(io::Error::other(
"unlimited rollout reference scan exceeded a deadline",
@@ -45,6 +52,19 @@ impl RolloutReferenceIndex {
Ok(index)
}
/// Scans only unarchived rollouts to locate files that still need to be archived.
///
/// Reference counts exclude archived history and must not be used to decide whether a
/// rollout can be deleted or compressed.
pub async fn scan_unarchived(codex_home: &Path) -> io::Result<Self> {
Self::scan_with_deadline(
vec![codex_home.join(SESSIONS_SUBDIR)],
ScanDeadline::Unlimited,
)
.await?
.ok_or_else(|| io::Error::other("unlimited rollout reference scan exceeded a deadline"))
}
/// Scans active and archived local rollout metadata until the worker deadline expires.
///
/// Returns None instead of a partial index when the deadline expires.
@@ -54,7 +74,10 @@ impl RolloutReferenceIndex {
max_runtime: Duration,
) -> io::Result<Option<Self>> {
Self::scan_with_deadline(
codex_home,
vec![
codex_home.join(ARCHIVED_SESSIONS_SUBDIR),
codex_home.join(SESSIONS_SUBDIR),
],
ScanDeadline::Until {
started_at,
max_runtime,
@@ -90,14 +113,10 @@ impl RolloutReferenceIndex {
}
async fn scan_with_deadline(
codex_home: &Path,
mut stack: Vec<PathBuf>,
deadline: ScanDeadline,
) -> io::Result<Option<Self>> {
let mut rollouts_by_id = HashMap::new();
let mut stack = vec![
codex_home.join(ARCHIVED_SESSIONS_SUBDIR),
codex_home.join(SESSIONS_SUBDIR),
];
while let Some(directory) = stack.pop() {
if deadline.expired() {
return Ok(None);