mirror of
https://github.com/openai/codex.git
synced 2026-11-22 07:13:50 +00:00
Seed prompt history from resumed messages (#24298)
## Why When the TUI resumes a thread, transcript replay renders prior user messages but did not seed the composer history. That leaves the resumed session with empty in-memory prompt history, so pressing Up can fall through to persisted global history and surface a prompt from another thread. The expected behavior is that prompts from the resumed thread are recalled first, with global history only as a fallback. ## What changed - Record replayed user messages into the composer history during resume replay. - Preserve the existing persisted history format and avoid any startup history scan. - Add focused TUI coverage showing replayed prompts are recalled before persisted global history. ## Validation - Added `replayed_user_messages_seed_composer_history` in `codex-rs/tui/src/chatwidget/tests/history_replay.rs`. - `just test -p codex-tui replayed_user_messages_seed_composer_history` passed.
This commit is contained in:
1 parent
f0a839ea0c
commit
56958f2512
6 files changed
+303
-25
No files matched your search
@@ -858,6 +858,10 @@ impl ChatComposer {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn record_replayed_user_message_history(&mut self, entry: HistoryEntry) {
|
||||
self.history.record_replayed_submission(entry);
|
||||
}
|
||||
|
||||
/// Integrate pasted text into the composer.
|
||||
///
|
||||
/// Acts as the only place where paste text is integrated, both for:
|
||||
|
||||
@@ -121,6 +121,8 @@ pub(crate) struct ChatComposerHistory {
|
||||
/// Messages submitted by the user *during this UI session* (newest at END).
|
||||
/// Local entries retain full draft state (text elements, image paths, pending pastes, remote image URLs).
|
||||
local_history: Vec<HistoryEntry>,
|
||||
/// Local entries seeded from resumed transcript replay.
|
||||
replay_seeded_history: Vec<HistoryEntry>,
|
||||
|
||||
/// Cache of persistent history entries fetched on-demand (text-only).
|
||||
fetched_history: HashMap<usize, HistoryEntry>,
|
||||
@@ -128,6 +130,7 @@ pub(crate) struct ChatComposerHistory {
|
||||
/// Current cursor within the combined (persistent + local) history. `None`
|
||||
/// indicates the user is *not* currently browsing history.
|
||||
history_cursor: Option<isize>,
|
||||
pending_navigation_direction: Option<HistorySearchDirection>,
|
||||
|
||||
/// The text that was last inserted into the composer as a result of
|
||||
/// history navigation. Used to decide if further Up/Down presses should be
|
||||
@@ -229,8 +232,10 @@ impl ChatComposerHistory {
|
||||
persistent_log_id: None,
|
||||
persistent_entry_count: 0,
|
||||
local_history: Vec::new(),
|
||||
replay_seeded_history: Vec::new(),
|
||||
fetched_history: HashMap::new(),
|
||||
history_cursor: None,
|
||||
pending_navigation_direction: None,
|
||||
last_history_text: None,
|
||||
search: None,
|
||||
at_mention_restore_enabled: false,
|
||||
@@ -259,7 +264,9 @@ impl ChatComposerHistory {
|
||||
self.persistent_entry_count = entry_count;
|
||||
self.fetched_history.clear();
|
||||
self.local_history.clear();
|
||||
self.replay_seeded_history.clear();
|
||||
self.history_cursor = None;
|
||||
self.pending_navigation_direction = None;
|
||||
self.last_history_text = None;
|
||||
self.search = None;
|
||||
}
|
||||
@@ -269,6 +276,16 @@ impl ChatComposerHistory {
|
||||
/// Empty submissions are ignored, adjacent duplicates are collapsed, and active navigation or
|
||||
/// search state is reset because a new newest entry changes the combined history offset space.
|
||||
pub fn record_local_submission(&mut self, entry: HistoryEntry) {
|
||||
self.record_local_submission_inner(entry);
|
||||
}
|
||||
|
||||
pub fn record_replayed_submission(&mut self, entry: HistoryEntry) {
|
||||
if self.record_local_submission_inner(entry.clone()) {
|
||||
self.replay_seeded_history.push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
fn record_local_submission_inner(&mut self, entry: HistoryEntry) -> bool {
|
||||
if entry.text.is_empty()
|
||||
&& entry.text_elements.is_empty()
|
||||
&& entry.local_image_paths.is_empty()
|
||||
@@ -276,18 +293,20 @@ impl ChatComposerHistory {
|
||||
&& entry.mention_bindings.is_empty()
|
||||
&& entry.pending_pastes.is_empty()
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
self.history_cursor = None;
|
||||
self.pending_navigation_direction = None;
|
||||
self.last_history_text = None;
|
||||
self.search = None;
|
||||
|
||||
// Avoid inserting a duplicate if identical to the previous entry.
|
||||
if self.local_history.last().is_some_and(|prev| prev == &entry) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
self.local_history.push(entry);
|
||||
true
|
||||
}
|
||||
|
||||
/// Resets normal history navigation so the next Up key resumes from the newest entry.
|
||||
@@ -297,6 +316,7 @@ impl ChatComposerHistory {
|
||||
/// influence later Up/Down recall.
|
||||
pub fn reset_navigation(&mut self) {
|
||||
self.history_cursor = None;
|
||||
self.pending_navigation_direction = None;
|
||||
self.last_history_text = None;
|
||||
self.search = None;
|
||||
}
|
||||
@@ -359,7 +379,11 @@ impl ChatComposerHistory {
|
||||
};
|
||||
|
||||
self.history_cursor = Some(next_idx);
|
||||
self.populate_history_at_index(next_idx as usize, app_event_tx)
|
||||
self.populate_history_at_index(
|
||||
next_idx as usize,
|
||||
HistorySearchDirection::Older,
|
||||
app_event_tx,
|
||||
)
|
||||
}
|
||||
|
||||
/// Handles Down by moving toward newer entries or clearing the composer past the newest entry.
|
||||
@@ -383,11 +407,16 @@ impl ChatComposerHistory {
|
||||
match next_idx_opt {
|
||||
Some(idx) => {
|
||||
self.history_cursor = Some(idx);
|
||||
self.populate_history_at_index(idx as usize, app_event_tx)
|
||||
self.populate_history_at_index(
|
||||
idx as usize,
|
||||
HistorySearchDirection::Newer,
|
||||
app_event_tx,
|
||||
)
|
||||
}
|
||||
None => {
|
||||
// Past newest – clear and exit browsing mode.
|
||||
self.history_cursor = None;
|
||||
self.pending_navigation_direction = None;
|
||||
self.last_history_text = None;
|
||||
Some(HistoryEntry::new(String::new()))
|
||||
}
|
||||
@@ -449,9 +478,22 @@ impl ChatComposerHistory {
|
||||
}
|
||||
|
||||
if self.history_cursor == Some(offset as isize) {
|
||||
let direction = self.pending_navigation_direction.take();
|
||||
let Some(entry) = entry else {
|
||||
return HistoryEntryResponse::Ignored;
|
||||
};
|
||||
if self.persistent_entry_duplicates_local(&entry)
|
||||
&& let Some(direction) = direction
|
||||
{
|
||||
let Some(offset) = self.next_history_offset(offset, direction) else {
|
||||
return HistoryEntryResponse::Ignored;
|
||||
};
|
||||
self.history_cursor = Some(offset as isize);
|
||||
return self
|
||||
.populate_history_at_index(offset, direction, app_event_tx)
|
||||
.map(HistoryEntryResponse::Found)
|
||||
.unwrap_or(HistoryEntryResponse::Ignored);
|
||||
}
|
||||
self.last_history_text = Some(entry.text.clone());
|
||||
return HistoryEntryResponse::Found(entry);
|
||||
}
|
||||
@@ -729,29 +771,61 @@ impl ChatComposerHistory {
|
||||
fn populate_history_at_index(
|
||||
&mut self,
|
||||
global_idx: usize,
|
||||
direction: HistorySearchDirection,
|
||||
app_event_tx: &AppEventSender,
|
||||
) -> Option<HistoryEntry> {
|
||||
if global_idx >= self.persistent_entry_count {
|
||||
// Local entry.
|
||||
if let Some(entry) = self
|
||||
.local_history
|
||||
.get(global_idx - self.persistent_entry_count)
|
||||
.cloned()
|
||||
{
|
||||
let mut global_idx = global_idx;
|
||||
loop {
|
||||
if let Some(entry) = self.entry_at_cached_offset(global_idx) {
|
||||
if global_idx < self.persistent_entry_count
|
||||
&& self.persistent_entry_duplicates_local(&entry)
|
||||
{
|
||||
let Some(next_idx) = self.next_history_offset(global_idx, direction) else {
|
||||
self.pending_navigation_direction = None;
|
||||
return None;
|
||||
};
|
||||
self.history_cursor = Some(next_idx as isize);
|
||||
global_idx = next_idx;
|
||||
continue;
|
||||
}
|
||||
self.pending_navigation_direction = None;
|
||||
self.last_history_text = Some(entry.text.clone());
|
||||
return Some(entry);
|
||||
}
|
||||
} else if let Some(entry) = self.fetched_history.get(&global_idx).cloned() {
|
||||
self.last_history_text = Some(entry.text.clone());
|
||||
return Some(entry);
|
||||
} else if let (Some(thread_id), Some(log_id)) = (self.thread_id, self.persistent_log_id) {
|
||||
app_event_tx.send(AppEvent::LookupMessageHistoryEntry {
|
||||
thread_id,
|
||||
offset: global_idx,
|
||||
log_id,
|
||||
});
|
||||
|
||||
if global_idx >= self.persistent_entry_count {
|
||||
return None;
|
||||
}
|
||||
|
||||
if let (Some(thread_id), Some(log_id)) = (self.thread_id, self.persistent_log_id) {
|
||||
self.pending_navigation_direction = Some(direction);
|
||||
app_event_tx.send(AppEvent::LookupMessageHistoryEntry {
|
||||
thread_id,
|
||||
offset: global_idx,
|
||||
log_id,
|
||||
});
|
||||
}
|
||||
return None;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn next_history_offset(
|
||||
&self,
|
||||
offset: usize,
|
||||
direction: HistorySearchDirection,
|
||||
) -> Option<usize> {
|
||||
match direction {
|
||||
HistorySearchDirection::Older => offset.checked_sub(1),
|
||||
HistorySearchDirection::Newer => offset
|
||||
.checked_add(1)
|
||||
.filter(|next| *next < self.total_entries()),
|
||||
}
|
||||
}
|
||||
|
||||
fn persistent_entry_duplicates_local(&self, entry: &HistoryEntry) -> bool {
|
||||
self.replay_seeded_history.iter().any(|local_entry| {
|
||||
local_entry.text == entry.text && local_entry.mention_bindings == entry.mention_bindings
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -939,9 +1013,16 @@ mod tests {
|
||||
// Pretend there are 3 persistent entries.
|
||||
let thread_id = test_thread_id();
|
||||
history.set_metadata(thread_id, /*log_id*/ 1, /*entry_count*/ 3);
|
||||
history.record_local_submission(HistoryEntry::new("latest".to_string()));
|
||||
|
||||
// First Up should request offset 2 (latest) and await async data.
|
||||
// First Up should recall current-session local history.
|
||||
assert!(history.should_handle_navigation("", /*cursor*/ 0));
|
||||
assert_eq!(
|
||||
Some(HistoryEntry::new("latest".to_string())),
|
||||
history.navigate_up(&tx)
|
||||
);
|
||||
|
||||
// Next Up should request offset 2 and await async data.
|
||||
assert!(history.navigate_up(&tx).is_none()); // don't replace the text yet
|
||||
|
||||
// Verify that a history lookup request was sent.
|
||||
|
||||
@@ -184,6 +184,7 @@ pub(crate) use chat_composer::ChatComposer;
|
||||
pub(crate) use chat_composer::ChatComposerConfig;
|
||||
pub(crate) use chat_composer::InputResult;
|
||||
pub(crate) use chat_composer::QueuedInputAction;
|
||||
pub(crate) use chat_composer_history::HistoryEntry;
|
||||
|
||||
use crate::status_indicator_widget::StatusDetailsCapitalization;
|
||||
use crate::status_indicator_widget::StatusIndicatorWidget;
|
||||
@@ -1543,6 +1544,10 @@ impl BottomPane {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn record_replayed_user_message_history(&mut self, entry: HistoryEntry) {
|
||||
self.composer.record_replayed_user_message_history(entry);
|
||||
}
|
||||
|
||||
pub(crate) fn on_file_search_result(&mut self, query: String, matches: Vec<FileMatch>) {
|
||||
self.composer.on_file_search_result(query, matches);
|
||||
self.request_redraw();
|
||||
|
||||
Reference in new issue
Block a user