Group consecutive tool calls in the full-screen conversation (#60556)

## What changed

- Combine adjacent tool calls into one expandable summary, including calls that move from live output into history. Label successful command groups with summaries such as `Explored files` and `Ran 2 commands`.
- Include web searches in tool groups, hide duplicate provisional search entries, and show a single completed search directly. Preserve its reading position when it completes.
- Refresh summaries as history changes while preserving pinned content during selection and reading.

## Testing

Add regression coverage for web search completion, grouped search expansion, loading older history during selection, and command completion with an expanded summary.

GitOrigin-RevId: 98f4e0c49cbf8014c25874cf47d1c0c92ee6b30d
This commit is contained in:
Eric Traut
2026-11-10 08:35:36 +00:00
committed by copyberry
parent 7539b0a856
commit bcbb7f5a9c
9 changed files with 425 additions and 66 deletions
+27 -9
View File
@@ -67,11 +67,26 @@ impl TranscriptView {
cells: &[Arc<dyn HistoryCell>],
ids: Vec<String>,
) -> bool {
// Disclosure follows its activity into history instead of an unrelated next live tail.
let previous = std::mem::replace(&mut self.disclosure.live_ids, ids);
let ids = &self.disclosure.live_ids;
if previous != *ids {
self.live_key = None;
}
// A completed single search retains its reading position even after its control goes away.
if self.snapshot().is_none()
&& let Position::Reading(anchor) = self.position
&& anchor.key == EntryKey::Live
&& let Some(index) = self.canonical_activity_index(cells, &self.disclosure.live_ids)
&& let Some(index) = self.canonical_activity_index(cells, &previous).or_else(|| {
let id = previous
.iter()
.find_map(|id| id.strip_prefix("summary:web:"))?;
(anchor.index..cells.len()).rfind(|&index| {
cells[index]
.as_any()
.downcast_ref::<crate::history_cell::WebSearchCell>()
.is_some_and(|search| search.is_completed() && search.call_id() == id)
})
})
&& let Some(cell) = cells.get(index)
{
self.position = Position::Reading(Anchor {
@@ -81,22 +96,25 @@ impl TranscriptView {
row_bias: 0,
});
}
if self.disclosure.live_ids != ids {
self.live_key = None;
if self.snapshot().is_none()
&& let Some((_, focused)) = &self.disclosure.focused
&& focused.iter().any(|id| id.starts_with("summary:web:"))
&& self.canonical_activity_index(cells, focused).is_none()
{
self.disclosure.focused = None;
}
let expanded = self.disclosure.is_expanded(&ids);
let expanded = self.disclosure.is_expanded(ids);
if expanded {
self.disclosure.expanded.extend(ids.iter().cloned());
}
self.disclosure.live_ids = ids;
expanded
}
pub(super) fn activity_ids(&self, cells: &[Arc<dyn HistoryCell>], index: usize) -> Vec<String> {
cells.get(index).map_or_else(
|| self.disclosure.live_ids.clone(),
|cell| match self.tool_summary(cell.as_ref(), EntryKey::cell(cell)) {
Some(row) if !self.is_search_expanded(EntryKey::cell(cell)) => row.ids,
|cell| match self.turn_row(cells, index) {
Some(row) if !self.is_search_expanded(EntryKey::cell(cell)) => row.ids().to_vec(),
_ => cell.activity_ids(),
},
)
@@ -340,7 +358,7 @@ impl TranscriptView {
if ids.is_empty() {
return;
}
let key = self.entry_key(cells, index);
let key = cells.get(index).map_or(EntryKey::Live, EntryKey::cell);
let matched = self.is_search_expanded(key);
self.disclosure.anchor_control = None;
let previous_top = self.visible.first().map(|visible| {
+26 -11
View File
@@ -127,6 +127,7 @@ struct CachedLayout {
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) struct CellPresentation {
summarized: bool,
separated: bool,
shares_prompt_background: bool,
turn_tip_space: bool,
@@ -151,6 +152,7 @@ impl TranscriptView {
self.disclosure.expanded.extend(ids);
}
CellPresentation {
summarized: false,
separated: false,
shares_prompt_background: false,
turn_tip_space: false,
@@ -238,14 +240,23 @@ impl TranscriptView {
}
let mode = self.mode;
let mut presentation = self.content_presentation(cell.as_ref(), full_content, mode);
if !full_content
&& let Some(summary) = self.tool_summary(cell.as_ref(), EntryKey::cell(cell))
{
let expanded = self.disclosure.is_expanded(&summary.ids);
if expanded {
self.disclosure.expanded.extend(summary.ids.iter().cloned());
if !full_content && let Some(row) = self.turn_row(cells, index) {
match &row {
turn_details::TurnRow::Tools(_) => {
let ids = row.ids();
presentation.expanded = self.disclosure.is_expanded(ids);
presentation.summarized = true;
if presentation.expanded {
self.disclosure.expanded.extend(ids.iter().cloned());
}
return Some(self.cache.get(cell, width, presentation, || {
row.layout(width, presentation.expanded, Vec::new())
}));
}
turn_details::TurnRow::Hidden => {
return Some(Arc::new(TextLayout::new(Vec::new(), width)));
}
}
return Some(Arc::new(summary.layout(width, expanded, Vec::new())));
}
let shortcut = self
.disclosure
@@ -254,8 +265,8 @@ impl TranscriptView {
let shares_prompt_background = is_rich_user_prompt(cell.as_ref(), mode)
&& (0..index)
.rev()
.find(|&previous| {
let previous = &cells[previous];
.find(|&previous_index| {
let previous = &cells[previous_index];
let key = EntryKey::cell(previous);
if let Some(layout) = self.search.match_layout(key).or_else(|| {
self.snapshot()
@@ -264,8 +275,8 @@ impl TranscriptView {
}) {
return layout.row_count() > 0;
}
if !full_content && self.tool_summary(previous.as_ref(), key).is_some() {
return true;
if !full_content && let Some(row) = self.turn_row(cells, previous_index) {
return !row.ids().is_empty();
}
let previous_presentation =
self.content_presentation(previous.as_ref(), self.detailed, mode);
@@ -390,6 +401,10 @@ impl LayoutCache {
self.get(cell, width, presentation, render).row_count() > 0
}
pub(super) fn clear_summaries(&mut self) {
self.entries.retain(|entry| !entry.presentation.summarized);
}
pub(super) fn clear(&mut self) {
self.entries.clear();
}
@@ -46,6 +46,7 @@ fn layouts_refresh_for_width_animation_and_mutable_frames() {
&history,
width,
CellPresentation {
summarized: false,
separated: false,
shares_prompt_background: false,
turn_tip_space: false,
@@ -104,6 +105,7 @@ fn recent_entries_are_reused_and_old_entries_are_evicted() {
&history,
/*width*/ 20,
CellPresentation {
summarized: false,
separated: false,
shares_prompt_background: false,
turn_tip_space: false,
@@ -126,6 +128,7 @@ fn recent_entries_are_reused_and_old_entries_are_evicted() {
&history,
/*width*/ 20,
CellPresentation {
summarized: false,
separated: false,
shares_prompt_background: false,
turn_tip_space: false,
@@ -46,6 +46,7 @@ impl TranscriptView {
if range.is_empty() {
return;
}
self.invalidate_turn_details_from(range.start);
self.retain_search_origin(cells, range.clone());
if range.end == cells.len() && self.last_tail == cells.last().map(EntryKey::cell) {
self.last_tail = Some(EntryKey::cell(replacement));
@@ -93,6 +94,7 @@ impl TranscriptView {
if range.is_empty() {
return;
}
self.invalidate_turn_details_from(range.start);
self.retain_search_origin(cells, range.clone());
// Older history may extend the final group without introducing new activity.
if range.end == cells.len() && self.last_tail == cells.last().map(EntryKey::cell) {
@@ -591,6 +591,7 @@ impl TranscriptView {
cells: &[Arc<dyn HistoryCell>],
inserted: Range<usize>,
) {
self.invalidate_turn_details_from(/*index*/ 0);
// With only a header and live output, the first older page extends the canonical tail.
if !inserted.is_empty()
&& inserted.end == cells.len()
+259 -40
View File
@@ -1,21 +1,37 @@
//! Output-free tool summaries for the full-screen conversation.
use super::*;
//! Summarize tool invocations on the full-screen conversation surface. The transcript, Find,
//! and scrollback retain their original cells, including command output.
pub(crate) struct ToolSummary {
pub(super) ids: Vec<String>,
calls: Vec<String>,
use super::*;
use crate::history_cell::DynamicToolCallCell;
use crate::history_cell::UserHistoryCell;
use crate::history_cell::WebSearchCell;
#[derive(Clone)]
pub(crate) enum TurnRow {
Tools(Arc<ToolGroup>),
Hidden,
}
impl ToolSummary {
impl TurnRow {
pub(crate) fn ids(&self) -> &[String] {
match self {
Self::Tools(group) => &group.ids,
Self::Hidden => &[],
}
}
pub(crate) fn layout(
&self,
width: u16,
expanded: bool,
auxiliary: Vec<HyperlinkLine>,
auxiliary: Vec<crate::terminal_hyperlinks::HyperlinkLine>,
) -> TextLayout {
let Self::Tools(group) = self else {
return TextLayout::new(auxiliary, width);
};
let mut lines = Vec::new();
if expanded {
for call in &self.calls {
for call in &group.calls {
let call = call.replace(char::is_control, " ");
let line = Line::from(format!(" {call}")).dim();
lines.push(crate::history_cell::activity_preview::clipped_line(
@@ -24,10 +40,10 @@ impl ToolSummary {
}
}
lines.extend(auxiliary);
let label = self.label();
let label = group.label();
let arrow = if expanded {
"▾"
} else if self
} else if group
.calls
.last()
.is_some_and(|call| call.starts_with("Search"))
@@ -39,9 +55,86 @@ impl ToolSummary {
TextLayout::new(lines, width)
.with_disclosure_control_at(format!("{arrow} {label}"), /*source_offset*/ 0)
}
}
#[derive(Default)]
pub(super) struct TurnDetails {
// Rows follow canonical cell order, followed by one slot for the live tail.
rows: Vec<Entry>,
start: usize,
source: Option<(usize, Option<EntryKey>)>,
live: (Vec<String>, Vec<String>),
pending: Vec<(usize, DynamicToolCallCell)>,
indexed_snapshot: bool,
}
struct Entry {
key: EntryKey,
row: Option<TurnRow>,
}
#[derive(Default)]
pub(crate) struct ToolGroup {
anchor: Option<usize>,
ids: Vec<String>,
calls: Vec<String>,
explored: usize,
ran: usize,
}
fn summary_ids(cell: &dyn HistoryCell) -> Vec<String> {
if let Some(search) = cell.as_any().downcast_ref::<WebSearchCell>() {
vec![format!("web:{}", search.call_id())]
} else {
cell.activity_ids()
}
}
impl ToolGroup {
// Walk backwards so completion and phase information are known before folding a row.
fn push(&mut self, rows: &mut [Entry], index: usize, ids: Vec<String>, calls: Vec<String>) {
self.anchor = Some(index);
let key = rows[index].key;
rows[index].row = Some(TurnRow::Hidden);
self.ids
.extend(ids.into_iter().map(|id| format!("summary:{id}")));
if key != EntryKey::Live {
self.ids.push(format!("work:{key:?}"));
}
self.calls.extend(calls.into_iter().rev());
}
fn finish(&mut self, rows: &mut [Entry], cells: &[Arc<dyn HistoryCell>]) {
if let Some(anchor) = self.anchor {
let mut group = std::mem::take(self);
if group.calls.len() == 1
&& cells
.get(anchor)
.and_then(|cell| cell.as_any().downcast_ref::<WebSearchCell>())
.is_some_and(WebSearchCell::is_completed)
{
rows[anchor].row = None;
return;
}
if group.ids.is_empty() {
group.ids.push("work:Live".into());
}
group.calls.reverse();
rows[anchor].row = Some(TurnRow::Tools(Arc::new(group)));
}
}
fn label(&self) -> String {
let calls = &self.calls;
let (explored, ran) = (self.explored, self.ran);
if calls.len() > 1 && explored + ran == calls.len() {
let noun = if ran == 1 { "command" } else { "commands" };
return match (explored, ran) {
(0, _) => format!("Ran {ran} {noun}"),
(_, 0) => "Explored files".into(),
_ => format!("Explored files · ran {ran} {noun}"),
};
}
let mut lines = calls.last().into_iter().flat_map(|last| last.lines());
let mut label = lines
.next()
@@ -57,38 +150,105 @@ impl ToolSummary {
}
}
impl TranscriptView {
pub(super) fn tool_summary(
&self,
cell: &dyn HistoryCell,
key: EntryKey,
) -> Option<ToolSummary> {
if !self.collapse_turn_details || self.detailed || self.mode != HistoryRenderMode::Rich {
return None;
impl TurnDetails {
fn index_turn(
&mut self,
cells: &[Arc<dyn HistoryCell>],
latest: bool,
snapshot: Option<&ViewSnapshot>,
) {
let rows = &mut self.rows;
let start = rows.len();
let keys = cells
.iter()
.map(EntryKey::cell)
.chain(latest.then_some(EntryKey::Live));
rows.extend(keys.map(|key| Entry { key, row: None }));
let rows = &mut rows[start..];
let mut group = ToolGroup::default();
if latest && !self.live.0.is_empty() {
group.push(rows, cells.len(), self.live.1.clone(), self.live.0.clone());
}
let calls = cell.tool_call_labels();
if calls.is_empty() {
return None;
let mut searches = std::collections::HashSet::new();
for (index, cell) in cells.iter().enumerate().rev() {
// Keep older calls outside the group whose disclosure is pinned on screen.
if group.anchor.is_some_and(|anchor| {
snapshot
.and_then(|snap| snap.pinned.get(&rows[anchor].key))
.is_some_and(|layout| layout.disclosure)
}) {
group.finish(rows, cells);
}
let any = cell.as_any();
// A live web search can leave its provisional cell before the completed one.
if let Some(search) = any.downcast_ref::<WebSearchCell>()
&& !searches.insert(search.call_id())
{
rows[index].row = Some(TurnRow::Hidden);
continue;
}
let calls = cell.tool_call_labels();
if let Some(tool) = any.downcast_ref::<DynamicToolCallCell>()
&& tool.is_active()
{
self.pending.push((start + index, tool.clone()));
}
if calls.is_empty() {
group.finish(rows, cells);
} else {
if let Some(exec) = any.downcast_ref::<crate::exec_cell::ExecCell>()
&& exec.iter_calls().all(|call| {
call.output
.as_ref()
.is_some_and(|output| output.exit_code == 0)
})
{
if exec.is_exploring_cell() {
group.explored += calls.len();
} else {
group.ran += calls.len();
}
}
group.push(rows, index, summary_ids(cell.as_ref()), calls);
}
}
let mut ids: Vec<_> = cell
.activity_ids()
.into_iter()
.map(|id| format!("summary:{id}"))
.collect();
if key != EntryKey::Live || ids.is_empty() {
ids.push(format!("work:{key:?}"));
}
Some(ToolSummary { ids, calls })
group.finish(rows, cells);
}
}
impl TranscriptView {
pub(crate) fn sync_live_tool_summary(
&mut self,
cells: &[Arc<dyn HistoryCell>],
enabled: bool,
active: Option<&dyn HistoryCell>,
) -> (Option<ToolSummary>, bool) {
if self.collapse_turn_details != enabled {
self.collapse_turn_details = enabled;
) -> (Option<TurnRow>, bool) {
self.sync_turn_details(cells, enabled, active);
let summary = self.turn_row(cells, cells.len());
let ids = summary.as_ref().map_or_else(
|| active.map_or_else(Vec::new, HistoryCell::activity_ids),
|row| row.ids().to_vec(),
);
let expanded = self.sync_live_activity(cells, ids) && self.mode == HistoryRenderMode::Rich;
(summary, expanded)
}
pub(crate) fn invalidate_turn_details_from(&mut self, index: usize) {
if let Some(details) = &mut self.turn_details {
details.source = None;
if index < details.start {
details.start = 0;
}
}
}
pub(crate) fn sync_turn_details(
&mut self,
cells: &[Arc<dyn HistoryCell>],
enabled: bool,
active: Option<&dyn HistoryCell>,
) {
if self.turn_details.is_some() != enabled {
self.selection = None;
self.select_mode = None;
self.copy_mode = None;
@@ -96,13 +256,72 @@ impl TranscriptView {
self.live_key = None;
self.cache.clear();
self.release_live_reading();
self.turn_details = enabled.then(TurnDetails::default);
}
let summary = active.and_then(|cell| self.tool_summary(cell, EntryKey::Live));
let ids = summary.as_ref().map_or_else(
|| active.map_or_else(Vec::new, HistoryCell::activity_ids),
|summary| summary.ids.clone(),
);
let expanded = self.sync_live_activity(cells, ids) && self.mode == HistoryRenderMode::Rich;
(summary, expanded)
let snapshot = self
.selection
.as_ref()
.map(|s| &s.snapshot)
.or(self.held_reading.as_ref());
let Some(details) = &mut self.turn_details else {
return;
};
let held = snapshot.map(|snapshot| snapshot.cells.as_ref());
// Appended calls keep their canonical ids while the snapshot paints its pinned rows.
let replaced_history = held.filter(|held| {
cells.get(..held.len()).is_none_or(|prefix| {
prefix.last().map(EntryKey::cell) != held.last().map(EntryKey::cell)
})
});
if details.indexed_snapshot && held.is_none() {
details.start = 0;
details.source = None;
details.indexed_snapshot = false;
}
if replaced_history.is_some() && details.source.is_some() {
return;
}
let cells = replaced_history.unwrap_or(cells);
let source = (cells.len(), cells.last().map(EntryKey::cell));
let live = active
.map(|cell| (cell.tool_call_labels(), summary_ids(cell)))
.unwrap_or_default();
let finished_tool = details.pending.iter().any(|(_, tool)| !tool.is_active());
if details.source == Some(source) && details.live == live && !finished_tool {
return;
}
if replaced_history.is_none() {
details.live = live;
}
let mut start = details.start;
if finished_tool
|| cells.get(start).map(EntryKey::cell)
!= details.rows.get(start).map(|entry| entry.key)
{
start = 0;
}
details.rows.truncate(start);
details.pending.retain(|(index, _)| *index < start);
let retained_end = held.map(<[_]>::len);
let prompts = (start + 1..cells.len())
.filter(|&i| Some(i) == retained_end || cells[i].as_any().is::<UserHistoryCell>());
for end in prompts.chain([cells.len()]) {
details.start = start;
details.index_turn(&cells[start..end], end == cells.len(), snapshot);
start = end;
}
details.indexed_snapshot = held.is_some();
details.source = Some(source);
self.cache.clear_summaries();
}
pub(crate) fn turn_row(&self, cells: &[Arc<dyn HistoryCell>], index: usize) -> Option<TurnRow> {
if self.detailed || self.mode != HistoryRenderMode::Rich {
return None;
}
let details = self.turn_details.as_ref()?;
let key = cells.get(index).map_or(EntryKey::Live, EntryKey::cell);
let entry = details.rows.get(index).filter(|entry| entry.key == key)?;
entry.row.clone()
}
}