Keep TUI tool groups independent when results arrive late (#63989)

## Why

When a command or tool finishes in a different transcript group from where it
started, both groups can share an identity. The later group can inherit the
original group's expanded state or steal its keyboard focus.

## What changed

Assign distinct identities to repeated tool activity in chronological order.
Keep those identities stable as the live transcript updates and results move
into history, so each group retains its own expansion state and focus.

## Testing

Add regression tests for a command that finishes after assistant commentary
and an MCP tool that returns an error in a later turn.

GitOrigin-RevId: ca69a68bb32746bdbe22ec7bc9633e4323f38347
This commit is contained in:
Felipe Coury
2026-11-25 22:39:45 +00:00
committed by copyberry
parent d800b00e88
commit ceed625df9
2 changed files with 138 additions and 1 deletions
@@ -73,6 +73,8 @@ pub(super) struct TurnDetails {
live: (Vec<String>, Vec<String>),
pending: Vec<(usize, DynamicToolCallCell)>,
indexed_snapshot: bool,
// Counts before the mutable tail, which is rebuilt whenever live activity changes.
prior_activity_counts: std::collections::HashMap<String, usize>,
}
struct Entry {
@@ -81,7 +83,7 @@ struct Entry {
row: Option<TurnRow>,
}
#[derive(Default)]
#[derive(Clone, Default)]
pub(crate) struct ToolGroup {
anchor: Option<usize>,
ids: Vec<String>,
@@ -276,6 +278,34 @@ impl TurnDetails {
}
}
group.finish(rows, cells);
// A tool can start in one group and finish in another. Assign repeated member
// IDs in chronological order so a late result never takes the original group's
// expansion or focus. Rebuild the live tail from the same prefix on every update.
let mut counts = std::collections::HashMap::new();
for entry in rows.iter_mut() {
let Some(TurnRow::Tools(group)) = &mut entry.row else {
continue;
};
for id in Arc::make_mut(group).ids.iter_mut().rev() {
if !id.starts_with("summary:") {
continue;
}
let count = counts.entry(id.clone()).or_insert_with(|| {
self.prior_activity_counts
.get(id)
.copied()
.unwrap_or_default()
});
if *count > 0 {
*id = format!("repeat:{count}:{id}");
}
*count += 1;
}
}
if !latest {
self.prior_activity_counts.extend(counts);
}
if hidden_count > 0 {
let label = completion
.and_then(FinalMessageSeparator::display_label)
@@ -375,6 +405,9 @@ impl TranscriptView {
{
start = 0;
}
if start == 0 {
details.prior_activity_counts.clear();
}
details.rows.truncate(start);
details.pending.retain(|(index, _)| *index < start);
let retained_end = held.map(<[_]>::len);