//! Side-conversation thread lifecycle. //! //! A side conversation is a fork used for a quick /side question while keeping the //! primary thread focused. This module owns the app-level lifecycle for those forks: switching into //! them, returning to their parent, retaining them across normal navigation, and explicitly //! discarding them. The fork receives hidden developer instructions that make inherited history //! material only and steer the agent away from mutations unless the side conversation explicitly asks //! for them. use super::*; use crate::app_event::SideStartMode; use crate::app_server_session::SideChatStorage; use crate::chatwidget::InterruptedTurnNoticeMode; const SIDE_RENAME_BLOCK_MESSAGE: &str = "Side conversations cannot be renamed."; const SIDE_MAIN_THREAD_UNAVAILABLE_MESSAGE: &str = "'/side' is unavailable until the main thread is ready."; const SIDE_NO_STARTED_CONVERSATION_MESSAGE: &str = concat!( "'/side' is unavailable until the current conversation has started. ", "Send a message first, then try /side again." ); const SIDE_ALREADY_OPEN_MESSAGE: &str = "A side conversation is already open. Press ctrl + c to return before starting another."; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(super) enum SideParentStatus { NeedsInput, NeedsApproval, Failed, Interrupted, Closed, Finished, } impl SideParentStatus { fn label(self, parent_is_main: bool) -> &'static str { match (self, parent_is_main) { (SideParentStatus::NeedsInput, true) => "main needs input", (SideParentStatus::NeedsInput, false) => "parent needs input", (SideParentStatus::NeedsApproval, true) => "main needs approval", (SideParentStatus::NeedsApproval, false) => "parent needs approval", (SideParentStatus::Failed, true) => "main failed", (SideParentStatus::Failed, false) => "parent failed", (SideParentStatus::Interrupted, true) => "main interrupted", (SideParentStatus::Interrupted, false) => "parent interrupted", (SideParentStatus::Closed, true) => "main closed", (SideParentStatus::Closed, false) => "parent closed", (SideParentStatus::Finished, true) => "main finished", (SideParentStatus::Finished, false) => "parent finished", } } fn is_actionable(self) -> bool { matches!( self, SideParentStatus::NeedsInput | SideParentStatus::NeedsApproval ) } pub(super) fn for_request(request: &ServerRequest) -> Option { match request { ServerRequest::ToolRequestUserInput { .. } => Some(SideParentStatus::NeedsInput), ServerRequest::CommandExecutionRequestApproval { .. } | ServerRequest::FileChangeRequestApproval { .. } | ServerRequest::McpServerElicitationRequest { .. } | ServerRequest::PermissionsRequestApproval { .. } | ServerRequest::ApplyPatchApproval { .. } | ServerRequest::ExecCommandApproval { .. } => Some(SideParentStatus::NeedsApproval), ServerRequest::DynamicToolCall { .. } | ServerRequest::AttestationGenerate { .. } | ServerRequest::ChatgptAuthTokensRefresh { .. } => None, } } } #[cfg(test)] mod tests { use super::*; use codex_context_fragments::SideConversationPolicy; use codex_context_fragments::side_conversation_boundary; use codex_protocol::models::ContentItem; use codex_protocol::models::ResponseItem; use pretty_assertions::assert_eq; #[test] fn side_boundary_prompt_marks_inherited_history_reference_only() { let item = side_conversation_boundary(); let ResponseItem::Message { role, content, .. } = item else { panic!("expected hidden side boundary prompt to be a user message"); }; assert_eq!(role, "user"); let [ContentItem::InputText { text }] = content.as_slice() else { panic!("expected hidden side boundary prompt text"); }; assert!(text.contains("Side conversation boundary.")); assert!(text.contains("Everything before this boundary is inherited history")); assert!(text.contains("It is not your current task.")); assert!(text.contains("Only messages submitted after this boundary are active")); assert!(text.contains("Do not continue, execute, or complete")); assert!(text.contains("separate from the main thread")); assert!( text.contains("External tools may be available according to this thread's current") ); assert!(text.contains("Any tool calls or outputs visible before this boundary happened")); assert!(text.contains("Sub-agents are off-limits in this side conversation.")); assert!(text.contains("Do not modify files")); } #[test] fn side_start_error_message_explains_missing_first_prompt() { let err = color_eyre::eyre::eyre!( "thread/fork failed during TUI bootstrap: thread/fork failed: no rollout found for thread id 019da1a1-bed9-7a43-88a2-b49d43915021" ); assert_eq!( App::side_start_error_message(&err), "'/side' is unavailable until the current conversation has started. Send a message first, then try /side again." ); } #[test] fn side_start_error_message_uses_generic_start_wording() { let err = color_eyre::eyre::eyre!("transport disconnected"); assert_eq!( App::side_start_error_message(&err), "Failed to start side conversation: transport disconnected" ); } #[test] fn side_developer_instructions_appends_existing_policy() { let developer_instructions = SideConversationPolicy::Temporary.instructions(Some("Existing developer policy.")); assert!(developer_instructions.contains("Existing developer policy.")); assert!( developer_instructions.contains("You are in a side conversation, not the main thread.") ); assert!( developer_instructions.contains("Sub-agents are off-limits in this side conversation.") ); } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(super) enum SideParentStatusChange { Set(SideParentStatus), Clear, ClearActionable, } impl SideParentStatusChange { pub(super) fn for_notification(notification: &ServerNotification) -> Option { match notification { ServerNotification::TurnStarted(_) => Some(SideParentStatusChange::Clear), ServerNotification::TurnCompleted(notification) => match ¬ification.turn.status { TurnStatus::Completed => { Some(SideParentStatusChange::Set(SideParentStatus::Finished)) } TurnStatus::Interrupted => { Some(SideParentStatusChange::Set(SideParentStatus::Interrupted)) } TurnStatus::Failed => Some(SideParentStatusChange::Set(SideParentStatus::Failed)), TurnStatus::InProgress => None, }, ServerNotification::ThreadClosed(_) => { Some(SideParentStatusChange::Set(SideParentStatus::Closed)) } ServerNotification::ItemStarted(_) | ServerNotification::ServerRequestResolved(_) => { Some(SideParentStatusChange::ClearActionable) } _ => None, } } } #[derive(Clone, Debug)] pub(super) struct SideThreadState { /// Thread to return to when the current side conversation is dismissed. pub(super) parent_thread_id: ThreadId, /// Parent-thread condition that changed while this side thread is visible. pub(super) parent_status: Option, pub(super) opening_message: Option, pub(super) close_request: Option, pub(super) storage: SideChatStorage, } impl SideThreadState { pub(super) fn new(parent_thread_id: ThreadId) -> Self { Self { parent_thread_id, parent_status: None, opening_message: None, close_request: None, storage: SideChatStorage::Temporary, } } } impl App { pub(super) async fn running_side_thread_id(&self) -> Option { for thread_id in self.side_threads.keys().copied() { if self.active_turn_id_for_thread(thread_id).await.is_some() { return Some(thread_id); } } None } pub(super) fn sync_side_thread_ui(&mut self) { if self.zoomed_thread.is_some() && self.zoomed_thread != self.open_pane_focus() { self.zoomed_thread = None; } let clear_side_ui = |chat_widget: &mut crate::chatwidget::ChatWidget| { chat_widget.set_side_conversation_context_label(/*label*/ None); chat_widget.set_side_conversation_active(/*active*/ false); chat_widget.clear_thread_rename_block(); chat_widget.set_interrupted_turn_notice_mode(InterruptedTurnNoticeMode::Default); }; let Some(active_thread_id) = self.current_displayed_thread_id() else { clear_side_ui(&mut self.chat_widget); return; }; let Some((parent_thread_id, parent_status, storage)) = self .side_threads .get(&active_thread_id) .map(|state| (state.parent_thread_id, state.parent_status, state.storage)) else { clear_side_ui(&mut self.chat_widget); if self .side_threads .values() .any(|state| state.parent_thread_id == active_thread_id) && let Some(binding) = self.keymap.primary_hint( crate::keymap::KeymapContext::Global, "toggle_side_conversation", ) { self.chat_widget .set_side_conversation_context_label(Some(format!( "{} for side", binding.display_label() ))); } return; }; match storage { SideChatStorage::Attached => self.chat_widget.clear_thread_rename_block(), SideChatStorage::Temporary => self .chat_widget .set_thread_rename_block_message(SIDE_RENAME_BLOCK_MESSAGE), } self.chat_widget .set_side_conversation_active(/*active*/ true); self.chat_widget .set_interrupted_turn_notice_mode(InterruptedTurnNoticeMode::Suppress); let mut label_parts = Vec::new(); let parent_is_main = self.primary_thread_id == Some(parent_thread_id); if parent_is_main { label_parts.push("from main thread".to_string()); } else { let parent_label = self.thread_label(parent_thread_id); label_parts.push(format!("from parent thread ({parent_label})")); } if let Some(parent_status) = parent_status { label_parts.push(parent_status.label(parent_is_main).to_string()); } if let Some(binding) = self.keymap.primary_hint( crate::keymap::KeymapContext::Global, "toggle_side_conversation", ) { label_parts.push(format!("{} to switch", binding.display_label())); } let action = if storage == SideChatStorage::Attached && self.local_settings.workspace_enabled(&self.config) { "archive" } else { "close" }; label_parts.push(format!( "{} to {action}", crate::key_hint::ctrl(KeyCode::Char('c')).display_label() )); self.chat_widget .set_side_conversation_context_label(Some(format!("Side {}", label_parts.join(" ยท ")))); } pub(super) fn active_side_parent_thread_id(&self) -> Option { self.current_displayed_thread_id() .and_then(|thread_id| self.side_threads.get(&thread_id)) .map(|state| state.parent_thread_id) } pub(super) fn side_retention_enabled(&self, tui: &tui::Tui) -> bool { self.local_settings.workspace_enabled(&self.config) && tui.is_owned_screen() } pub(super) fn conversation_focus_available(&self, tui: &tui::Tui) -> bool { self.side_retention_enabled(tui) && self.overlay.is_none() && (self.chat_widget.no_modal_or_popup_active() // Replaying the parent may reopen a completion for its parked draft. // The focused chooser hides it and owns the keystrokes. || (self.panel_focus() == Some(sidebar::PaneFocus::Sidebar) && self.sidebar_chooser_available() && self.chat_widget.completion_can_yield_focus())) } pub(super) fn main_composer_shortcut_available(&self) -> bool { !self.reconnect.offline || self.active_side_parent_thread_id().is_none() } /// Zoom follows the displayed conversation even when its peer cannot be selected offline. pub(super) fn open_pane_focus(&self) -> Option { let focused = self.current_displayed_thread_id()?; if self.panel_focus() == Some(sidebar::PaneFocus::Sidebar) && matches!( self.sidebar()?.visible_content(), Some(sidebar::SidebarContent::Document(_)) ) { return Some(focused); } let child = self.side_pair_key(focused)?; (self.sidebar()?.visible_content() == Some(sidebar::SidebarContent::Chat(child))) .then_some(focused) } pub(super) fn switchable_side_child(&self) -> Option { if self.reconnect.offline { return None; } self.side_pair_key(self.current_displayed_thread_id()?) } pub(super) fn side_pair_key(&self, thread_id: ThreadId) -> Option { if self.side_threads.contains_key(&thread_id) { Some(thread_id) } else { self.side_thread_for_parent(thread_id) } } pub(super) fn side_thread_for_parent(&self, parent_thread_id: ThreadId) -> Option { self.sidebars .get(&Some(parent_thread_id)) .and_then(sidebar::SidebarPane::selected_chat) .or_else(|| { self.side_threads.iter().find_map(|(thread_id, state)| { (state.parent_thread_id == parent_thread_id).then_some(*thread_id) }) }) } pub(super) fn set_side_parent_status( &mut self, parent_thread_id: ThreadId, status: Option, ) { let mut changed = false; for state in self .side_threads .values_mut() .filter(|state| state.parent_thread_id == parent_thread_id) { if state.parent_status != status { state.parent_status = status; changed = true; } } if changed { self.sync_side_thread_ui(); } } pub(super) fn clear_side_parent_action_status(&mut self, parent_thread_id: ThreadId) { let mut changed = false; for state in self .side_threads .values_mut() .filter(|state| state.parent_thread_id == parent_thread_id) { if state .parent_status .is_some_and(SideParentStatus::is_actionable) { state.parent_status = None; changed = true; } } if changed { self.sync_side_thread_ui(); } } pub(super) fn apply_side_parent_status_change( &mut self, parent_thread_id: ThreadId, change: SideParentStatusChange, ) { match change { SideParentStatusChange::Set(status) => { self.set_side_parent_status(parent_thread_id, Some(status)); } SideParentStatusChange::Clear => { self.set_side_parent_status(parent_thread_id, /*status*/ None); } SideParentStatusChange::ClearActionable => { self.clear_side_parent_action_status(parent_thread_id); } } } pub(super) async fn maybe_return_from_side( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, key: KeyEvent, ) -> bool { if self.overlay.is_none() && self.chat_widget.no_modal_or_popup_active() && self.chat_widget.composer_is_empty() && let Some(target) = self.current_displayed_thread_id() && let Some(side) = self.side_threads.get(&target) { if matches!(key.code, KeyCode::Char(c) if c.eq_ignore_ascii_case(&'c')) && self.side_retention_enabled(tui) && self.sidebar_chooser_available() && !self.transcript_view.owns_interaction_key(key) && side.storage == SideChatStorage::Attached { if let Err(err) = sidebar_close::request( self, tui, app_server, target, sidebar_close::Action::Archive, ) .await { self.chat_widget.add_error_message(err.to_string()); } return true; } if self .select_agent_thread_and_discard_side(tui, app_server, side.parent_thread_id) .await .is_err() { return false; } self.active_side_parent_thread_id().is_none() } else { false } } pub(super) fn side_thread_to_discard_after_switch( &self, target_thread_id: ThreadId, ) -> Option { let active_thread_id = self.current_displayed_thread_id()?; let state = self.side_threads.get(&active_thread_id)?; (target_thread_id == state.parent_thread_id).then_some(active_thread_id) } pub(super) async fn toggle_side_conversation( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, ) -> Result<()> { let Some(active_thread_id) = self.current_displayed_thread_id() else { return Ok(()); }; let target_thread_id = if let Some(state) = self.side_threads.get(&active_thread_id) { state.parent_thread_id } else if let Some(side_thread_id) = self.side_thread_for_parent(active_thread_id) { side_thread_id } else { return Ok(()); }; self.select_agent_thread(tui, app_server, target_thread_id) .await?; if self.active_thread_id == Some(target_thread_id) && self.active_side_parent_thread_id().is_none() { self.surface_pending_inactive_thread_interactive_requests() .await?; } Ok(()) } pub(super) async fn hide_focused_side_pane( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, ) -> Result<()> { let Some(child) = self.current_displayed_thread_id() else { return Ok(()); }; let Some(side) = self.side_threads.get(&child) else { return Ok(()); }; let parent = side.parent_thread_id; self.select_agent_thread(tui, app_server, parent).await?; if self.current_displayed_thread_id() == Some(parent) { self.sidebar_mut().hide(); tui.frame_requester().schedule_frame(); self.focus_returned_parent_composer(parent).await?; } Ok(()) } pub(super) async fn discard_side_thread( &mut self, app_server: &mut AppServerSession, thread_id: ThreadId, ) -> bool { if let Err(message) = self.interrupt_side_thread(app_server, thread_id).await { tracing::warn!("{message}"); self.add_agents_overview_error(message); return false; } if let Err(err) = app_server.thread_unsubscribe(thread_id).await { let message = format!("Failed to close side conversation {thread_id}; it is still open: {err}"); tracing::warn!("{message}"); self.add_agents_overview_error(message); return false; } self.abandoned_side_threads.insert(thread_id); self.discard_thread_local_state(thread_id).await; true } pub(super) async fn discard_side_thread_in_background( &mut self, app_server: &mut AppServerSession, thread_id: ThreadId, ) { self.abandoned_side_threads.insert(thread_id); self.pending_app_server_requests .cancel_thread_verification(&thread_id.to_string()); let turn_id = self.active_turn_id_for_thread(thread_id).await; let attachment = self .thread_event_channels .get(&thread_id) .map(ThreadEventChannel::attachment); let mut view = app_server.request_view(); let saved_parent = self .side_threads .get(&thread_id) .filter(|side| side.storage == SideChatStorage::Attached) .map(|side| side.parent_thread_id); self.discard_thread_local_state(thread_id).await; let cleanup = tokio::spawn(async move { if let Err(error) = interrupt_for_close(&mut view, thread_id, turn_id, attachment).await { tracing::warn!(%error, "failed to interrupt side conversation"); } if let Err(error) = view.thread_unsubscribe(thread_id).await { tracing::warn!(%error, "failed to unsubscribe side conversation"); } }); if let Some(parent) = saved_parent { self.sidebars .entry(Some(parent)) .or_default() .closing_chats .insert(thread_id, cleanup); } } pub(super) async fn discard_closed_side_thread(&mut self, thread_id: ThreadId) { self.discard_thread_local_state(thread_id).await; } pub(super) async fn discard_thread_local_state(&mut self, thread_id: ThreadId) { self.pending_app_server_requests .cancel_thread_verification(&thread_id.to_string()); let app_event_tx = self.app_event_tx.clone(); self.dynamic_tool_tasks .retain(|request_id, (source, task)| { if source == &thread_id.to_string() { app_event_tx.send(AppEvent::DynamicToolCallCompleted { request_id: request_id.clone(), response: crate::dynamic_tools::failure_response( "Source task was closed while handling a dynamic tool call", ), reply: None, }); task.abort(); false } else { true } }); self.abort_thread_event_listener(thread_id); self.thread_event_channels.remove(&thread_id); self.agents_overview .draft_cloud_environments .remove(&thread_id); self.pending_server_profiles.remove(&thread_id); self.agents_overview.activity.remove(&thread_id); self.sidebars.remove(&Some(thread_id)); if let Some(side) = self.side_threads.remove(&thread_id) && let Some(pane) = self.sidebars.get_mut(&Some(side.parent_thread_id)) { pane.remove_chat(thread_id, side.storage); } self.side_companions.remove(&thread_id); self.agent_navigation.remove(thread_id); if self.active_thread_id == Some(thread_id) { self.clear_active_thread().await; } else { self.refresh_pending_thread_approvals().await; } self.forget_realtime_replay_thread(thread_id); self.sync_active_agent_label(); } async fn interrupt_side_thread( &self, app_server: &mut AppServerSession, thread_id: ThreadId, ) -> std::result::Result<(), String> { let turn_id = self.active_turn_id_for_thread(thread_id).await; let attachment = self .thread_event_channels .get(&thread_id) .map(ThreadEventChannel::attachment); interrupt_for_close(app_server, thread_id, turn_id, attachment) .await .map_err(|err| { format!("Failed to close side conversation {thread_id}; it is still open: {err}") }) } async fn keep_side_thread_visible_after_cleanup_failure( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, thread_id: ThreadId, ) { if self.active_thread_id != Some(thread_id) && let Err(err) = self.select_agent_thread(tui, app_server, thread_id).await { tracing::warn!( "failed to restore side conversation after cleanup failure for {thread_id}: {err}" ); } } pub(super) async fn discard_side_thread_or_keep_visible( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, thread_id: ThreadId, ) -> bool { if self.discard_side_thread(app_server, thread_id).await { true } else { self.keep_side_thread_visible_after_cleanup_failure(tui, app_server, thread_id) .await; false } } pub(super) fn side_fork_config(&self) -> Config { let mut fork_config = self.chat_widget.config_ref().clone(); let parent_model = self.chat_widget.current_model(); if !parent_model.trim().is_empty() { fork_config.model = Some(parent_model.to_string()); } fork_config.model_reasoning_effort = self.chat_widget.current_reasoning_effort(); fork_config.service_tier = self.chat_widget.configured_service_tier(); fork_config.daybreak_enabled = false; fork_config } pub(super) fn side_start_block_message(&self) -> Option<&'static str> { if self.primary_thread_id.is_none() { Some(SIDE_MAIN_THREAD_UNAVAILABLE_MESSAGE) } else if self.active_side_parent_thread_id().is_some() { Some(SIDE_ALREADY_OPEN_MESSAGE) } else { None } } pub(super) fn side_start_error_message(err: &color_eyre::Report) -> String { if err.chain().any(|cause| { let message = cause.to_string(); message.contains("no rollout found for thread id") || message.contains("includeTurns is unavailable before first user message") }) { SIDE_NO_STARTED_CONVERSATION_MESSAGE.to_string() } else { format!("Failed to start side conversation: {err}") } } pub(super) fn restore_side_user_message( &mut self, user_message: Option, ) { if let Some(user_message) = user_message { self.chat_widget .restore_user_message_to_composer(user_message); } } pub(super) fn install_side_thread_snapshot( store: &mut ThreadEventStore, mut session: ThreadSessionState, _forked_turns: Vec, ) { // The forked history remains available to the model through core state, but side // conversations should visually start at the side boundary. session.forked_from_id = None; store.set_session(session, Vec::new()); } pub(super) async fn select_agent_thread_and_discard_side( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, thread_id: ThreadId, ) -> Result<()> { let side_thread_to_discard = self.side_thread_to_discard_after_switch(thread_id); self.select_agent_thread(tui, app_server, thread_id).await?; if self.active_thread_id == Some(thread_id) && let Some(side_thread_id) = side_thread_to_discard { self.discard_side_thread_in_background(app_server, side_thread_id) .await; self.surface_pending_inactive_thread_interactive_requests() .await?; } Ok(()) } pub(super) async fn handle_start_side( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, parent_thread_id: ThreadId, mut user_message: Option, mode: SideStartMode, ) -> Result { let workspace = self.side_retention_enabled(tui); let existing = (mode == SideStartMode::Reuse || !workspace) .then(|| self.side_thread_for_parent(parent_thread_id)) .flatten(); if let Some(pane) = self.sidebars.get_mut(&Some(parent_thread_id)) && pane.creation.is_some() { // Retry and Back consume the saved attempt. A new /side must not overwrite its prompt. pane.show( sidebar::SidebarContent::Chooser, sidebar::PaneFocus::Sidebar, ); tui.frame_requester().schedule_frame(); self.restore_side_user_message(user_message); return Ok(AppRunControl::Continue); } if let Some(message) = self.side_start_block_message() { self.restore_side_user_message(user_message.take()); self.sync_side_thread_ui(); self.chat_widget.add_error_message(message.to_string()); return Ok(AppRunControl::Continue); } if self.pending_server_profiles.contains_key(&parent_thread_id) { self.restore_side_user_message(user_message.take()); self.sync_side_thread_ui(); self.chat_widget .add_error_message("Wait for permissions to update before forking.".into()); return Ok(AppRunControl::Continue); } if let Some(side_thread_id) = existing { self.select_agent_thread(tui, app_server, side_thread_id) .await?; if self.active_thread_id == Some(side_thread_id) { if let Some(user_message) = user_message.take() { let _ = self .chat_widget .submit_user_message_as_plain_user_turn(user_message); } } else { self.restore_side_user_message(user_message.take()); } return Ok(AppRunControl::Continue); } self.session_telemetry.counter( "codex.thread.side", /*inc*/ 1, &[("source", "slash_command")], ); let request = uuid::Uuid::new_v4(); if workspace { let pane = self.sidebars.entry(Some(parent_thread_id)).or_default(); pane.creation = Some(sidebar::SideCreation { owner: app_server.backend_id(), state: sidebar::CreationState::Waiting { request, started: Instant::now(), }, message: user_message.take(), }); pane.show( sidebar::SidebarContent::Chooser, sidebar::PaneFocus::Sidebar, ); let size = tui.prepare_draw_size()?; self.render_chat_widget_frame(tui, size)?; } self.refresh_in_memory_config_from_disk_best_effort("starting a side conversation") .await; let fork = app_server.sidebar_fork( self.local_settings.clone(), self.side_fork_config(), parent_thread_id, self.selected_server_profile(parent_thread_id), if workspace && app_server.can_list_saved_side_chats(parent_thread_id) { SideChatStorage::Attached } else { SideChatStorage::Temporary }, ); let create = async move { fork.await .map(Box::new) .map_err(|err| Self::side_start_error_message(&err)) }; if workspace { let tx = self.app_event_tx.clone(); let backend_id = app_server.backend_id(); tokio::spawn(async move { tx.send(AppEvent::SideCreated { backend_id, parent: parent_thread_id, request, result: create.await, }); }); } else { self.apply_side_creation( tui, app_server, parent_thread_id, user_message, /*enter*/ true, create.await, ) .await; } Ok(AppRunControl::Continue) } pub(super) async fn finish_side_creation( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, parent: ThreadId, request: uuid::Uuid, result: Result, String>, ) { let enter = !self.reconnect.offline && self.current_displayed_thread_id() == Some(parent) && self.chooser_focus() == Some(sidebar::PaneFocus::Sidebar) && self.conversation_focus_available(tui); let pending = self .sidebars .get_mut(&Some(parent)) .and_then(|pane| pane.creation.as_mut()) .filter(|creation| creation.request() == Some(request)); let Some(pending) = pending else { if let Ok(fork) = result && let Err(error) = app_server .thread_unsubscribe(fork.thread.session.thread_id) .await { tracing::warn!(%error, "failed to release unclaimed side chat"); } return; }; tui.frame_requester().schedule_frame(); if let Err(message) = result { pending.message = None; pending.state = sidebar::CreationState::Failed { message, cleanup: None, }; self.sync_side_thread_ui(); return; } let message = pending.message.take(); self.apply_side_creation(tui, app_server, parent, message, enter, result) .await } async fn apply_side_creation( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, parent_thread_id: ThreadId, mut user_message: Option, enter: bool, result: Result, String>, ) { match result { Ok(fork) => { app_server.observe_sidebar_fork(&fork); let forked = fork.thread; let child_thread_id = forked.session.thread_id; if let Err(err) = fork.boundary { let message = format!("Failed to prepare side conversation {child_thread_id}: {err}"); let discarded = self.discard_side_thread(app_server, child_thread_id).await; if let Some(pending) = self .sidebars .get_mut(&Some(parent_thread_id)) .and_then(|pane| pane.creation.as_mut()) { pending.state = sidebar::CreationState::Failed { message, cleanup: (!discarded).then_some(child_thread_id), }; pending.message = user_message; } else { if !discarded { self.side_threads .insert(child_thread_id, SideThreadState::new(parent_thread_id)); self.keep_side_thread_visible_after_cleanup_failure( tui, app_server, child_thread_id, ) .await; } self.restore_side_user_message(user_message); self.chat_widget.add_error_message(message); } return; } let already_selected = self.active_thread_id == Some(child_thread_id); let enter = enter || already_selected; if fork.inherited_profile && let Some(profile) = &forked.session.active_permission_profile { self.agents_overview .selected_permission_profiles .insert(child_thread_id, profile.id.clone()); } if !enter { self.side_companions .entry(child_thread_id) .or_insert_with(|| SideCompanion::empty(forked.session.cwd.clone())); } let channel = self.ensure_thread_channel(child_thread_id); channel.backend_id = Some(app_server.backend_id()); { let mut store = channel.store.lock().await; Self::install_side_thread_snapshot(&mut store, forked.session, forked.turns); } let mut child = SideThreadState::new(parent_thread_id); child.storage = fork.storage; child.parent_status = self .side_thread_for_parent(parent_thread_id) .and_then(|id| self.side_threads.get(&id)) .and_then(|side| side.parent_status); if !enter { child.opening_message = user_message.take(); } self.side_threads.insert(child_thread_id, child); // `thread/started` is delivered after the fork response; seed navigation before // the first selection without blocking on another app-server read. self.upsert_agent_picker_thread( child_thread_id, /*agent_nickname*/ None, /*agent_role*/ None, /*is_closed*/ false, ); self.sync_agent_picker_thread(child_thread_id); self.sidebars .entry(Some(parent_thread_id)) .or_default() .chat_ready(child_thread_id); if !enter { self.sync_side_thread_ui(); return; } if already_selected { // Selection normally skips the live thread. Replay the initialized snapshot // even if the picker exposed this child before the boundary RPC completed. self.store_active_thread_receiver().await; self.active_thread_id = None; } if let Err(err) = self .select_agent_thread(tui, app_server, child_thread_id) .await { let discarded = self .discard_side_thread_or_keep_visible(tui, app_server, child_thread_id) .await; if discarded && self.active_thread_id != Some(parent_thread_id) && let Err(restore_err) = self .select_agent_thread(tui, app_server, parent_thread_id) .await { tracing::warn!( "failed to restore parent thread after side conversation switch failure: {restore_err}" ); } self.restore_side_user_message(user_message.take()); self.chat_widget.add_error_message(format!( "Failed to switch into side conversation {child_thread_id}: {err}" )); return; } if self.active_thread_id == Some(child_thread_id) { if let Some(user_message) = user_message.take() { let _ = self .chat_widget .submit_user_message_as_plain_user_turn(user_message); } } else { self.discard_side_thread_or_keep_visible(tui, app_server, child_thread_id) .await; self.restore_side_user_message(user_message.take()); self.chat_widget.add_error_message(format!( "Failed to switch into side conversation {child_thread_id}." )); } } Err(err) => { self.chat_widget .set_side_conversation_context_label(/*label*/ None); self.chat_widget.add_error_message(err); } } } } /// A cached turn can finish or be replaced before the server handles an interrupt. pub(super) async fn interrupt_for_close( server: &mut AppServerSession, thread: ThreadId, turn: Option, attachment: Option, ) -> std::result::Result<(), TypedRequestError> { let mut result = match turn { Some(turn) => server.turn_interrupt(thread, turn).await, None => server.startup_interrupt(thread).await, }; if let Err(error) = &result && let Some(current) = active_turn_interrupt_race(error) { result = server.turn_interrupt(thread, current).await; } match result { Err(TypedRequestError::Server { method, source }) if method == "turn/interrupt" && source.code == -32600 && (source.message == "no active turn to interrupt" || (attachment == Some(ThreadEventAttachment::ReplayOnly) && source.message == format!("thread not found: {thread}"))) => { Ok(()) } result => result, } }