diff --git a/codex-rs/app-server/tests/suite/v2/thread_list.rs b/codex-rs/app-server/tests/suite/v2/thread_list.rs index d8c652aed7..45e77b806c 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_list.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_list.rs @@ -1820,6 +1820,15 @@ async fn thread_list_empty_source_kinds_defaults_to_interactive_only() -> Result Some("mock_provider"), /*git_info*/ None, )?; + let desktop_id = create_fake_rollout_with_source( + codex_home.path(), + "2025-02-01T10-30-00", + "2025-02-01T10:30:00Z", + "Desktop", + Some("mock_provider"), + /*git_info*/ None, + CoreSessionSource::Custom("chatgpt".to_string()), + )?; let exec_id = create_fake_rollout_with_source( codex_home.path(), "2025-02-01T11-00-00", @@ -1846,9 +1855,9 @@ async fn thread_list_empty_source_kinds_defaults_to_interactive_only() -> Result assert_eq!(next_cursor, None); let ids: Vec<_> = data.iter().map(|thread| thread.id.as_str()).collect(); - assert_eq!(ids, vec![cli_id.as_str()]); + assert_eq!(ids, vec![desktop_id.as_str(), cli_id.as_str()]); assert_ne!(cli_id, exec_id); - assert_eq!(data[0].source, SessionSource::Cli); + assert_eq!(data[1].source, SessionSource::Cli); Ok(()) } diff --git a/codex-rs/state/migrations/0063_thread_listing_indexes.sql b/codex-rs/state/migrations/0063_thread_listing_indexes.sql new file mode 100644 index 0000000000..75b3181beb --- /dev/null +++ b/codex-rs/state/migrations/0063_thread_listing_indexes.sql @@ -0,0 +1,22 @@ +-- Let attached-thread listings seek by both filters, even without planner statistics. +DROP INDEX idx_threads_attached_parent; +CREATE INDEX idx_threads_attached_parent + ON threads(attached_thread_parent_id, archived) + WHERE attached_thread_parent_id IS NOT NULL; + +-- Keep the default visible chat listing in timestamp order without scanning subagents. +-- The literal source predicates match the default-source branch of push_list_threads_query. +CREATE INDEX idx_threads_interactive_created_at_ms + ON threads(archived, created_at_ms DESC, id DESC) + WHERE archived = 0 AND preview <> '' AND attached_thread_parent_id IS NULL + AND source IN ('cli', 'vscode', '{"custom":"atlas"}', '{"custom":"chatgpt"}'); + +CREATE INDEX idx_threads_interactive_updated_at_ms + ON threads(archived, updated_at_ms DESC, id DESC) + WHERE archived = 0 AND preview <> '' AND attached_thread_parent_id IS NULL + AND source IN ('cli', 'vscode', '{"custom":"atlas"}', '{"custom":"chatgpt"}'); + +CREATE INDEX idx_threads_interactive_recency_at_ms + ON threads(archived, recency_at_ms DESC, id DESC) + WHERE archived = 0 AND preview <> '' AND attached_thread_parent_id IS NULL + AND source IN ('cli', 'vscode', '{"custom":"atlas"}', '{"custom":"chatgpt"}'); diff --git a/codex-rs/state/src/runtime/threads.rs b/codex-rs/state/src/runtime/threads.rs index 2f67a26c3a..7bbcb712c3 100644 --- a/codex-rs/state/src/runtime/threads.rs +++ b/codex-rs/state/src/runtime/threads.rs @@ -1391,12 +1391,49 @@ WITH RECURSIVE subtree(child_thread_id, parent_thread_id) AS ( } None => builder.push(" FROM threads"), }; + // Keep the stored source values and their order in sync with migration 0063. + // SQLite needs a literal IN list to recognize the partial-index predicate. + const INDEXED_INTERACTIVE_SOURCES: [&str; 4] = [ + "cli", + "vscode", + r#"{"custom":"atlas"}"#, + r#"{"custom":"chatgpt"}"#, + ]; + let use_interactive_source_literals = relation_filter.is_none() + && !filters.archived_only + && filters.cwd_filters.is_none() + && filters.section.is_none() + && filters.project_id.is_none() + && filters.search_term.is_none() + && filters.allowed_sources.len() == INDEXED_INTERACTIVE_SOURCES.len() + && INDEXED_INTERACTIVE_SOURCES.iter().all(|source| { + filters + .allowed_sources + .iter() + .any(|allowed| allowed.as_str() == *source) + }); + let allowed_sources = if use_interactive_source_literals { + &[] + } else { + filters.allowed_sources + }; push_thread_filters_with_preview( builder, - filters, + ThreadFilterOptions { + allowed_sources, + ..filters + }, /*include_thread_id_tiebreaker*/ true, relation_filter, ); + if use_interactive_source_literals { + builder.push(" AND threads.source IN ("); + let mut separated = builder.separated(", "); + for source in INDEXED_INTERACTIVE_SOURCES { + separated.push(format_args!("'{source}'")); + } + separated.push_unseparated(")"); + } if !matches!( relation_filter, Some( @@ -2809,6 +2846,58 @@ mod tests { } } + #[tokio::test] + async fn default_sidebar_listing_uses_the_interactive_timestamp_index() -> anyhow::Result<()> { + let codex_home = unique_temp_dir(); + let runtime = StateRuntime::init( + crate::SqliteConfig::new_for_testing(codex_home.as_path().abs()), + "test-provider".to_string(), + ) + .await?; + let allowed_sources = [ + SessionSource::VSCode, + SessionSource::Custom("chatgpt".to_string()), + SessionSource::Cli, + SessionSource::Custom("atlas".to_string()), + ] + .iter() + .map(crate::extract::enum_to_string) + .collect::>(); + let model_providers = ["test-provider".to_string()]; + + let mut builder = QueryBuilder::::new("EXPLAIN QUERY PLAN "); + push_list_threads_query( + &mut builder, + ThreadFilterOptions { + archived_only: false, + allowed_sources: &allowed_sources, + model_providers: Some(&model_providers), + cwd_filters: None, + section: None, + project_id: None, + anchor: None, + sort_key: SortKey::UpdatedAt, + sort_direction: SortDirection::Desc, + search_term: None, + }, + /*relation_filter*/ None, + /*limit*/ 56, + ); + let plan = builder + .build() + .fetch_all(runtime.pool.as_ref()) + .await? + .into_iter() + .map(|row| row.get::("detail")) + .collect::>(); + assert!( + plan.iter() + .any(|detail| detail.contains("idx_threads_interactive_updated_at_ms")), + "the sidebar listing should skip unrelated sources: {plan:?}" + ); + Ok(()) + } + #[tokio::test] async fn archived_thread_pages_use_ordered_indexes_without_sorting() { let codex_home = unique_temp_dir();