Files
codex/codex-rs/state/src/audit.rs
T
Charlie Marsh dddc3c0a79 Skip unrelated indexed rollouts during selected thread migration (#57483)
## Why

Migrating selected threads reads rollout headers even for unrelated threads. Use the state database to skip files whose indexed owner and canonical filename agree that they belong to an unselected thread.

## What changed

- Read thread IDs and rollout paths without creating or migrating the state database, and fall back to header inspection if the index cannot be read immediately.
- Keep inspecting selected, unindexed, misnamed, and conflicting paths so rollout headers still determine ownership.
- Recognize stable thread IDs in reverted rollout filenames and match plain and compressed paths when filtering.

## Testing

Add a regression test covering pruning of unrelated indexed rollouts, including archived and compressed files, while retaining header inspection for selected and ambiguously named files.

GitOrigin-RevId: d76827174c00f5bc9e38ad9b69dadeee91f4415f
2026-10-27 15:19:58 +00:00

66 lines
2.1 KiB
Rust

//! Read-only queries against an existing state database.
use anyhow::Result;
use codex_protocol::ThreadId;
use sqlx::Row;
use std::path::PathBuf;
use std::time::Duration;
use crate::SqliteConfig;
/// Minimal thread metadata used by read-only state database audits.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThreadStateAuditRow {
pub id: String,
pub rollout_path: PathBuf,
pub archived: bool,
pub source: String,
pub model_provider: String,
}
/// Read persisted thread rows from a state DB without creating, migrating, or repairing it.
pub async fn read_thread_state_audit_rows(
sqlite: &SqliteConfig,
) -> Result<Vec<ThreadStateAuditRow>> {
let pool = sqlite
.open_read_only_pool(&sqlite.state_db_path(), /*busy_timeout*/ None)
.await?;
let rows = sqlx::query(
r#"
SELECT id, rollout_path, archived, source, model_provider
FROM threads
"#,
)
.fetch_all(&pool)
.await?;
pool.close().await;
rows.into_iter()
.map(|row| {
let archived: i64 = row.try_get("archived")?;
Ok(ThreadStateAuditRow {
id: row.try_get("id")?,
rollout_path: PathBuf::from(row.try_get::<String, _>("rollout_path")?),
archived: archived != 0,
source: row.try_get("source")?,
model_provider: row.try_get("model_provider")?,
})
})
.collect()
}
/// Reads only the thread IDs and selected paths, without creating or migrating the state DB.
pub async fn read_thread_rollout_paths(sqlite: &SqliteConfig) -> Result<Vec<(ThreadId, PathBuf)>> {
// Migration can scan the rollout headers immediately if the index is locked.
let pool = sqlite
.open_read_only_pool(&sqlite.state_db_path(), Some(Duration::ZERO))
.await?;
let rows = sqlx::query_as::<_, (String, String)>("SELECT id, rollout_path FROM threads")
.fetch_all(&pool)
.await?;
pool.close().await;
rows.into_iter()
.map(|(id, path)| Ok((ThreadId::from_string(&id)?, PathBuf::from(path))))
.collect()
}