Protect fork history references during rollout cleanup (#34566)

## Why

Paginated forks can retain a `history_base` pointer to another rollout. Deleting or compressing either side of that relationship can leave the fork without the rollout history it references.

## What changed

- Add a rollout reference index that scans active, archived, and compressed rollout metadata.
- Reject deletion when a rollout is still referenced by a fork outside the requested deletion set.
- Delete spawned thread subtrees as one store operation so internal references do not block deleting the complete subtree, while external references preflight the request before any files are removed.
- Skip referenced rollouts, fork-pointer rollouts, and rollouts with unreadable metadata during background compression.

## Testing

- Cover reference indexing across active, archived, compressed, duplicate, and self-referencing rollouts.
- Verify deletion rejects external references without partially deleting a subtree and permits batch deletion of internally referenced threads.
- Verify compression preserves fork pointer chains and continues past unreadable metadata.

GitOrigin-RevId: a8672727f8706e743d7fb3ca895a1d9ceac98a3c
This commit is contained in:
Owen Lin authored and copyberry committed 2026-07-21 15:18:56 +00:00
1 parent 7bb13ab846
commit 0b175e6439
13 files changed
+801 -21

No files matched your search

+28 -1
View File
@@ -242,6 +242,7 @@ mod worker {
use tokio::task::JoinSet;
use crate::ARCHIVED_SESSIONS_SUBDIR;
use crate::RolloutReferenceIndex;
use crate::SESSIONS_SUBDIR;
use super::RolloutFile;
@@ -361,6 +362,15 @@ mod worker {
let started_at = Instant::now();
let result = async {
cleanup_stale_temps(codex_home.as_path()).await?;
let Some(reference_index) = RolloutReferenceIndex::scan_until(
codex_home.as_path(),
started_at,
WORKER_MAX_RUNTIME,
)
.await?
else {
return Ok(CompressionStats::default());
};
let mut stats = CompressionStats::default();
for root in [
codex_home.join(ARCHIVED_SESSIONS_SUBDIR),
@@ -369,7 +379,8 @@ mod worker {
if started_at.elapsed() >= WORKER_MAX_RUNTIME {
break;
}
compress_rollouts_in_root(root.as_path(), started_at, &mut stats).await?;
compress_rollouts_in_root(root.as_path(), started_at, &reference_index, &mut stats)
.await?;
}
Ok::<_, io::Error>(stats)
}
@@ -409,6 +420,7 @@ mod worker {
async fn compress_rollouts_in_root(
root: &Path,
started_at: Instant,
reference_index: &RolloutReferenceIndex,
stats: &mut CompressionStats,
) -> io::Result<()> {
if !tokio::fs::try_exists(root).await.unwrap_or(false) {
@@ -467,6 +479,21 @@ mod worker {
continue;
}
let path = rollout_file.into_path();
let Ok(meta) = crate::read_session_meta_line(path.as_path()).await else {
stats.skipped = stats.skipped.saturating_add(1);
metrics::file("skipped_unreadable_meta");
continue;
};
if reference_index.reference_count(meta.meta.id) > 0 {
stats.skipped = stats.skipped.saturating_add(1);
metrics::file("skipped_referenced");
continue;
}
if meta.meta.history_base.is_some() {
stats.skipped = stats.skipped.saturating_add(1);
metrics::file("skipped_fork_pointer");
continue;
}
stats.scanned = stats.scanned.saturating_add(1);
metrics::file("scanned");
while jobs.len() >= MAX_CONCURRENT_COMPRESSION_JOBS {