Include older itemless Cloud failures in full transcripts (#60320)

## Why

Failed Cloud turns can contain an error without any items. Loading history through item pages alone can omit these older failures from the full transcript.

## What changed

- Add a `FullTranscript` hydration scope that fetches remaining Cloud turn metadata in pages of 100 after loading items.
- Deduplicate overlapping turn pages while preserving chronological order and already loaded items.
- Use the new scope for session transcripts while retaining `Complete` hydration for exports.

## Testing

Add a regression test covering older itemless failures, overlapping turn pages, repeated pagination cursors, transcript ordering, and item-only exports.

GitOrigin-RevId: 51183e35984aa0183d044061768555bb6f5d1189
This commit is contained in:
Benjamin Carlsson
2026-11-09 01:53:50 +00:00
committed by copyberry
parent 8fcba7fae9
commit b9796386e5
3 changed files with 152 additions and 3 deletions
+30 -2
View File
@@ -29,6 +29,7 @@ use color_eyre::eyre::Result;
use color_eyre::eyre::WrapErr;
pub(crate) const INITIAL_HISTORY_TURN_LIMIT: u32 = 5;
const HISTORY_TURN_PAGE_LIMIT: u32 = 100;
pub(crate) const HISTORY_ITEM_PAGE_LIMIT: u32 = 100;
pub(crate) const HISTORY_ITEM_SCAN_LIMIT: usize = 4 * HISTORY_ITEM_PAGE_LIMIT as usize;
@@ -36,6 +37,8 @@ pub(crate) const HISTORY_ITEM_SCAN_LIMIT: usize = 4 * HISTORY_ITEM_PAGE_LIMIT as
pub(crate) enum HistoryHydrationScope<'a> {
Initial,
Complete,
/// Include older turns that have no items.
FullTranscript,
ThroughTurn(&'a str),
}
@@ -68,7 +71,7 @@ impl HistoryLoadBudget {
let rows = local_settings
.and_then(|settings| resize_reflow_max_rows(settings.terminal_resize_reflow()));
let items = match (scope, config, rows) {
(HistoryHydrationScope::Complete, _, _)
(HistoryHydrationScope::Complete | HistoryHydrationScope::FullTranscript, _, _)
| (HistoryHydrationScope::ThroughTurn(_), _, _)
| (HistoryHydrationScope::Initial, Some(_), None) => None,
(HistoryHydrationScope::Initial, Some(_), Some(max_rows)) => {
@@ -127,6 +130,15 @@ fn advancing_cursor(
next.filter(|next| seen_cursors.insert(next.clone()))
}
fn prepend_older_turns(turns: &mut Vec<Turn>, mut older: Vec<Turn>) {
let mut new_ids: HashSet<_> = older.iter().map(|turn| turn.id.clone()).collect();
for known in turns.iter() {
new_ids.remove(&known.id);
}
older.retain(|turn| new_ids.remove(&turn.id));
turns.splice(0..0, older.into_iter().rev());
}
#[derive(Clone, Debug, Default)]
pub(crate) struct ThreadHistoryPagination {
pub(super) history_mode: ThreadHistoryMode,
@@ -307,7 +319,7 @@ impl AppServerSession {
for turn in &page.data {
missing_turn_ids.remove(&turn.id);
}
turns.splice(0..0, page.data.into_iter().rev());
prepend_older_turns(turns, page.data);
}
if let Some(turn) = turns.iter_mut().find(|turn| turn.id == entry.turn_id)
&& !turn.items.iter().any(|item| item.id() == entry.item.id())
@@ -422,6 +434,22 @@ impl AppServerSession {
rendered_rows = rendered_rows.saturating_add(items.len());
}
}
// A failed Cloud turn may have only an error and no items.
if self.thread_params_mode() == super::ThreadParamsMode::Cloud
&& scope == HistoryHydrationScope::FullTranscript
{
while let Some(cursor) = state.next_turn_cursor.take() {
let page = self
.thread_turns_page(thread_id, Some(cursor.clone()), HISTORY_TURN_PAGE_LIMIT)
.await?;
state.next_turn_cursor = advancing_cursor(
Some(&cursor),
page.next_cursor,
&mut state.seen_turn_cursors,
);
prepend_older_turns(&mut thread.turns, page.data);
}
}
if config.is_some() {
self.history_pagination.insert(thread_id, state);
}