mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Revert the current thread when editing an earlier TUI prompt (#45845)
## What changed Use `thread/revert` to remove the selected turn and subsequent history, then restore the selected prompt in the composer. Keep the thread identity, settings, goal, and unrelated thread channels intact. Show a notice that conversation history was reverted and file changes are unchanged. Resolve selections against persisted history, accounting for partially loaded transcripts and hidden review prompts. Reject stale selections and mid-turn steers. Refresh replay state and pagination after a revert, clear queued input and obsolete actions, and require a session reload when the revert cannot be confirmed. ## Testing Add regression coverage for reverting earlier and first visible prompts in place, preserving unloaded history and thread state, rejecting steers in clipped history, and clearing streaming output, queued input, and stale approval actions. GitOrigin-RevId: d3f0342539392bdbfec925d0c3d6f67369937f1b
This commit is contained in:
1 parent
8ece31a7bf
commit
ffae979216
16 files changed
+659
-409
No files matched your search
@@ -15,6 +15,8 @@ use codex_app_server_protocol::ThreadHistoryMode;
|
||||
use codex_app_server_protocol::ThreadItem;
|
||||
use codex_app_server_protocol::ThreadItemsListParams;
|
||||
use codex_app_server_protocol::ThreadItemsListResponse;
|
||||
use codex_app_server_protocol::ThreadRevertParams;
|
||||
use codex_app_server_protocol::ThreadRevertResponse;
|
||||
use codex_app_server_protocol::ThreadTurnsListParams;
|
||||
use codex_app_server_protocol::ThreadTurnsListResponse;
|
||||
use codex_app_server_protocol::Turn;
|
||||
@@ -28,9 +30,10 @@ pub(crate) const HISTORY_ITEM_PAGE_LIMIT: u32 = 100;
|
||||
pub(crate) const HISTORY_ITEM_SCAN_LIMIT: usize = 4 * HISTORY_ITEM_PAGE_LIMIT as usize;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum HistoryHydrationScope {
|
||||
pub(crate) enum HistoryHydrationScope<'a> {
|
||||
Initial,
|
||||
Complete,
|
||||
ThroughTurn(&'a str),
|
||||
}
|
||||
|
||||
pub(crate) fn thread_items_page_params(
|
||||
@@ -70,6 +73,41 @@ pub(crate) struct ThreadHistoryPagination {
|
||||
}
|
||||
|
||||
impl AppServerSession {
|
||||
pub(crate) async fn revert_thread(
|
||||
&mut self,
|
||||
thread_id: ThreadId,
|
||||
before_turn_id: String,
|
||||
retained_turns: &[Turn],
|
||||
) -> std::result::Result<ThreadRevertResponse, codex_app_server_client::TypedRequestError> {
|
||||
let request_id = self.next_request_id();
|
||||
let response: ThreadRevertResponse = self
|
||||
.client
|
||||
.request_typed(ClientRequest::ThreadRevert {
|
||||
request_id,
|
||||
params: ThreadRevertParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
before_turn_id,
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
// Older cursors in the retained prefix remain valid. If the entire displayed window
|
||||
// disappeared, resume paging at the replacement history's end instead.
|
||||
if retained_turns.iter().all(|turn| turn.items.is_empty()) {
|
||||
self.history_pagination.insert(
|
||||
thread_id,
|
||||
ThreadHistoryPagination {
|
||||
history_mode: ThreadHistoryMode::Paginated,
|
||||
next_turn_cursor: response.turns_backwards_cursor.clone(),
|
||||
next_item_cursor: response.items_backwards_cursor.clone(),
|
||||
..ThreadHistoryPagination::default()
|
||||
},
|
||||
);
|
||||
} else {
|
||||
self.cancel_older_history_page(thread_id);
|
||||
}
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub(crate) fn has_older_history(&self, thread_id: ThreadId) -> bool {
|
||||
self.history_pagination
|
||||
.get(&thread_id)
|
||||
@@ -202,7 +240,7 @@ impl AppServerSession {
|
||||
item_cursor: Option<String>,
|
||||
config: Option<&Config>,
|
||||
local_settings: Option<&crate::local_settings::LocalSettings>,
|
||||
scope: HistoryHydrationScope,
|
||||
scope: HistoryHydrationScope<'_>,
|
||||
) -> Result<()> {
|
||||
let thread_id = ThreadId::from_string(&thread.id)
|
||||
.wrap_err("invalid thread id in bounded history response")?;
|
||||
@@ -231,6 +269,7 @@ impl AppServerSession {
|
||||
.and_then(|settings| resize_reflow_max_rows(settings.terminal_resize_reflow()));
|
||||
let item_budget = match (scope, config, row_budget) {
|
||||
(HistoryHydrationScope::Complete, _, _)
|
||||
| (HistoryHydrationScope::ThroughTurn(_), _, _)
|
||||
| (HistoryHydrationScope::Initial, Some(_), None) => None,
|
||||
(HistoryHydrationScope::Initial, Some(_), Some(max_rows)) => {
|
||||
Some(max_rows.saturating_add(HISTORY_ITEM_SCAN_LIMIT))
|
||||
@@ -269,6 +308,16 @@ impl AppServerSession {
|
||||
let items = self
|
||||
.merge_thread_item_page(thread_id, page, &mut state, &mut thread.turns)
|
||||
.await?;
|
||||
// Finish the anchor turn so prompt editing can detect earlier steers,
|
||||
// but do not scan history older than the displayed transcript.
|
||||
if let HistoryHydrationScope::ThroughTurn(anchor) = scope
|
||||
&& let Some(index) = thread.turns.iter().position(|turn| turn.id == anchor)
|
||||
&& thread.turns[..index]
|
||||
.iter()
|
||||
.any(|turn| !turn.items.is_empty())
|
||||
{
|
||||
break;
|
||||
}
|
||||
if let Some((config, local_settings)) = config.zip(local_settings) {
|
||||
rendered_rows = rendered_history_rows(
|
||||
thread_id,
|
||||
|
||||
Reference in new issue
Block a user