edit_agent.rs

   1mod create_file_parser;
   2mod edit_parser;
   3#[cfg(test)]
   4mod evals;
   5mod streaming_fuzzy_matcher;
   6
   7use crate::{Template, Templates};
   8use action_log::ActionLog;
   9use anyhow::Result;
  10use cloud_llm_client::CompletionIntent;
  11use create_file_parser::{CreateFileParser, CreateFileParserEvent};
  12pub use edit_parser::EditFormat;
  13use edit_parser::{EditParser, EditParserEvent, EditParserMetrics};
  14use futures::{
  15    Stream, StreamExt,
  16    channel::mpsc::{self, UnboundedReceiver},
  17    pin_mut,
  18    stream::BoxStream,
  19};
  20use gpui::{AppContext, AsyncApp, Entity, Task};
  21use language::{Anchor, Buffer, BufferSnapshot, LineIndent, Point, TextBufferSnapshot};
  22use language_model::{
  23    LanguageModel, LanguageModelCompletionError, LanguageModelRequest, LanguageModelRequestMessage,
  24    LanguageModelToolChoice, MessageContent, Role,
  25};
  26use project::{AgentLocation, Project};
  27use schemars::JsonSchema;
  28use serde::{Deserialize, Serialize};
  29use std::{cmp, iter, mem, ops::Range, pin::Pin, sync::Arc, task::Poll};
  30use streaming_diff::{CharOperation, StreamingDiff};
  31use streaming_fuzzy_matcher::StreamingFuzzyMatcher;
  32
  33#[derive(Serialize)]
  34struct CreateFilePromptTemplate {
  35    path: Option<String>,
  36    edit_description: String,
  37}
  38
  39impl Template for CreateFilePromptTemplate {
  40    const TEMPLATE_NAME: &'static str = "create_file_prompt.hbs";
  41}
  42
  43#[derive(Serialize)]
  44struct EditFileXmlPromptTemplate {
  45    path: Option<String>,
  46    edit_description: String,
  47}
  48
  49impl Template for EditFileXmlPromptTemplate {
  50    const TEMPLATE_NAME: &'static str = "edit_file_prompt_xml.hbs";
  51}
  52
  53#[derive(Serialize)]
  54struct EditFileDiffFencedPromptTemplate {
  55    path: Option<String>,
  56    edit_description: String,
  57}
  58
  59impl Template for EditFileDiffFencedPromptTemplate {
  60    const TEMPLATE_NAME: &'static str = "edit_file_prompt_diff_fenced.hbs";
  61}
  62
  63#[derive(Clone, Debug, PartialEq, Eq)]
  64pub enum EditAgentOutputEvent {
  65    ResolvingEditRange(Range<Anchor>),
  66    UnresolvedEditRange,
  67    AmbiguousEditRange(Vec<Range<usize>>),
  68    Edited(Range<Anchor>),
  69}
  70
  71#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
  72pub struct EditAgentOutput {
  73    pub raw_edits: String,
  74    pub parser_metrics: EditParserMetrics,
  75}
  76
  77#[derive(Clone)]
  78pub struct EditAgent {
  79    model: Arc<dyn LanguageModel>,
  80    action_log: Entity<ActionLog>,
  81    project: Entity<Project>,
  82    templates: Arc<Templates>,
  83    edit_format: EditFormat,
  84}
  85
  86impl EditAgent {
  87    pub fn new(
  88        model: Arc<dyn LanguageModel>,
  89        project: Entity<Project>,
  90        action_log: Entity<ActionLog>,
  91        templates: Arc<Templates>,
  92        edit_format: EditFormat,
  93    ) -> Self {
  94        EditAgent {
  95            model,
  96            project,
  97            action_log,
  98            templates,
  99            edit_format,
 100        }
 101    }
 102
 103    pub fn overwrite(
 104        &self,
 105        buffer: Entity<Buffer>,
 106        edit_description: String,
 107        conversation: &LanguageModelRequest,
 108        cx: &mut AsyncApp,
 109    ) -> (
 110        Task<Result<EditAgentOutput>>,
 111        mpsc::UnboundedReceiver<EditAgentOutputEvent>,
 112    ) {
 113        let this = self.clone();
 114        let (events_tx, events_rx) = mpsc::unbounded();
 115        let conversation = conversation.clone();
 116        let output = cx.spawn(async move |cx| {
 117            let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot())?;
 118            let path = cx.update(|cx| snapshot.resolve_file_path(true, cx))?;
 119            let prompt = CreateFilePromptTemplate {
 120                path,
 121                edit_description,
 122            }
 123            .render(&this.templates)?;
 124            let new_chunks = this
 125                .request(conversation, CompletionIntent::CreateFile, prompt, cx)
 126                .await?;
 127
 128            let (output, mut inner_events) = this.overwrite_with_chunks(buffer, new_chunks, cx);
 129            while let Some(event) = inner_events.next().await {
 130                events_tx.unbounded_send(event).ok();
 131            }
 132            output.await
 133        });
 134        (output, events_rx)
 135    }
 136
 137    fn overwrite_with_chunks(
 138        &self,
 139        buffer: Entity<Buffer>,
 140        edit_chunks: impl 'static + Send + Stream<Item = Result<String, LanguageModelCompletionError>>,
 141        cx: &mut AsyncApp,
 142    ) -> (
 143        Task<Result<EditAgentOutput>>,
 144        mpsc::UnboundedReceiver<EditAgentOutputEvent>,
 145    ) {
 146        let (output_events_tx, output_events_rx) = mpsc::unbounded();
 147        let (parse_task, parse_rx) = Self::parse_create_file_chunks(edit_chunks, cx);
 148        let this = self.clone();
 149        let task = cx.spawn(async move |cx| {
 150            this.action_log
 151                .update(cx, |log, cx| log.buffer_created(buffer.clone(), cx))?;
 152            this.overwrite_with_chunks_internal(buffer, parse_rx, output_events_tx, cx)
 153                .await?;
 154            parse_task.await
 155        });
 156        (task, output_events_rx)
 157    }
 158
 159    async fn overwrite_with_chunks_internal(
 160        &self,
 161        buffer: Entity<Buffer>,
 162        mut parse_rx: UnboundedReceiver<Result<CreateFileParserEvent>>,
 163        output_events_tx: mpsc::UnboundedSender<EditAgentOutputEvent>,
 164        cx: &mut AsyncApp,
 165    ) -> Result<()> {
 166        cx.update(|cx| {
 167            buffer.update(cx, |buffer, cx| buffer.set_text("", cx));
 168            self.action_log.update(cx, |log, cx| {
 169                log.buffer_edited(buffer.clone(), cx);
 170            });
 171            self.project.update(cx, |project, cx| {
 172                project.set_agent_location(
 173                    Some(AgentLocation {
 174                        buffer: buffer.downgrade(),
 175                        position: language::Anchor::MAX,
 176                    }),
 177                    cx,
 178                )
 179            });
 180            output_events_tx
 181                .unbounded_send(EditAgentOutputEvent::Edited(
 182                    language::Anchor::MIN..language::Anchor::MAX,
 183                ))
 184                .ok();
 185        })?;
 186
 187        while let Some(event) = parse_rx.next().await {
 188            match event? {
 189                CreateFileParserEvent::NewTextChunk { chunk } => {
 190                    cx.update(|cx| {
 191                        buffer.update(cx, |buffer, cx| buffer.append(chunk, cx));
 192                        self.action_log
 193                            .update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));
 194                        self.project.update(cx, |project, cx| {
 195                            project.set_agent_location(
 196                                Some(AgentLocation {
 197                                    buffer: buffer.downgrade(),
 198                                    position: language::Anchor::MAX,
 199                                }),
 200                                cx,
 201                            )
 202                        });
 203                    })?;
 204                    output_events_tx
 205                        .unbounded_send(EditAgentOutputEvent::Edited(
 206                            language::Anchor::MIN..language::Anchor::MAX,
 207                        ))
 208                        .ok();
 209                }
 210            }
 211        }
 212
 213        Ok(())
 214    }
 215
 216    pub fn edit(
 217        &self,
 218        buffer: Entity<Buffer>,
 219        edit_description: String,
 220        conversation: &LanguageModelRequest,
 221        cx: &mut AsyncApp,
 222    ) -> (
 223        Task<Result<EditAgentOutput>>,
 224        mpsc::UnboundedReceiver<EditAgentOutputEvent>,
 225    ) {
 226        let this = self.clone();
 227        let (events_tx, events_rx) = mpsc::unbounded();
 228        let conversation = conversation.clone();
 229        let edit_format = self.edit_format;
 230        let output = cx.spawn(async move |cx| {
 231            let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot())?;
 232            let path = cx.update(|cx| snapshot.resolve_file_path(true, cx))?;
 233            let prompt = match edit_format {
 234                EditFormat::XmlTags => EditFileXmlPromptTemplate {
 235                    path,
 236                    edit_description,
 237                }
 238                .render(&this.templates)?,
 239                EditFormat::DiffFenced => EditFileDiffFencedPromptTemplate {
 240                    path,
 241                    edit_description,
 242                }
 243                .render(&this.templates)?,
 244            };
 245
 246            let edit_chunks = this
 247                .request(conversation, CompletionIntent::EditFile, prompt, cx)
 248                .await?;
 249            this.apply_edit_chunks(buffer, edit_chunks, events_tx, cx)
 250                .await
 251        });
 252        (output, events_rx)
 253    }
 254
 255    async fn apply_edit_chunks(
 256        &self,
 257        buffer: Entity<Buffer>,
 258        edit_chunks: impl 'static + Send + Stream<Item = Result<String, LanguageModelCompletionError>>,
 259        output_events: mpsc::UnboundedSender<EditAgentOutputEvent>,
 260        cx: &mut AsyncApp,
 261    ) -> Result<EditAgentOutput> {
 262        self.action_log
 263            .update(cx, |log, cx| log.buffer_read(buffer.clone(), cx))?;
 264
 265        let (output, edit_events) = Self::parse_edit_chunks(edit_chunks, self.edit_format, cx);
 266        let mut edit_events = edit_events.peekable();
 267        while let Some(edit_event) = Pin::new(&mut edit_events).peek().await {
 268            // Skip events until we're at the start of a new edit.
 269            let Ok(EditParserEvent::OldTextChunk { .. }) = edit_event else {
 270                edit_events.next().await.unwrap()?;
 271                continue;
 272            };
 273
 274            let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot())?;
 275
 276            // Resolve the old text in the background, updating the agent
 277            // location as we keep refining which range it corresponds to.
 278            let (resolve_old_text, mut old_range) =
 279                Self::resolve_old_text(snapshot.text.clone(), edit_events, cx);
 280            while let Ok(old_range) = old_range.recv().await {
 281                if let Some(old_range) = old_range {
 282                    let old_range = snapshot.anchor_before(old_range.start)
 283                        ..snapshot.anchor_before(old_range.end);
 284                    self.project.update(cx, |project, cx| {
 285                        project.set_agent_location(
 286                            Some(AgentLocation {
 287                                buffer: buffer.downgrade(),
 288                                position: old_range.end,
 289                            }),
 290                            cx,
 291                        );
 292                    })?;
 293                    output_events
 294                        .unbounded_send(EditAgentOutputEvent::ResolvingEditRange(old_range))
 295                        .ok();
 296                }
 297            }
 298
 299            let (edit_events_, mut resolved_old_text) = resolve_old_text.await?;
 300            edit_events = edit_events_;
 301
 302            // If we can't resolve the old text, restart the loop waiting for a
 303            // new edit (or for the stream to end).
 304            let resolved_old_text = match resolved_old_text.len() {
 305                1 => resolved_old_text.pop().unwrap(),
 306                0 => {
 307                    output_events
 308                        .unbounded_send(EditAgentOutputEvent::UnresolvedEditRange)
 309                        .ok();
 310                    continue;
 311                }
 312                _ => {
 313                    let ranges = resolved_old_text
 314                        .into_iter()
 315                        .map(|text| {
 316                            let start_line =
 317                                (snapshot.offset_to_point(text.range.start).row + 1) as usize;
 318                            let end_line =
 319                                (snapshot.offset_to_point(text.range.end).row + 1) as usize;
 320                            start_line..end_line
 321                        })
 322                        .collect();
 323                    output_events
 324                        .unbounded_send(EditAgentOutputEvent::AmbiguousEditRange(ranges))
 325                        .ok();
 326                    continue;
 327                }
 328            };
 329
 330            // Compute edits in the background and apply them as they become
 331            // available.
 332            let (compute_edits, edits) =
 333                Self::compute_edits(snapshot, resolved_old_text, edit_events, cx);
 334            let mut edits = edits.ready_chunks(32);
 335            while let Some(edits) = edits.next().await {
 336                if edits.is_empty() {
 337                    continue;
 338                }
 339
 340                // Edit the buffer and report edits to the action log as part of the
 341                // same effect cycle, otherwise the edit will be reported as if the
 342                // user made it.
 343                let (min_edit_start, max_edit_end) = cx.update(|cx| {
 344                    let (min_edit_start, max_edit_end) = buffer.update(cx, |buffer, cx| {
 345                        buffer.edit(edits.iter().cloned(), None, cx);
 346                        let max_edit_end = buffer
 347                            .summaries_for_anchors::<Point, _>(
 348                                edits.iter().map(|(range, _)| &range.end),
 349                            )
 350                            .max()
 351                            .unwrap();
 352                        let min_edit_start = buffer
 353                            .summaries_for_anchors::<Point, _>(
 354                                edits.iter().map(|(range, _)| &range.start),
 355                            )
 356                            .min()
 357                            .unwrap();
 358                        (
 359                            buffer.anchor_after(min_edit_start),
 360                            buffer.anchor_before(max_edit_end),
 361                        )
 362                    });
 363                    self.action_log
 364                        .update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));
 365                    self.project.update(cx, |project, cx| {
 366                        project.set_agent_location(
 367                            Some(AgentLocation {
 368                                buffer: buffer.downgrade(),
 369                                position: max_edit_end,
 370                            }),
 371                            cx,
 372                        );
 373                    });
 374                    (min_edit_start, max_edit_end)
 375                })?;
 376                output_events
 377                    .unbounded_send(EditAgentOutputEvent::Edited(min_edit_start..max_edit_end))
 378                    .ok();
 379            }
 380
 381            edit_events = compute_edits.await?;
 382        }
 383
 384        output.await
 385    }
 386
 387    fn parse_edit_chunks(
 388        chunks: impl 'static + Send + Stream<Item = Result<String, LanguageModelCompletionError>>,
 389        edit_format: EditFormat,
 390        cx: &mut AsyncApp,
 391    ) -> (
 392        Task<Result<EditAgentOutput>>,
 393        UnboundedReceiver<Result<EditParserEvent>>,
 394    ) {
 395        let (tx, rx) = mpsc::unbounded();
 396        let output = cx.background_spawn(async move {
 397            pin_mut!(chunks);
 398
 399            let mut parser = EditParser::new(edit_format);
 400            let mut raw_edits = String::new();
 401            while let Some(chunk) = chunks.next().await {
 402                match chunk {
 403                    Ok(chunk) => {
 404                        raw_edits.push_str(&chunk);
 405                        for event in parser.push(&chunk) {
 406                            tx.unbounded_send(Ok(event))?;
 407                        }
 408                    }
 409                    Err(error) => {
 410                        tx.unbounded_send(Err(error.into()))?;
 411                    }
 412                }
 413            }
 414            Ok(EditAgentOutput {
 415                raw_edits,
 416                parser_metrics: parser.finish(),
 417            })
 418        });
 419        (output, rx)
 420    }
 421
 422    fn parse_create_file_chunks(
 423        chunks: impl 'static + Send + Stream<Item = Result<String, LanguageModelCompletionError>>,
 424        cx: &mut AsyncApp,
 425    ) -> (
 426        Task<Result<EditAgentOutput>>,
 427        UnboundedReceiver<Result<CreateFileParserEvent>>,
 428    ) {
 429        let (tx, rx) = mpsc::unbounded();
 430        let output = cx.background_spawn(async move {
 431            pin_mut!(chunks);
 432
 433            let mut parser = CreateFileParser::new();
 434            let mut raw_edits = String::new();
 435            while let Some(chunk) = chunks.next().await {
 436                match chunk {
 437                    Ok(chunk) => {
 438                        raw_edits.push_str(&chunk);
 439                        for event in parser.push(Some(&chunk)) {
 440                            tx.unbounded_send(Ok(event))?;
 441                        }
 442                    }
 443                    Err(error) => {
 444                        tx.unbounded_send(Err(error.into()))?;
 445                    }
 446                }
 447            }
 448            // Send final events with None to indicate completion
 449            for event in parser.push(None) {
 450                tx.unbounded_send(Ok(event))?;
 451            }
 452            Ok(EditAgentOutput {
 453                raw_edits,
 454                parser_metrics: EditParserMetrics::default(),
 455            })
 456        });
 457        (output, rx)
 458    }
 459
 460    fn resolve_old_text<T>(
 461        snapshot: TextBufferSnapshot,
 462        mut edit_events: T,
 463        cx: &mut AsyncApp,
 464    ) -> (
 465        Task<Result<(T, Vec<ResolvedOldText>)>>,
 466        watch::Receiver<Option<Range<usize>>>,
 467    )
 468    where
 469        T: 'static + Send + Unpin + Stream<Item = Result<EditParserEvent>>,
 470    {
 471        let (mut old_range_tx, old_range_rx) = watch::channel(None);
 472        let task = cx.background_spawn(async move {
 473            let mut matcher = StreamingFuzzyMatcher::new(snapshot);
 474            while let Some(edit_event) = edit_events.next().await {
 475                let EditParserEvent::OldTextChunk {
 476                    chunk,
 477                    done,
 478                    line_hint,
 479                } = edit_event?
 480                else {
 481                    break;
 482                };
 483
 484                old_range_tx.send(matcher.push(&chunk, line_hint))?;
 485                if done {
 486                    break;
 487                }
 488            }
 489
 490            let matches = matcher.finish();
 491            let best_match = matcher.select_best_match();
 492
 493            old_range_tx.send(best_match.clone())?;
 494
 495            let indent = LineIndent::from_iter(
 496                matcher
 497                    .query_lines()
 498                    .first()
 499                    .unwrap_or(&String::new())
 500                    .chars(),
 501            );
 502
 503            let resolved_old_texts = if let Some(best_match) = best_match {
 504                vec![ResolvedOldText {
 505                    range: best_match,
 506                    indent,
 507                }]
 508            } else {
 509                matches
 510                    .into_iter()
 511                    .map(|range| ResolvedOldText { range, indent })
 512                    .collect::<Vec<_>>()
 513            };
 514
 515            Ok((edit_events, resolved_old_texts))
 516        });
 517
 518        (task, old_range_rx)
 519    }
 520
 521    fn compute_edits<T>(
 522        snapshot: BufferSnapshot,
 523        resolved_old_text: ResolvedOldText,
 524        mut edit_events: T,
 525        cx: &mut AsyncApp,
 526    ) -> (
 527        Task<Result<T>>,
 528        UnboundedReceiver<(Range<Anchor>, Arc<str>)>,
 529    )
 530    where
 531        T: 'static + Send + Unpin + Stream<Item = Result<EditParserEvent>>,
 532    {
 533        let (edits_tx, edits_rx) = mpsc::unbounded();
 534        let compute_edits = cx.background_spawn(async move {
 535            let buffer_start_indent = snapshot
 536                .line_indent_for_row(snapshot.offset_to_point(resolved_old_text.range.start).row);
 537            let indent_delta = if buffer_start_indent.tabs > 0 {
 538                IndentDelta::Tabs(
 539                    buffer_start_indent.tabs as isize - resolved_old_text.indent.tabs as isize,
 540                )
 541            } else {
 542                IndentDelta::Spaces(
 543                    buffer_start_indent.spaces as isize - resolved_old_text.indent.spaces as isize,
 544                )
 545            };
 546
 547            let old_text = snapshot
 548                .text_for_range(resolved_old_text.range.clone())
 549                .collect::<String>();
 550            let mut diff = StreamingDiff::new(old_text);
 551            let mut edit_start = resolved_old_text.range.start;
 552            let mut new_text_chunks =
 553                Self::reindent_new_text_chunks(indent_delta, &mut edit_events);
 554            let mut done = false;
 555            while !done {
 556                let char_operations = if let Some(new_text_chunk) = new_text_chunks.next().await {
 557                    diff.push_new(&new_text_chunk?)
 558                } else {
 559                    done = true;
 560                    mem::take(&mut diff).finish()
 561                };
 562
 563                for op in char_operations {
 564                    match op {
 565                        CharOperation::Insert { text } => {
 566                            let edit_start = snapshot.anchor_after(edit_start);
 567                            edits_tx.unbounded_send((edit_start..edit_start, Arc::from(text)))?;
 568                        }
 569                        CharOperation::Delete { bytes } => {
 570                            let edit_end = edit_start + bytes;
 571                            let edit_range =
 572                                snapshot.anchor_after(edit_start)..snapshot.anchor_before(edit_end);
 573                            edit_start = edit_end;
 574                            edits_tx.unbounded_send((edit_range, Arc::from("")))?;
 575                        }
 576                        CharOperation::Keep { bytes } => edit_start += bytes,
 577                    }
 578                }
 579            }
 580
 581            drop(new_text_chunks);
 582            anyhow::Ok(edit_events)
 583        });
 584
 585        (compute_edits, edits_rx)
 586    }
 587
 588    fn reindent_new_text_chunks(
 589        delta: IndentDelta,
 590        mut stream: impl Unpin + Stream<Item = Result<EditParserEvent>>,
 591    ) -> impl Stream<Item = Result<String>> {
 592        let mut buffer = String::new();
 593        let mut in_leading_whitespace = true;
 594        let mut done = false;
 595        futures::stream::poll_fn(move |cx| {
 596            while !done {
 597                let (chunk, is_last_chunk) = match stream.poll_next_unpin(cx) {
 598                    Poll::Ready(Some(Ok(EditParserEvent::NewTextChunk { chunk, done }))) => {
 599                        (chunk, done)
 600                    }
 601                    Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(err))),
 602                    Poll::Pending => return Poll::Pending,
 603                    _ => return Poll::Ready(None),
 604                };
 605
 606                buffer.push_str(&chunk);
 607
 608                let mut indented_new_text = String::new();
 609                let mut start_ix = 0;
 610                let mut newlines = buffer.match_indices('\n').peekable();
 611                loop {
 612                    let (line_end, is_pending_line) = match newlines.next() {
 613                        Some((ix, _)) => (ix, false),
 614                        None => (buffer.len(), true),
 615                    };
 616                    let line = &buffer[start_ix..line_end];
 617
 618                    if in_leading_whitespace {
 619                        if let Some(non_whitespace_ix) = line.find(|c| delta.character() != c) {
 620                            // We found a non-whitespace character, adjust
 621                            // indentation based on the delta.
 622                            let new_indent_len =
 623                                cmp::max(0, non_whitespace_ix as isize + delta.len()) as usize;
 624                            indented_new_text
 625                                .extend(iter::repeat(delta.character()).take(new_indent_len));
 626                            indented_new_text.push_str(&line[non_whitespace_ix..]);
 627                            in_leading_whitespace = false;
 628                        } else if is_pending_line {
 629                            // We're still in leading whitespace and this line is incomplete.
 630                            // Stop processing until we receive more input.
 631                            break;
 632                        } else {
 633                            // This line is entirely whitespace. Push it without indentation.
 634                            indented_new_text.push_str(line);
 635                        }
 636                    } else {
 637                        indented_new_text.push_str(line);
 638                    }
 639
 640                    if is_pending_line {
 641                        start_ix = line_end;
 642                        break;
 643                    } else {
 644                        in_leading_whitespace = true;
 645                        indented_new_text.push('\n');
 646                        start_ix = line_end + 1;
 647                    }
 648                }
 649                buffer.replace_range(..start_ix, "");
 650
 651                // This was the last chunk, push all the buffered content as-is.
 652                if is_last_chunk {
 653                    indented_new_text.push_str(&buffer);
 654                    buffer.clear();
 655                    done = true;
 656                }
 657
 658                if !indented_new_text.is_empty() {
 659                    return Poll::Ready(Some(Ok(indented_new_text)));
 660                }
 661            }
 662
 663            Poll::Ready(None)
 664        })
 665    }
 666
 667    async fn request(
 668        &self,
 669        mut conversation: LanguageModelRequest,
 670        intent: CompletionIntent,
 671        prompt: String,
 672        cx: &mut AsyncApp,
 673    ) -> Result<BoxStream<'static, Result<String, LanguageModelCompletionError>>> {
 674        let mut messages_iter = conversation.messages.iter_mut();
 675        if let Some(last_message) = messages_iter.next_back()
 676            && last_message.role == Role::Assistant
 677        {
 678            let old_content_len = last_message.content.len();
 679            last_message
 680                .content
 681                .retain(|content| !matches!(content, MessageContent::ToolUse(_)));
 682            let new_content_len = last_message.content.len();
 683
 684            // We just removed pending tool uses from the content of the
 685            // last message, so it doesn't make sense to cache it anymore
 686            // (e.g., the message will look very different on the next
 687            // request). Thus, we move the flag to the message prior to it,
 688            // as it will still be a valid prefix of the conversation.
 689            if old_content_len != new_content_len
 690                && last_message.cache
 691                && let Some(prev_message) = messages_iter.next_back()
 692            {
 693                last_message.cache = false;
 694                prev_message.cache = true;
 695            }
 696
 697            if last_message.content.is_empty() {
 698                conversation.messages.pop();
 699            }
 700        }
 701
 702        conversation.messages.push(LanguageModelRequestMessage {
 703            role: Role::User,
 704            content: vec![MessageContent::Text(prompt)],
 705            cache: false,
 706        });
 707
 708        // Include tools in the request so that we can take advantage of
 709        // caching when ToolChoice::None is supported.
 710        let mut tool_choice = None;
 711        let mut tools = Vec::new();
 712        if !conversation.tools.is_empty()
 713            && self
 714                .model
 715                .supports_tool_choice(LanguageModelToolChoice::None)
 716        {
 717            tool_choice = Some(LanguageModelToolChoice::None);
 718            tools = conversation.tools.clone();
 719        }
 720
 721        let request = LanguageModelRequest {
 722            thread_id: conversation.thread_id,
 723            prompt_id: conversation.prompt_id,
 724            intent: Some(intent),
 725            mode: conversation.mode,
 726            messages: conversation.messages,
 727            tool_choice,
 728            tools,
 729            stop: Vec::new(),
 730            temperature: None,
 731            thinking_allowed: true,
 732        };
 733
 734        Ok(self.model.stream_completion_text(request, cx).await?.stream)
 735    }
 736}
 737
 738struct ResolvedOldText {
 739    range: Range<usize>,
 740    indent: LineIndent,
 741}
 742
 743#[derive(Copy, Clone, Debug)]
 744enum IndentDelta {
 745    Spaces(isize),
 746    Tabs(isize),
 747}
 748
 749impl IndentDelta {
 750    fn character(&self) -> char {
 751        match self {
 752            IndentDelta::Spaces(_) => ' ',
 753            IndentDelta::Tabs(_) => '\t',
 754        }
 755    }
 756
 757    fn len(&self) -> isize {
 758        match self {
 759            IndentDelta::Spaces(n) => *n,
 760            IndentDelta::Tabs(n) => *n,
 761        }
 762    }
 763}
 764
 765#[cfg(test)]
 766mod tests {
 767    use super::*;
 768    use fs::FakeFs;
 769    use futures::stream;
 770    use gpui::{AppContext, TestAppContext};
 771    use indoc::indoc;
 772    use language_model::fake_provider::FakeLanguageModel;
 773    use pretty_assertions::assert_matches;
 774    use project::{AgentLocation, Project};
 775    use rand::prelude::*;
 776    use rand::rngs::StdRng;
 777    use std::cmp;
 778
 779    #[gpui::test(iterations = 100)]
 780    async fn test_empty_old_text(cx: &mut TestAppContext, mut rng: StdRng) {
 781        let agent = init_test(cx).await;
 782        let buffer = cx.new(|cx| {
 783            Buffer::local(
 784                indoc! {"
 785                    abc
 786                    def
 787                    ghi
 788                "},
 789                cx,
 790            )
 791        });
 792        let (apply, _events) = agent.edit(
 793            buffer.clone(),
 794            String::new(),
 795            &LanguageModelRequest::default(),
 796            &mut cx.to_async(),
 797        );
 798        cx.run_until_parked();
 799
 800        simulate_llm_output(
 801            &agent,
 802            indoc! {"
 803                <old_text></old_text>
 804                <new_text>jkl</new_text>
 805                <old_text>def</old_text>
 806                <new_text>DEF</new_text>
 807            "},
 808            &mut rng,
 809            cx,
 810        );
 811        apply.await.unwrap();
 812
 813        pretty_assertions::assert_eq!(
 814            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 815            indoc! {"
 816                abc
 817                DEF
 818                ghi
 819            "}
 820        );
 821    }
 822
 823    #[gpui::test(iterations = 100)]
 824    async fn test_indentation(cx: &mut TestAppContext, mut rng: StdRng) {
 825        let agent = init_test(cx).await;
 826        let buffer = cx.new(|cx| {
 827            Buffer::local(
 828                indoc! {"
 829                    lorem
 830                            ipsum
 831                            dolor
 832                            sit
 833                "},
 834                cx,
 835            )
 836        });
 837        let (apply, _events) = agent.edit(
 838            buffer.clone(),
 839            String::new(),
 840            &LanguageModelRequest::default(),
 841            &mut cx.to_async(),
 842        );
 843        cx.run_until_parked();
 844
 845        simulate_llm_output(
 846            &agent,
 847            indoc! {"
 848                <old_text>
 849                    ipsum
 850                    dolor
 851                    sit
 852                </old_text>
 853                <new_text>
 854                    ipsum
 855                    dolor
 856                    sit
 857                amet
 858                </new_text>
 859            "},
 860            &mut rng,
 861            cx,
 862        );
 863        apply.await.unwrap();
 864
 865        pretty_assertions::assert_eq!(
 866            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 867            indoc! {"
 868                lorem
 869                        ipsum
 870                        dolor
 871                        sit
 872                    amet
 873            "}
 874        );
 875    }
 876
 877    #[gpui::test(iterations = 100)]
 878    async fn test_dependent_edits(cx: &mut TestAppContext, mut rng: StdRng) {
 879        let agent = init_test(cx).await;
 880        let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi", cx));
 881        let (apply, _events) = agent.edit(
 882            buffer.clone(),
 883            String::new(),
 884            &LanguageModelRequest::default(),
 885            &mut cx.to_async(),
 886        );
 887        cx.run_until_parked();
 888
 889        simulate_llm_output(
 890            &agent,
 891            indoc! {"
 892                <old_text>
 893                def
 894                </old_text>
 895                <new_text>
 896                DEF
 897                </new_text>
 898
 899                <old_text>
 900                DEF
 901                </old_text>
 902                <new_text>
 903                DeF
 904                </new_text>
 905            "},
 906            &mut rng,
 907            cx,
 908        );
 909        apply.await.unwrap();
 910
 911        assert_eq!(
 912            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 913            "abc\nDeF\nghi"
 914        );
 915    }
 916
 917    #[gpui::test(iterations = 100)]
 918    async fn test_old_text_hallucination(cx: &mut TestAppContext, mut rng: StdRng) {
 919        let agent = init_test(cx).await;
 920        let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi", cx));
 921        let (apply, _events) = agent.edit(
 922            buffer.clone(),
 923            String::new(),
 924            &LanguageModelRequest::default(),
 925            &mut cx.to_async(),
 926        );
 927        cx.run_until_parked();
 928
 929        simulate_llm_output(
 930            &agent,
 931            indoc! {"
 932                <old_text>
 933                jkl
 934                </old_text>
 935                <new_text>
 936                mno
 937                </new_text>
 938
 939                <old_text>
 940                abc
 941                </old_text>
 942                <new_text>
 943                ABC
 944                </new_text>
 945            "},
 946            &mut rng,
 947            cx,
 948        );
 949        apply.await.unwrap();
 950
 951        assert_eq!(
 952            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 953            "ABC\ndef\nghi"
 954        );
 955    }
 956
 957    #[gpui::test]
 958    async fn test_edit_events(cx: &mut TestAppContext) {
 959        let agent = init_test(cx).await;
 960        let model = agent.model.as_fake();
 961        let project = agent
 962            .action_log
 963            .read_with(cx, |log, _| log.project().clone());
 964        let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi\njkl", cx));
 965
 966        let mut async_cx = cx.to_async();
 967        let (apply, mut events) = agent.edit(
 968            buffer.clone(),
 969            String::new(),
 970            &LanguageModelRequest::default(),
 971            &mut async_cx,
 972        );
 973        cx.run_until_parked();
 974
 975        model.send_last_completion_stream_text_chunk("<old_text>a");
 976        cx.run_until_parked();
 977        assert_eq!(drain_events(&mut events), vec![]);
 978        assert_eq!(
 979            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 980            "abc\ndef\nghi\njkl"
 981        );
 982        assert_eq!(
 983            project.read_with(cx, |project, _| project.agent_location()),
 984            None
 985        );
 986
 987        model.send_last_completion_stream_text_chunk("bc</old_text>");
 988        cx.run_until_parked();
 989        assert_eq!(
 990            drain_events(&mut events),
 991            vec![EditAgentOutputEvent::ResolvingEditRange(buffer.read_with(
 992                cx,
 993                |buffer, _| buffer.anchor_before(Point::new(0, 0))
 994                    ..buffer.anchor_before(Point::new(0, 3))
 995            ))]
 996        );
 997        assert_eq!(
 998            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 999            "abc\ndef\nghi\njkl"
1000        );
1001        assert_eq!(
1002            project.read_with(cx, |project, _| project.agent_location()),
1003            Some(AgentLocation {
1004                buffer: buffer.downgrade(),
1005                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 3)))
1006            })
1007        );
1008
1009        model.send_last_completion_stream_text_chunk("<new_text>abX");
1010        cx.run_until_parked();
1011        assert_matches!(
1012            drain_events(&mut events).as_slice(),
1013            [EditAgentOutputEvent::Edited(_)]
1014        );
1015        assert_eq!(
1016            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1017            "abXc\ndef\nghi\njkl"
1018        );
1019        assert_eq!(
1020            project.read_with(cx, |project, _| project.agent_location()),
1021            Some(AgentLocation {
1022                buffer: buffer.downgrade(),
1023                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 3)))
1024            })
1025        );
1026
1027        model.send_last_completion_stream_text_chunk("cY");
1028        cx.run_until_parked();
1029        assert_matches!(
1030            drain_events(&mut events).as_slice(),
1031            [EditAgentOutputEvent::Edited { .. }]
1032        );
1033        assert_eq!(
1034            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1035            "abXcY\ndef\nghi\njkl"
1036        );
1037        assert_eq!(
1038            project.read_with(cx, |project, _| project.agent_location()),
1039            Some(AgentLocation {
1040                buffer: buffer.downgrade(),
1041                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 5)))
1042            })
1043        );
1044
1045        model.send_last_completion_stream_text_chunk("</new_text>");
1046        model.send_last_completion_stream_text_chunk("<old_text>hall");
1047        cx.run_until_parked();
1048        assert_eq!(drain_events(&mut events), vec![]);
1049        assert_eq!(
1050            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1051            "abXcY\ndef\nghi\njkl"
1052        );
1053        assert_eq!(
1054            project.read_with(cx, |project, _| project.agent_location()),
1055            Some(AgentLocation {
1056                buffer: buffer.downgrade(),
1057                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 5)))
1058            })
1059        );
1060
1061        model.send_last_completion_stream_text_chunk("ucinated old</old_text>");
1062        model.send_last_completion_stream_text_chunk("<new_text>");
1063        cx.run_until_parked();
1064        assert_eq!(
1065            drain_events(&mut events),
1066            vec![EditAgentOutputEvent::UnresolvedEditRange]
1067        );
1068        assert_eq!(
1069            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1070            "abXcY\ndef\nghi\njkl"
1071        );
1072        assert_eq!(
1073            project.read_with(cx, |project, _| project.agent_location()),
1074            Some(AgentLocation {
1075                buffer: buffer.downgrade(),
1076                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 5)))
1077            })
1078        );
1079
1080        model.send_last_completion_stream_text_chunk("hallucinated new</new_");
1081        model.send_last_completion_stream_text_chunk("text>");
1082        cx.run_until_parked();
1083        assert_eq!(drain_events(&mut events), vec![]);
1084        assert_eq!(
1085            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1086            "abXcY\ndef\nghi\njkl"
1087        );
1088        assert_eq!(
1089            project.read_with(cx, |project, _| project.agent_location()),
1090            Some(AgentLocation {
1091                buffer: buffer.downgrade(),
1092                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 5)))
1093            })
1094        );
1095
1096        model.send_last_completion_stream_text_chunk("<old_text>\nghi\nj");
1097        cx.run_until_parked();
1098        assert_eq!(
1099            drain_events(&mut events),
1100            vec![EditAgentOutputEvent::ResolvingEditRange(buffer.read_with(
1101                cx,
1102                |buffer, _| buffer.anchor_before(Point::new(2, 0))
1103                    ..buffer.anchor_before(Point::new(2, 3))
1104            ))]
1105        );
1106        assert_eq!(
1107            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1108            "abXcY\ndef\nghi\njkl"
1109        );
1110        assert_eq!(
1111            project.read_with(cx, |project, _| project.agent_location()),
1112            Some(AgentLocation {
1113                buffer: buffer.downgrade(),
1114                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(2, 3)))
1115            })
1116        );
1117
1118        model.send_last_completion_stream_text_chunk("kl</old_text>");
1119        model.send_last_completion_stream_text_chunk("<new_text>");
1120        cx.run_until_parked();
1121        assert_eq!(
1122            drain_events(&mut events),
1123            vec![EditAgentOutputEvent::ResolvingEditRange(buffer.read_with(
1124                cx,
1125                |buffer, _| buffer.anchor_before(Point::new(2, 0))
1126                    ..buffer.anchor_before(Point::new(3, 3))
1127            ))]
1128        );
1129        assert_eq!(
1130            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1131            "abXcY\ndef\nghi\njkl"
1132        );
1133        assert_eq!(
1134            project.read_with(cx, |project, _| project.agent_location()),
1135            Some(AgentLocation {
1136                buffer: buffer.downgrade(),
1137                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(3, 3)))
1138            })
1139        );
1140
1141        model.send_last_completion_stream_text_chunk("GHI</new_text>");
1142        cx.run_until_parked();
1143        assert_matches!(
1144            drain_events(&mut events).as_slice(),
1145            [EditAgentOutputEvent::Edited { .. }]
1146        );
1147        assert_eq!(
1148            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1149            "abXcY\ndef\nGHI"
1150        );
1151        assert_eq!(
1152            project.read_with(cx, |project, _| project.agent_location()),
1153            Some(AgentLocation {
1154                buffer: buffer.downgrade(),
1155                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(2, 3)))
1156            })
1157        );
1158
1159        model.end_last_completion_stream();
1160        apply.await.unwrap();
1161        assert_eq!(
1162            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1163            "abXcY\ndef\nGHI"
1164        );
1165        assert_eq!(drain_events(&mut events), vec![]);
1166        assert_eq!(
1167            project.read_with(cx, |project, _| project.agent_location()),
1168            Some(AgentLocation {
1169                buffer: buffer.downgrade(),
1170                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(2, 3)))
1171            })
1172        );
1173    }
1174
1175    #[gpui::test]
1176    async fn test_overwrite_events(cx: &mut TestAppContext) {
1177        let agent = init_test(cx).await;
1178        let project = agent
1179            .action_log
1180            .read_with(cx, |log, _| log.project().clone());
1181        let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi", cx));
1182        let (chunks_tx, chunks_rx) = mpsc::unbounded();
1183        let (apply, mut events) = agent.overwrite_with_chunks(
1184            buffer.clone(),
1185            chunks_rx.map(|chunk: &str| Ok(chunk.to_string())),
1186            &mut cx.to_async(),
1187        );
1188
1189        cx.run_until_parked();
1190        assert_matches!(
1191            drain_events(&mut events).as_slice(),
1192            [EditAgentOutputEvent::Edited(_)]
1193        );
1194        assert_eq!(
1195            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1196            ""
1197        );
1198        assert_eq!(
1199            project.read_with(cx, |project, _| project.agent_location()),
1200            Some(AgentLocation {
1201                buffer: buffer.downgrade(),
1202                position: language::Anchor::MAX
1203            })
1204        );
1205
1206        chunks_tx.unbounded_send("```\njkl\n").unwrap();
1207        cx.run_until_parked();
1208        assert_matches!(
1209            drain_events(&mut events).as_slice(),
1210            [EditAgentOutputEvent::Edited { .. }]
1211        );
1212        assert_eq!(
1213            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1214            "jkl"
1215        );
1216        assert_eq!(
1217            project.read_with(cx, |project, _| project.agent_location()),
1218            Some(AgentLocation {
1219                buffer: buffer.downgrade(),
1220                position: language::Anchor::MAX
1221            })
1222        );
1223
1224        chunks_tx.unbounded_send("mno\n").unwrap();
1225        cx.run_until_parked();
1226        assert_matches!(
1227            drain_events(&mut events).as_slice(),
1228            [EditAgentOutputEvent::Edited { .. }]
1229        );
1230        assert_eq!(
1231            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1232            "jkl\nmno"
1233        );
1234        assert_eq!(
1235            project.read_with(cx, |project, _| project.agent_location()),
1236            Some(AgentLocation {
1237                buffer: buffer.downgrade(),
1238                position: language::Anchor::MAX
1239            })
1240        );
1241
1242        chunks_tx.unbounded_send("pqr\n```").unwrap();
1243        cx.run_until_parked();
1244        assert_matches!(
1245            drain_events(&mut events).as_slice(),
1246            [EditAgentOutputEvent::Edited(_)],
1247        );
1248        assert_eq!(
1249            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1250            "jkl\nmno\npqr"
1251        );
1252        assert_eq!(
1253            project.read_with(cx, |project, _| project.agent_location()),
1254            Some(AgentLocation {
1255                buffer: buffer.downgrade(),
1256                position: language::Anchor::MAX
1257            })
1258        );
1259
1260        drop(chunks_tx);
1261        apply.await.unwrap();
1262        assert_eq!(
1263            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1264            "jkl\nmno\npqr"
1265        );
1266        assert_eq!(drain_events(&mut events), vec![]);
1267        assert_eq!(
1268            project.read_with(cx, |project, _| project.agent_location()),
1269            Some(AgentLocation {
1270                buffer: buffer.downgrade(),
1271                position: language::Anchor::MAX
1272            })
1273        );
1274    }
1275
1276    #[gpui::test(iterations = 100)]
1277    async fn test_indent_new_text_chunks(mut rng: StdRng) {
1278        let chunks = to_random_chunks(&mut rng, "    abc\n  def\n      ghi");
1279        let new_text_chunks = stream::iter(chunks.iter().enumerate().map(|(index, chunk)| {
1280            Ok(EditParserEvent::NewTextChunk {
1281                chunk: chunk.clone(),
1282                done: index == chunks.len() - 1,
1283            })
1284        }));
1285        let indented_chunks =
1286            EditAgent::reindent_new_text_chunks(IndentDelta::Spaces(2), new_text_chunks)
1287                .collect::<Vec<_>>()
1288                .await;
1289        let new_text = indented_chunks
1290            .into_iter()
1291            .collect::<Result<String>>()
1292            .unwrap();
1293        assert_eq!(new_text, "      abc\n    def\n        ghi");
1294    }
1295
1296    #[gpui::test(iterations = 100)]
1297    async fn test_outdent_new_text_chunks(mut rng: StdRng) {
1298        let chunks = to_random_chunks(&mut rng, "\t\t\t\tabc\n\t\tdef\n\t\t\t\t\t\tghi");
1299        let new_text_chunks = stream::iter(chunks.iter().enumerate().map(|(index, chunk)| {
1300            Ok(EditParserEvent::NewTextChunk {
1301                chunk: chunk.clone(),
1302                done: index == chunks.len() - 1,
1303            })
1304        }));
1305        let indented_chunks =
1306            EditAgent::reindent_new_text_chunks(IndentDelta::Tabs(-2), new_text_chunks)
1307                .collect::<Vec<_>>()
1308                .await;
1309        let new_text = indented_chunks
1310            .into_iter()
1311            .collect::<Result<String>>()
1312            .unwrap();
1313        assert_eq!(new_text, "\t\tabc\ndef\n\t\t\t\tghi");
1314    }
1315
1316    #[gpui::test(iterations = 100)]
1317    async fn test_random_indents(mut rng: StdRng) {
1318        let len = rng.random_range(1..=100);
1319        let new_text = util::RandomCharIter::new(&mut rng)
1320            .with_simple_text()
1321            .take(len)
1322            .collect::<String>();
1323        let new_text = new_text
1324            .split('\n')
1325            .map(|line| format!("{}{}", " ".repeat(rng.random_range(0..=8)), line))
1326            .collect::<Vec<_>>()
1327            .join("\n");
1328        let delta = IndentDelta::Spaces(rng.random_range(-4i8..=4i8) as isize);
1329
1330        let chunks = to_random_chunks(&mut rng, &new_text);
1331        let new_text_chunks = stream::iter(chunks.iter().enumerate().map(|(index, chunk)| {
1332            Ok(EditParserEvent::NewTextChunk {
1333                chunk: chunk.clone(),
1334                done: index == chunks.len() - 1,
1335            })
1336        }));
1337        let reindented_chunks = EditAgent::reindent_new_text_chunks(delta, new_text_chunks)
1338            .collect::<Vec<_>>()
1339            .await;
1340        let actual_reindented_text = reindented_chunks
1341            .into_iter()
1342            .collect::<Result<String>>()
1343            .unwrap();
1344        let expected_reindented_text = new_text
1345            .split('\n')
1346            .map(|line| {
1347                if let Some(ix) = line.find(|c| c != ' ') {
1348                    let new_indent = cmp::max(0, ix as isize + delta.len()) as usize;
1349                    format!("{}{}", " ".repeat(new_indent), &line[ix..])
1350                } else {
1351                    line.to_string()
1352                }
1353            })
1354            .collect::<Vec<_>>()
1355            .join("\n");
1356        assert_eq!(actual_reindented_text, expected_reindented_text);
1357    }
1358
1359    fn to_random_chunks(rng: &mut StdRng, input: &str) -> Vec<String> {
1360        let chunk_count = rng.random_range(1..=cmp::min(input.len(), 50));
1361        let mut chunk_indices = (0..input.len()).choose_multiple(rng, chunk_count);
1362        chunk_indices.sort();
1363        chunk_indices.push(input.len());
1364
1365        let mut chunks = Vec::new();
1366        let mut last_ix = 0;
1367        for chunk_ix in chunk_indices {
1368            chunks.push(input[last_ix..chunk_ix].to_string());
1369            last_ix = chunk_ix;
1370        }
1371        chunks
1372    }
1373
1374    fn simulate_llm_output(
1375        agent: &EditAgent,
1376        output: &str,
1377        rng: &mut StdRng,
1378        cx: &mut TestAppContext,
1379    ) {
1380        let executor = cx.executor();
1381        let chunks = to_random_chunks(rng, output);
1382        let model = agent.model.clone();
1383        cx.background_spawn(async move {
1384            for chunk in chunks {
1385                executor.simulate_random_delay().await;
1386                model
1387                    .as_fake()
1388                    .send_last_completion_stream_text_chunk(chunk);
1389            }
1390            model.as_fake().end_last_completion_stream();
1391        })
1392        .detach();
1393    }
1394
1395    async fn init_test(cx: &mut TestAppContext) -> EditAgent {
1396        cx.update(settings::init);
1397        cx.update(Project::init_settings);
1398        let project = Project::test(FakeFs::new(cx.executor()), [], cx).await;
1399        let model = Arc::new(FakeLanguageModel::default());
1400        let action_log = cx.new(|_| ActionLog::new(project.clone()));
1401        EditAgent::new(
1402            model,
1403            project,
1404            action_log,
1405            Templates::new(),
1406            EditFormat::XmlTags,
1407        )
1408    }
1409
1410    #[gpui::test(iterations = 10)]
1411    async fn test_non_unique_text_error(cx: &mut TestAppContext, mut rng: StdRng) {
1412        let agent = init_test(cx).await;
1413        let original_text = indoc! {"
1414                function foo() {
1415                    return 42;
1416                }
1417
1418                function bar() {
1419                    return 42;
1420                }
1421
1422                function baz() {
1423                    return 42;
1424                }
1425            "};
1426        let buffer = cx.new(|cx| Buffer::local(original_text, cx));
1427        let (apply, mut events) = agent.edit(
1428            buffer.clone(),
1429            String::new(),
1430            &LanguageModelRequest::default(),
1431            &mut cx.to_async(),
1432        );
1433        cx.run_until_parked();
1434
1435        // When <old_text> matches text in more than one place
1436        simulate_llm_output(
1437            &agent,
1438            indoc! {"
1439                <old_text>
1440                    return 42;
1441                }
1442                </old_text>
1443                <new_text>
1444                    return 100;
1445                }
1446                </new_text>
1447            "},
1448            &mut rng,
1449            cx,
1450        );
1451        apply.await.unwrap();
1452
1453        // Then the text should remain unchanged
1454        let result_text = buffer.read_with(cx, |buffer, _| buffer.snapshot().text());
1455        assert_eq!(
1456            result_text,
1457            indoc! {"
1458                function foo() {
1459                    return 42;
1460                }
1461
1462                function bar() {
1463                    return 42;
1464                }
1465
1466                function baz() {
1467                    return 42;
1468                }
1469            "},
1470            "Text should remain unchanged when there are multiple matches"
1471        );
1472
1473        // And AmbiguousEditRange even should be emitted
1474        let events = drain_events(&mut events);
1475        let ambiguous_ranges = vec![2..3, 6..7, 10..11];
1476        assert!(
1477            events.contains(&EditAgentOutputEvent::AmbiguousEditRange(ambiguous_ranges)),
1478            "Should emit AmbiguousEditRange for non-unique text"
1479        );
1480    }
1481
1482    fn drain_events(
1483        stream: &mut UnboundedReceiver<EditAgentOutputEvent>,
1484    ) -> Vec<EditAgentOutputEvent> {
1485        let mut events = Vec::new();
1486        while let Ok(Some(event)) = stream.try_next() {
1487            events.push(event);
1488        }
1489        events
1490    }
1491}