Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions codex-rs/tui/src/app/event_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! actions are delegated to focused app submodules so the central match remains the routing layer.

use super::resize_reflow::trailing_run_start;
use super::session_lifecycle::ThreadAttachPresentation;
use super::*;
use crate::config_update::format_config_error;
use crate::external_agent_config_migration_flow::ExternalAgentConfigMigrationFlowOutcome;
Expand Down Expand Up @@ -182,7 +183,11 @@ impl App {
self.shutdown_current_thread(app_server).await;
match self
.replace_chat_widget_with_app_server_thread(
tui, app_server, forked, /*initial_user_message*/ None,
tui,
app_server,
forked,
ThreadAttachPresentation::SessionLineage,
/*initial_user_message*/ None,
)
.await
{
Expand Down Expand Up @@ -224,6 +229,72 @@ impl App {

tui.frame_requester().schedule_frame();
}
AppEvent::ForkSessionForPromptEdit {
thread_id,
nth_user_message,
mut prompt,
} => {
if self.chat_widget.thread_id() != Some(thread_id) {
return Ok(AppRunControl::Continue);
}
self.session_telemetry.counter(
"codex.thread.fork",
/*inc*/ 1,
&[("source", "transcript")],
);
self.refresh_in_memory_config_from_disk_best_effort("forking the thread")
.await;
let config = self.fresh_session_config();
let started = match app_server
.thread_read(thread_id, /*include_turns*/ true)
.await
{
Ok(thread) => match crate::app_backtrack::backtrack_fork_last_turn_id(
&thread.turns,
nth_user_message,
&mut prompt,
) {
Ok(Some(last_turn_id)) => {
app_server
.fork_thread_after(config.clone(), thread_id, last_turn_id)
.await
}
Ok(None) => {
app_server
.start_thread_with_session_start_source(
&config, /*session_start_source*/ None,
)
.await
}
Err(err) => Err(err),
},
Err(err) => Err(err),
};
match started {
Ok(forked) => {
self.shutdown_current_thread(app_server).await;
match self
.replace_chat_widget_with_app_server_thread(
tui,
app_server,
forked,
ThreadAttachPresentation::PromptEdit,
/*initial_user_message*/ None,
)
.await
{
Ok(()) => self.chat_widget.restore_user_message_to_composer(prompt),
Err(err) => {
self.restore_backtrack_prompt_after_branch_error(prompt, err);
}
}
}
Err(err) => {
self.restore_backtrack_prompt_after_branch_error(prompt, err);
}
}
tui.frame_requester().schedule_frame();
}
AppEvent::BeginInitialHistoryReplayBuffer => {
self.begin_initial_history_replay_buffer();
}
Expand Down Expand Up @@ -288,11 +359,6 @@ impl App {
self.chat_widget.note_stream_consolidation_completed();
self.insert_pending_usage_output_after_stream_shutdown(tui);
}
AppEvent::ApplyThreadRollback { num_turns } => {
if self.apply_non_pending_thread_rollback(num_turns) {
tui.frame_requester().schedule_frame();
}
}
AppEvent::StartCommitAnimation => {
if self
.commit_anim_running
Expand Down Expand Up @@ -400,11 +466,10 @@ impl App {
};

self.chat_widget.prepare_safety_buffering_retry();
self.handle_thread_rollback_response_with_origin(
self.handle_thread_rollback_response(
thread_id,
/*num_turns*/ 1,
&rollback_response,
super::thread_routing::ThreadRollbackOrigin::SafetyBufferingRetry,
)
.await;

Expand Down
3 changes: 2 additions & 1 deletion codex-rs/tui/src/app/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ impl App {
&& self.chat_widget.composer_is_empty() =>
{
if let Some(selection) = self.confirm_backtrack_from_main() {
self.apply_backtrack_selection(tui, selection);
self.apply_backtrack_selection(selection);
tui.frame_requester().schedule_frame();
}
}
KeyEvent {
Expand Down
22 changes: 19 additions & 3 deletions codex-rs/tui/src/app/session_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

use super::*;

#[derive(Clone, Copy)]
pub(super) enum ThreadAttachPresentation {
SessionLineage,
PromptEdit,
}

impl App {
pub(super) async fn open_agent_picker(&mut self, app_server: &mut AppServerSession) {
self.backfill_loaded_subagent_threads(app_server).await;
Expand Down Expand Up @@ -571,6 +577,7 @@ impl App {
tui,
app_server,
started,
ThreadAttachPresentation::SessionLineage,
initial_user_message,
)
.await
Expand Down Expand Up @@ -605,6 +612,7 @@ impl App {
tui: &mut tui::Tui,
app_server: &mut AppServerSession,
started: AppServerStartedThread,
presentation: ThreadAttachPresentation,
initial_user_message: Option<crate::chatwidget::UserMessage>,
) -> Result<()> {
// Initial messages are for freshly attached primary threads only. Thread switches and
Expand All @@ -617,8 +625,12 @@ impl App {
initial_user_message,
);
self.replace_chat_widget(ChatWidget::new_with_app_event(init));
self.enqueue_primary_thread_session(started.session, started.turns)
.await?;
self.enqueue_primary_thread_session_with_presentation(
started.session,
started.turns,
presentation,
)
.await?;
self.backfill_loaded_subagent_threads(app_server).await;
Ok(())
}
Expand Down Expand Up @@ -808,7 +820,11 @@ impl App {
.update_search_dir(self.config.cwd.to_path_buf());
match self
.replace_chat_widget_with_app_server_thread(
tui, app_server, resumed, /*initial_user_message*/ None,
tui,
app_server,
resumed,
ThreadAttachPresentation::SessionLineage,
/*initial_user_message*/ None,
)
.await
{
Expand Down
Loading
Loading