mirror of
https://github.com/openai/codex.git
synced 2026-11-29 15:36:12 +00:00
Restore archived sessions from the resume picker (#37371)
## What changed - Add an Active/Archived status control to the resume picker and use it when listing sessions. - Restore an archived session before resuming it, with inline error handling and duplicate-request prevention while restoration is pending. - Keep directory filtering available for archived sessions and hide the archive shortcut while viewing them. ## Testing - Add coverage for archived-session filtering, successful restoration, failure and retry behavior, toolbar rendering, and keyboard navigation. GitOrigin-RevId: db3f5935e0f0bba12a110ba9c7aaf9d40abd4a4f
This commit is contained in:
1 parent
1efd8fcb26
commit
0bdce9f424
4 files changed
+325
-41
No files matched your search
@@ -10,6 +10,9 @@ use super::Row;
|
||||
use super::SearchState;
|
||||
use super::SessionPickerAction;
|
||||
use super::SessionPickerLaunchContext;
|
||||
use super::SessionSelection;
|
||||
use super::SessionStatus;
|
||||
use super::SessionTarget;
|
||||
use crate::keymap::KeymapContext;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
@@ -19,11 +22,16 @@ pub(super) enum ArchiveState {
|
||||
Pending {
|
||||
thread_id: ThreadId,
|
||||
},
|
||||
Restoring {
|
||||
thread_id: ThreadId,
|
||||
},
|
||||
}
|
||||
|
||||
impl PickerState {
|
||||
pub(super) fn archive_shortcut_available(&self) -> bool {
|
||||
if !matches!(self.action, SessionPickerAction::Resume) {
|
||||
if !matches!(self.action, SessionPickerAction::Resume)
|
||||
|| self.status == SessionStatus::Archived
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -127,6 +135,36 @@ impl PickerState {
|
||||
self.load_more_if_needed(LoadTrigger::Search { token });
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn request_unarchive(&mut self, thread_id: ThreadId) {
|
||||
if !matches!(self.archive_state, ArchiveState::Idle) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.archive_state = ArchiveState::Restoring { thread_id };
|
||||
self.request_frame();
|
||||
(self.picker_loader)(PickerLoadRequest::Unarchive { thread_id });
|
||||
}
|
||||
|
||||
pub(super) fn handle_unarchive_result(
|
||||
&mut self,
|
||||
thread_id: ThreadId,
|
||||
result: std::io::Result<SessionTarget>,
|
||||
) -> Option<SessionSelection> {
|
||||
if self.archive_state != (ArchiveState::Restoring { thread_id }) {
|
||||
return None;
|
||||
}
|
||||
self.archive_state = ArchiveState::Idle;
|
||||
|
||||
match result {
|
||||
Ok(target) => Some(SessionSelection::Resume(target)),
|
||||
Err(error) => {
|
||||
self.inline_error = Some(format!("Failed to restore archived session: {error}"));
|
||||
self.request_frame();
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -5,16 +5,21 @@ use super::super::ProviderFilter;
|
||||
use super::super::Row;
|
||||
use super::super::SessionPickerAction;
|
||||
use super::super::SessionPickerLaunchContext;
|
||||
use super::super::SessionSelection;
|
||||
use super::super::SessionStatus;
|
||||
use super::super::SessionTarget;
|
||||
use super::ArchiveState;
|
||||
use crate::key_hint::KeyBinding;
|
||||
use crate::tui::FrameRequester;
|
||||
use codex_app_server_client::TypedRequestError;
|
||||
use codex_app_server_protocol::JSONRPCErrorError;
|
||||
use codex_app_server_protocol::ThreadSortKey;
|
||||
use codex_protocol::ThreadId;
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
use crossterm::event::KeyModifiers;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
@@ -22,7 +27,9 @@ fn archive_picker_state() -> (PickerState, Arc<Mutex<Vec<ThreadId>>>) {
|
||||
let requests = Arc::new(Mutex::new(Vec::new()));
|
||||
let request_sink = Arc::clone(&requests);
|
||||
let picker_loader: PickerLoader = Arc::new(move |request| {
|
||||
if let PickerLoadRequest::Archive { thread_id } = request {
|
||||
if let PickerLoadRequest::Archive { thread_id }
|
||||
| PickerLoadRequest::Unarchive { thread_id } = request
|
||||
{
|
||||
request_sink
|
||||
.lock()
|
||||
.expect("archive request sink")
|
||||
@@ -155,3 +162,133 @@ fn archive_footer_shows_shortcut_for_resume_sessions() {
|
||||
ctrl+o dense view ctrl+t transcript ctrl+e expand ↑/↓ browse
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn archived_status_preserves_directory_filter_and_hides_archive_shortcut() {
|
||||
let requested_filters = Arc::new(Mutex::new(Vec::new()));
|
||||
let request_sink = Arc::clone(&requested_filters);
|
||||
let picker_loader: PickerLoader = Arc::new(move |request| {
|
||||
if let PickerLoadRequest::Page(request) = request {
|
||||
request_sink
|
||||
.lock()
|
||||
.expect("page request sink")
|
||||
.push((request.status, request.cwd_filter));
|
||||
}
|
||||
});
|
||||
let mut state = PickerState::new(
|
||||
FrameRequester::test_dummy(),
|
||||
picker_loader,
|
||||
ProviderFilter::Any,
|
||||
/*show_all*/ false,
|
||||
Some(PathBuf::from("/tmp/project")),
|
||||
SessionPickerAction::Resume,
|
||||
);
|
||||
|
||||
state.toggle_status();
|
||||
|
||||
assert_eq!(state.status, SessionStatus::Archived);
|
||||
let params = super::super::thread_list_params(
|
||||
/*cursor*/ None,
|
||||
Some(std::path::Path::new("/tmp/project")),
|
||||
SessionStatus::Archived,
|
||||
ProviderFilter::Any,
|
||||
ThreadSortKey::UpdatedAt,
|
||||
/*include_non_interactive*/ false,
|
||||
/*use_state_db_only*/ false,
|
||||
);
|
||||
assert_eq!(params.archived, Some(true));
|
||||
|
||||
set_selected_session(&mut state, ThreadId::new());
|
||||
assert!(!state.archive_shortcut_available());
|
||||
let footer = super::super::footer_hint_lines(&state, /*width*/ 220)
|
||||
.into_iter()
|
||||
.map(|line| line.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
insta::assert_snapshot!(footer, @r"
|
||||
enter restore esc start new ctrl+c quit tab focus sort/filter ←/→ change option
|
||||
ctrl+o dense view ctrl+t transcript ctrl+e expand ↑/↓ browse
|
||||
");
|
||||
insta::assert_snapshot!(
|
||||
super::super::toolbar_line(&state, /*compact*/ true).to_string(),
|
||||
@"Filter:[Cwd] [Archived] Sort:[Updated]"
|
||||
);
|
||||
|
||||
state.toggle_filter_mode();
|
||||
|
||||
assert_eq!(
|
||||
*requested_filters.lock().unwrap(),
|
||||
vec![
|
||||
(SessionStatus::Archived, Some(PathBuf::from("/tmp/project")),),
|
||||
(SessionStatus::Archived, None),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn archived_session_restore_resumes_after_completion() {
|
||||
let (mut state, requests) = archive_picker_state();
|
||||
let thread_id = ThreadId::new();
|
||||
state.status = SessionStatus::Archived;
|
||||
set_selected_session(&mut state, thread_id);
|
||||
|
||||
assert!(
|
||||
state
|
||||
.handle_key(KeyEvent::from(KeyCode::Enter))
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
assert_eq!(state.archive_state, ArchiveState::Restoring { thread_id });
|
||||
assert_eq!(*requests.lock().unwrap(), vec![thread_id]);
|
||||
|
||||
assert!(
|
||||
state
|
||||
.handle_key(KeyEvent::from(KeyCode::Enter))
|
||||
.await
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
assert_eq!(*requests.lock().unwrap(), vec![thread_id]);
|
||||
|
||||
let selection = state.handle_unarchive_result(
|
||||
thread_id,
|
||||
Ok(SessionTarget {
|
||||
path: None,
|
||||
thread_id,
|
||||
}),
|
||||
);
|
||||
|
||||
assert!(matches!(
|
||||
selection,
|
||||
Some(SessionSelection::Resume(SessionTarget {
|
||||
path: None,
|
||||
thread_id: resumed_thread_id,
|
||||
})) if resumed_thread_id == thread_id
|
||||
));
|
||||
assert_eq!(state.archive_state, ArchiveState::Idle);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn archived_session_restore_failure_preserves_session_and_allows_retry() {
|
||||
let (mut state, requests) = archive_picker_state();
|
||||
let thread_id = ThreadId::new();
|
||||
state.status = SessionStatus::Archived;
|
||||
set_selected_session(&mut state, thread_id);
|
||||
state.request_unarchive(thread_id);
|
||||
|
||||
let selection =
|
||||
state.handle_unarchive_result(thread_id, Err(std::io::Error::other("restore denied")));
|
||||
|
||||
assert!(selection.is_none());
|
||||
assert_eq!(state.archive_state, ArchiveState::Idle);
|
||||
assert_eq!(
|
||||
state.inline_error.as_deref(),
|
||||
Some("Failed to restore archived session: restore denied")
|
||||
);
|
||||
assert_eq!(state.filtered_rows.len(), 1);
|
||||
|
||||
state.request_unarchive(thread_id);
|
||||
|
||||
assert_eq!(*requests.lock().unwrap(), vec![thread_id, thread_id]);
|
||||
}
|
||||
Reference in new issue
Block a user