Optimize default and attached thread listings with targeted indexes (#61727)

## Why

Default thread listings should stay in timestamp order without scanning subagent threads. Attached-thread listings should use both parent and archive filters even without planner statistics.

## What changed

- Add partial indexes for visible interactive threads sorted by creation, update, or recency timestamps.
- Use literal source predicates for default interactive listings so SQLite can select the partial indexes.
- Extend the attached-parent index to include archive status.

## Testing

Add a query-plan test verifying that the default sidebar listing uses the interactive update-time index. Extend app-server coverage to include `chatgpt` custom-source threads in default interactive listings.

GitOrigin-RevId: 5844622b7af629cad71e15ec95a8960739d6d447
This commit is contained in:
Dmitriy Kovalenko
2026-11-16 02:53:45 +00:00
committed by copyberry
parent eff3036bf4
commit ca0bbbdf0c
3 changed files with 123 additions and 3 deletions
@@ -1820,6 +1820,15 @@ async fn thread_list_empty_source_kinds_defaults_to_interactive_only() -> Result
Some("mock_provider"), Some("mock_provider"),
/*git_info*/ None, /*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( let exec_id = create_fake_rollout_with_source(
codex_home.path(), codex_home.path(),
"2025-02-01T11-00-00", "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); assert_eq!(next_cursor, None);
let ids: Vec<_> = data.iter().map(|thread| thread.id.as_str()).collect(); 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_ne!(cli_id, exec_id);
assert_eq!(data[0].source, SessionSource::Cli); assert_eq!(data[1].source, SessionSource::Cli);
Ok(()) Ok(())
} }
@@ -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"}');
+90 -1
View File
@@ -1391,12 +1391,49 @@ WITH RECURSIVE subtree(child_thread_id, parent_thread_id) AS (
} }
None => builder.push(" FROM threads"), 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( push_thread_filters_with_preview(
builder, builder,
filters, ThreadFilterOptions {
allowed_sources,
..filters
},
/*include_thread_id_tiebreaker*/ true, /*include_thread_id_tiebreaker*/ true,
relation_filter, 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!( if !matches!(
relation_filter, relation_filter,
Some( 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::<Vec<_>>();
let model_providers = ["test-provider".to_string()];
let mut builder = QueryBuilder::<Sqlite>::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::<String, _>("detail"))
.collect::<Vec<_>>();
assert!(
plan.iter()
.any(|detail| detail.contains("idx_threads_interactive_updated_at_ms")),
"the sidebar listing should skip unrelated sources: {plan:?}"
);
Ok(())
}
#[tokio::test] #[tokio::test]
async fn archived_thread_pages_use_ordered_indexes_without_sorting() { async fn archived_thread_pages_use_ordered_indexes_without_sorting() {
let codex_home = unique_temp_dir(); let codex_home = unique_temp_dir();