Use the state database first for TUI analytics thread lists (#62138)

## Why

Listing root threads for chat and task analytics can trigger filesystem
scans and metadata repair. The state database can serve these lists
without that work.

## What changed

Start root thread pagination with `thread/list` and `useStateDbOnly`
enabled. If the server returns JSON-RPC error `-32603`, discard the
partial results and restart from the first page with filesystem repair
enabled. SQLite cursors may identify different pages in the filesystem
listing.

## Testing

The chat analytics regression test simulates an error on the second
indexed page and checks that the fallback restarts pagination, excludes
discarded results, and preserves the usage estimate budget. The task
analytics test checks that only root listings use the database-only mode.

GitOrigin-RevId: 9de78a9eca4662df06d22a86cffb6e7bcba428cb
This commit is contained in:
Charlie Marsh
2026-11-18 00:18:42 +00:00
committed by copyberry
parent 44de9d99ef
commit 3e5f072ca1
3 changed files with 53 additions and 9 deletions
+20 -3
View File
@@ -4,6 +4,7 @@ use super::client::Live;
use super::client::Session;
use super::data;
use codex_app_server_client::AppServerRequestHandle;
use codex_app_server_client::TypedRequestError;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::SortDirection;
@@ -113,17 +114,33 @@ pub(super) async fn roots(
let mut threads = Vec::new();
let mut cursor = None;
let mut seen_cursors = HashSet::new();
let mut use_state_db_only = true;
loop {
let page: ThreadListResponse = handle
let result = handle
.request_typed(ClientRequest::ThreadList {
request_id: RequestId::String(uuid::Uuid::new_v4().to_string()),
params: ThreadListParams {
cursor,
use_state_db_only,
..list_params()
},
})
.await
.map_err(data::error)?;
.await;
let page: ThreadListResponse = match result {
Ok(page) => page,
Err(TypedRequestError::Server { source, .. })
if use_state_db_only && source.code == -32603 =>
{
// Restart the entire walk: a cursor from SQLite need not identify the
// same page in the repairing filesystem listing.
use_state_db_only = false;
threads.clear();
cursor = None;
seen_cursors.clear();
continue;
}
Err(err) => return Err(data::error(err)),
};
let reached_cutoff = page.data.iter().any(|thread| thread.updated_at < cutoff);
threads.extend(page.data.into_iter().filter(|thread| {
thread.updated_at >= cutoff && !thread.ephemeral && thread.parent_thread_id.is_none()
+29 -5
View File
@@ -43,7 +43,7 @@ async fn incomplete_chat_batches_retain_available_estimates_without_inventing_ze
}
#[tokio::test]
async fn slow_repairing_chat_listing_keeps_estimate_budget_and_ranks_across_pages() {
async fn indexed_chat_listing_restarts_on_database_error_and_keeps_estimate_budget() {
use codex_app_server_client::RemoteAppServerClient;
use codex_app_server_client::RemoteAppServerConnectArgs;
use codex_app_server_client::RemoteAppServerEndpoint;
@@ -74,12 +74,33 @@ async fn slow_repairing_chat_listing_keeps_estimate_budget_and_ranks_across_page
params["modelProviders"]
]));
if pages.len() == 1 {
// Simulate repair exceeding the HTTP estimate budget without a wall-clock wait.
// Listing has its own budget, separate from the usage estimate requests.
tokio::time::pause();
tokio::time::advance(std::time::Duration::from_secs(/*secs*/ 26)).await;
tokio::time::resume();
}
let range = if pages.len() == 1 { 0..100 } else { 100..101 };
if pages.len() == 2 {
socket
.send(Message::Text(
json!({
"jsonrpc":"2.0", "id":request["id"],
"error":{"code":-32603,"message":"state DB unavailable"}
})
.to_string()
.into(),
))
.await
.unwrap();
continue;
}
let first_page = params["cursor"].is_null();
let range = if pages.len() == 1 {
2..102
} else if first_page {
0..100
} else {
100..101
};
let data = range.map(|index| json!({
"id": format!("chat-{index}"), "sessionId": format!("chat-{index}"),
"preview": "", "name": format!("Chat {index}"), "ephemeral": false,
@@ -87,7 +108,7 @@ async fn slow_repairing_chat_listing_keeps_estimate_budget_and_ranks_across_page
"updatedAt": chrono::Utc::now().timestamp(), "status": {"type": "idle"},
"cwd": std::env::temp_dir(), "cliVersion": "test", "source": "cli", "turns": [],
})).collect::<Vec<_>>();
json!({"data": data, "nextCursor": if pages.len() == 1 { Some("next") } else { None }})
json!({"data": data, "nextCursor": if first_page { Some("next") } else { None }})
}
_ => continue,
};
@@ -99,7 +120,7 @@ async fn slow_repairing_chat_listing_keeps_estimate_budget_and_ranks_across_page
))
.await
.unwrap();
if pages.len() == 2 {
if pages.len() == 4 {
break;
}
}
@@ -142,6 +163,7 @@ async fn slow_repairing_chat_listing_keeps_estimate_budget_and_ranks_across_page
.await
.unwrap()
.unwrap();
assert!(chats.rows.iter().all(|chat| chat.title != "Chat 101"));
assert_eq!(
(
chats.rows.len(),
@@ -154,6 +176,8 @@ async fn slow_repairing_chat_listing_keeps_estimate_budget_and_ranks_across_page
assert_eq!(
rpc.await.unwrap(),
vec![
json!([null, 100, true, []]),
json!(["next", 100, true, []]),
json!([null, 100, false, []]),
json!(["next", 100, false, []])
]
+4 -1
View File
@@ -49,8 +49,11 @@ async fn consumer_roots_include_paginated_archived_descendants_and_keep_missing_
}
let params = &request["params"];
assert_eq!(params["modelProviders"], json!([]));
assert_ne!(params["useStateDbOnly"], json!(true));
let ancestor = params["ancestorThreadId"].as_str();
assert_eq!(
params["useStateDbOnly"].as_bool().unwrap_or_default(),
ancestor.is_none()
);
let archived = params["archived"] == true;
let cursor = params["cursor"].as_str();
let ids = match (ancestor, archived, cursor) {