From 816b339e272be1c4aa3fd1152c9c068408bfda1b Mon Sep 17 00:00:00 2001 From: Yaroslav Volovich Date: Wed, 25 Feb 2026 17:53:29 +0000 Subject: [PATCH] Document /fork turn picker behavior --- codex-rs/tui/src/app.rs | 3 ++ codex-rs/tui/src/fork_turn_picker.rs | 46 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 63f4644f19..b3a3455b1c 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -1840,6 +1840,9 @@ impl App { ); self.chat_widget .add_plain_history_lines(vec!["/fork".magenta().into()]); + // The picker returns the exact `nth_user_message` value expected by the + // fork RPC, including the existing `usize::MAX` sentinel for "fork from + // the latest surviving turn". match self .server .fork_thread(nth_user_message, self.config.clone(), path.clone(), false) diff --git a/codex-rs/tui/src/fork_turn_picker.rs b/codex-rs/tui/src/fork_turn_picker.rs index 99a1c56ee5..e6fac14071 100644 --- a/codex-rs/tui/src/fork_turn_picker.rs +++ b/codex-rs/tui/src/fork_turn_picker.rs @@ -1,3 +1,14 @@ +//! Rollback-aware UI for choosing which turn `/fork` should branch from. +//! +//! This module turns the current rollout log into a presentation-only list of surviving turns, +//! then renders an alternate-screen picker that mirrors the conversation as the user currently +//! sees it. Rollback events prune the derived list so abandoned branches never appear as fork +//! targets. +//! +//! The picker returns the `nth_user_message` value expected by the existing fork RPC. Selecting +//! the latest surviving turn intentionally maps to the RPC's `usize::MAX` sentinel so "fork from +//! the current head" keeps the same semantics as the pre-picker `/fork` flow. + use std::path::Path; use crate::key_hint; @@ -35,11 +46,27 @@ use ratatui::widgets::Wrap; use tokio_stream::StreamExt; #[derive(Debug, Clone, PartialEq, Eq)] +/// Display model for one forkable turn in the current conversation. +/// +/// Each entry pairs the surviving user request with the assistant response that still belongs to +/// that request after replaying rollback events. The stored strings are normalized for preview +/// rendering and are not a lossless transcript of the underlying rollout items. pub(crate) struct ForkTurnEntry { + /// Flattened preview of the user-authored request text for this turn. pub(crate) user_request: String, + /// Flattened preview of the assistant reply, if one has been recorded for this turn. pub(crate) model_response: Option, } +/// Loads the current conversation into rollback-aware picker entries. +/// +/// The returned vector reflects only the turns that are still visible in the active branch of the +/// conversation. Callers should treat an empty result as "nothing can be forked yet" and skip +/// presenting the picker rather than rendering an empty selection screen. +/// +/// # Errors +/// +/// Returns an I/O error if the rollout history file cannot be read. pub(crate) async fn load_fork_turn_entries(path: &Path) -> std::io::Result> { let history = RolloutRecorder::get_rollout_history(path).await?; Ok(fork_turn_entries_from_rollout_items( @@ -47,6 +74,15 @@ pub(crate) async fn load_fork_turn_entries(path: &Path) -> std::io::Result, @@ -85,6 +121,11 @@ pub(crate) async fn run_fork_turn_picker( Ok(screen.outcome()) } +/// Replays rollout items into the set of turns the picker is allowed to present. +/// +/// User messages start new entries, assistant messages attach to the most recent entry, and +/// rollback events drop the newest derived turns so the picker mirrors the active branch instead +/// of the full historical event log. fn fork_turn_entries_from_rollout_items(items: &[RolloutItem]) -> Vec { let mut turns: Vec = Vec::new(); @@ -189,6 +230,11 @@ fn agent_turn_preview(content: &[AgentMessageContent]) -> String { text.split_whitespace().collect::>().join(" ") } +/// Maps a visible picker selection onto the `nth_user_message` contract used by `fork_thread`. +/// +/// The final visible turn maps to `usize::MAX`, which is the existing sentinel for "fork from the +/// current head". Returning `turn_count` instead would point one user message past the end of the +/// surviving conversation and rely on server-side bounds handling to recover. fn nth_user_message_for_fork(selected_index: usize, turn_count: usize) -> usize { if selected_index.saturating_add(1) >= turn_count { usize::MAX