mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Filter thread listings before scanning rollout previews (#61902)
## What changed Apply source, model provider, working directory, and attached-thread filters as soon as the first session metadata record is read. Stop reading excluded rollouts before scanning for preview and user-message events. ## Testing Add a regression test confirming that copied metadata with a different source cannot override the canonical session metadata when filtering thread listings. GitOrigin-RevId: 2a9ab6cf419c94884b672b6f03ec23b43b244c83
This commit is contained in:
@@ -790,34 +790,15 @@ async fn build_thread_item(
|
||||
) -> Option<ThreadItem> {
|
||||
// Read head and detect preview-bearing events; goal previews can appear before
|
||||
// the first normal user message.
|
||||
let summary = read_head_summary(&path, HEAD_RECORD_LIMIT)
|
||||
let filters = SummaryFilters {
|
||||
allowed_sources,
|
||||
provider_matcher,
|
||||
cwd_filters,
|
||||
include_attached_threads,
|
||||
};
|
||||
let summary = read_head_summary(&path, HEAD_RECORD_LIMIT, filters)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
if summary.attached_to_thread_id.is_some() && !include_attached_threads {
|
||||
return None;
|
||||
}
|
||||
if !allowed_sources.is_empty()
|
||||
&& !summary
|
||||
.source
|
||||
.as_ref()
|
||||
.is_some_and(|source| allowed_sources.contains(source))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
if let Some(matcher) = provider_matcher
|
||||
&& !matcher.matches(summary.model_provider.as_deref())
|
||||
{
|
||||
return None;
|
||||
}
|
||||
if let Some(cwd_filters) = cwd_filters
|
||||
&& !summary.cwd.as_ref().is_some_and(|cwd| {
|
||||
cwd_filters
|
||||
.iter()
|
||||
.any(|filter| path_utils::paths_match_after_normalization(cwd, filter))
|
||||
})
|
||||
{
|
||||
return None;
|
||||
}
|
||||
.ok()??;
|
||||
if summary.saw_session_meta
|
||||
&& (matches!(preview_filter, PreviewFilter::IncludeEmpty)
|
||||
|| summary.preview.is_some()
|
||||
@@ -974,7 +955,40 @@ impl<'a> ProviderMatcher<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_head_summary(path: &Path, head_limit: usize) -> io::Result<HeadTailSummary> {
|
||||
struct SummaryFilters<'a> {
|
||||
allowed_sources: &'a [SessionSource],
|
||||
provider_matcher: Option<&'a ProviderMatcher<'a>>,
|
||||
cwd_filters: Option<&'a [PathBuf]>,
|
||||
include_attached_threads: bool,
|
||||
}
|
||||
|
||||
impl SummaryFilters<'_> {
|
||||
/// Filters only the rollout's first session metadata; copied fork metadata cannot override it.
|
||||
fn matches(&self, summary: &HeadTailSummary) -> bool {
|
||||
(self.include_attached_threads || summary.attached_to_thread_id.is_none())
|
||||
&& (self.allowed_sources.is_empty()
|
||||
|| summary
|
||||
.source
|
||||
.as_ref()
|
||||
.is_some_and(|source| self.allowed_sources.contains(source)))
|
||||
&& self
|
||||
.provider_matcher
|
||||
.is_none_or(|matcher| matcher.matches(summary.model_provider.as_deref()))
|
||||
&& self.cwd_filters.is_none_or(|filters| {
|
||||
summary.cwd.as_ref().is_some_and(|cwd| {
|
||||
filters
|
||||
.iter()
|
||||
.any(|filter| path_utils::paths_match_after_normalization(cwd, filter))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_head_summary(
|
||||
path: &Path,
|
||||
head_limit: usize,
|
||||
filters: SummaryFilters<'_>,
|
||||
) -> io::Result<Option<HeadTailSummary>> {
|
||||
let mut lines = compression::open_rollout_line_reader(path).await?;
|
||||
let mut summary = HeadTailSummary::default();
|
||||
let mut lines_scanned = 0usize;
|
||||
@@ -1047,6 +1061,9 @@ async fn read_head_summary(path: &Path, head_limit: usize) -> io::Result<HeadTai
|
||||
summary.cli_version = Some(session_meta_line.meta.cli_version);
|
||||
summary.created_at = Some(session_meta_line.meta.timestamp.clone());
|
||||
summary.saw_session_meta = true;
|
||||
if !filters.matches(&summary) {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
if codex_state::is_guardian_review_source(&session_meta_line.meta.source) {
|
||||
// The synthetic prompt is not needed for the thread summary.
|
||||
@@ -1108,7 +1125,7 @@ async fn read_head_summary(path: &Path, head_limit: usize) -> io::Result<HeadTai
|
||||
}
|
||||
}
|
||||
|
||||
Ok(summary)
|
||||
Ok(Some(summary))
|
||||
}
|
||||
|
||||
/// Read up to `HEAD_RECORD_LIMIT` records from the start of the rollout file at `path`.
|
||||
|
||||
@@ -1928,6 +1928,44 @@ async fn test_source_filter_excludes_non_matching_sessions() {
|
||||
}));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_listing_filters_use_canonical_session_metadata() -> Result<()> {
|
||||
let home = TempDir::new()?;
|
||||
let id = Uuid::from_u128(78);
|
||||
let ts = "2025-08-03T10-00-00";
|
||||
write_session_file(
|
||||
home.path(),
|
||||
ts,
|
||||
id,
|
||||
/*num_records*/ 0,
|
||||
Some(SessionSource::Exec),
|
||||
)?;
|
||||
let path = home
|
||||
.path()
|
||||
.join(format!("sessions/2025/08/03/rollout-{ts}-{id}.jsonl"));
|
||||
let contents = fs::read_to_string(&path)?;
|
||||
let mut lines = contents.lines();
|
||||
let canonical = lines.next().expect("canonical metadata");
|
||||
let user = lines.next().expect("user event");
|
||||
let mut copied: serde_json::Value = serde_json::from_str(canonical)?;
|
||||
copied["payload"]["source"] = serde_json::json!("cli");
|
||||
fs::write(&path, format!("{canonical}\n{copied}\n{user}\n"))?;
|
||||
|
||||
let page = get_threads(
|
||||
home.path(),
|
||||
/*page_size*/ 10,
|
||||
/*cursor*/ None,
|
||||
ThreadSortKey::CreatedAt,
|
||||
INTERACTIVE_SESSION_SOURCES.as_slice(),
|
||||
/*model_providers*/ None,
|
||||
/*cwd_filters*/ None,
|
||||
TEST_PROVIDER,
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(page.items, Vec::new());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_model_provider_filter_selects_only_matching_sessions() -> Result<()> {
|
||||
let temp = TempDir::new().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user