From 4f5e8eea1064e7e1bece76f7021572867b5c0a9f Mon Sep 17 00:00:00 2001 From: Matthew Ball <13823657+matthewrball@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:48:11 -0800 Subject: [PATCH 1/4] Agent: List the files a turn changed under its reply With a turn's steps folded, what Maple changed was only visible by opening them and reading each edit. Once a turn is over, its reply carries a card: how many files changed, each file with the lines added and removed, and the diff on a click. It adds up the turn's edits and writes that went through, from the diffs their steps already derive, and is built once per turn. --- apps/maple-agent/app/locales/de/app.ftl | 5 + apps/maple-agent/app/locales/en/app.ftl | 5 + apps/maple-agent/app/locales/es/app.ftl | 5 + apps/maple-agent/app/src/ui/chat/changes.rs | 411 ++++++++++++++++++ apps/maple-agent/app/src/ui/chat/mod.rs | 26 ++ apps/maple-agent/app/src/ui/chat/paging.rs | 3 + .../maple-agent/app/src/ui/chat/transcript.rs | 107 ++++- apps/maple-agent/app/src/ui/chat/turn.rs | 77 +++- .../app/translation-revisions/de.txt | 1 + .../app/translation-revisions/es.txt | 1 + 10 files changed, 628 insertions(+), 13 deletions(-) create mode 100644 apps/maple-agent/app/src/ui/chat/changes.rs diff --git a/apps/maple-agent/app/locales/de/app.ftl b/apps/maple-agent/app/locales/de/app.ftl index 6f0198904..d148e4c31 100644 --- a/apps/maple-agent/app/locales/de/app.ftl +++ b/apps/maple-agent/app/locales/de/app.ftl @@ -1568,6 +1568,11 @@ chat-turn-worked = Gearbeitet chat-turn-worked-seconds = { $seconds } s gearbeitet # $minutes is a whole number of minutes; $seconds is the rest, under 60. chat-turn-worked-minutes = { $minutes } min { $seconds } s gearbeitet +# $count is the number of files a turn changed. +chat-turn-files-changed = { $count -> + [one] { $count } Datei geändert + *[other] { $count } Dateien geändert + } chat-settings = Einstellungen chat-usage = Nutzung chat-usage-unavailable = Nutzung nicht verfügbar diff --git a/apps/maple-agent/app/locales/en/app.ftl b/apps/maple-agent/app/locales/en/app.ftl index c091ecc1b..4834a8866 100644 --- a/apps/maple-agent/app/locales/en/app.ftl +++ b/apps/maple-agent/app/locales/en/app.ftl @@ -1568,6 +1568,11 @@ chat-turn-worked = Worked chat-turn-worked-seconds = Worked for { $seconds }s # $minutes is a whole number of minutes; $seconds is the rest, under 60. chat-turn-worked-minutes = Worked for { $minutes }m { $seconds }s +# $count is the number of files a turn changed. +chat-turn-files-changed = { $count -> + [one] { $count } file changed + *[other] { $count } files changed + } chat-settings = Settings chat-usage = Usage chat-usage-unavailable = Usage unavailable diff --git a/apps/maple-agent/app/locales/es/app.ftl b/apps/maple-agent/app/locales/es/app.ftl index 94e111ff7..004477fc0 100644 --- a/apps/maple-agent/app/locales/es/app.ftl +++ b/apps/maple-agent/app/locales/es/app.ftl @@ -1455,6 +1455,11 @@ chat-turn-worked = Trabajó chat-turn-worked-seconds = Trabajó { $seconds } s # $minutes is a whole number of minutes; $seconds is the rest, under 60. chat-turn-worked-minutes = Trabajó { $minutes } min { $seconds } s +# $count is the number of files a turn changed. +chat-turn-files-changed = { $count -> + [one] { $count } archivo modificado + *[other] { $count } archivos modificados + } chat-settings = Configuración chat-usage = Uso chat-usage-unavailable = Uso no disponible diff --git a/apps/maple-agent/app/src/ui/chat/changes.rs b/apps/maple-agent/app/src/ui/chat/changes.rs new file mode 100644 index 000000000..e19d83007 --- /dev/null +++ b/apps/maple-agent/app/src/ui/chat/changes.rs @@ -0,0 +1,411 @@ +//! The files a turn changed, listed in a card under its reply. +//! +//! A turn's edits and writes each say what they did on their own step. +//! Once the turn is over, the card adds them up by file, so what changed is +//! one glance away without opening the steps. A file's row opens the +//! changes the turn made to it. + +use std::cell::RefCell; +use std::collections::{BTreeSet, HashMap}; +use std::rc::Rc; +use std::sync::Arc; + +use gpui::{Context, Div, SharedString, div, prelude::*, px}; +use maple_agent::agent::AgentTimelineItem; + +use super::ChatScreen; +use super::step::{self, StepState}; +use super::transcript::diff_rows; +use super::turn::TurnRow; +use crate::ui::i18n::MessageCache; +use crate::ui::motion::{self, HoverBg as _}; +use crate::ui::theme; + +/// The +/- lines of an edit or a write, as its row derives them. +type DiffLines = Vec<(char, SharedString)>; + +/// One file a turn changed. +struct FileChange { + /// The path as the tool call gave it. + path: SharedString, + name: SharedString, + /// The folders before the name, with their trailing slash. + folder: SharedString, + /// "+6" and "−2". + counts: (SharedString, SharedString), + /// The +/- lines of every step that changed the file, in order. + lines: DiffLines, +} + +/// What a turn changed, file by file in the order it first touched them. +pub(super) struct TurnChanges { + files: Vec, + counts: (SharedString, SharedString), +} + +/// The card a reply row carries. +pub(super) struct ReplyChanges<'a> { + pub(super) changes: Rc, + /// The id of the turn's lead row, which the card's state is kept under. + pub(super) lead: &'a str, + /// The files whose changes show. + pub(super) open: Option<&'a BTreeSet>, +} + +/// The path an edit or a write call names. +fn changed_path(item: &AgentTimelineItem) -> Option<&str> { + let input = item.input.as_ref()?.as_object()?; + if !input.contains_key("edits") && !input.contains_key("content") { + return None; + } + input.get("path")?.as_str() +} + +fn counts(added: usize, removed: usize) -> (SharedString, SharedString) { + ( + format!("+{added}").into(), + format!("\u{2212}{removed}").into(), + ) +} + +impl TurnChanges { + /// Add up the edits and writes among a turn's `steps` that went + /// through, each with the +/- lines its row derived. `None` when the + /// turn changed nothing. + pub(super) fn of<'a>( + steps: impl Iterator)>, + ) -> Option { + struct Sum<'a> { + path: &'a str, + added: usize, + removed: usize, + lines: DiffLines, + } + let mut sums: Vec = Vec::new(); + for (item, lines) in steps { + // A call that failed or was refused changed nothing. + if StepState::of(item.status.as_deref()) != StepState::Done { + continue; + } + let Some(path) = changed_path(item) else { + continue; + }; + let index = sums + .iter() + .position(|sum| sum.path == path) + .unwrap_or_else(|| { + sums.push(Sum { + path, + added: 0, + removed: 0, + lines: Vec::new(), + }); + sums.len() - 1 + }); + let sum = &mut sums[index]; + for (sign, line) in lines.iter() { + match sign { + '+' => sum.added += 1, + '-' => sum.removed += 1, + // The line that names the file, which the row does. + _ => continue, + } + sum.lines.push((*sign, line.clone())); + } + } + if sums.is_empty() { + return None; + } + let total = sums.iter().fold((0, 0), |(added, removed), sum| { + (added + sum.added, removed + sum.removed) + }); + Some(Self { + counts: counts(total.0, total.1), + files: sums + .into_iter() + .map(|sum| { + let name = sum.path.rfind(['/', '\\']).map_or(0, |at| at + 1); + FileChange { + path: sum.path.to_owned().into(), + name: sum.path[name..].to_owned().into(), + folder: sum.path[..name].to_owned().into(), + counts: counts(sum.added, sum.removed), + lines: sum.lines, + } + }) + .collect(), + }) + } + + /// Each file's path with its "+n" and "−n". + #[cfg(test)] + pub(super) fn files(&self) -> Vec<(&str, &str, &str)> { + self.files + .iter() + .map(|file| { + ( + file.path.as_ref(), + file.counts.0.as_ref(), + file.counts.1.as_ref(), + ) + }) + .collect() + } +} + +/// Each finished turn's changes, by the id of its lead row. An entry is +/// dropped when one of the turn's steps changes, and built again the next +/// time its card draws. +#[derive(Default)] +pub(super) struct ChangesCache(RefCell>>>); + +impl ChangesCache { + pub(super) fn clear(&self) { + self.0.borrow_mut().clear(); + } + + pub(super) fn forget(&self, lead: &str) { + self.0.borrow_mut().remove(lead); + } + + fn get( + &self, + lead: &str, + build: impl FnOnce() -> Option, + ) -> Option> { + if let Some(changes) = self.0.borrow().get(lead) { + return changes.clone(); + } + let changes = build().map(Rc::new); + self.0.borrow_mut().insert(lead.to_owned(), changes.clone()); + changes + } +} + +/// The card under a turn's reply. `reply` is the id of the row it sits on. +pub(super) fn render_changes( + card: &ReplyChanges, + reply: &str, + chat: &gpui::WeakEntity, + messages: &MessageCache, +) -> Div { + const ROW: gpui::Pixels = px(34.); + let rule = gpui::rgb(theme::border_subtle()); + let changes = &card.changes; + let mut body = div() + .debug_selector(|| "turn-changes".to_string()) + .mt_2() + .w_full() + .max_w(px(620.)) + .rounded(theme::RADIUS_MD) + .bg(gpui::rgb(theme::bg_tool_card())) + .border_1() + .border_color(rule) + .overflow_hidden() + .flex() + .flex_col() + .child( + div() + .h(ROW) + .px_3() + .flex() + .items_center() + .justify_between() + .gap_3() + .text_xs() + .text_color(gpui::rgb(theme::text_muted())) + .child(gpui::text!(messages.get( + "chat-turn-files-changed", + [("count", changes.files.len().into())], + ))) + .child(step::changed(&changes.counts)), + ); + for (index, file) in changes.files.iter().enumerate() { + let shown = card.open.is_some_and(|open| open.contains(&index)); + let (chat, reply, lead) = (chat.clone(), reply.to_owned(), card.lead.to_owned()); + body = body.child( + div() + .id(SharedString::from(format!( + "turn-change-{}-{index}", + card.lead + ))) + .debug_selector(|| "turn-change".to_string()) + .role(gpui::Role::DisclosureTriangle) + .aria_label(file.path.clone()) + .aria_expanded(shown) + .h(ROW) + .px_3() + .flex() + .items_center() + .gap_2p5() + .border_t_1() + .border_color(rule) + .font_family(crate::assets::FONT_MONO) + .text_xs() + .cursor_pointer() + .child(motion::chevron( + SharedString::from(format!("turn-change-chevron-{}-{index}", card.lead)), + shown, + px(12.), + theme::text_muted(), + )) + .child( + div() + .flex_none() + .text_color(gpui::rgb(theme::text_primary())) + .child(file.name.clone()), + ) + .child( + div() + .flex_1() + .min_w_0() + .truncate() + .text_color(gpui::rgb(theme::text_muted())) + .child(file.folder.clone()), + ) + .child(step::changed(&file.counts)) + .hover_bg(theme::overlay_hover()) + .on_click(move |_event, _window, cx: &mut gpui::App| { + chat.update(cx, |chat, cx| chat.toggle_change(&reply, &lead, index, cx)) + .ok(); + }), + ); + if shown { + body = body.child( + div() + .debug_selector(|| "turn-change-diff".to_string()) + .py_1() + .border_t_1() + .border_color(rule) + .bg(gpui::rgb(theme::bg_code_block())) + .child(diff_rows(&file.lines)), + ); + } + } + body +} + +impl ChatScreen { + /// The changes to list under the row at `reply`, with the lead of + /// their turn: the row is the reply that ends a turn, the turn is + /// over, and it changed files. + pub(super) fn changes_under(&self, reply: usize) -> Option<(usize, Rc)> { + let lead = self.turns.reply_of(reply)?; + if self.turn_runs(lead) { + return None; + } + let lead_id = &self.timeline.get(lead)?.id; + let changes = self.turn_changes.get(lead_id, || { + TurnChanges::of( + self.timeline[lead..reply] + .iter() + .enumerate() + .filter(|(offset, _)| self.turns.row(lead + offset) != TurnRow::Plain) + .map(|(_, item)| { + let revision = self + .timeline_index + .get(&item.id) + .map_or(0, |(_, revision)| *revision); + (item, self.derived.get(item, revision).diff_lines.clone()) + }), + ) + })?; + Some((lead, changes)) + } + + /// Show or fold one file's changes in the card under the row `reply`. + pub(super) fn toggle_change( + &mut self, + reply: &str, + lead: &str, + file: usize, + cx: &mut Context, + ) { + let open = self.open_changes.entry(lead.to_owned()).or_default(); + if !open.remove(&file) { + open.insert(file); + } + if let Some(&(index, _)) = self.timeline_index.get(reply) { + self.list_state.remeasure_items(index..index + 1); + } + cx.notify(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn call(status: &str, input: serde_json::Value) -> AgentTimelineItem { + AgentTimelineItem { + label: None, + persisted: None, + live: None, + truncated: None, + revision: 0, + id: String::new(), + item_type: "tool".into(), + role: None, + title: None, + text: None, + status: Some(status.into()), + input: Some(input), + output: None, + created_ms: 0, + merge: "replace".into(), + } + } + + fn changes(calls: &[AgentTimelineItem]) -> Option { + TurnChanges::of(calls.iter().map(|call| { + ( + call, + Arc::new(super::super::transcript::diff_lines_for(call)), + ) + })) + } + + #[test] + fn a_turns_changes_add_up_by_file() { + let edit = |status: &str, path: &str, old: &str, new: &str| { + call( + status, + serde_json::json!({ "path": path, "edits": [{ "oldText": old, "newText": new }] }), + ) + }; + let turn = [ + edit("completed", "src/net/retry.rs", "a", "b\nc"), + call("completed", serde_json::json!({ "command": "cargo test" })), + call( + "completed", + serde_json::json!({ "path": "notes.md", "content": "one\ntwo\nthree" }), + ), + // A second edit of the same file joins the first. + edit("completed", "src/net/retry.rs", "d\ne", "f"), + // An edit that failed changed nothing. + edit("failed", "src/lib.rs", "x", "y"), + ]; + let changes = changes(&turn).expect("the turn changed files"); + assert_eq!( + changes.files(), + [ + ("src/net/retry.rs", "+3", "\u{2212}3"), + ("notes.md", "+3", "\u{2212}0"), + ] + ); + assert_eq!(changes.counts.0.as_ref(), "+6"); + assert_eq!(changes.counts.1.as_ref(), "\u{2212}3"); + assert_eq!(changes.files[0].name.as_ref(), "retry.rs"); + assert_eq!(changes.files[0].folder.as_ref(), "src/net/"); + assert_eq!(changes.files[1].folder.as_ref(), ""); + } + + #[test] + fn a_turn_that_only_ran_commands_changed_nothing() { + let turn = [call( + "completed", + serde_json::json!({ "command": "cargo test" }), + )]; + assert!(changes(&turn).is_none()); + } +} diff --git a/apps/maple-agent/app/src/ui/chat/mod.rs b/apps/maple-agent/app/src/ui/chat/mod.rs index 158f48498..eb2fb7d95 100644 --- a/apps/maple-agent/app/src/ui/chat/mod.rs +++ b/apps/maple-agent/app/src/ui/chat/mod.rs @@ -34,6 +34,7 @@ use crate::ui::titlebar; use crate::ui::widgets; mod cache; +mod changes; mod commands; mod composer; pub(crate) mod computers; @@ -232,6 +233,8 @@ struct TranscriptCtx<'a> { rail: step::Rail, /// For a tool call the user was asked to allow: where that stands. asked: Option, + /// For the reply that ends a turn: the files the turn changed. + changes: Option>, } /// Text-to-speech progress for one message. @@ -856,6 +859,14 @@ pub struct ChatScreen { /// What the user said to the permission requests answered in this /// view, by the id of the tool call each was about: allowed, or not. step_permissions: HashMap, + /// What each finished turn changed, built when its card first draws. + turn_changes: changes::ChangesCache, + /// The files whose changes show in a turn's card, by the turn's lead + /// item id. + open_changes: HashMap>, + /// The reply of the turn at the end, once that turn is over and the + /// row was measured with its card. + settled_reply: Option, /// Clocks of the runs this window saw start, by session id. run_clocks: HashMap, /// A repaint for a running turn's time is on its way. @@ -1694,6 +1705,9 @@ impl ChatScreen { toggled_turns: HashSet::new(), turn_worked: HashMap::new(), step_permissions: HashMap::new(), + turn_changes: changes::ChangesCache::default(), + open_changes: HashMap::new(), + settled_reply: None, run_clocks: HashMap::new(), run_tick_pending: std::cell::Cell::new(false), question_selected: HashMap::new(), @@ -3482,6 +3496,7 @@ impl ChatScreen { self.pages.weights.clear(); self.bound_transcript_window(true); self.turns.rebuild(&self.timeline); + self.turn_changes.clear(); self.timeline_index = self .timeline .iter() @@ -3772,6 +3787,8 @@ impl ChatScreen { self.toggled_turns.clear(); self.turn_worked.clear(); self.step_permissions.clear(); + self.turn_changes.clear(); + self.open_changes.clear(); if let Some(composer) = self.composer.clone() { composer.update(cx, |input, cx| input.reset_vim_context(cx)); } @@ -6368,6 +6385,15 @@ impl ChatScreen { .timeline_revisions .entry(session_id.to_string()) .or_insert(0) += 1; + // A step that arrived or changed may change what its turn's card + // lists. + if let Some(lead) = match self.turns.row(index) { + turn::TurnRow::Lead => Some(index), + turn::TurnRow::Step(lead) => Some(lead), + turn::TurnRow::Plain => None, + } { + self.turn_changes.forget(&self.timeline[lead].id); + } let id = self.timeline[index].id.clone(); let anchor = self.scroll_anchor(); let count = self.timeline.len(); diff --git a/apps/maple-agent/app/src/ui/chat/paging.rs b/apps/maple-agent/app/src/ui/chat/paging.rs index b07a4bb18..d4a03c391 100644 --- a/apps/maple-agent/app/src/ui/chat/paging.rs +++ b/apps/maple-agent/app/src/ui/chat/paging.rs @@ -266,6 +266,9 @@ impl ChatScreen { .retain(|id, _| self.timeline_index.contains_key(id)); self.step_permissions .retain(|id, _| self.timeline_index.contains_key(id)); + self.turn_changes.clear(); + self.open_changes + .retain(|id, _| self.timeline_index.contains_key(id)); self.markdown_cache.clear(); self.derived.clear(); self.messages = Default::default(); diff --git a/apps/maple-agent/app/src/ui/chat/transcript.rs b/apps/maple-agent/app/src/ui/chat/transcript.rs index ecc33337b..998dbe44d 100644 --- a/apps/maple-agent/app/src/ui/chat/transcript.rs +++ b/apps/maple-agent/app/src/ui/chat/transcript.rs @@ -16,6 +16,7 @@ use maple_agent::agent::{ }; use super::cache::{MAX_DIFF_LINES, MarkdownKind}; +use super::changes; use super::speech::speak_message_button; use super::step::{self, StepState}; use super::turn::{self, TurnRow}; @@ -135,6 +136,14 @@ impl ChatScreen { } }) }, + changes: chat.changes_under(ix).map(|(lead, changes)| { + let lead = chat.timeline[lead].id.as_str(); + changes::ReplyChanges { + changes, + lead, + open: chat.open_changes.get(lead), + } + }), }; let expanded = tool_details != chat.toggled_tools.contains(&item.id); let revision = chat @@ -1268,6 +1277,9 @@ fn render_message(item: &AgentTimelineItem, revision: u64, transcript: &Transcri }) .child(timestamp_label(item, &group, transcript.messages)) })) + .children(transcript.changes.as_ref().map(|card| { + changes::render_changes(card, &item.id, transcript.chat, transcript.messages) + })) } } @@ -1726,20 +1738,25 @@ pub(super) fn diff_lines_for(item: &AgentTimelineItem) -> Vec<(char, SharedStrin lines } -/// The +/- lines of an edit or write, coloured. +/// The +/- lines of an edit or write, coloured, in the box a step's +/// details show them in. fn render_diff(lines: &[(char, SharedString)]) -> Option
{ if lines.is_empty() { return None; } - let mut diff = div() - .flex() - .flex_col() - .mt_1() - .rounded(theme::RADIUS_SM) - .bg(gpui::rgb(theme::bg_code_block())) - .border_1() - .border_color(gpui::rgb(theme::border_subtle())) - .overflow_x_hidden(); + Some( + diff_rows(lines) + .mt_1() + .rounded(theme::RADIUS_SM) + .bg(gpui::rgb(theme::bg_code_block())) + .border_1() + .border_color(gpui::rgb(theme::border_subtle())), + ) +} + +/// One row per +/- line, each clipped to the width it has. +pub(super) fn diff_rows(lines: &[(char, SharedString)]) -> Div { + let mut diff = div().flex().flex_col().overflow_x_hidden(); for (sign, line) in lines { let color = match sign { '+' => theme::status_success(), @@ -1770,7 +1787,7 @@ fn render_diff(lines: &[(char, SharedString)]) -> Option
{ ), ); } - Some(diff) + diff } /// Title shown before the model summary arrives: just the tool label from @@ -3656,4 +3673,72 @@ mod motion_tests { ); }); } + + #[gpui::test] + fn a_finished_turn_lists_what_it_changed_under_its_reply(cx: &mut TestAppContext) { + use maple_agent::agent::{AgentRunEvent, AgentRunTerminal}; + + let _clock = Clock::start(); + let (chat, cx) = pane(cx, 0); + chat.update(cx, |chat, cx| { + chat.replace_timeline(vec![user_item("ask", "Cap the wait")]); + chat.handle_service_event(run_event(AgentRunEvent::Started), cx); + for row in [ + AgentTimelineItem { + status: Some("completed".to_string()), + input: Some(serde_json::json!({ + "path": "src/net/retry.rs", + "edits": [{ + "oldText": "loop {", + "newText": "let deadline = now + MAX;\nloop {", + }], + })), + ..item("edit", "tool", None) + }, + item("reply", "message", Some("Done.")), + ] { + chat.handle_service_event(run_event(AgentRunEvent::TimelineItem(row)), cx); + } + }); + redraw(&chat, cx); + assert!( + cx.debug_bounds("turn-changes").is_none(), + "a turn still running has not finished changing things" + ); + + chat.update(cx, |chat, cx| { + chat.handle_service_event( + run_event(AgentRunEvent::Finished(AgentRunTerminal::Completed)), + cx, + ); + }); + // Past the reply's entrance, so its row has its full height. + motion::set_test_now(Some(motion::now() + motion::ENTER.settle * 2)); + redraw(&chat, cx); + let card = bounds(cx, "turn-changes"); + let reply = bounds(cx, "timeline-row-reply"); + assert!( + card.top() > reply.top() && card.bottom() <= reply.bottom(), + "the card sits in the reply's row, which grew to hold it: {card:?} in {reply:?}" + ); + assert!( + cx.debug_bounds("turn-change-diff").is_none(), + "a file's changes start folded" + ); + + let file = bounds(cx, "turn-change").center(); + cx.simulate_click(file, gpui::Modifiers::default()); + redraw(&chat, cx); + let diff = bounds(cx, "turn-change-diff"); + let opened = bounds(cx, "turn-changes"); + let reply = bounds(cx, "timeline-row-reply"); + assert!( + opened.size.height > card.size.height + && diff.top() > opened.top() + && diff.bottom() <= opened.bottom() + && opened.bottom() <= reply.bottom(), + "the file's changes open inside the card, and the row grows again: \ + {diff:?} in {opened:?} in {reply:?}" + ); + } } diff --git a/apps/maple-agent/app/src/ui/chat/turn.rs b/apps/maple-agent/app/src/ui/chat/turn.rs index 3db37b867..f4bc3967b 100644 --- a/apps/maple-agent/app/src/ui/chat/turn.rs +++ b/apps/maple-agent/app/src/ui/chat/turn.rs @@ -6,6 +6,7 @@ //! steps stay folded under the line until it is clicked. Before a turn has //! a step, the same line waits in the slot under the transcript. +use std::collections::HashMap; use std::time::{Duration, Instant}; use gpui::{Context, Div, SharedString, div, prelude::*, px}; @@ -49,6 +50,11 @@ pub(super) struct Turns { /// The lead of the turn at the end of the loaded rows, once that turn /// has a step. tail: Option, + /// The reply that ends a turn, by its row, with the turn's lead: the + /// last message from Maple after the turn's last step. + replies: HashMap, + /// The reply of the turn at the end, while no step has followed it. + tail_reply: Option, } fn starts_turn(item: &AgentTimelineItem) -> bool { @@ -80,6 +86,8 @@ impl Turns { pub(super) fn rebuild(&mut self, timeline: &[AgentTimelineItem]) { self.rows.clear(); self.tail = None; + self.replies.clear(); + self.tail_reply = None; for item in timeline { self.push(item); } @@ -87,15 +95,30 @@ impl Turns { /// One row arrived at the end. pub(super) fn push(&mut self, item: &AgentTimelineItem) { + let index = self.rows.len(); let row = if starts_turn(item) { self.tail = None; + self.tail_reply = None; TurnRow::Plain } else if !is_step(item) { + // What Maple says after the turn's steps is its reply, until + // it says more or takes another step. + if let Some(lead) = self.tail + && item.item_type == "message" + { + if let Some(earlier) = self.tail_reply.replace(index) { + self.replies.remove(&earlier); + } + self.replies.insert(index, lead); + } TurnRow::Plain } else if let Some(lead) = self.tail { + if let Some(reply) = self.tail_reply.take() { + self.replies.remove(&reply); + } TurnRow::Step(lead) } else { - self.tail = Some(self.rows.len()); + self.tail = Some(index); TurnRow::Lead }; self.rows.push(row); @@ -109,6 +132,16 @@ impl Turns { self.tail } + /// The lead of the turn the row at `row` is the reply of. + pub(super) fn reply_of(&self, row: usize) -> Option { + self.replies.get(&row).copied() + } + + /// The reply of the turn at the end, if it has one yet. + pub(super) fn tail_reply(&self) -> Option { + self.tail_reply + } + /// One past the last step of the turn led by the row at `lead`. pub(super) fn end(&self, lead: usize) -> usize { let mut end = lead + 1; @@ -413,7 +446,7 @@ impl ChatScreen { /// Whether the run on screen is working on the turn led by the row at /// `lead`. Until a new run's first row lands the last turn is an /// earlier one, and the line waits in the slot instead. - fn turn_runs(&self, lead: usize) -> bool { + pub(super) fn turn_runs(&self, lead: usize) -> bool { self.turns.tail() == Some(lead) && self.is_run_active() && !self.awaiting_first_token } @@ -520,6 +553,16 @@ impl ChatScreen { /// the turn on screen keeps how long it worked. A run that waits on /// the user holds its time. pub(super) fn sync_run_clocks(&mut self) { + // Once its turn is over, a reply may carry the card of what the + // turn changed: that row is taller from then on, so measure it + // again the first time it is seen settled. + let running = self.turns.tail().is_some_and(|lead| self.turn_runs(lead)); + let settled = self.turns.tail_reply().filter(|_| !running); + if std::mem::replace(&mut self.settled_reply, settled) != settled + && let Some(reply) = settled + { + self.list_state.remeasure_items(reply..reply + 1); + } if self.run_clocks.is_empty() { return; } @@ -565,6 +608,10 @@ impl ChatScreen { if let Some(allowed) = self.step_permissions.remove(old) { self.step_permissions.insert(new.to_owned(), allowed); } + self.turn_changes.forget(old); + if let Some(open) = self.open_changes.remove(old) { + self.open_changes.insert(new.to_owned(), open); + } } /// The permission request on screen, if the question card shows one. @@ -713,6 +760,7 @@ mod tests { } assert_eq!(live.rows, snapshot.rows); assert_eq!(live.tail, snapshot.tail); + assert_eq!(live.replies, snapshot.replies); // A window that starts mid-turn leads with its first loaded step. assert_eq!(snapshot.row(0), TurnRow::Lead); // A new message from the user starts a turn with no line yet. @@ -720,6 +768,31 @@ mod tests { assert_eq!(live.tail(), None); } + #[test] + fn a_turns_reply_is_what_maple_says_after_its_last_step() { + let mut turns = Turns::default(); + turns.rebuild(&[ + item("message", Some("user")), + // Said before any step: not a reply to carry what changed. + item("message", Some("assistant")), + item("tool", None), + item("message", Some("assistant")), + ]); + assert_eq!(turns.reply_of(1), None); + assert_eq!(turns.reply_of(3), Some(2)); + // Another step: the turn is not over, and what was said is no + // longer its last word. + turns.push(&item("tool", None)); + assert_eq!(turns.reply_of(3), None); + turns.push(&item("message", Some("assistant"))); + assert_eq!(turns.reply_of(5), Some(2)); + assert_eq!(turns.tail_reply(), Some(5)); + // A plain turn has no steps, so nothing to list. + turns.push(&item("message", Some("user"))); + turns.push(&item("message", Some("assistant"))); + assert_eq!(turns.reply_of(7), None); + } + #[test] fn the_clock_stops_while_a_question_waits() { let began = Instant::now(); diff --git a/apps/maple-agent/app/translation-revisions/de.txt b/apps/maple-agent/app/translation-revisions/de.txt index 6ca13e166..978ea452f 100644 --- a/apps/maple-agent/app/translation-revisions/de.txt +++ b/apps/maple-agent/app/translation-revisions/de.txt @@ -397,6 +397,7 @@ chat-trust-description 1e3842500e30ef07 chat-trust-keep d6e02ad22aeeeba6 chat-trust-saved 92a4e34ef0b734ae chat-trust-title 4a6389783686eec6 +chat-turn-files-changed 708debceb2de53ea chat-turn-waiting 130514d0bacc91a7 chat-turn-worked 5dcfb6d76e519bf9 chat-turn-worked-minutes 9b13b6f2c1f967be diff --git a/apps/maple-agent/app/translation-revisions/es.txt b/apps/maple-agent/app/translation-revisions/es.txt index 6ca13e166..978ea452f 100644 --- a/apps/maple-agent/app/translation-revisions/es.txt +++ b/apps/maple-agent/app/translation-revisions/es.txt @@ -397,6 +397,7 @@ chat-trust-description 1e3842500e30ef07 chat-trust-keep d6e02ad22aeeeba6 chat-trust-saved 92a4e34ef0b734ae chat-trust-title 4a6389783686eec6 +chat-turn-files-changed 708debceb2de53ea chat-turn-waiting 130514d0bacc91a7 chat-turn-worked 5dcfb6d76e519bf9 chat-turn-worked-minutes 9b13b6f2c1f967be From f1acc8306cb2e5d283271f3efa19beeaabef57c1 Mon Sep 17 00:00:00 2001 From: Matthew Ball <13823657+matthewrball@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:53:08 -0800 Subject: [PATCH 2/4] Agent: Count a turn's changed lines from the whole edits The card added up the lines kept for display, which stop at a cap, so a large write was undercounted. It now counts from each call's whole input, as the steps do. --- apps/maple-agent/app/src/ui/chat/changes.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/apps/maple-agent/app/src/ui/chat/changes.rs b/apps/maple-agent/app/src/ui/chat/changes.rs index e19d83007..cf5bea8a7 100644 --- a/apps/maple-agent/app/src/ui/chat/changes.rs +++ b/apps/maple-agent/app/src/ui/chat/changes.rs @@ -103,15 +103,17 @@ impl TurnChanges { sums.len() - 1 }); let sum = &mut sums[index]; - for (sign, line) in lines.iter() { - match sign { - '+' => sum.added += 1, - '-' => sum.removed += 1, - // The line that names the file, which the row does. - _ => continue, - } - sum.lines.push((*sign, line.clone())); - } + // Counted from the whole edit; the lines shown stop at a cap. + let (added, removed) = super::transcript::diff_counts(item); + sum.added += added; + sum.removed += removed; + // All but the line that names the file, which the row does. + sum.lines.extend( + lines + .iter() + .filter(|(sign, _)| matches!(sign, '+' | '-')) + .cloned(), + ); } if sums.is_empty() { return None; From 81a3e1d07bbff923da7d835ef7ec2d79ffd648d6 Mon Sep 17 00:00:00 2001 From: Matthew Ball <13823657+matthewrball@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:12:50 -0800 Subject: [PATCH 3/4] Agent: Keep the changes card true when its rows change under it Loading a paged row's whole input left the card with the counts of the cut one, and a queued message ended a turn without the reply's row being measured for its new card. Both now drop the cached card and measure the row again. The card also asks only its edit and write steps for their derived rows, so a turn of commands derives nothing to learn it changed no file. --- apps/maple-agent/app/src/ui/chat/changes.rs | 8 +++++++- apps/maple-agent/app/src/ui/chat/mod.rs | 6 ++++++ apps/maple-agent/app/src/ui/chat/paging.rs | 6 +++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/apps/maple-agent/app/src/ui/chat/changes.rs b/apps/maple-agent/app/src/ui/chat/changes.rs index cf5bea8a7..ce9b3b1f0 100644 --- a/apps/maple-agent/app/src/ui/chat/changes.rs +++ b/apps/maple-agent/app/src/ui/chat/changes.rs @@ -302,7 +302,13 @@ impl ChatScreen { self.timeline[lead..reply] .iter() .enumerate() - .filter(|(offset, _)| self.turns.row(lead + offset) != TurnRow::Plain) + // Only the calls that changed a file have their + // rows derived: a turn of commands derives none. + .filter(|(offset, item)| { + self.turns.row(lead + offset) != TurnRow::Plain + && StepState::of(item.status.as_deref()) == StepState::Done + && changed_path(item).is_some() + }) .map(|(_, item)| { let revision = self .timeline_index diff --git a/apps/maple-agent/app/src/ui/chat/mod.rs b/apps/maple-agent/app/src/ui/chat/mod.rs index 0c96ca665..5434020b2 100644 --- a/apps/maple-agent/app/src/ui/chat/mod.rs +++ b/apps/maple-agent/app/src/ui/chat/mod.rs @@ -6377,9 +6377,15 @@ impl ChatScreen { motion::now(), self.list_state.is_following_tail(), ); + // A message from the user ends the turn before it, whose + // reply may now carry the card of what the turn changed. + let settled = self.turns.tail_reply().filter(|_| turn::starts_turn(&item)); self.turns.push(&item); self.timeline.push(item); self.list_state.splice(index..index, 1); + if let Some(reply) = settled { + self.list_state.remeasure_items(reply..reply + 1); + } // The step before a new one loses the gap under it, so // the rail joins them: its height changed too. if index > 0 diff --git a/apps/maple-agent/app/src/ui/chat/paging.rs b/apps/maple-agent/app/src/ui/chat/paging.rs index d4a03c391..1859fddd2 100644 --- a/apps/maple-agent/app/src/ui/chat/paging.rs +++ b/apps/maple-agent/app/src/ui/chat/paging.rs @@ -439,7 +439,11 @@ impl ChatScreen { this.markdown_cache.clear(); this.derived.clear(); this.messages = Default::default(); - this.remeasure_item(index); + // The whole input may change what its turn's + // card lists, and so the height of the reply + // that carries it. + this.turn_changes.clear(); + this.list_state.remeasure_items(index..this.timeline.len()); this.warm_markdown(cx); } } From 60dd35b38fc69c9b2a6ca6ae0eb694d424601127 Mon Sep 17 00:00:00 2001 From: Matthew Ball <13823657+matthewrball@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:19:27 -0800 Subject: [PATCH 4/4] Agent: keep the files-changed card under a real reply, and open its files from the keyboard An empty streamed message took the reply's place and drew nothing, so the card vanished. A file row had no tab stop, key handler or focus ring, and its name was not in the accessibility tree. --- apps/maple-agent/app/src/ui/chat/changes.rs | 8 +++++-- apps/maple-agent/app/src/ui/chat/turn.rs | 26 +++++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/apps/maple-agent/app/src/ui/chat/changes.rs b/apps/maple-agent/app/src/ui/chat/changes.rs index ce9b3b1f0..2821ca716 100644 --- a/apps/maple-agent/app/src/ui/chat/changes.rs +++ b/apps/maple-agent/app/src/ui/chat/changes.rs @@ -20,6 +20,7 @@ use super::turn::TurnRow; use crate::ui::i18n::MessageCache; use crate::ui::motion::{self, HoverBg as _}; use crate::ui::theme; +use crate::ui::widgets; /// The +/- lines of an edit or a write, as its row derives them. type DiffLines = Vec<(char, SharedString)>; @@ -235,6 +236,9 @@ pub(super) fn render_changes( .role(gpui::Role::DisclosureTriangle) .aria_label(file.path.clone()) .aria_expanded(shown) + .tab_index(0) + .on_key_down(ChatScreen::sidebar_button_key_down) + .focus_visible(widgets::focus_ring) .h(ROW) .px_3() .flex() @@ -255,7 +259,7 @@ pub(super) fn render_changes( div() .flex_none() .text_color(gpui::rgb(theme::text_primary())) - .child(file.name.clone()), + .child(gpui::text!(file.name.clone())), ) .child( div() @@ -263,7 +267,7 @@ pub(super) fn render_changes( .min_w_0() .truncate() .text_color(gpui::rgb(theme::text_muted())) - .child(file.folder.clone()), + .child(gpui::text!(file.folder.clone())), ) .child(step::changed(&file.counts)) .hover_bg(theme::overlay_hover()) diff --git a/apps/maple-agent/app/src/ui/chat/turn.rs b/apps/maple-agent/app/src/ui/chat/turn.rs index b9f0b911c..59b80f661 100644 --- a/apps/maple-agent/app/src/ui/chat/turn.rs +++ b/apps/maple-agent/app/src/ui/chat/turn.rs @@ -200,8 +200,10 @@ impl Turns { self.chunk = None; } // What Maple says after the turn's steps is its reply, until - // it says more or takes another step. + // it says more or takes another step. A message with nothing + // in it yet draws no row, so it cannot carry the card. if let Some(lead) = self.tail + && breaks_chunk && item.item_type == "message" { if let Some(earlier) = self.tail_reply.replace(index) { @@ -1110,13 +1112,17 @@ mod tests { #[test] fn a_turns_reply_is_what_maple_says_after_its_last_step() { + let said = || AgentTimelineItem { + text: Some("Done.".into()), + ..item("message", Some("assistant")) + }; let mut turns = Turns::default(); turns.rebuild(&[ item("message", Some("user")), // Said before any step: not a reply to carry what changed. - item("message", Some("assistant")), + said(), item("tool", None), - item("message", Some("assistant")), + said(), ]); assert_eq!(turns.reply_of(1), None); assert_eq!(turns.reply_of(3), Some(2)); @@ -1124,13 +1130,23 @@ mod tests { // longer its last word. turns.push(&item("tool", None)); assert_eq!(turns.reply_of(3), None); - turns.push(&item("message", Some("assistant"))); + turns.push(&said()); assert_eq!(turns.reply_of(5), Some(2)); assert_eq!(turns.tail_reply(), Some(5)); // A plain turn has no steps, so nothing to list. turns.push(&item("message", Some("user"))); - turns.push(&item("message", Some("assistant"))); + turns.push(&said()); assert_eq!(turns.reply_of(7), None); + // A message that is still empty draws nothing, so the reply before + // it keeps the card. + let mut turns = Turns::default(); + turns.rebuild(&[ + item("tool", None), + said(), + item("message", Some("assistant")), + ]); + assert_eq!(turns.reply_of(1), Some(0)); + assert_eq!(turns.tail_reply(), Some(1)); } #[test]