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, path::PathBuf, 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<PathBuf>,
  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<PathBuf>,
  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<PathBuf>,
  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(cx, true))?;
 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(cx, true))?;
 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                let old_content_len = last_message.content.len();
 678                last_message
 679                    .content
 680                    .retain(|content| !matches!(content, MessageContent::ToolUse(_)));
 681                let new_content_len = last_message.content.len();
 682
 683                // We just removed pending tool uses from the content of the
 684                // last message, so it doesn't make sense to cache it anymore
 685                // (e.g., the message will look very different on the next
 686                // request). Thus, we move the flag to the message prior to it,
 687                // as it will still be a valid prefix of the conversation.
 688                if old_content_len != new_content_len && last_message.cache
 689                    && let Some(prev_message) = messages_iter.next_back() {
 690                        last_message.cache = false;
 691                        prev_message.cache = true;
 692                    }
 693
 694                if last_message.content.is_empty() {
 695                    conversation.messages.pop();
 696                }
 697            }
 698
 699        conversation.messages.push(LanguageModelRequestMessage {
 700            role: Role::User,
 701            content: vec![MessageContent::Text(prompt)],
 702            cache: false,
 703        });
 704
 705        // Include tools in the request so that we can take advantage of
 706        // caching when ToolChoice::None is supported.
 707        let mut tool_choice = None;
 708        let mut tools = Vec::new();
 709        if !conversation.tools.is_empty()
 710            && self
 711                .model
 712                .supports_tool_choice(LanguageModelToolChoice::None)
 713        {
 714            tool_choice = Some(LanguageModelToolChoice::None);
 715            tools = conversation.tools.clone();
 716        }
 717
 718        let request = LanguageModelRequest {
 719            thread_id: conversation.thread_id,
 720            prompt_id: conversation.prompt_id,
 721            intent: Some(intent),
 722            mode: conversation.mode,
 723            messages: conversation.messages,
 724            tool_choice,
 725            tools,
 726            stop: Vec::new(),
 727            temperature: None,
 728            thinking_allowed: true,
 729        };
 730
 731        Ok(self.model.stream_completion_text(request, cx).await?.stream)
 732    }
 733}
 734
 735struct ResolvedOldText {
 736    range: Range<usize>,
 737    indent: LineIndent,
 738}
 739
 740#[derive(Copy, Clone, Debug)]
 741enum IndentDelta {
 742    Spaces(isize),
 743    Tabs(isize),
 744}
 745
 746impl IndentDelta {
 747    fn character(&self) -> char {
 748        match self {
 749            IndentDelta::Spaces(_) => ' ',
 750            IndentDelta::Tabs(_) => '\t',
 751        }
 752    }
 753
 754    fn len(&self) -> isize {
 755        match self {
 756            IndentDelta::Spaces(n) => *n,
 757            IndentDelta::Tabs(n) => *n,
 758        }
 759    }
 760}
 761
 762#[cfg(test)]
 763mod tests {
 764    use super::*;
 765    use fs::FakeFs;
 766    use futures::stream;
 767    use gpui::{AppContext, TestAppContext};
 768    use indoc::indoc;
 769    use language_model::fake_provider::FakeLanguageModel;
 770    use pretty_assertions::assert_matches;
 771    use project::{AgentLocation, Project};
 772    use rand::prelude::*;
 773    use rand::rngs::StdRng;
 774    use std::cmp;
 775
 776    #[gpui::test(iterations = 100)]
 777    async fn test_empty_old_text(cx: &mut TestAppContext, mut rng: StdRng) {
 778        let agent = init_test(cx).await;
 779        let buffer = cx.new(|cx| {
 780            Buffer::local(
 781                indoc! {"
 782                    abc
 783                    def
 784                    ghi
 785                "},
 786                cx,
 787            )
 788        });
 789        let (apply, _events) = agent.edit(
 790            buffer.clone(),
 791            String::new(),
 792            &LanguageModelRequest::default(),
 793            &mut cx.to_async(),
 794        );
 795        cx.run_until_parked();
 796
 797        simulate_llm_output(
 798            &agent,
 799            indoc! {"
 800                <old_text></old_text>
 801                <new_text>jkl</new_text>
 802                <old_text>def</old_text>
 803                <new_text>DEF</new_text>
 804            "},
 805            &mut rng,
 806            cx,
 807        );
 808        apply.await.unwrap();
 809
 810        pretty_assertions::assert_eq!(
 811            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 812            indoc! {"
 813                abc
 814                DEF
 815                ghi
 816            "}
 817        );
 818    }
 819
 820    #[gpui::test(iterations = 100)]
 821    async fn test_indentation(cx: &mut TestAppContext, mut rng: StdRng) {
 822        let agent = init_test(cx).await;
 823        let buffer = cx.new(|cx| {
 824            Buffer::local(
 825                indoc! {"
 826                    lorem
 827                            ipsum
 828                            dolor
 829                            sit
 830                "},
 831                cx,
 832            )
 833        });
 834        let (apply, _events) = agent.edit(
 835            buffer.clone(),
 836            String::new(),
 837            &LanguageModelRequest::default(),
 838            &mut cx.to_async(),
 839        );
 840        cx.run_until_parked();
 841
 842        simulate_llm_output(
 843            &agent,
 844            indoc! {"
 845                <old_text>
 846                    ipsum
 847                    dolor
 848                    sit
 849                </old_text>
 850                <new_text>
 851                    ipsum
 852                    dolor
 853                    sit
 854                amet
 855                </new_text>
 856            "},
 857            &mut rng,
 858            cx,
 859        );
 860        apply.await.unwrap();
 861
 862        pretty_assertions::assert_eq!(
 863            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 864            indoc! {"
 865                lorem
 866                        ipsum
 867                        dolor
 868                        sit
 869                    amet
 870            "}
 871        );
 872    }
 873
 874    #[gpui::test(iterations = 100)]
 875    async fn test_dependent_edits(cx: &mut TestAppContext, mut rng: StdRng) {
 876        let agent = init_test(cx).await;
 877        let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi", cx));
 878        let (apply, _events) = agent.edit(
 879            buffer.clone(),
 880            String::new(),
 881            &LanguageModelRequest::default(),
 882            &mut cx.to_async(),
 883        );
 884        cx.run_until_parked();
 885
 886        simulate_llm_output(
 887            &agent,
 888            indoc! {"
 889                <old_text>
 890                def
 891                </old_text>
 892                <new_text>
 893                DEF
 894                </new_text>
 895
 896                <old_text>
 897                DEF
 898                </old_text>
 899                <new_text>
 900                DeF
 901                </new_text>
 902            "},
 903            &mut rng,
 904            cx,
 905        );
 906        apply.await.unwrap();
 907
 908        assert_eq!(
 909            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 910            "abc\nDeF\nghi"
 911        );
 912    }
 913
 914    #[gpui::test(iterations = 100)]
 915    async fn test_old_text_hallucination(cx: &mut TestAppContext, mut rng: StdRng) {
 916        let agent = init_test(cx).await;
 917        let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi", cx));
 918        let (apply, _events) = agent.edit(
 919            buffer.clone(),
 920            String::new(),
 921            &LanguageModelRequest::default(),
 922            &mut cx.to_async(),
 923        );
 924        cx.run_until_parked();
 925
 926        simulate_llm_output(
 927            &agent,
 928            indoc! {"
 929                <old_text>
 930                jkl
 931                </old_text>
 932                <new_text>
 933                mno
 934                </new_text>
 935
 936                <old_text>
 937                abc
 938                </old_text>
 939                <new_text>
 940                ABC
 941                </new_text>
 942            "},
 943            &mut rng,
 944            cx,
 945        );
 946        apply.await.unwrap();
 947
 948        assert_eq!(
 949            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 950            "ABC\ndef\nghi"
 951        );
 952    }
 953
 954    #[gpui::test]
 955    async fn test_edit_events(cx: &mut TestAppContext) {
 956        let agent = init_test(cx).await;
 957        let model = agent.model.as_fake();
 958        let project = agent
 959            .action_log
 960            .read_with(cx, |log, _| log.project().clone());
 961        let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi\njkl", cx));
 962
 963        let mut async_cx = cx.to_async();
 964        let (apply, mut events) = agent.edit(
 965            buffer.clone(),
 966            String::new(),
 967            &LanguageModelRequest::default(),
 968            &mut async_cx,
 969        );
 970        cx.run_until_parked();
 971
 972        model.send_last_completion_stream_text_chunk("<old_text>a");
 973        cx.run_until_parked();
 974        assert_eq!(drain_events(&mut events), vec![]);
 975        assert_eq!(
 976            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 977            "abc\ndef\nghi\njkl"
 978        );
 979        assert_eq!(
 980            project.read_with(cx, |project, _| project.agent_location()),
 981            None
 982        );
 983
 984        model.send_last_completion_stream_text_chunk("bc</old_text>");
 985        cx.run_until_parked();
 986        assert_eq!(
 987            drain_events(&mut events),
 988            vec![EditAgentOutputEvent::ResolvingEditRange(buffer.read_with(
 989                cx,
 990                |buffer, _| buffer.anchor_before(Point::new(0, 0))
 991                    ..buffer.anchor_before(Point::new(0, 3))
 992            ))]
 993        );
 994        assert_eq!(
 995            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
 996            "abc\ndef\nghi\njkl"
 997        );
 998        assert_eq!(
 999            project.read_with(cx, |project, _| project.agent_location()),
1000            Some(AgentLocation {
1001                buffer: buffer.downgrade(),
1002                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 3)))
1003            })
1004        );
1005
1006        model.send_last_completion_stream_text_chunk("<new_text>abX");
1007        cx.run_until_parked();
1008        assert_matches!(
1009            drain_events(&mut events).as_slice(),
1010            [EditAgentOutputEvent::Edited(_)]
1011        );
1012        assert_eq!(
1013            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1014            "abXc\ndef\nghi\njkl"
1015        );
1016        assert_eq!(
1017            project.read_with(cx, |project, _| project.agent_location()),
1018            Some(AgentLocation {
1019                buffer: buffer.downgrade(),
1020                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 3)))
1021            })
1022        );
1023
1024        model.send_last_completion_stream_text_chunk("cY");
1025        cx.run_until_parked();
1026        assert_matches!(
1027            drain_events(&mut events).as_slice(),
1028            [EditAgentOutputEvent::Edited { .. }]
1029        );
1030        assert_eq!(
1031            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1032            "abXcY\ndef\nghi\njkl"
1033        );
1034        assert_eq!(
1035            project.read_with(cx, |project, _| project.agent_location()),
1036            Some(AgentLocation {
1037                buffer: buffer.downgrade(),
1038                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 5)))
1039            })
1040        );
1041
1042        model.send_last_completion_stream_text_chunk("</new_text>");
1043        model.send_last_completion_stream_text_chunk("<old_text>hall");
1044        cx.run_until_parked();
1045        assert_eq!(drain_events(&mut events), vec![]);
1046        assert_eq!(
1047            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1048            "abXcY\ndef\nghi\njkl"
1049        );
1050        assert_eq!(
1051            project.read_with(cx, |project, _| project.agent_location()),
1052            Some(AgentLocation {
1053                buffer: buffer.downgrade(),
1054                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 5)))
1055            })
1056        );
1057
1058        model.send_last_completion_stream_text_chunk("ucinated old</old_text>");
1059        model.send_last_completion_stream_text_chunk("<new_text>");
1060        cx.run_until_parked();
1061        assert_eq!(
1062            drain_events(&mut events),
1063            vec![EditAgentOutputEvent::UnresolvedEditRange]
1064        );
1065        assert_eq!(
1066            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1067            "abXcY\ndef\nghi\njkl"
1068        );
1069        assert_eq!(
1070            project.read_with(cx, |project, _| project.agent_location()),
1071            Some(AgentLocation {
1072                buffer: buffer.downgrade(),
1073                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 5)))
1074            })
1075        );
1076
1077        model.send_last_completion_stream_text_chunk("hallucinated new</new_");
1078        model.send_last_completion_stream_text_chunk("text>");
1079        cx.run_until_parked();
1080        assert_eq!(drain_events(&mut events), vec![]);
1081        assert_eq!(
1082            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1083            "abXcY\ndef\nghi\njkl"
1084        );
1085        assert_eq!(
1086            project.read_with(cx, |project, _| project.agent_location()),
1087            Some(AgentLocation {
1088                buffer: buffer.downgrade(),
1089                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(0, 5)))
1090            })
1091        );
1092
1093        model.send_last_completion_stream_text_chunk("<old_text>\nghi\nj");
1094        cx.run_until_parked();
1095        assert_eq!(
1096            drain_events(&mut events),
1097            vec![EditAgentOutputEvent::ResolvingEditRange(buffer.read_with(
1098                cx,
1099                |buffer, _| buffer.anchor_before(Point::new(2, 0))
1100                    ..buffer.anchor_before(Point::new(2, 3))
1101            ))]
1102        );
1103        assert_eq!(
1104            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1105            "abXcY\ndef\nghi\njkl"
1106        );
1107        assert_eq!(
1108            project.read_with(cx, |project, _| project.agent_location()),
1109            Some(AgentLocation {
1110                buffer: buffer.downgrade(),
1111                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(2, 3)))
1112            })
1113        );
1114
1115        model.send_last_completion_stream_text_chunk("kl</old_text>");
1116        model.send_last_completion_stream_text_chunk("<new_text>");
1117        cx.run_until_parked();
1118        assert_eq!(
1119            drain_events(&mut events),
1120            vec![EditAgentOutputEvent::ResolvingEditRange(buffer.read_with(
1121                cx,
1122                |buffer, _| buffer.anchor_before(Point::new(2, 0))
1123                    ..buffer.anchor_before(Point::new(3, 3))
1124            ))]
1125        );
1126        assert_eq!(
1127            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1128            "abXcY\ndef\nghi\njkl"
1129        );
1130        assert_eq!(
1131            project.read_with(cx, |project, _| project.agent_location()),
1132            Some(AgentLocation {
1133                buffer: buffer.downgrade(),
1134                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(3, 3)))
1135            })
1136        );
1137
1138        model.send_last_completion_stream_text_chunk("GHI</new_text>");
1139        cx.run_until_parked();
1140        assert_matches!(
1141            drain_events(&mut events).as_slice(),
1142            [EditAgentOutputEvent::Edited { .. }]
1143        );
1144        assert_eq!(
1145            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1146            "abXcY\ndef\nGHI"
1147        );
1148        assert_eq!(
1149            project.read_with(cx, |project, _| project.agent_location()),
1150            Some(AgentLocation {
1151                buffer: buffer.downgrade(),
1152                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(2, 3)))
1153            })
1154        );
1155
1156        model.end_last_completion_stream();
1157        apply.await.unwrap();
1158        assert_eq!(
1159            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1160            "abXcY\ndef\nGHI"
1161        );
1162        assert_eq!(drain_events(&mut events), vec![]);
1163        assert_eq!(
1164            project.read_with(cx, |project, _| project.agent_location()),
1165            Some(AgentLocation {
1166                buffer: buffer.downgrade(),
1167                position: buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(2, 3)))
1168            })
1169        );
1170    }
1171
1172    #[gpui::test]
1173    async fn test_overwrite_events(cx: &mut TestAppContext) {
1174        let agent = init_test(cx).await;
1175        let project = agent
1176            .action_log
1177            .read_with(cx, |log, _| log.project().clone());
1178        let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi", cx));
1179        let (chunks_tx, chunks_rx) = mpsc::unbounded();
1180        let (apply, mut events) = agent.overwrite_with_chunks(
1181            buffer.clone(),
1182            chunks_rx.map(|chunk: &str| Ok(chunk.to_string())),
1183            &mut cx.to_async(),
1184        );
1185
1186        cx.run_until_parked();
1187        assert_matches!(
1188            drain_events(&mut events).as_slice(),
1189            [EditAgentOutputEvent::Edited(_)]
1190        );
1191        assert_eq!(
1192            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1193            ""
1194        );
1195        assert_eq!(
1196            project.read_with(cx, |project, _| project.agent_location()),
1197            Some(AgentLocation {
1198                buffer: buffer.downgrade(),
1199                position: language::Anchor::MAX
1200            })
1201        );
1202
1203        chunks_tx.unbounded_send("```\njkl\n").unwrap();
1204        cx.run_until_parked();
1205        assert_matches!(
1206            drain_events(&mut events).as_slice(),
1207            [EditAgentOutputEvent::Edited { .. }]
1208        );
1209        assert_eq!(
1210            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1211            "jkl"
1212        );
1213        assert_eq!(
1214            project.read_with(cx, |project, _| project.agent_location()),
1215            Some(AgentLocation {
1216                buffer: buffer.downgrade(),
1217                position: language::Anchor::MAX
1218            })
1219        );
1220
1221        chunks_tx.unbounded_send("mno\n").unwrap();
1222        cx.run_until_parked();
1223        assert_matches!(
1224            drain_events(&mut events).as_slice(),
1225            [EditAgentOutputEvent::Edited { .. }]
1226        );
1227        assert_eq!(
1228            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1229            "jkl\nmno"
1230        );
1231        assert_eq!(
1232            project.read_with(cx, |project, _| project.agent_location()),
1233            Some(AgentLocation {
1234                buffer: buffer.downgrade(),
1235                position: language::Anchor::MAX
1236            })
1237        );
1238
1239        chunks_tx.unbounded_send("pqr\n```").unwrap();
1240        cx.run_until_parked();
1241        assert_matches!(
1242            drain_events(&mut events).as_slice(),
1243            [EditAgentOutputEvent::Edited(_)],
1244        );
1245        assert_eq!(
1246            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1247            "jkl\nmno\npqr"
1248        );
1249        assert_eq!(
1250            project.read_with(cx, |project, _| project.agent_location()),
1251            Some(AgentLocation {
1252                buffer: buffer.downgrade(),
1253                position: language::Anchor::MAX
1254            })
1255        );
1256
1257        drop(chunks_tx);
1258        apply.await.unwrap();
1259        assert_eq!(
1260            buffer.read_with(cx, |buffer, _| buffer.snapshot().text()),
1261            "jkl\nmno\npqr"
1262        );
1263        assert_eq!(drain_events(&mut events), vec![]);
1264        assert_eq!(
1265            project.read_with(cx, |project, _| project.agent_location()),
1266            Some(AgentLocation {
1267                buffer: buffer.downgrade(),
1268                position: language::Anchor::MAX
1269            })
1270        );
1271    }
1272
1273    #[gpui::test(iterations = 100)]
1274    async fn test_indent_new_text_chunks(mut rng: StdRng) {
1275        let chunks = to_random_chunks(&mut rng, "    abc\n  def\n      ghi");
1276        let new_text_chunks = stream::iter(chunks.iter().enumerate().map(|(index, chunk)| {
1277            Ok(EditParserEvent::NewTextChunk {
1278                chunk: chunk.clone(),
1279                done: index == chunks.len() - 1,
1280            })
1281        }));
1282        let indented_chunks =
1283            EditAgent::reindent_new_text_chunks(IndentDelta::Spaces(2), new_text_chunks)
1284                .collect::<Vec<_>>()
1285                .await;
1286        let new_text = indented_chunks
1287            .into_iter()
1288            .collect::<Result<String>>()
1289            .unwrap();
1290        assert_eq!(new_text, "      abc\n    def\n        ghi");
1291    }
1292
1293    #[gpui::test(iterations = 100)]
1294    async fn test_outdent_new_text_chunks(mut rng: StdRng) {
1295        let chunks = to_random_chunks(&mut rng, "\t\t\t\tabc\n\t\tdef\n\t\t\t\t\t\tghi");
1296        let new_text_chunks = stream::iter(chunks.iter().enumerate().map(|(index, chunk)| {
1297            Ok(EditParserEvent::NewTextChunk {
1298                chunk: chunk.clone(),
1299                done: index == chunks.len() - 1,
1300            })
1301        }));
1302        let indented_chunks =
1303            EditAgent::reindent_new_text_chunks(IndentDelta::Tabs(-2), new_text_chunks)
1304                .collect::<Vec<_>>()
1305                .await;
1306        let new_text = indented_chunks
1307            .into_iter()
1308            .collect::<Result<String>>()
1309            .unwrap();
1310        assert_eq!(new_text, "\t\tabc\ndef\n\t\t\t\tghi");
1311    }
1312
1313    #[gpui::test(iterations = 100)]
1314    async fn test_random_indents(mut rng: StdRng) {
1315        let len = rng.gen_range(1..=100);
1316        let new_text = util::RandomCharIter::new(&mut rng)
1317            .with_simple_text()
1318            .take(len)
1319            .collect::<String>();
1320        let new_text = new_text
1321            .split('\n')
1322            .map(|line| format!("{}{}", " ".repeat(rng.gen_range(0..=8)), line))
1323            .collect::<Vec<_>>()
1324            .join("\n");
1325        let delta = IndentDelta::Spaces(rng.gen_range(-4..=4));
1326
1327        let chunks = to_random_chunks(&mut rng, &new_text);
1328        let new_text_chunks = stream::iter(chunks.iter().enumerate().map(|(index, chunk)| {
1329            Ok(EditParserEvent::NewTextChunk {
1330                chunk: chunk.clone(),
1331                done: index == chunks.len() - 1,
1332            })
1333        }));
1334        let reindented_chunks = EditAgent::reindent_new_text_chunks(delta, new_text_chunks)
1335            .collect::<Vec<_>>()
1336            .await;
1337        let actual_reindented_text = reindented_chunks
1338            .into_iter()
1339            .collect::<Result<String>>()
1340            .unwrap();
1341        let expected_reindented_text = new_text
1342            .split('\n')
1343            .map(|line| {
1344                if let Some(ix) = line.find(|c| c != ' ') {
1345                    let new_indent = cmp::max(0, ix as isize + delta.len()) as usize;
1346                    format!("{}{}", " ".repeat(new_indent), &line[ix..])
1347                } else {
1348                    line.to_string()
1349                }
1350            })
1351            .collect::<Vec<_>>()
1352            .join("\n");
1353        assert_eq!(actual_reindented_text, expected_reindented_text);
1354    }
1355
1356    fn to_random_chunks(rng: &mut StdRng, input: &str) -> Vec<String> {
1357        let chunk_count = rng.gen_range(1..=cmp::min(input.len(), 50));
1358        let mut chunk_indices = (0..input.len()).choose_multiple(rng, chunk_count);
1359        chunk_indices.sort();
1360        chunk_indices.push(input.len());
1361
1362        let mut chunks = Vec::new();
1363        let mut last_ix = 0;
1364        for chunk_ix in chunk_indices {
1365            chunks.push(input[last_ix..chunk_ix].to_string());
1366            last_ix = chunk_ix;
1367        }
1368        chunks
1369    }
1370
1371    fn simulate_llm_output(
1372        agent: &EditAgent,
1373        output: &str,
1374        rng: &mut StdRng,
1375        cx: &mut TestAppContext,
1376    ) {
1377        let executor = cx.executor();
1378        let chunks = to_random_chunks(rng, output);
1379        let model = agent.model.clone();
1380        cx.background_spawn(async move {
1381            for chunk in chunks {
1382                executor.simulate_random_delay().await;
1383                model
1384                    .as_fake()
1385                    .send_last_completion_stream_text_chunk(chunk);
1386            }
1387            model.as_fake().end_last_completion_stream();
1388        })
1389        .detach();
1390    }
1391
1392    async fn init_test(cx: &mut TestAppContext) -> EditAgent {
1393        cx.update(settings::init);
1394        cx.update(Project::init_settings);
1395        let project = Project::test(FakeFs::new(cx.executor()), [], cx).await;
1396        let model = Arc::new(FakeLanguageModel::default());
1397        let action_log = cx.new(|_| ActionLog::new(project.clone()));
1398        EditAgent::new(
1399            model,
1400            project,
1401            action_log,
1402            Templates::new(),
1403            EditFormat::XmlTags,
1404        )
1405    }
1406
1407    #[gpui::test(iterations = 10)]
1408    async fn test_non_unique_text_error(cx: &mut TestAppContext, mut rng: StdRng) {
1409        let agent = init_test(cx).await;
1410        let original_text = indoc! {"
1411                function foo() {
1412                    return 42;
1413                }
1414
1415                function bar() {
1416                    return 42;
1417                }
1418
1419                function baz() {
1420                    return 42;
1421                }
1422            "};
1423        let buffer = cx.new(|cx| Buffer::local(original_text, cx));
1424        let (apply, mut events) = agent.edit(
1425            buffer.clone(),
1426            String::new(),
1427            &LanguageModelRequest::default(),
1428            &mut cx.to_async(),
1429        );
1430        cx.run_until_parked();
1431
1432        // When <old_text> matches text in more than one place
1433        simulate_llm_output(
1434            &agent,
1435            indoc! {"
1436                <old_text>
1437                    return 42;
1438                }
1439                </old_text>
1440                <new_text>
1441                    return 100;
1442                }
1443                </new_text>
1444            "},
1445            &mut rng,
1446            cx,
1447        );
1448        apply.await.unwrap();
1449
1450        // Then the text should remain unchanged
1451        let result_text = buffer.read_with(cx, |buffer, _| buffer.snapshot().text());
1452        assert_eq!(
1453            result_text,
1454            indoc! {"
1455                function foo() {
1456                    return 42;
1457                }
1458
1459                function bar() {
1460                    return 42;
1461                }
1462
1463                function baz() {
1464                    return 42;
1465                }
1466            "},
1467            "Text should remain unchanged when there are multiple matches"
1468        );
1469
1470        // And AmbiguousEditRange even should be emitted
1471        let events = drain_events(&mut events);
1472        let ambiguous_ranges = vec![2..3, 6..7, 10..11];
1473        assert!(
1474            events.contains(&EditAgentOutputEvent::AmbiguousEditRange(ambiguous_ranges)),
1475            "Should emit AmbiguousEditRange for non-unique text"
1476        );
1477    }
1478
1479    fn drain_events(
1480        stream: &mut UnboundedReceiver<EditAgentOutputEvent>,
1481    ) -> Vec<EditAgentOutputEvent> {
1482        let mut events = Vec::new();
1483        while let Ok(Some(event)) = stream.try_next() {
1484            events.push(event);
1485        }
1486        events
1487    }
1488}