diff --git a/codex-rs/tui/src/analytics/chats.rs b/codex-rs/tui/src/analytics/chats.rs index 3fea700319..ffed3ac661 100644 --- a/codex-rs/tui/src/analytics/chats.rs +++ b/codex-rs/tui/src/analytics/chats.rs @@ -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() diff --git a/codex-rs/tui/src/analytics/chats_tests.rs b/codex-rs/tui/src/analytics/chats_tests.rs index c852cbc749..a78390a76f 100644 --- a/codex-rs/tui/src/analytics/chats_tests.rs +++ b/codex-rs/tui/src/analytics/chats_tests.rs @@ -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::>(); - 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, []]) ] diff --git a/codex-rs/tui/src/analytics/tasks_tests.rs b/codex-rs/tui/src/analytics/tasks_tests.rs index bc1a6b1f31..6583d525ca 100644 --- a/codex-rs/tui/src/analytics/tasks_tests.rs +++ b/codex-rs/tui/src/analytics/tasks_tests.rs @@ -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) {