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