Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

agent_ui: Keep pending subagent edits when regenerating a prompt (#59060)

MartinYe1234c78bd36fd8bf
diff --git a/crates/agent_ui/src/conversation_view.rs b/crates/agent_ui/src/conversation_view.rs
index 95f8b7d8097..bc748f944d1 100644
--- a/crates/agent_ui/src/conversation_view.rs
+++ b/crates/agent_ui/src/conversation_view.rs
@@ -5829,6 +5829,178 @@ pub(crate) mod tests {
});
}
+ #[gpui::test]
+ async fn test_regenerate_keeps_pending_subagent_edits(cx: &mut TestAppContext) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ "/project",
+ json!({
+ "file.txt": "original content"
+ }),
+ )
+ .await;
+ let project = Project::test(fs, [Path::new("/project")], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
+
+ let thread_store = cx.update(|_window, cx| cx.new(|cx| ThreadStore::new(cx)));
+ let connection_store =
+ cx.update(|_window, cx| cx.new(|cx| AgentConnectionStore::new(project.clone(), cx)));
+
+ let connection = Rc::new(StubAgentConnection::new());
+ let conversation_view = cx.update(|window, cx| {
+ cx.new(|cx| {
+ ConversationView::new(
+ Rc::new(StubAgentServer::new(connection.as_ref().clone())),
+ connection_store,
+ Agent::Custom { id: "Test".into() },
+ None,
+ None,
+ None,
+ None,
+ None,
+ workspace.downgrade(),
+ project.clone(),
+ Some(thread_store.clone()),
+ AgentThreadSource::AgentPanel,
+ window,
+ cx,
+ )
+ })
+ });
+
+ cx.run_until_parked();
+
+ let thread = conversation_view
+ .read_with(cx, |view, cx| {
+ view.active_thread().map(|r| r.read(cx).thread.clone())
+ })
+ .unwrap();
+
+ // First turn: a subagent tool call. Subagent edits never appear as
+ // diffs in the parent thread's entries; they are only forwarded to the
+ // parent's action log through the linked-log mechanism.
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::ToolCall(
+ acp::ToolCall::new("spawn1", "Subagent task")
+ .kind(acp::ToolKind::Other)
+ .status(acp::ToolCallStatus::Completed)
+ .meta(acp_thread::meta_with_tool_name("spawn_agent")),
+ )]);
+
+ thread
+ .update(cx, |thread, cx| thread.send_raw("Use a subagent", cx))
+ .await
+ .unwrap();
+ cx.run_until_parked();
+
+ // Simulate the subagent editing a file: edits performed through a
+ // child action log are forwarded to the parent thread's action log,
+ // just like `Thread::new_subagent` wires it up.
+ let parent_action_log = thread.read_with(cx, |thread, _| thread.action_log().clone());
+ let subagent_action_log = cx.update(|_, cx| {
+ cx.new(|_| {
+ ActionLog::new(project.clone()).with_linked_action_log(parent_action_log.clone())
+ })
+ });
+
+ let buffer = project
+ .update(cx, |project, cx| {
+ let path = project.find_project_path("file.txt", cx).unwrap();
+ project.open_buffer(path, cx)
+ })
+ .await
+ .unwrap();
+ cx.update(|_, cx| {
+ subagent_action_log.update(cx, |log, cx| log.buffer_read(buffer.clone(), cx));
+ buffer.update(cx, |buffer, cx| {
+ buffer.set_text("edited by subagent", cx);
+ });
+ subagent_action_log.update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));
+ });
+ cx.run_until_parked();
+
+ parent_action_log.read_with(cx, |log, cx| {
+ assert_eq!(
+ log.changed_buffers(cx).count(),
+ 1,
+ "the subagent edit should be pending review in the parent's action log"
+ );
+ });
+
+ // Second turn: a plain follow-up.
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Response".into()),
+ )]);
+ thread
+ .update(cx, |thread, cx| thread.send_raw("Follow-up", cx))
+ .await
+ .unwrap();
+ cx.run_until_parked();
+
+ let follow_up_ix = thread.read_with(cx, |thread, cx| {
+ thread
+ .entries()
+ .iter()
+ .position(|entry| entry.to_markdown(cx) == "## User\n\nFollow-up\n\n")
+ .unwrap()
+ });
+
+ // Edit and regenerate the follow-up message.
+ let user_message_editor = conversation_view.read_with(cx, |view, cx| {
+ view.active_thread()
+ .unwrap()
+ .read(cx)
+ .entry_view_state
+ .read(cx)
+ .entry(follow_up_ix)
+ .unwrap()
+ .message_editor()
+ .unwrap()
+ .clone()
+ });
+ user_message_editor.update_in(cx, |editor, window, cx| {
+ editor.set_text("Edited follow-up", window, cx);
+ });
+
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("New response".into()),
+ )]);
+ active_thread(&conversation_view, cx).update_in(cx, |view, window, cx| {
+ view.regenerate(follow_up_ix, user_message_editor.clone(), window, cx);
+ });
+ cx.run_until_parked();
+
+ // The thread should have been rewound and the edited message resent.
+ thread.read_with(cx, |thread, cx| {
+ let entries = thread.entries();
+ assert_eq!(entries.len(), 4);
+ assert_eq!(
+ entries[2].to_markdown(cx),
+ "## User\n\nEdited follow-up\n\n"
+ );
+ });
+
+ // The subagent's edits predate the regenerated prompt, so they must be
+ // auto-kept rather than rejected by the rewind.
+ buffer.read_with(cx, |buffer, _| {
+ assert_eq!(
+ buffer.text(),
+ "edited by subagent",
+ "pending subagent edits should be kept when regenerating a later prompt"
+ );
+ });
+ parent_action_log.read_with(cx, |log, cx| {
+ assert_eq!(
+ log.changed_buffers(cx).count(),
+ 0,
+ "the subagent edit should have been auto-kept"
+ );
+ });
+ }
+
#[gpui::test]
async fn test_scroll_to_most_recent_user_prompt(cx: &mut TestAppContext) {
init_test(cx);
diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rs
index e92cb7dd051..6180ad43f7d 100644
--- a/crates/agent_ui/src/conversation_view/thread_view.rs
+++ b/crates/agent_ui/src/conversation_view/thread_view.rs
@@ -1942,12 +1942,19 @@ impl ThreadView {
//
// If editing the prompt that generated the edits, they are auto-rejected
// through the `rewind` function in the `acp_thread`.
+ //
+ // Subagent edits never show up as diffs in the parent thread's entries (they
+ // are only forwarded to the parent's action log), so treat any earlier
+ // subagent tool call as potentially having edits. Keeping all edits is a
+ // no-op when the subagent didn't make any.
let has_earlier_edits = thread.read_with(cx, |thread, _| {
- thread
- .entries()
- .iter()
- .take(entry_ix)
- .any(|entry| entry.diffs().next().is_some())
+ thread.entries().iter().take(entry_ix).any(|entry| {
+ entry.diffs().next().is_some()
+ || matches!(
+ entry,
+ AgentThreadEntry::ToolCall(tool_call) if tool_call.is_subagent()
+ )
+ })
});
if has_earlier_edits {