Add archive and delete actions to the agents overview (#44433)

## What changed

- Add `Ctrl+E` to archive the selected task and its child agents, and `Delete` to permanently delete their history. Both actions require confirmation, with Cancel selected by default.
- Expose `agents.archive` and `agents.delete` in keymap configuration and the shortcut picker. Default bindings yield to existing custom bindings and chords.
- Show progress while requests run, keeping rendering responsive and blocking task switching. Refresh overview state afterward and keep the dashboard open when the current task is removed.
- Report failures and preserve the current attachment and draft when a rejected operation leaves the task loaded.

## Testing

Add coverage for confirmation and cancellation, active and background task removal, child-agent attachments, stale refresh invalidation, failure recovery, and draft preservation. Add progress input-isolation tests, shortcut conflict tests, and snapshots for confirmation, errors, progress, and keymap rendering.

GitOrigin-RevId: b327bb8834699d0058f9ffb903ace525ae270500
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-10 06:06:21 +00:00
1 parent bf5ebd98c5
commit b348fc2667
32 files changed
+1095 -46

No files matched your search

@@ -1,10 +1,22 @@
use super::*;
use crate::app_event::AgentsOverviewAction;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ThreadLoadedListParams;
use codex_app_server_protocol::TurnStartParams;
use codex_app_server_protocol::TurnStartResponse;
use codex_model_provider_info::ModelProviderInfo;
use core_test_support::responses;
use core_test_support::streaming_sse::StreamingSseChunk;
use core_test_support::streaming_sse::start_streaming_sse_server;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn lifecycle_shortcuts_target_filtered_task_in_any_state() {
let mut app = make_test_app().await;
let mut keymap = TuiKeymap::default();
keymap.agents.archive = Some(KeybindingsSpec::One(KeybindingSpec("f5".into())));
keymap.agents.delete = Some(KeybindingsSpec::One(KeybindingSpec("f6".into())));
keymap.agents.hide = Some(KeybindingsSpec::One(KeybindingSpec("f7".into())));
app.keymap = RuntimeKeymap::from_config(&keymap).unwrap();
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
@@ -39,6 +51,17 @@ async fn lifecycle_shortcuts_target_filtered_task_in_any_state() {
for character in "Target".chars() {
view.handle_key_event(KeyCode::Char(character).into());
}
for (key, expected) in [
(5, AgentsOverviewAction::Archive),
(6, AgentsOverviewAction::Delete),
] {
view.handle_key_event(KeyCode::F(key).into());
let event = rx.try_recv();
assert!(
matches!(event, Ok(AppEvent::ConfirmAgentsOverviewAction { thread_id, action }) if thread_id == target && action == expected),
"unexpected event: {event:?}"
);
}
view.handle_key_event(KeyCode::F(7).into());
assert!(
matches!(rx.try_recv(), Ok(AppEvent::HideAgentsOverviewThread { thread_id }) if thread_id == target)
@@ -129,12 +152,424 @@ async fn hidden_task_stays_hidden_through_activity_and_seed_until_explicit_resum
Ok(())
}
#[tokio::test]
async fn rejected_delete_preserves_a_live_attachment_and_draft() -> Result<()> {
let (mut app, _rx, _op_rx) = crate::app::tests::make_test_app_with_channels().await;
app.config.ephemeral = true;
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
let started = app_server.start_thread(&app.config).await?;
let id = started.session.thread_id;
app.enqueue_primary_thread_session(started.session, started.turns)
.await?;
app.chat_widget
.apply_external_edit("Keep this draft".into());
let draft = app.chat_widget.capture_thread_input_state();
let mut tui = crate::tui::test_support::make_test_tui()?;
tui.pause_events();
// Ephemeral tasks provide a deterministic rejection before server teardown.
Box::pin(app.run_agents_overview_action(
&mut tui,
&mut app_server,
id,
AgentsOverviewAction::Delete,
))
.await?;
assert_eq!(
(app.primary_thread_id, app.chat_widget.thread_id()),
(Some(id), Some(id))
);
assert_eq!(app.chat_widget.capture_thread_input_state(), draft);
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("Could not delete task"));
app_server.shutdown().await?;
Ok(())
}
#[tokio::test]
async fn lifecycle_removes_background_and_current_tasks_without_losing_the_dashboard() -> Result<()>
{
for (action, snapshot, attach_child) in [
(AgentsOverviewAction::Archive, "archive_task", false),
(AgentsOverviewAction::Delete, "delete_task", false),
(AgentsOverviewAction::Archive, "archive_task", true),
(AgentsOverviewAction::Delete, "delete_task", true),
] {
let key = match action {
AgentsOverviewAction::Archive => {
KeyEvent::new(KeyCode::Char('e'), KeyModifiers::CONTROL)
}
AgentsOverviewAction::Delete => KeyCode::Delete.into(),
};
let (mut app, mut rx, _op_rx) =
Box::pin(crate::app::tests::make_test_app_with_channels()).await;
let (release, gate) = tokio::sync::oneshot::channel();
let (server, _completions) = start_streaming_sse_server(vec![vec![
StreamingSseChunk {
gate: None,
body: responses::sse(vec![responses::ev_response_created("running")]),
},
StreamingSseChunk {
gate: Some(gate),
body: responses::sse(vec![responses::ev_completed("running")]),
},
]])
.await;
app.config.model = Some("gpt-5.2".into());
app.config.model_provider_id = "lifecycle-test".into();
app.config.model_provider = ModelProviderInfo {
name: "Lifecycle test".into(),
base_url: Some(format!("{}/v1", server.uri())),
request_max_retries: Some(0),
stream_max_retries: Some(0),
..ModelProviderInfo::default()
};
app_test_support::MockResponsesConfig::new(server.uri())
.with_model("gpt-5.2")
.with_model_provider("lifecycle-test")
.with_provider_name("Lifecycle test")
.write(app.config.codex_home.as_path())?;
let mut app_server =
Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
let id = ThreadId::from_string(
&app_test_support::create_fake_rollout(
&app.config.codex_home,
"2025-01-05T12-00-00",
"2025-01-05T12:00:00Z",
"Current task",
Some(&app.config.model_provider_id),
/*git_info*/ None,
)
.expect("materialize session"),
)?;
let primary = if attach_child {
let child = ThreadId::from_string(
&app_test_support::create_fake_parented_rollout_with_source(
&app.config.codex_home,
"2025-01-05T12-01-00",
"2025-01-05T12:01:00Z",
"Current child",
Some(&app.config.model_provider_id),
/*git_info*/ None,
codex_protocol::protocol::SessionSource::SubAgent(
SubAgentSource::ThreadSpawn {
parent_thread_id: id,
depth: 1,
agent_path: None,
agent_nickname: None,
agent_role: None,
},
),
codex_protocol::SessionId::from(id),
id,
)
.expect("materialize child session"),
)?;
let state_db = codex_state::StateRuntime::init(
codex_state::SqliteConfig::new_for_testing(app.config.codex_home.clone()),
app.config.model_provider_id.clone(),
)
.await
.expect("initialize spawn state");
state_db
.upsert_thread_spawn_edge(
id,
child,
codex_state::DirectionalThreadSpawnEdgeStatus::Open,
)
.await
.expect("persist spawn edge");
app.agents_overview.threads.insert(
child,
Some(overview_thread(
child,
Some(id),
"Current child",
ThreadStatus::Idle,
)),
);
child
} else {
id
};
let resumed = Box::pin(app_server.resume_thread(
&app.local_settings,
app.config.clone(),
primary,
crate::app_server_session::ResumeModelSettings::PreserveExistingThread,
))
.await?;
app.enqueue_primary_thread_session(resumed.session, resumed.turns)
.await?;
app.agents_overview.threads.insert(
id,
Some(overview_thread(
id,
/*parent_thread_id*/ None,
"Current task",
ThreadStatus::Idle,
)),
);
app.app_server_target = AppServerTarget::LocalDaemon {
endpoint: crate::RemoteAppServerEndpoint::UnixSocket {
socket_path: test_path_buf("/tmp/unused.sock").abs(),
},
};
let mut tui = crate::tui::test_support::make_test_tui()?;
tui.pause_events();
app.open_agents_overview(&app_server);
if action == AgentsOverviewAction::Archive {
let rollout = app_server
.thread_read(id, /*include_turns*/ false)
.await?
.path
.expect("saved root history");
let blocked_archive = app
.config
.codex_home
.join("archived_sessions")
.join(rollout.file_name().unwrap());
std::fs::create_dir_all(&blocked_archive)?;
Box::pin(app.run_agents_overview_action(&mut tui, &mut app_server, id, action)).await?;
assert_eq!(
(app.primary_thread_id, app.chat_widget.thread_id()),
(None, None)
);
assert_eq!(app.agents_overview.visible_thread_ids, vec![id]);
assert_eq!(
app_server
.thread_read(primary, /*include_turns*/ false)
.await?
.status,
ThreadStatus::NotLoaded
);
let error = render_bottom_popup(&app.chat_widget, /*width*/ 40);
assert!(error.contains("Could not archive task"));
assert!(error.contains("Work may have stopped."));
app.chat_widget.handle_key_event(KeyCode::Enter.into());
assert!(
app.chat_widget
.selected_index_for_present_view(AGENTS_OVERVIEW_VIEW_ID)
.is_some()
);
std::fs::remove_dir(&blocked_archive)?;
let resumed = Box::pin(app_server.resume_thread(
&app.local_settings,
app.config.clone(),
primary,
crate::app_server_session::ResumeModelSettings::PreserveExistingThread,
))
.await?;
app.enqueue_primary_thread_session(resumed.session, resumed.turns)
.await?;
app.open_agents_overview(&app_server);
}
let background = ThreadId::from_string(
&app_test_support::create_fake_rollout(
&app.config.codex_home,
"2025-01-05T13-00-00",
"2025-01-05T13:00:00Z",
"Background task",
Some(&app.config.model_provider_id),
/*git_info*/ None,
)
.expect("materialize background session"),
)?;
let child = ThreadId::new();
let grandchild = ThreadId::new();
for (target, parent) in [
(background, None),
(child, Some(background)),
(grandchild, Some(child)),
] {
app.agents_overview.threads.insert(
target,
Some(overview_thread(
target,
parent,
"Background task",
ThreadStatus::NotLoaded,
)),
);
}
// Cached details and approval state are invalidated as a group after success.
let request_id = Uuid::new_v4();
app.agents_overview.request_id = Some(request_id);
let stale_threads = app.agents_overview.threads.clone();
Box::pin(app.run_agents_overview_action(&mut tui, &mut app_server, background, action))
.await?;
app.apply_agents_overview_thread_refresh(
&app_server,
request_id,
Ok(AgentsOverviewThreadRefresh {
threads: stale_threads,
last_messages: HashMap::new(),
recent_seed_complete: true,
}),
);
assert_eq!(
(app.primary_thread_id, app.chat_widget.thread_id()),
(Some(primary), Some(primary))
);
assert_eq!(app.agents_overview.visible_thread_ids, vec![id]);
assert_eq!(
app.agents_overview
.threads
.keys()
.copied()
.collect::<std::collections::HashSet<_>>(),
std::collections::HashSet::from([id, primary])
);
Box::pin(app.run_agents_overview_action(
&mut tui,
&mut app_server,
ThreadId::from_string("00000000-0000-0000-0000-000000000001")?,
action,
))
.await?;
assert_eq!(
(app.primary_thread_id, app.chat_widget.thread_id()),
(Some(primary), Some(primary))
);
insta::assert_snapshot!(
format!("{snapshot}_failure"),
render_bottom_popup(&app.chat_widget, /*width*/ 80)
);
app.chat_widget.handle_key_event(KeyCode::Enter.into());
app.chat_widget.handle_key_event(KeyCode::Esc.into());
app_server
.request_handle()
.request_typed::<TurnStartResponse>(ClientRequest::TurnStart {
request_id: RequestId::String(Uuid::new_v4().to_string()),
params: TurnStartParams {
thread_id: primary.to_string(),
input: vec![codex_app_server_protocol::UserInput::Text {
text: "Keep working".into(),
text_elements: Vec::new(),
}],
..Default::default()
},
})
.await?;
tokio::time::timeout(
std::time::Duration::from_secs(/*secs*/ 5),
server.wait_for_request_count(/*count*/ 1),
)
.await?;
assert!(matches!(
app_server
.thread_read(primary, /*include_turns*/ false)
.await?
.status,
ThreadStatus::Active { .. }
));
app.chat_widget.handle_key_event(key);
let confirmation = std::iter::from_fn(|| rx.try_recv().ok())
.find(|event| matches!(event, AppEvent::ConfirmAgentsOverviewAction { .. }))
.expect("shortcut requests confirmation");
Box::pin(app.handle_event(&mut tui, &mut app_server, confirmation)).await?;
insta::assert_snapshot!(
format!("{snapshot}_confirmation"),
render_bottom_popup(&app.chat_widget, /*width*/ 72)
);
app.chat_widget.handle_key_event(KeyCode::Enter.into());
assert!(
!std::iter::from_fn(|| rx.try_recv().ok())
.any(|event| matches!(event, AppEvent::RunAgentsOverviewAction { .. }))
);
app.chat_widget.handle_key_event(key);
let confirmation = std::iter::from_fn(|| rx.try_recv().ok())
.find(|event| matches!(event, AppEvent::ConfirmAgentsOverviewAction { .. }))
.expect("shortcut requests confirmation again");
Box::pin(app.handle_event(&mut tui, &mut app_server, confirmation)).await?;
app.chat_widget.handle_key_event(KeyCode::Down.into());
app.chat_widget.handle_key_event(KeyCode::Enter.into());
let confirmed = std::iter::from_fn(|| rx.try_recv().ok())
.find(|event| matches!(event, AppEvent::RunAgentsOverviewAction { .. }))
.expect("confirmation requests lifecycle action");
if attach_child {
// Removal must not depend on the overview having the primary or its ancestors cached.
app.agents_overview.threads.remove(&primary);
}
Box::pin(app.handle_event(&mut tui, &mut app_server, confirmed)).await?;
assert_eq!(
(
app.primary_thread_id,
app.current_displayed_thread_id(),
app.chat_widget.thread_id()
),
(None, None, None)
);
assert_eq!(
app.agents_overview.visible_thread_ids,
Vec::<ThreadId>::new()
);
assert_eq!(
app_server
.thread_loaded_list(ThreadLoadedListParams::default())
.await?
.data,
Vec::<String>::new()
);
assert!(
app.chat_widget
.selected_index_for_present_view(AGENTS_OVERVIEW_VIEW_ID)
.is_some()
);
match action {
AgentsOverviewAction::Archive => {
app_server.thread_unarchive(id).await?;
}
AgentsOverviewAction::Delete => {
assert!(
app_server
.thread_read(id, /*include_turns*/ false)
.await
.is_err()
);
}
}
app_server.shutdown().await?;
drop(release);
server.shutdown().await;
}
Ok(())
}
#[tokio::test]
async fn disabled_footer_shortcuts_stay_bold_when_wrapped() {
let app = make_test_app().await;
let mut view = app.agents_overview_view(Vec::new(), /*selected_thread_id*/ None);
view.handle_key_event(KeyCode::Esc.into());
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 84, /*height*/ 24,
);
let mut buffer = ratatui::buffer::Buffer::empty(area);
view.render(area, &mut buffer);
let delete_key = crate::key_hint::plain(KeyCode::Delete).display_label();
for key in ["ctrl+x", "ctrl+w", "ctrl+e", delete_key.as_str()] {
let cells = buffer
.content()
.windows(key.len())
.find(|cells| {
cells
.iter()
.map(ratatui::buffer::Cell::symbol)
.collect::<String>()
== key
})
.expect("footer shortcut");
assert_eq!(
cells.iter().map(|cell| cell.modifier).collect::<Vec<_>>(),
vec![ratatui::style::Modifier::BOLD | ratatui::style::Modifier::DIM; key.len()]
);
}
}
#[tokio::test]
async fn lifecycle_footer_keeps_custom_chords_with_labels() {
let mut app = make_test_app().await;
app.keymap = RuntimeKeymap::from_config(
&serde_json::from_value(serde_json::json!({
"agents": { "hide": "f5 f8" }
"agents": { "archive": "f5 f6", "delete": "f5 f7", "hide": "f5 f8" }
}))
.unwrap(),
)
@@ -164,11 +599,13 @@ async fn lifecycle_footer_keeps_custom_chords_with_labels() {
.to_string()
})
.collect::<Vec<_>>();
assert!(
lines
.iter()
.any(|line| line.contains("hide") && line.contains("f5"))
);
for label in ["archive", "delete", "hide"] {
assert!(
lines
.iter()
.any(|line| line.contains(label) && line.contains("f5"))
);
}
assert!(
lines
.iter()