acp_thread.rs

   1mod connection;
   2mod diff;
   3mod mention;
   4mod terminal;
   5
   6/// Key used in ACP ToolCall meta to store the tool's programmatic name.
   7/// This is a workaround since ACP's ToolCall doesn't have a dedicated name field.
   8pub const TOOL_NAME_META_KEY: &str = "tool_name";
   9
  10/// Key used in ACP ToolCall meta to store the session id when a subagent is spawned.
  11pub const SUBAGENT_SESSION_ID_META_KEY: &str = "subagent_session_id";
  12
  13/// Helper to extract tool name from ACP meta
  14pub fn tool_name_from_meta(meta: &Option<acp::Meta>) -> Option<SharedString> {
  15    meta.as_ref()
  16        .and_then(|m| m.get(TOOL_NAME_META_KEY))
  17        .and_then(|v| v.as_str())
  18        .map(|s| SharedString::from(s.to_owned()))
  19}
  20
  21/// Helper to extract subagent session id from ACP meta
  22pub fn subagent_session_id_from_meta(meta: &Option<acp::Meta>) -> Option<acp::SessionId> {
  23    meta.as_ref()
  24        .and_then(|m| m.get(SUBAGENT_SESSION_ID_META_KEY))
  25        .and_then(|v| v.as_str())
  26        .map(|s| acp::SessionId::from(s.to_string()))
  27}
  28
  29/// Helper to create meta with tool name
  30pub fn meta_with_tool_name(tool_name: &str) -> acp::Meta {
  31    acp::Meta::from_iter([(TOOL_NAME_META_KEY.into(), tool_name.into())])
  32}
  33use collections::HashSet;
  34pub use connection::*;
  35pub use diff::*;
  36use language::language_settings::FormatOnSave;
  37pub use mention::*;
  38use project::lsp_store::{FormatTrigger, LspFormatTarget};
  39use serde::{Deserialize, Serialize};
  40use serde_json::to_string_pretty;
  41
  42use task::{Shell, ShellBuilder};
  43pub use terminal::*;
  44
  45use action_log::{ActionLog, ActionLogTelemetry};
  46use agent_client_protocol::{self as acp};
  47use anyhow::{Context as _, Result, anyhow};
  48use futures::{FutureExt, channel::oneshot, future::BoxFuture};
  49use gpui::{AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Task, WeakEntity};
  50use itertools::Itertools;
  51use language::{Anchor, Buffer, BufferSnapshot, LanguageRegistry, Point, ToPoint, text_diff};
  52use markdown::Markdown;
  53use project::{AgentLocation, Project, git_store::GitStoreCheckpoint};
  54use std::collections::HashMap;
  55use std::error::Error;
  56use std::fmt::{Formatter, Write};
  57use std::ops::Range;
  58use std::process::ExitStatus;
  59use std::rc::Rc;
  60use std::time::{Duration, Instant};
  61use std::{fmt::Display, mem, path::PathBuf, sync::Arc};
  62use text::Bias;
  63use ui::App;
  64use util::{ResultExt, get_default_system_shell_preferring_bash, paths::PathStyle};
  65use uuid::Uuid;
  66
  67#[derive(Debug)]
  68pub struct UserMessage {
  69    pub id: Option<UserMessageId>,
  70    pub content: ContentBlock,
  71    pub chunks: Vec<acp::ContentBlock>,
  72    pub checkpoint: Option<Checkpoint>,
  73    pub indented: bool,
  74}
  75
  76#[derive(Debug)]
  77pub struct Checkpoint {
  78    git_checkpoint: GitStoreCheckpoint,
  79    pub show: bool,
  80}
  81
  82impl UserMessage {
  83    fn to_markdown(&self, cx: &App) -> String {
  84        let mut markdown = String::new();
  85        if self
  86            .checkpoint
  87            .as_ref()
  88            .is_some_and(|checkpoint| checkpoint.show)
  89        {
  90            writeln!(markdown, "## User (checkpoint)").unwrap();
  91        } else {
  92            writeln!(markdown, "## User").unwrap();
  93        }
  94        writeln!(markdown).unwrap();
  95        writeln!(markdown, "{}", self.content.to_markdown(cx)).unwrap();
  96        writeln!(markdown).unwrap();
  97        markdown
  98    }
  99}
 100
 101#[derive(Debug, PartialEq)]
 102pub struct AssistantMessage {
 103    pub chunks: Vec<AssistantMessageChunk>,
 104    pub indented: bool,
 105}
 106
 107impl AssistantMessage {
 108    pub fn to_markdown(&self, cx: &App) -> String {
 109        format!(
 110            "## Assistant\n\n{}\n\n",
 111            self.chunks
 112                .iter()
 113                .map(|chunk| chunk.to_markdown(cx))
 114                .join("\n\n")
 115        )
 116    }
 117}
 118
 119#[derive(Debug, PartialEq)]
 120pub enum AssistantMessageChunk {
 121    Message { block: ContentBlock },
 122    Thought { block: ContentBlock },
 123}
 124
 125impl AssistantMessageChunk {
 126    pub fn from_str(
 127        chunk: &str,
 128        language_registry: &Arc<LanguageRegistry>,
 129        path_style: PathStyle,
 130        cx: &mut App,
 131    ) -> Self {
 132        Self::Message {
 133            block: ContentBlock::new(chunk.into(), language_registry, path_style, cx),
 134        }
 135    }
 136
 137    fn to_markdown(&self, cx: &App) -> String {
 138        match self {
 139            Self::Message { block } => block.to_markdown(cx).to_string(),
 140            Self::Thought { block } => {
 141                format!("<thinking>\n{}\n</thinking>", block.to_markdown(cx))
 142            }
 143        }
 144    }
 145}
 146
 147#[derive(Debug)]
 148pub enum AgentThreadEntry {
 149    UserMessage(UserMessage),
 150    AssistantMessage(AssistantMessage),
 151    ToolCall(ToolCall),
 152}
 153
 154impl AgentThreadEntry {
 155    pub fn is_indented(&self) -> bool {
 156        match self {
 157            Self::UserMessage(message) => message.indented,
 158            Self::AssistantMessage(message) => message.indented,
 159            Self::ToolCall(_) => false,
 160        }
 161    }
 162
 163    pub fn to_markdown(&self, cx: &App) -> String {
 164        match self {
 165            Self::UserMessage(message) => message.to_markdown(cx),
 166            Self::AssistantMessage(message) => message.to_markdown(cx),
 167            Self::ToolCall(tool_call) => tool_call.to_markdown(cx),
 168        }
 169    }
 170
 171    pub fn user_message(&self) -> Option<&UserMessage> {
 172        if let AgentThreadEntry::UserMessage(message) = self {
 173            Some(message)
 174        } else {
 175            None
 176        }
 177    }
 178
 179    pub fn diffs(&self) -> impl Iterator<Item = &Entity<Diff>> {
 180        if let AgentThreadEntry::ToolCall(call) = self {
 181            itertools::Either::Left(call.diffs())
 182        } else {
 183            itertools::Either::Right(std::iter::empty())
 184        }
 185    }
 186
 187    pub fn terminals(&self) -> impl Iterator<Item = &Entity<Terminal>> {
 188        if let AgentThreadEntry::ToolCall(call) = self {
 189            itertools::Either::Left(call.terminals())
 190        } else {
 191            itertools::Either::Right(std::iter::empty())
 192        }
 193    }
 194
 195    pub fn location(&self, ix: usize) -> Option<(acp::ToolCallLocation, AgentLocation)> {
 196        if let AgentThreadEntry::ToolCall(ToolCall {
 197            locations,
 198            resolved_locations,
 199            ..
 200        }) = self
 201        {
 202            Some((
 203                locations.get(ix)?.clone(),
 204                resolved_locations.get(ix)?.clone()?,
 205            ))
 206        } else {
 207            None
 208        }
 209    }
 210}
 211
 212#[derive(Debug)]
 213pub struct ToolCall {
 214    pub id: acp::ToolCallId,
 215    pub label: Entity<Markdown>,
 216    pub kind: acp::ToolKind,
 217    pub content: Vec<ToolCallContent>,
 218    pub status: ToolCallStatus,
 219    pub locations: Vec<acp::ToolCallLocation>,
 220    pub resolved_locations: Vec<Option<AgentLocation>>,
 221    pub raw_input: Option<serde_json::Value>,
 222    pub raw_input_markdown: Option<Entity<Markdown>>,
 223    pub raw_output: Option<serde_json::Value>,
 224    pub tool_name: Option<SharedString>,
 225    pub subagent_session_id: Option<acp::SessionId>,
 226}
 227
 228impl ToolCall {
 229    fn from_acp(
 230        tool_call: acp::ToolCall,
 231        status: ToolCallStatus,
 232        language_registry: Arc<LanguageRegistry>,
 233        path_style: PathStyle,
 234        terminals: &HashMap<acp::TerminalId, Entity<Terminal>>,
 235        cx: &mut App,
 236    ) -> Result<Self> {
 237        let title = if tool_call.kind == acp::ToolKind::Execute {
 238            tool_call.title
 239        } else if let Some((first_line, _)) = tool_call.title.split_once("\n") {
 240            first_line.to_owned() + ""
 241        } else {
 242            tool_call.title
 243        };
 244        let mut content = Vec::with_capacity(tool_call.content.len());
 245        for item in tool_call.content {
 246            if let Some(item) = ToolCallContent::from_acp(
 247                item,
 248                language_registry.clone(),
 249                path_style,
 250                terminals,
 251                cx,
 252            )? {
 253                content.push(item);
 254            }
 255        }
 256
 257        let raw_input_markdown = tool_call
 258            .raw_input
 259            .as_ref()
 260            .and_then(|input| markdown_for_raw_output(input, &language_registry, cx));
 261
 262        let tool_name = tool_name_from_meta(&tool_call.meta);
 263
 264        let subagent_session = subagent_session_id_from_meta(&tool_call.meta);
 265
 266        let result = Self {
 267            id: tool_call.tool_call_id,
 268            label: cx
 269                .new(|cx| Markdown::new(title.into(), Some(language_registry.clone()), None, cx)),
 270            kind: tool_call.kind,
 271            content,
 272            locations: tool_call.locations,
 273            resolved_locations: Vec::default(),
 274            status,
 275            raw_input: tool_call.raw_input,
 276            raw_input_markdown,
 277            raw_output: tool_call.raw_output,
 278            tool_name,
 279            subagent_session_id: subagent_session,
 280        };
 281        Ok(result)
 282    }
 283
 284    fn update_fields(
 285        &mut self,
 286        fields: acp::ToolCallUpdateFields,
 287        meta: Option<acp::Meta>,
 288        language_registry: Arc<LanguageRegistry>,
 289        path_style: PathStyle,
 290        terminals: &HashMap<acp::TerminalId, Entity<Terminal>>,
 291        cx: &mut App,
 292    ) -> Result<()> {
 293        let acp::ToolCallUpdateFields {
 294            kind,
 295            status,
 296            title,
 297            content,
 298            locations,
 299            raw_input,
 300            raw_output,
 301            ..
 302        } = fields;
 303
 304        if let Some(kind) = kind {
 305            self.kind = kind;
 306        }
 307
 308        if let Some(status) = status {
 309            self.status = status.into();
 310        }
 311
 312        if let Some(subagent_session_id) = subagent_session_id_from_meta(&meta) {
 313            self.subagent_session_id = Some(subagent_session_id);
 314        }
 315
 316        if let Some(title) = title {
 317            if self.kind == acp::ToolKind::Execute {
 318                for terminal in self.terminals() {
 319                    terminal.update(cx, |terminal, cx| {
 320                        terminal.update_command_label(&title, cx);
 321                    });
 322                }
 323            }
 324            self.label.update(cx, |label, cx| {
 325                if self.kind == acp::ToolKind::Execute {
 326                    label.replace(title, cx);
 327                } else if let Some((first_line, _)) = title.split_once("\n") {
 328                    label.replace(first_line.to_owned() + "", cx);
 329                } else {
 330                    label.replace(title, cx);
 331                }
 332            });
 333        }
 334
 335        if let Some(content) = content {
 336            let mut new_content_len = content.len();
 337            let mut content = content.into_iter();
 338
 339            // Reuse existing content if we can
 340            for (old, new) in self.content.iter_mut().zip(content.by_ref()) {
 341                let valid_content =
 342                    old.update_from_acp(new, language_registry.clone(), path_style, terminals, cx)?;
 343                if !valid_content {
 344                    new_content_len -= 1;
 345                }
 346            }
 347            for new in content {
 348                if let Some(new) = ToolCallContent::from_acp(
 349                    new,
 350                    language_registry.clone(),
 351                    path_style,
 352                    terminals,
 353                    cx,
 354                )? {
 355                    self.content.push(new);
 356                } else {
 357                    new_content_len -= 1;
 358                }
 359            }
 360            self.content.truncate(new_content_len);
 361        }
 362
 363        if let Some(locations) = locations {
 364            self.locations = locations;
 365        }
 366
 367        if let Some(raw_input) = raw_input {
 368            self.raw_input_markdown = markdown_for_raw_output(&raw_input, &language_registry, cx);
 369            self.raw_input = Some(raw_input);
 370        }
 371
 372        if let Some(raw_output) = raw_output {
 373            if self.content.is_empty()
 374                && let Some(markdown) = markdown_for_raw_output(&raw_output, &language_registry, cx)
 375            {
 376                self.content
 377                    .push(ToolCallContent::ContentBlock(ContentBlock::Markdown {
 378                        markdown,
 379                    }));
 380            }
 381            self.raw_output = Some(raw_output);
 382        }
 383        Ok(())
 384    }
 385
 386    pub fn diffs(&self) -> impl Iterator<Item = &Entity<Diff>> {
 387        self.content.iter().filter_map(|content| match content {
 388            ToolCallContent::Diff(diff) => Some(diff),
 389            ToolCallContent::ContentBlock(_) => None,
 390            ToolCallContent::Terminal(_) => None,
 391        })
 392    }
 393
 394    pub fn terminals(&self) -> impl Iterator<Item = &Entity<Terminal>> {
 395        self.content.iter().filter_map(|content| match content {
 396            ToolCallContent::Terminal(terminal) => Some(terminal),
 397            ToolCallContent::ContentBlock(_) => None,
 398            ToolCallContent::Diff(_) => None,
 399        })
 400    }
 401
 402    pub fn is_subagent(&self) -> bool {
 403        self.tool_name.as_ref().is_some_and(|s| s == "subagent")
 404            || self.subagent_session_id.is_some()
 405    }
 406
 407    pub fn to_markdown(&self, cx: &App) -> String {
 408        let mut markdown = format!(
 409            "**Tool Call: {}**\nStatus: {}\n\n",
 410            self.label.read(cx).source(),
 411            self.status
 412        );
 413        for content in &self.content {
 414            markdown.push_str(content.to_markdown(cx).as_str());
 415            markdown.push_str("\n\n");
 416        }
 417        markdown
 418    }
 419
 420    async fn resolve_location(
 421        location: acp::ToolCallLocation,
 422        project: WeakEntity<Project>,
 423        cx: &mut AsyncApp,
 424    ) -> Option<ResolvedLocation> {
 425        let buffer = project
 426            .update(cx, |project, cx| {
 427                project
 428                    .project_path_for_absolute_path(&location.path, cx)
 429                    .map(|path| project.open_buffer(path, cx))
 430            })
 431            .ok()??;
 432        let buffer = buffer.await.log_err()?;
 433        let position = buffer.update(cx, |buffer, _| {
 434            let snapshot = buffer.snapshot();
 435            if let Some(row) = location.line {
 436                let column = snapshot.indent_size_for_line(row).len;
 437                let point = snapshot.clip_point(Point::new(row, column), Bias::Left);
 438                snapshot.anchor_before(point)
 439            } else {
 440                Anchor::min_for_buffer(snapshot.remote_id())
 441            }
 442        });
 443
 444        Some(ResolvedLocation { buffer, position })
 445    }
 446
 447    fn resolve_locations(
 448        &self,
 449        project: Entity<Project>,
 450        cx: &mut App,
 451    ) -> Task<Vec<Option<ResolvedLocation>>> {
 452        let locations = self.locations.clone();
 453        project.update(cx, |_, cx| {
 454            cx.spawn(async move |project, cx| {
 455                let mut new_locations = Vec::new();
 456                for location in locations {
 457                    new_locations.push(Self::resolve_location(location, project.clone(), cx).await);
 458                }
 459                new_locations
 460            })
 461        })
 462    }
 463}
 464
 465// Separate so we can hold a strong reference to the buffer
 466// for saving on the thread
 467#[derive(Clone, Debug, PartialEq, Eq)]
 468struct ResolvedLocation {
 469    buffer: Entity<Buffer>,
 470    position: Anchor,
 471}
 472
 473impl From<&ResolvedLocation> for AgentLocation {
 474    fn from(value: &ResolvedLocation) -> Self {
 475        Self {
 476            buffer: value.buffer.downgrade(),
 477            position: value.position,
 478        }
 479    }
 480}
 481
 482#[derive(Debug)]
 483pub enum ToolCallStatus {
 484    /// The tool call hasn't started running yet, but we start showing it to
 485    /// the user.
 486    Pending,
 487    /// The tool call is waiting for confirmation from the user.
 488    WaitingForConfirmation {
 489        options: PermissionOptions,
 490        respond_tx: oneshot::Sender<acp::PermissionOptionId>,
 491    },
 492    /// The tool call is currently running.
 493    InProgress,
 494    /// The tool call completed successfully.
 495    Completed,
 496    /// The tool call failed.
 497    Failed,
 498    /// The user rejected the tool call.
 499    Rejected,
 500    /// The user canceled generation so the tool call was canceled.
 501    Canceled,
 502}
 503
 504impl From<acp::ToolCallStatus> for ToolCallStatus {
 505    fn from(status: acp::ToolCallStatus) -> Self {
 506        match status {
 507            acp::ToolCallStatus::Pending => Self::Pending,
 508            acp::ToolCallStatus::InProgress => Self::InProgress,
 509            acp::ToolCallStatus::Completed => Self::Completed,
 510            acp::ToolCallStatus::Failed => Self::Failed,
 511            _ => Self::Pending,
 512        }
 513    }
 514}
 515
 516impl Display for ToolCallStatus {
 517    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 518        write!(
 519            f,
 520            "{}",
 521            match self {
 522                ToolCallStatus::Pending => "Pending",
 523                ToolCallStatus::WaitingForConfirmation { .. } => "Waiting for confirmation",
 524                ToolCallStatus::InProgress => "In Progress",
 525                ToolCallStatus::Completed => "Completed",
 526                ToolCallStatus::Failed => "Failed",
 527                ToolCallStatus::Rejected => "Rejected",
 528                ToolCallStatus::Canceled => "Canceled",
 529            }
 530        )
 531    }
 532}
 533
 534#[derive(Debug, PartialEq, Clone)]
 535pub enum ContentBlock {
 536    Empty,
 537    Markdown { markdown: Entity<Markdown> },
 538    ResourceLink { resource_link: acp::ResourceLink },
 539    Image { image: Arc<gpui::Image> },
 540}
 541
 542impl ContentBlock {
 543    pub fn new(
 544        block: acp::ContentBlock,
 545        language_registry: &Arc<LanguageRegistry>,
 546        path_style: PathStyle,
 547        cx: &mut App,
 548    ) -> Self {
 549        let mut this = Self::Empty;
 550        this.append(block, language_registry, path_style, cx);
 551        this
 552    }
 553
 554    pub fn new_combined(
 555        blocks: impl IntoIterator<Item = acp::ContentBlock>,
 556        language_registry: Arc<LanguageRegistry>,
 557        path_style: PathStyle,
 558        cx: &mut App,
 559    ) -> Self {
 560        let mut this = Self::Empty;
 561        for block in blocks {
 562            this.append(block, &language_registry, path_style, cx);
 563        }
 564        this
 565    }
 566
 567    pub fn append(
 568        &mut self,
 569        block: acp::ContentBlock,
 570        language_registry: &Arc<LanguageRegistry>,
 571        path_style: PathStyle,
 572        cx: &mut App,
 573    ) {
 574        match (&mut *self, &block) {
 575            (ContentBlock::Empty, acp::ContentBlock::ResourceLink(resource_link)) => {
 576                *self = ContentBlock::ResourceLink {
 577                    resource_link: resource_link.clone(),
 578                };
 579            }
 580            (ContentBlock::Empty, acp::ContentBlock::Image(image_content)) => {
 581                if let Some(image) = Self::decode_image(image_content) {
 582                    *self = ContentBlock::Image { image };
 583                } else {
 584                    let new_content = Self::image_md(image_content);
 585                    *self = Self::create_markdown_block(new_content, language_registry, cx);
 586                }
 587            }
 588            (ContentBlock::Empty, _) => {
 589                let new_content = Self::block_string_contents(&block, path_style);
 590                *self = Self::create_markdown_block(new_content, language_registry, cx);
 591            }
 592            (ContentBlock::Markdown { markdown }, _) => {
 593                let new_content = Self::block_string_contents(&block, path_style);
 594                markdown.update(cx, |markdown, cx| markdown.append(&new_content, cx));
 595            }
 596            (ContentBlock::ResourceLink { resource_link }, _) => {
 597                let existing_content = Self::resource_link_md(&resource_link.uri, path_style);
 598                let new_content = Self::block_string_contents(&block, path_style);
 599                let combined = format!("{}\n{}", existing_content, new_content);
 600                *self = Self::create_markdown_block(combined, language_registry, cx);
 601            }
 602            (ContentBlock::Image { .. }, _) => {
 603                let new_content = Self::block_string_contents(&block, path_style);
 604                let combined = format!("`Image`\n{}", new_content);
 605                *self = Self::create_markdown_block(combined, language_registry, cx);
 606            }
 607        }
 608    }
 609
 610    fn decode_image(image_content: &acp::ImageContent) -> Option<Arc<gpui::Image>> {
 611        use base64::Engine as _;
 612
 613        let bytes = base64::engine::general_purpose::STANDARD
 614            .decode(image_content.data.as_bytes())
 615            .ok()?;
 616        let format = gpui::ImageFormat::from_mime_type(&image_content.mime_type)?;
 617        Some(Arc::new(gpui::Image::from_bytes(format, bytes)))
 618    }
 619
 620    fn create_markdown_block(
 621        content: String,
 622        language_registry: &Arc<LanguageRegistry>,
 623        cx: &mut App,
 624    ) -> ContentBlock {
 625        ContentBlock::Markdown {
 626            markdown: cx
 627                .new(|cx| Markdown::new(content.into(), Some(language_registry.clone()), None, cx)),
 628        }
 629    }
 630
 631    fn block_string_contents(block: &acp::ContentBlock, path_style: PathStyle) -> String {
 632        match block {
 633            acp::ContentBlock::Text(text_content) => text_content.text.clone(),
 634            acp::ContentBlock::ResourceLink(resource_link) => {
 635                Self::resource_link_md(&resource_link.uri, path_style)
 636            }
 637            acp::ContentBlock::Resource(acp::EmbeddedResource {
 638                resource:
 639                    acp::EmbeddedResourceResource::TextResourceContents(acp::TextResourceContents {
 640                        uri,
 641                        ..
 642                    }),
 643                ..
 644            }) => Self::resource_link_md(uri, path_style),
 645            acp::ContentBlock::Image(image) => Self::image_md(image),
 646            _ => String::new(),
 647        }
 648    }
 649
 650    fn resource_link_md(uri: &str, path_style: PathStyle) -> String {
 651        if let Some(uri) = MentionUri::parse(uri, path_style).log_err() {
 652            uri.as_link().to_string()
 653        } else {
 654            uri.to_string()
 655        }
 656    }
 657
 658    fn image_md(_image: &acp::ImageContent) -> String {
 659        "`Image`".into()
 660    }
 661
 662    pub fn to_markdown<'a>(&'a self, cx: &'a App) -> &'a str {
 663        match self {
 664            ContentBlock::Empty => "",
 665            ContentBlock::Markdown { markdown } => markdown.read(cx).source(),
 666            ContentBlock::ResourceLink { resource_link } => &resource_link.uri,
 667            ContentBlock::Image { .. } => "`Image`",
 668        }
 669    }
 670
 671    pub fn markdown(&self) -> Option<&Entity<Markdown>> {
 672        match self {
 673            ContentBlock::Empty => None,
 674            ContentBlock::Markdown { markdown } => Some(markdown),
 675            ContentBlock::ResourceLink { .. } => None,
 676            ContentBlock::Image { .. } => None,
 677        }
 678    }
 679
 680    pub fn resource_link(&self) -> Option<&acp::ResourceLink> {
 681        match self {
 682            ContentBlock::ResourceLink { resource_link } => Some(resource_link),
 683            _ => None,
 684        }
 685    }
 686
 687    pub fn image(&self) -> Option<&Arc<gpui::Image>> {
 688        match self {
 689            ContentBlock::Image { image } => Some(image),
 690            _ => None,
 691        }
 692    }
 693}
 694
 695#[derive(Debug)]
 696pub enum ToolCallContent {
 697    ContentBlock(ContentBlock),
 698    Diff(Entity<Diff>),
 699    Terminal(Entity<Terminal>),
 700}
 701
 702impl ToolCallContent {
 703    pub fn from_acp(
 704        content: acp::ToolCallContent,
 705        language_registry: Arc<LanguageRegistry>,
 706        path_style: PathStyle,
 707        terminals: &HashMap<acp::TerminalId, Entity<Terminal>>,
 708        cx: &mut App,
 709    ) -> Result<Option<Self>> {
 710        match content {
 711            acp::ToolCallContent::Content(acp::Content { content, .. }) => {
 712                Ok(Some(Self::ContentBlock(ContentBlock::new(
 713                    content,
 714                    &language_registry,
 715                    path_style,
 716                    cx,
 717                ))))
 718            }
 719            acp::ToolCallContent::Diff(diff) => Ok(Some(Self::Diff(cx.new(|cx| {
 720                Diff::finalized(
 721                    diff.path.to_string_lossy().into_owned(),
 722                    diff.old_text,
 723                    diff.new_text,
 724                    language_registry,
 725                    cx,
 726                )
 727            })))),
 728            acp::ToolCallContent::Terminal(acp::Terminal { terminal_id, .. }) => terminals
 729                .get(&terminal_id)
 730                .cloned()
 731                .map(|terminal| Some(Self::Terminal(terminal)))
 732                .ok_or_else(|| anyhow::anyhow!("Terminal with id `{}` not found", terminal_id)),
 733            _ => Ok(None),
 734        }
 735    }
 736
 737    pub fn update_from_acp(
 738        &mut self,
 739        new: acp::ToolCallContent,
 740        language_registry: Arc<LanguageRegistry>,
 741        path_style: PathStyle,
 742        terminals: &HashMap<acp::TerminalId, Entity<Terminal>>,
 743        cx: &mut App,
 744    ) -> Result<bool> {
 745        let needs_update = match (&self, &new) {
 746            (Self::Diff(old_diff), acp::ToolCallContent::Diff(new_diff)) => {
 747                old_diff.read(cx).needs_update(
 748                    new_diff.old_text.as_deref().unwrap_or(""),
 749                    &new_diff.new_text,
 750                    cx,
 751                )
 752            }
 753            _ => true,
 754        };
 755
 756        if let Some(update) = Self::from_acp(new, language_registry, path_style, terminals, cx)? {
 757            if needs_update {
 758                *self = update;
 759            }
 760            Ok(true)
 761        } else {
 762            Ok(false)
 763        }
 764    }
 765
 766    pub fn to_markdown(&self, cx: &App) -> String {
 767        match self {
 768            Self::ContentBlock(content) => content.to_markdown(cx).to_string(),
 769            Self::Diff(diff) => diff.read(cx).to_markdown(cx),
 770            Self::Terminal(terminal) => terminal.read(cx).to_markdown(cx),
 771        }
 772    }
 773
 774    pub fn image(&self) -> Option<&Arc<gpui::Image>> {
 775        match self {
 776            Self::ContentBlock(content) => content.image(),
 777            _ => None,
 778        }
 779    }
 780}
 781
 782#[derive(Debug, PartialEq)]
 783pub enum ToolCallUpdate {
 784    UpdateFields(acp::ToolCallUpdate),
 785    UpdateDiff(ToolCallUpdateDiff),
 786    UpdateTerminal(ToolCallUpdateTerminal),
 787}
 788
 789impl ToolCallUpdate {
 790    fn id(&self) -> &acp::ToolCallId {
 791        match self {
 792            Self::UpdateFields(update) => &update.tool_call_id,
 793            Self::UpdateDiff(diff) => &diff.id,
 794            Self::UpdateTerminal(terminal) => &terminal.id,
 795        }
 796    }
 797}
 798
 799impl From<acp::ToolCallUpdate> for ToolCallUpdate {
 800    fn from(update: acp::ToolCallUpdate) -> Self {
 801        Self::UpdateFields(update)
 802    }
 803}
 804
 805impl From<ToolCallUpdateDiff> for ToolCallUpdate {
 806    fn from(diff: ToolCallUpdateDiff) -> Self {
 807        Self::UpdateDiff(diff)
 808    }
 809}
 810
 811#[derive(Debug, PartialEq)]
 812pub struct ToolCallUpdateDiff {
 813    pub id: acp::ToolCallId,
 814    pub diff: Entity<Diff>,
 815}
 816
 817impl From<ToolCallUpdateTerminal> for ToolCallUpdate {
 818    fn from(terminal: ToolCallUpdateTerminal) -> Self {
 819        Self::UpdateTerminal(terminal)
 820    }
 821}
 822
 823#[derive(Debug, PartialEq)]
 824pub struct ToolCallUpdateTerminal {
 825    pub id: acp::ToolCallId,
 826    pub terminal: Entity<Terminal>,
 827}
 828
 829#[derive(Debug, Default)]
 830pub struct Plan {
 831    pub entries: Vec<PlanEntry>,
 832}
 833
 834#[derive(Debug)]
 835pub struct PlanStats<'a> {
 836    pub in_progress_entry: Option<&'a PlanEntry>,
 837    pub pending: u32,
 838    pub completed: u32,
 839}
 840
 841impl Plan {
 842    pub fn is_empty(&self) -> bool {
 843        self.entries.is_empty()
 844    }
 845
 846    pub fn stats(&self) -> PlanStats<'_> {
 847        let mut stats = PlanStats {
 848            in_progress_entry: None,
 849            pending: 0,
 850            completed: 0,
 851        };
 852
 853        for entry in &self.entries {
 854            match &entry.status {
 855                acp::PlanEntryStatus::Pending => {
 856                    stats.pending += 1;
 857                }
 858                acp::PlanEntryStatus::InProgress => {
 859                    stats.in_progress_entry = stats.in_progress_entry.or(Some(entry));
 860                }
 861                acp::PlanEntryStatus::Completed => {
 862                    stats.completed += 1;
 863                }
 864                _ => {}
 865            }
 866        }
 867
 868        stats
 869    }
 870}
 871
 872#[derive(Debug)]
 873pub struct PlanEntry {
 874    pub content: Entity<Markdown>,
 875    pub priority: acp::PlanEntryPriority,
 876    pub status: acp::PlanEntryStatus,
 877}
 878
 879impl PlanEntry {
 880    pub fn from_acp(entry: acp::PlanEntry, cx: &mut App) -> Self {
 881        Self {
 882            content: cx.new(|cx| Markdown::new(entry.content.into(), None, None, cx)),
 883            priority: entry.priority,
 884            status: entry.status,
 885        }
 886    }
 887}
 888
 889#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 890pub struct TokenUsage {
 891    pub max_tokens: u64,
 892    pub used_tokens: u64,
 893    pub input_tokens: u64,
 894    pub output_tokens: u64,
 895}
 896
 897impl TokenUsage {
 898    pub fn ratio(&self) -> TokenUsageRatio {
 899        #[cfg(debug_assertions)]
 900        let warning_threshold: f32 = std::env::var("ZED_THREAD_WARNING_THRESHOLD")
 901            .unwrap_or("0.8".to_string())
 902            .parse()
 903            .unwrap();
 904        #[cfg(not(debug_assertions))]
 905        let warning_threshold: f32 = 0.8;
 906
 907        // When the maximum is unknown because there is no selected model,
 908        // avoid showing the token limit warning.
 909        if self.max_tokens == 0 {
 910            TokenUsageRatio::Normal
 911        } else if self.used_tokens >= self.max_tokens {
 912            TokenUsageRatio::Exceeded
 913        } else if self.used_tokens as f32 / self.max_tokens as f32 >= warning_threshold {
 914            TokenUsageRatio::Warning
 915        } else {
 916            TokenUsageRatio::Normal
 917        }
 918    }
 919}
 920
 921#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
 922pub enum TokenUsageRatio {
 923    Normal,
 924    Warning,
 925    Exceeded,
 926}
 927
 928#[derive(Debug, Clone)]
 929pub struct RetryStatus {
 930    pub last_error: SharedString,
 931    pub attempt: usize,
 932    pub max_attempts: usize,
 933    pub started_at: Instant,
 934    pub duration: Duration,
 935}
 936
 937pub struct AcpThread {
 938    parent_session_id: Option<acp::SessionId>,
 939    title: SharedString,
 940    entries: Vec<AgentThreadEntry>,
 941    plan: Plan,
 942    project: Entity<Project>,
 943    action_log: Entity<ActionLog>,
 944    shared_buffers: HashMap<Entity<Buffer>, BufferSnapshot>,
 945    send_task: Option<Task<()>>,
 946    connection: Rc<dyn AgentConnection>,
 947    session_id: acp::SessionId,
 948    token_usage: Option<TokenUsage>,
 949    prompt_capabilities: acp::PromptCapabilities,
 950    _observe_prompt_capabilities: Task<anyhow::Result<()>>,
 951    terminals: HashMap<acp::TerminalId, Entity<Terminal>>,
 952    pending_terminal_output: HashMap<acp::TerminalId, Vec<Vec<u8>>>,
 953    pending_terminal_exit: HashMap<acp::TerminalId, acp::TerminalExitStatus>,
 954    // subagent cancellation fields
 955    user_stopped: Arc<std::sync::atomic::AtomicBool>,
 956    user_stop_tx: watch::Sender<bool>,
 957}
 958
 959impl From<&AcpThread> for ActionLogTelemetry {
 960    fn from(value: &AcpThread) -> Self {
 961        Self {
 962            agent_telemetry_id: value.connection().telemetry_id(),
 963            session_id: value.session_id.0.clone(),
 964        }
 965    }
 966}
 967
 968#[derive(Debug)]
 969pub enum AcpThreadEvent {
 970    NewEntry,
 971    TitleUpdated,
 972    TokenUsageUpdated,
 973    EntryUpdated(usize),
 974    EntriesRemoved(Range<usize>),
 975    ToolAuthorizationRequired,
 976    Retry(RetryStatus),
 977    SubagentSpawned(acp::SessionId),
 978    Stopped,
 979    Error,
 980    LoadError(LoadError),
 981    PromptCapabilitiesUpdated,
 982    Refusal,
 983    AvailableCommandsUpdated(Vec<acp::AvailableCommand>),
 984    ModeUpdated(acp::SessionModeId),
 985    ConfigOptionsUpdated(Vec<acp::SessionConfigOption>),
 986}
 987
 988impl EventEmitter<AcpThreadEvent> for AcpThread {}
 989
 990#[derive(Debug, Clone)]
 991pub enum TerminalProviderEvent {
 992    Created {
 993        terminal_id: acp::TerminalId,
 994        label: String,
 995        cwd: Option<PathBuf>,
 996        output_byte_limit: Option<u64>,
 997        terminal: Entity<::terminal::Terminal>,
 998    },
 999    Output {
1000        terminal_id: acp::TerminalId,
1001        data: Vec<u8>,
1002    },
1003    TitleChanged {
1004        terminal_id: acp::TerminalId,
1005        title: String,
1006    },
1007    Exit {
1008        terminal_id: acp::TerminalId,
1009        status: acp::TerminalExitStatus,
1010    },
1011}
1012
1013#[derive(Debug, Clone)]
1014pub enum TerminalProviderCommand {
1015    WriteInput {
1016        terminal_id: acp::TerminalId,
1017        bytes: Vec<u8>,
1018    },
1019    Resize {
1020        terminal_id: acp::TerminalId,
1021        cols: u16,
1022        rows: u16,
1023    },
1024    Close {
1025        terminal_id: acp::TerminalId,
1026    },
1027}
1028
1029impl AcpThread {
1030    pub fn on_terminal_provider_event(
1031        &mut self,
1032        event: TerminalProviderEvent,
1033        cx: &mut Context<Self>,
1034    ) {
1035        match event {
1036            TerminalProviderEvent::Created {
1037                terminal_id,
1038                label,
1039                cwd,
1040                output_byte_limit,
1041                terminal,
1042            } => {
1043                let entity = self.register_terminal_created(
1044                    terminal_id.clone(),
1045                    label,
1046                    cwd,
1047                    output_byte_limit,
1048                    terminal,
1049                    cx,
1050                );
1051
1052                if let Some(mut chunks) = self.pending_terminal_output.remove(&terminal_id) {
1053                    for data in chunks.drain(..) {
1054                        entity.update(cx, |term, cx| {
1055                            term.inner().update(cx, |inner, cx| {
1056                                inner.write_output(&data, cx);
1057                            })
1058                        });
1059                    }
1060                }
1061
1062                if let Some(_status) = self.pending_terminal_exit.remove(&terminal_id) {
1063                    entity.update(cx, |_term, cx| {
1064                        cx.notify();
1065                    });
1066                }
1067
1068                cx.notify();
1069            }
1070            TerminalProviderEvent::Output { terminal_id, data } => {
1071                if let Some(entity) = self.terminals.get(&terminal_id) {
1072                    entity.update(cx, |term, cx| {
1073                        term.inner().update(cx, |inner, cx| {
1074                            inner.write_output(&data, cx);
1075                        })
1076                    });
1077                } else {
1078                    self.pending_terminal_output
1079                        .entry(terminal_id)
1080                        .or_default()
1081                        .push(data);
1082                }
1083            }
1084            TerminalProviderEvent::TitleChanged { terminal_id, title } => {
1085                if let Some(entity) = self.terminals.get(&terminal_id) {
1086                    entity.update(cx, |term, cx| {
1087                        term.inner().update(cx, |inner, cx| {
1088                            inner.breadcrumb_text = title;
1089                            cx.emit(::terminal::Event::BreadcrumbsChanged);
1090                        })
1091                    });
1092                }
1093            }
1094            TerminalProviderEvent::Exit {
1095                terminal_id,
1096                status,
1097            } => {
1098                if let Some(entity) = self.terminals.get(&terminal_id) {
1099                    entity.update(cx, |_term, cx| {
1100                        cx.notify();
1101                    });
1102                } else {
1103                    self.pending_terminal_exit.insert(terminal_id, status);
1104                }
1105            }
1106        }
1107    }
1108}
1109
1110#[derive(PartialEq, Eq, Debug)]
1111pub enum ThreadStatus {
1112    Idle,
1113    Generating,
1114}
1115
1116#[derive(Debug, Clone)]
1117pub enum LoadError {
1118    Unsupported {
1119        command: SharedString,
1120        current_version: SharedString,
1121        minimum_version: SharedString,
1122    },
1123    FailedToInstall(SharedString),
1124    Exited {
1125        status: ExitStatus,
1126    },
1127    Other(SharedString),
1128}
1129
1130impl Display for LoadError {
1131    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1132        match self {
1133            LoadError::Unsupported {
1134                command: path,
1135                current_version,
1136                minimum_version,
1137            } => {
1138                write!(
1139                    f,
1140                    "version {current_version} from {path} is not supported (need at least {minimum_version})"
1141                )
1142            }
1143            LoadError::FailedToInstall(msg) => write!(f, "Failed to install: {msg}"),
1144            LoadError::Exited { status } => write!(f, "Server exited with status {status}"),
1145            LoadError::Other(msg) => write!(f, "{msg}"),
1146        }
1147    }
1148}
1149
1150impl Error for LoadError {}
1151
1152impl AcpThread {
1153    pub fn new(
1154        parent_session_id: Option<acp::SessionId>,
1155        title: impl Into<SharedString>,
1156        connection: Rc<dyn AgentConnection>,
1157        project: Entity<Project>,
1158        action_log: Entity<ActionLog>,
1159        session_id: acp::SessionId,
1160        mut prompt_capabilities_rx: watch::Receiver<acp::PromptCapabilities>,
1161        cx: &mut Context<Self>,
1162    ) -> Self {
1163        let prompt_capabilities = prompt_capabilities_rx.borrow().clone();
1164        let task = cx.spawn::<_, anyhow::Result<()>>(async move |this, cx| {
1165            loop {
1166                let caps = prompt_capabilities_rx.recv().await?;
1167                this.update(cx, |this, cx| {
1168                    this.prompt_capabilities = caps;
1169                    cx.emit(AcpThreadEvent::PromptCapabilitiesUpdated);
1170                })?;
1171            }
1172        });
1173
1174        let (user_stop_tx, _user_stop_rx) = watch::channel(false);
1175
1176        Self {
1177            parent_session_id,
1178            action_log,
1179            shared_buffers: Default::default(),
1180            entries: Default::default(),
1181            plan: Default::default(),
1182            title: title.into(),
1183            project,
1184            send_task: None,
1185            connection,
1186            session_id,
1187            token_usage: None,
1188            prompt_capabilities,
1189            _observe_prompt_capabilities: task,
1190            terminals: HashMap::default(),
1191            pending_terminal_output: HashMap::default(),
1192            pending_terminal_exit: HashMap::default(),
1193            user_stopped: Arc::new(std::sync::atomic::AtomicBool::new(false)),
1194            user_stop_tx,
1195        }
1196    }
1197
1198    pub fn parent_session_id(&self) -> Option<&acp::SessionId> {
1199        self.parent_session_id.as_ref()
1200    }
1201
1202    pub fn prompt_capabilities(&self) -> acp::PromptCapabilities {
1203        self.prompt_capabilities.clone()
1204    }
1205
1206    /// Marks this thread as stopped by user action and signals any listeners.
1207    pub fn stop_by_user(&mut self) {
1208        self.user_stopped
1209            .store(true, std::sync::atomic::Ordering::SeqCst);
1210        self.user_stop_tx.send(true).ok();
1211        self.send_task.take();
1212    }
1213
1214    pub fn was_stopped_by_user(&self) -> bool {
1215        self.user_stopped.load(std::sync::atomic::Ordering::SeqCst)
1216    }
1217
1218    pub fn user_stop_receiver(&self) -> watch::Receiver<bool> {
1219        self.user_stop_tx.receiver()
1220    }
1221
1222    pub fn connection(&self) -> &Rc<dyn AgentConnection> {
1223        &self.connection
1224    }
1225
1226    pub fn action_log(&self) -> &Entity<ActionLog> {
1227        &self.action_log
1228    }
1229
1230    pub fn project(&self) -> &Entity<Project> {
1231        &self.project
1232    }
1233
1234    pub fn title(&self) -> SharedString {
1235        self.title.clone()
1236    }
1237
1238    pub fn entries(&self) -> &[AgentThreadEntry] {
1239        &self.entries
1240    }
1241
1242    pub fn session_id(&self) -> &acp::SessionId {
1243        &self.session_id
1244    }
1245
1246    pub fn status(&self) -> ThreadStatus {
1247        if self.send_task.is_some() {
1248            ThreadStatus::Generating
1249        } else {
1250            ThreadStatus::Idle
1251        }
1252    }
1253
1254    pub fn token_usage(&self) -> Option<&TokenUsage> {
1255        self.token_usage.as_ref()
1256    }
1257
1258    pub fn has_pending_edit_tool_calls(&self) -> bool {
1259        for entry in self.entries.iter().rev() {
1260            match entry {
1261                AgentThreadEntry::UserMessage(_) => return false,
1262                AgentThreadEntry::ToolCall(
1263                    call @ ToolCall {
1264                        status: ToolCallStatus::InProgress | ToolCallStatus::Pending,
1265                        ..
1266                    },
1267                ) if call.diffs().next().is_some() => {
1268                    return true;
1269                }
1270                AgentThreadEntry::ToolCall(_) | AgentThreadEntry::AssistantMessage(_) => {}
1271            }
1272        }
1273
1274        false
1275    }
1276
1277    pub fn has_in_progress_tool_calls(&self) -> bool {
1278        for entry in self.entries.iter().rev() {
1279            match entry {
1280                AgentThreadEntry::UserMessage(_) => return false,
1281                AgentThreadEntry::ToolCall(ToolCall {
1282                    status: ToolCallStatus::InProgress | ToolCallStatus::Pending,
1283                    ..
1284                }) => {
1285                    return true;
1286                }
1287                AgentThreadEntry::ToolCall(_) | AgentThreadEntry::AssistantMessage(_) => {}
1288            }
1289        }
1290
1291        false
1292    }
1293
1294    pub fn used_tools_since_last_user_message(&self) -> bool {
1295        for entry in self.entries.iter().rev() {
1296            match entry {
1297                AgentThreadEntry::UserMessage(..) => return false,
1298                AgentThreadEntry::AssistantMessage(..) => continue,
1299                AgentThreadEntry::ToolCall(..) => return true,
1300            }
1301        }
1302
1303        false
1304    }
1305
1306    pub fn handle_session_update(
1307        &mut self,
1308        update: acp::SessionUpdate,
1309        cx: &mut Context<Self>,
1310    ) -> Result<(), acp::Error> {
1311        match update {
1312            acp::SessionUpdate::UserMessageChunk(acp::ContentChunk { content, .. }) => {
1313                self.push_user_content_block(None, content, cx);
1314            }
1315            acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk { content, .. }) => {
1316                self.push_assistant_content_block(content, false, cx);
1317            }
1318            acp::SessionUpdate::AgentThoughtChunk(acp::ContentChunk { content, .. }) => {
1319                self.push_assistant_content_block(content, true, cx);
1320            }
1321            acp::SessionUpdate::ToolCall(tool_call) => {
1322                self.upsert_tool_call(tool_call, cx)?;
1323            }
1324            acp::SessionUpdate::ToolCallUpdate(tool_call_update) => {
1325                self.update_tool_call(tool_call_update, cx)?;
1326            }
1327            acp::SessionUpdate::Plan(plan) => {
1328                self.update_plan(plan, cx);
1329            }
1330            acp::SessionUpdate::AvailableCommandsUpdate(acp::AvailableCommandsUpdate {
1331                available_commands,
1332                ..
1333            }) => cx.emit(AcpThreadEvent::AvailableCommandsUpdated(available_commands)),
1334            acp::SessionUpdate::CurrentModeUpdate(acp::CurrentModeUpdate {
1335                current_mode_id,
1336                ..
1337            }) => cx.emit(AcpThreadEvent::ModeUpdated(current_mode_id)),
1338            acp::SessionUpdate::ConfigOptionUpdate(acp::ConfigOptionUpdate {
1339                config_options,
1340                ..
1341            }) => cx.emit(AcpThreadEvent::ConfigOptionsUpdated(config_options)),
1342            _ => {}
1343        }
1344        Ok(())
1345    }
1346
1347    pub fn push_user_content_block(
1348        &mut self,
1349        message_id: Option<UserMessageId>,
1350        chunk: acp::ContentBlock,
1351        cx: &mut Context<Self>,
1352    ) {
1353        self.push_user_content_block_with_indent(message_id, chunk, false, cx)
1354    }
1355
1356    pub fn push_user_content_block_with_indent(
1357        &mut self,
1358        message_id: Option<UserMessageId>,
1359        chunk: acp::ContentBlock,
1360        indented: bool,
1361        cx: &mut Context<Self>,
1362    ) {
1363        let language_registry = self.project.read(cx).languages().clone();
1364        let path_style = self.project.read(cx).path_style(cx);
1365        let entries_len = self.entries.len();
1366
1367        if let Some(last_entry) = self.entries.last_mut()
1368            && let AgentThreadEntry::UserMessage(UserMessage {
1369                id,
1370                content,
1371                chunks,
1372                indented: existing_indented,
1373                ..
1374            }) = last_entry
1375            && *existing_indented == indented
1376        {
1377            *id = message_id.or(id.take());
1378            content.append(chunk.clone(), &language_registry, path_style, cx);
1379            chunks.push(chunk);
1380            let idx = entries_len - 1;
1381            cx.emit(AcpThreadEvent::EntryUpdated(idx));
1382        } else {
1383            let content = ContentBlock::new(chunk.clone(), &language_registry, path_style, cx);
1384            self.push_entry(
1385                AgentThreadEntry::UserMessage(UserMessage {
1386                    id: message_id,
1387                    content,
1388                    chunks: vec![chunk],
1389                    checkpoint: None,
1390                    indented,
1391                }),
1392                cx,
1393            );
1394        }
1395    }
1396
1397    pub fn push_assistant_content_block(
1398        &mut self,
1399        chunk: acp::ContentBlock,
1400        is_thought: bool,
1401        cx: &mut Context<Self>,
1402    ) {
1403        self.push_assistant_content_block_with_indent(chunk, is_thought, false, cx)
1404    }
1405
1406    pub fn push_assistant_content_block_with_indent(
1407        &mut self,
1408        chunk: acp::ContentBlock,
1409        is_thought: bool,
1410        indented: bool,
1411        cx: &mut Context<Self>,
1412    ) {
1413        let language_registry = self.project.read(cx).languages().clone();
1414        let path_style = self.project.read(cx).path_style(cx);
1415        let entries_len = self.entries.len();
1416        if let Some(last_entry) = self.entries.last_mut()
1417            && let AgentThreadEntry::AssistantMessage(AssistantMessage {
1418                chunks,
1419                indented: existing_indented,
1420            }) = last_entry
1421            && *existing_indented == indented
1422        {
1423            let idx = entries_len - 1;
1424            cx.emit(AcpThreadEvent::EntryUpdated(idx));
1425            match (chunks.last_mut(), is_thought) {
1426                (Some(AssistantMessageChunk::Message { block }), false)
1427                | (Some(AssistantMessageChunk::Thought { block }), true) => {
1428                    block.append(chunk, &language_registry, path_style, cx)
1429                }
1430                _ => {
1431                    let block = ContentBlock::new(chunk, &language_registry, path_style, cx);
1432                    if is_thought {
1433                        chunks.push(AssistantMessageChunk::Thought { block })
1434                    } else {
1435                        chunks.push(AssistantMessageChunk::Message { block })
1436                    }
1437                }
1438            }
1439        } else {
1440            let block = ContentBlock::new(chunk, &language_registry, path_style, cx);
1441            let chunk = if is_thought {
1442                AssistantMessageChunk::Thought { block }
1443            } else {
1444                AssistantMessageChunk::Message { block }
1445            };
1446
1447            self.push_entry(
1448                AgentThreadEntry::AssistantMessage(AssistantMessage {
1449                    chunks: vec![chunk],
1450                    indented,
1451                }),
1452                cx,
1453            );
1454        }
1455    }
1456
1457    fn push_entry(&mut self, entry: AgentThreadEntry, cx: &mut Context<Self>) {
1458        self.entries.push(entry);
1459        cx.emit(AcpThreadEvent::NewEntry);
1460    }
1461
1462    pub fn can_set_title(&mut self, cx: &mut Context<Self>) -> bool {
1463        self.connection.set_title(&self.session_id, cx).is_some()
1464    }
1465
1466    pub fn set_title(&mut self, title: SharedString, cx: &mut Context<Self>) -> Task<Result<()>> {
1467        if title != self.title {
1468            self.title = title.clone();
1469            cx.emit(AcpThreadEvent::TitleUpdated);
1470            if let Some(set_title) = self.connection.set_title(&self.session_id, cx) {
1471                return set_title.run(title, cx);
1472            }
1473        }
1474        Task::ready(Ok(()))
1475    }
1476
1477    pub fn subagent_spawned(&mut self, session_id: acp::SessionId, cx: &mut Context<Self>) {
1478        cx.emit(AcpThreadEvent::SubagentSpawned(session_id));
1479    }
1480
1481    pub fn update_token_usage(&mut self, usage: Option<TokenUsage>, cx: &mut Context<Self>) {
1482        self.token_usage = usage;
1483        cx.emit(AcpThreadEvent::TokenUsageUpdated);
1484    }
1485
1486    pub fn update_retry_status(&mut self, status: RetryStatus, cx: &mut Context<Self>) {
1487        cx.emit(AcpThreadEvent::Retry(status));
1488    }
1489
1490    pub fn update_tool_call(
1491        &mut self,
1492        update: impl Into<ToolCallUpdate>,
1493        cx: &mut Context<Self>,
1494    ) -> Result<()> {
1495        let update = update.into();
1496        let languages = self.project.read(cx).languages().clone();
1497        let path_style = self.project.read(cx).path_style(cx);
1498
1499        let ix = match self.index_for_tool_call(update.id()) {
1500            Some(ix) => ix,
1501            None => {
1502                // Tool call not found - create a failed tool call entry
1503                let failed_tool_call = ToolCall {
1504                    id: update.id().clone(),
1505                    label: cx.new(|cx| Markdown::new("Tool call not found".into(), None, None, cx)),
1506                    kind: acp::ToolKind::Fetch,
1507                    content: vec![ToolCallContent::ContentBlock(ContentBlock::new(
1508                        "Tool call not found".into(),
1509                        &languages,
1510                        path_style,
1511                        cx,
1512                    ))],
1513                    status: ToolCallStatus::Failed,
1514                    locations: Vec::new(),
1515                    resolved_locations: Vec::new(),
1516                    raw_input: None,
1517                    raw_input_markdown: None,
1518                    raw_output: None,
1519                    tool_name: None,
1520                    subagent_session_id: None,
1521                };
1522                self.push_entry(AgentThreadEntry::ToolCall(failed_tool_call), cx);
1523                return Ok(());
1524            }
1525        };
1526        let AgentThreadEntry::ToolCall(call) = &mut self.entries[ix] else {
1527            unreachable!()
1528        };
1529
1530        match update {
1531            ToolCallUpdate::UpdateFields(update) => {
1532                let location_updated = update.fields.locations.is_some();
1533                call.update_fields(
1534                    update.fields,
1535                    update.meta,
1536                    languages,
1537                    path_style,
1538                    &self.terminals,
1539                    cx,
1540                )?;
1541                if location_updated {
1542                    self.resolve_locations(update.tool_call_id, cx);
1543                }
1544            }
1545            ToolCallUpdate::UpdateDiff(update) => {
1546                call.content.clear();
1547                call.content.push(ToolCallContent::Diff(update.diff));
1548            }
1549            ToolCallUpdate::UpdateTerminal(update) => {
1550                call.content.clear();
1551                call.content
1552                    .push(ToolCallContent::Terminal(update.terminal));
1553            }
1554        }
1555
1556        cx.emit(AcpThreadEvent::EntryUpdated(ix));
1557
1558        Ok(())
1559    }
1560
1561    /// Updates a tool call if id matches an existing entry, otherwise inserts a new one.
1562    pub fn upsert_tool_call(
1563        &mut self,
1564        tool_call: acp::ToolCall,
1565        cx: &mut Context<Self>,
1566    ) -> Result<(), acp::Error> {
1567        let status = tool_call.status.into();
1568        self.upsert_tool_call_inner(tool_call.into(), status, cx)
1569    }
1570
1571    /// Fails if id does not match an existing entry.
1572    pub fn upsert_tool_call_inner(
1573        &mut self,
1574        update: acp::ToolCallUpdate,
1575        status: ToolCallStatus,
1576        cx: &mut Context<Self>,
1577    ) -> Result<(), acp::Error> {
1578        let language_registry = self.project.read(cx).languages().clone();
1579        let path_style = self.project.read(cx).path_style(cx);
1580        let id = update.tool_call_id.clone();
1581
1582        let agent_telemetry_id = self.connection().telemetry_id();
1583        let session = self.session_id();
1584        if let ToolCallStatus::Completed | ToolCallStatus::Failed = status {
1585            let status = if matches!(status, ToolCallStatus::Completed) {
1586                "completed"
1587            } else {
1588                "failed"
1589            };
1590            telemetry::event!(
1591                "Agent Tool Call Completed",
1592                agent_telemetry_id,
1593                session,
1594                status
1595            );
1596        }
1597
1598        if let Some(ix) = self.index_for_tool_call(&id) {
1599            let AgentThreadEntry::ToolCall(call) = &mut self.entries[ix] else {
1600                unreachable!()
1601            };
1602
1603            call.update_fields(
1604                update.fields,
1605                update.meta,
1606                language_registry,
1607                path_style,
1608                &self.terminals,
1609                cx,
1610            )?;
1611            call.status = status;
1612
1613            cx.emit(AcpThreadEvent::EntryUpdated(ix));
1614        } else {
1615            let call = ToolCall::from_acp(
1616                update.try_into()?,
1617                status,
1618                language_registry,
1619                self.project.read(cx).path_style(cx),
1620                &self.terminals,
1621                cx,
1622            )?;
1623            self.push_entry(AgentThreadEntry::ToolCall(call), cx);
1624        };
1625
1626        self.resolve_locations(id, cx);
1627        Ok(())
1628    }
1629
1630    fn index_for_tool_call(&self, id: &acp::ToolCallId) -> Option<usize> {
1631        self.entries
1632            .iter()
1633            .enumerate()
1634            .rev()
1635            .find_map(|(index, entry)| {
1636                if let AgentThreadEntry::ToolCall(tool_call) = entry
1637                    && &tool_call.id == id
1638                {
1639                    Some(index)
1640                } else {
1641                    None
1642                }
1643            })
1644    }
1645
1646    fn tool_call_mut(&mut self, id: &acp::ToolCallId) -> Option<(usize, &mut ToolCall)> {
1647        // The tool call we are looking for is typically the last one, or very close to the end.
1648        // At the moment, it doesn't seem like a hashmap would be a good fit for this use case.
1649        self.entries
1650            .iter_mut()
1651            .enumerate()
1652            .rev()
1653            .find_map(|(index, tool_call)| {
1654                if let AgentThreadEntry::ToolCall(tool_call) = tool_call
1655                    && &tool_call.id == id
1656                {
1657                    Some((index, tool_call))
1658                } else {
1659                    None
1660                }
1661            })
1662    }
1663
1664    pub fn tool_call(&mut self, id: &acp::ToolCallId) -> Option<(usize, &ToolCall)> {
1665        self.entries
1666            .iter()
1667            .enumerate()
1668            .rev()
1669            .find_map(|(index, tool_call)| {
1670                if let AgentThreadEntry::ToolCall(tool_call) = tool_call
1671                    && &tool_call.id == id
1672                {
1673                    Some((index, tool_call))
1674                } else {
1675                    None
1676                }
1677            })
1678    }
1679
1680    pub fn resolve_locations(&mut self, id: acp::ToolCallId, cx: &mut Context<Self>) {
1681        let project = self.project.clone();
1682        let Some((_, tool_call)) = self.tool_call_mut(&id) else {
1683            return;
1684        };
1685        let task = tool_call.resolve_locations(project, cx);
1686        cx.spawn(async move |this, cx| {
1687            let resolved_locations = task.await;
1688
1689            this.update(cx, |this, cx| {
1690                let project = this.project.clone();
1691
1692                for location in resolved_locations.iter().flatten() {
1693                    this.shared_buffers
1694                        .insert(location.buffer.clone(), location.buffer.read(cx).snapshot());
1695                }
1696                let Some((ix, tool_call)) = this.tool_call_mut(&id) else {
1697                    return;
1698                };
1699
1700                if let Some(Some(location)) = resolved_locations.last() {
1701                    project.update(cx, |project, cx| {
1702                        let should_ignore = if let Some(agent_location) = project
1703                            .agent_location()
1704                            .filter(|agent_location| agent_location.buffer == location.buffer)
1705                        {
1706                            let snapshot = location.buffer.read(cx).snapshot();
1707                            let old_position = agent_location.position.to_point(&snapshot);
1708                            let new_position = location.position.to_point(&snapshot);
1709
1710                            // ignore this so that when we get updates from the edit tool
1711                            // the position doesn't reset to the startof line
1712                            old_position.row == new_position.row
1713                                && old_position.column > new_position.column
1714                        } else {
1715                            false
1716                        };
1717                        if !should_ignore {
1718                            project.set_agent_location(Some(location.into()), cx);
1719                        }
1720                    });
1721                }
1722
1723                let resolved_locations = resolved_locations
1724                    .iter()
1725                    .map(|l| l.as_ref().map(|l| AgentLocation::from(l)))
1726                    .collect::<Vec<_>>();
1727
1728                if tool_call.resolved_locations != resolved_locations {
1729                    tool_call.resolved_locations = resolved_locations;
1730                    cx.emit(AcpThreadEvent::EntryUpdated(ix));
1731                }
1732            })
1733        })
1734        .detach();
1735    }
1736
1737    pub fn request_tool_call_authorization(
1738        &mut self,
1739        tool_call: acp::ToolCallUpdate,
1740        options: PermissionOptions,
1741        cx: &mut Context<Self>,
1742    ) -> Result<BoxFuture<'static, acp::RequestPermissionOutcome>> {
1743        let (tx, rx) = oneshot::channel();
1744
1745        let status = ToolCallStatus::WaitingForConfirmation {
1746            options,
1747            respond_tx: tx,
1748        };
1749
1750        self.upsert_tool_call_inner(tool_call, status, cx)?;
1751        cx.emit(AcpThreadEvent::ToolAuthorizationRequired);
1752
1753        let fut = async {
1754            match rx.await {
1755                Ok(option) => acp::RequestPermissionOutcome::Selected(
1756                    acp::SelectedPermissionOutcome::new(option),
1757                ),
1758                Err(oneshot::Canceled) => acp::RequestPermissionOutcome::Cancelled,
1759            }
1760        }
1761        .boxed();
1762
1763        Ok(fut)
1764    }
1765
1766    pub fn authorize_tool_call(
1767        &mut self,
1768        id: acp::ToolCallId,
1769        option_id: acp::PermissionOptionId,
1770        option_kind: acp::PermissionOptionKind,
1771        cx: &mut Context<Self>,
1772    ) {
1773        let Some((ix, call)) = self.tool_call_mut(&id) else {
1774            return;
1775        };
1776
1777        let new_status = match option_kind {
1778            acp::PermissionOptionKind::RejectOnce | acp::PermissionOptionKind::RejectAlways => {
1779                ToolCallStatus::Rejected
1780            }
1781            acp::PermissionOptionKind::AllowOnce | acp::PermissionOptionKind::AllowAlways => {
1782                ToolCallStatus::InProgress
1783            }
1784            _ => ToolCallStatus::InProgress,
1785        };
1786
1787        let curr_status = mem::replace(&mut call.status, new_status);
1788
1789        if let ToolCallStatus::WaitingForConfirmation { respond_tx, .. } = curr_status {
1790            respond_tx.send(option_id).log_err();
1791        } else if cfg!(debug_assertions) {
1792            panic!("tried to authorize an already authorized tool call");
1793        }
1794
1795        cx.emit(AcpThreadEvent::EntryUpdated(ix));
1796    }
1797
1798    pub fn first_tool_awaiting_confirmation(&self) -> Option<&ToolCall> {
1799        let mut first_tool_call = None;
1800
1801        for entry in self.entries.iter().rev() {
1802            match &entry {
1803                AgentThreadEntry::ToolCall(call) => {
1804                    if let ToolCallStatus::WaitingForConfirmation { .. } = call.status {
1805                        first_tool_call = Some(call);
1806                    } else {
1807                        continue;
1808                    }
1809                }
1810                AgentThreadEntry::UserMessage(_) | AgentThreadEntry::AssistantMessage(_) => {
1811                    // Reached the beginning of the turn.
1812                    // If we had pending permission requests in the previous turn, they have been cancelled.
1813                    break;
1814                }
1815            }
1816        }
1817
1818        first_tool_call
1819    }
1820
1821    pub fn plan(&self) -> &Plan {
1822        &self.plan
1823    }
1824
1825    pub fn update_plan(&mut self, request: acp::Plan, cx: &mut Context<Self>) {
1826        let new_entries_len = request.entries.len();
1827        let mut new_entries = request.entries.into_iter();
1828
1829        // Reuse existing markdown to prevent flickering
1830        for (old, new) in self.plan.entries.iter_mut().zip(new_entries.by_ref()) {
1831            let PlanEntry {
1832                content,
1833                priority,
1834                status,
1835            } = old;
1836            content.update(cx, |old, cx| {
1837                old.replace(new.content, cx);
1838            });
1839            *priority = new.priority;
1840            *status = new.status;
1841        }
1842        for new in new_entries {
1843            self.plan.entries.push(PlanEntry::from_acp(new, cx))
1844        }
1845        self.plan.entries.truncate(new_entries_len);
1846
1847        cx.notify();
1848    }
1849
1850    fn clear_completed_plan_entries(&mut self, cx: &mut Context<Self>) {
1851        self.plan
1852            .entries
1853            .retain(|entry| !matches!(entry.status, acp::PlanEntryStatus::Completed));
1854        cx.notify();
1855    }
1856
1857    #[cfg(any(test, feature = "test-support"))]
1858    pub fn send_raw(
1859        &mut self,
1860        message: &str,
1861        cx: &mut Context<Self>,
1862    ) -> BoxFuture<'static, Result<()>> {
1863        self.send(vec![message.into()], cx)
1864    }
1865
1866    pub fn send(
1867        &mut self,
1868        message: Vec<acp::ContentBlock>,
1869        cx: &mut Context<Self>,
1870    ) -> BoxFuture<'static, Result<()>> {
1871        let block = ContentBlock::new_combined(
1872            message.clone(),
1873            self.project.read(cx).languages().clone(),
1874            self.project.read(cx).path_style(cx),
1875            cx,
1876        );
1877        let request = acp::PromptRequest::new(self.session_id.clone(), message.clone());
1878        let git_store = self.project.read(cx).git_store().clone();
1879
1880        let message_id = if self.connection.truncate(&self.session_id, cx).is_some() {
1881            Some(UserMessageId::new())
1882        } else {
1883            None
1884        };
1885
1886        self.run_turn(cx, async move |this, cx| {
1887            this.update(cx, |this, cx| {
1888                this.push_entry(
1889                    AgentThreadEntry::UserMessage(UserMessage {
1890                        id: message_id.clone(),
1891                        content: block,
1892                        chunks: message,
1893                        checkpoint: None,
1894                        indented: false,
1895                    }),
1896                    cx,
1897                );
1898            })
1899            .ok();
1900
1901            let old_checkpoint = git_store
1902                .update(cx, |git, cx| git.checkpoint(cx))
1903                .await
1904                .context("failed to get old checkpoint")
1905                .log_err();
1906            this.update(cx, |this, cx| {
1907                if let Some((_ix, message)) = this.last_user_message() {
1908                    message.checkpoint = old_checkpoint.map(|git_checkpoint| Checkpoint {
1909                        git_checkpoint,
1910                        show: false,
1911                    });
1912                }
1913                this.connection.prompt(message_id, request, cx)
1914            })?
1915            .await
1916        })
1917    }
1918
1919    pub fn can_retry(&self, cx: &App) -> bool {
1920        self.connection.retry(&self.session_id, cx).is_some()
1921    }
1922
1923    pub fn retry(&mut self, cx: &mut Context<Self>) -> BoxFuture<'static, Result<()>> {
1924        self.run_turn(cx, async move |this, cx| {
1925            this.update(cx, |this, cx| {
1926                this.connection
1927                    .retry(&this.session_id, cx)
1928                    .map(|retry| retry.run(cx))
1929            })?
1930            .context("retrying a session is not supported")?
1931            .await
1932        })
1933    }
1934
1935    fn run_turn(
1936        &mut self,
1937        cx: &mut Context<Self>,
1938        f: impl 'static + AsyncFnOnce(WeakEntity<Self>, &mut AsyncApp) -> Result<acp::PromptResponse>,
1939    ) -> BoxFuture<'static, Result<()>> {
1940        self.clear_completed_plan_entries(cx);
1941
1942        let (tx, rx) = oneshot::channel();
1943        let cancel_task = self.cancel(cx);
1944
1945        self.send_task = Some(cx.spawn(async move |this, cx| {
1946            cancel_task.await;
1947            tx.send(f(this, cx).await).ok();
1948        }));
1949
1950        cx.spawn(async move |this, cx| {
1951            let response = rx.await;
1952
1953            this.update(cx, |this, cx| this.update_last_checkpoint(cx))?
1954                .await?;
1955
1956            this.update(cx, |this, cx| {
1957                this.project
1958                    .update(cx, |project, cx| project.set_agent_location(None, cx));
1959                match response {
1960                    Ok(Err(e)) => {
1961                        this.send_task.take();
1962                        cx.emit(AcpThreadEvent::Error);
1963                        Err(e)
1964                    }
1965                    result => {
1966                        let canceled = matches!(
1967                            result,
1968                            Ok(Ok(acp::PromptResponse {
1969                                stop_reason: acp::StopReason::Cancelled,
1970                                ..
1971                            }))
1972                        );
1973
1974                        // We only take the task if the current prompt wasn't canceled.
1975                        //
1976                        // This prompt may have been canceled because another one was sent
1977                        // while it was still generating. In these cases, dropping `send_task`
1978                        // would cause the next generation to be canceled.
1979                        if !canceled {
1980                            this.send_task.take();
1981                        }
1982
1983                        // Handle refusal - distinguish between user prompt and tool call refusals
1984                        if let Ok(Ok(acp::PromptResponse {
1985                            stop_reason: acp::StopReason::Refusal,
1986                            ..
1987                        })) = result
1988                        {
1989                            if let Some((user_msg_ix, _)) = this.last_user_message() {
1990                                // Check if there's a completed tool call with results after the last user message
1991                                // This indicates the refusal is in response to tool output, not the user's prompt
1992                                let has_completed_tool_call_after_user_msg =
1993                                    this.entries.iter().skip(user_msg_ix + 1).any(|entry| {
1994                                        if let AgentThreadEntry::ToolCall(tool_call) = entry {
1995                                            // Check if the tool call has completed and has output
1996                                            matches!(tool_call.status, ToolCallStatus::Completed)
1997                                                && tool_call.raw_output.is_some()
1998                                        } else {
1999                                            false
2000                                        }
2001                                    });
2002
2003                                if has_completed_tool_call_after_user_msg {
2004                                    // Refusal is due to tool output - don't truncate, just notify
2005                                    // The model refused based on what the tool returned
2006                                    cx.emit(AcpThreadEvent::Refusal);
2007                                } else {
2008                                    // User prompt was refused - truncate back to before the user message
2009                                    let range = user_msg_ix..this.entries.len();
2010                                    if range.start < range.end {
2011                                        this.entries.truncate(user_msg_ix);
2012                                        cx.emit(AcpThreadEvent::EntriesRemoved(range));
2013                                    }
2014                                    cx.emit(AcpThreadEvent::Refusal);
2015                                }
2016                            } else {
2017                                // No user message found, treat as general refusal
2018                                cx.emit(AcpThreadEvent::Refusal);
2019                            }
2020                        }
2021
2022                        cx.emit(AcpThreadEvent::Stopped);
2023                        Ok(())
2024                    }
2025                }
2026            })?
2027        })
2028        .boxed()
2029    }
2030
2031    pub fn cancel(&mut self, cx: &mut Context<Self>) -> Task<()> {
2032        let Some(send_task) = self.send_task.take() else {
2033            return Task::ready(());
2034        };
2035
2036        for entry in self.entries.iter_mut() {
2037            if let AgentThreadEntry::ToolCall(call) = entry {
2038                let cancel = matches!(
2039                    call.status,
2040                    ToolCallStatus::Pending
2041                        | ToolCallStatus::WaitingForConfirmation { .. }
2042                        | ToolCallStatus::InProgress
2043                );
2044
2045                if cancel {
2046                    call.status = ToolCallStatus::Canceled;
2047                }
2048            }
2049        }
2050
2051        self.connection.cancel(&self.session_id, cx);
2052
2053        // Wait for the send task to complete
2054        cx.foreground_executor().spawn(send_task)
2055    }
2056
2057    /// Restores the git working tree to the state at the given checkpoint (if one exists)
2058    pub fn restore_checkpoint(
2059        &mut self,
2060        id: UserMessageId,
2061        cx: &mut Context<Self>,
2062    ) -> Task<Result<()>> {
2063        let Some((_, message)) = self.user_message_mut(&id) else {
2064            return Task::ready(Err(anyhow!("message not found")));
2065        };
2066
2067        let checkpoint = message
2068            .checkpoint
2069            .as_ref()
2070            .map(|c| c.git_checkpoint.clone());
2071
2072        // Cancel any in-progress generation before restoring
2073        let cancel_task = self.cancel(cx);
2074        let rewind = self.rewind(id.clone(), cx);
2075        let git_store = self.project.read(cx).git_store().clone();
2076
2077        cx.spawn(async move |_, cx| {
2078            cancel_task.await;
2079            rewind.await?;
2080            if let Some(checkpoint) = checkpoint {
2081                git_store
2082                    .update(cx, |git, cx| git.restore_checkpoint(checkpoint, cx))
2083                    .await?;
2084            }
2085
2086            Ok(())
2087        })
2088    }
2089
2090    /// Rewinds this thread to before the entry at `index`, removing it and all
2091    /// subsequent entries while rejecting any action_log changes made from that point.
2092    /// Unlike `restore_checkpoint`, this method does not restore from git.
2093    pub fn rewind(&mut self, id: UserMessageId, cx: &mut Context<Self>) -> Task<Result<()>> {
2094        let Some(truncate) = self.connection.truncate(&self.session_id, cx) else {
2095            return Task::ready(Err(anyhow!("not supported")));
2096        };
2097
2098        let telemetry = ActionLogTelemetry::from(&*self);
2099        cx.spawn(async move |this, cx| {
2100            cx.update(|cx| truncate.run(id.clone(), cx)).await?;
2101            this.update(cx, |this, cx| {
2102                if let Some((ix, _)) = this.user_message_mut(&id) {
2103                    // Collect all terminals from entries that will be removed
2104                    let terminals_to_remove: Vec<acp::TerminalId> = this.entries[ix..]
2105                        .iter()
2106                        .flat_map(|entry| entry.terminals())
2107                        .filter_map(|terminal| terminal.read(cx).id().clone().into())
2108                        .collect();
2109
2110                    let range = ix..this.entries.len();
2111                    this.entries.truncate(ix);
2112                    cx.emit(AcpThreadEvent::EntriesRemoved(range));
2113
2114                    // Kill and remove the terminals
2115                    for terminal_id in terminals_to_remove {
2116                        if let Some(terminal) = this.terminals.remove(&terminal_id) {
2117                            terminal.update(cx, |terminal, cx| {
2118                                terminal.kill(cx);
2119                            });
2120                        }
2121                    }
2122                }
2123                this.action_log().update(cx, |action_log, cx| {
2124                    action_log.reject_all_edits(Some(telemetry), cx)
2125                })
2126            })?
2127            .await;
2128            Ok(())
2129        })
2130    }
2131
2132    fn update_last_checkpoint(&mut self, cx: &mut Context<Self>) -> Task<Result<()>> {
2133        let git_store = self.project.read(cx).git_store().clone();
2134
2135        let Some((_, message)) = self.last_user_message() else {
2136            return Task::ready(Ok(()));
2137        };
2138        let Some(user_message_id) = message.id.clone() else {
2139            return Task::ready(Ok(()));
2140        };
2141        let Some(checkpoint) = message.checkpoint.as_ref() else {
2142            return Task::ready(Ok(()));
2143        };
2144        let old_checkpoint = checkpoint.git_checkpoint.clone();
2145
2146        let new_checkpoint = git_store.update(cx, |git, cx| git.checkpoint(cx));
2147        cx.spawn(async move |this, cx| {
2148            let Some(new_checkpoint) = new_checkpoint
2149                .await
2150                .context("failed to get new checkpoint")
2151                .log_err()
2152            else {
2153                return Ok(());
2154            };
2155
2156            let equal = git_store
2157                .update(cx, |git, cx| {
2158                    git.compare_checkpoints(old_checkpoint.clone(), new_checkpoint, cx)
2159                })
2160                .await
2161                .unwrap_or(true);
2162
2163            this.update(cx, |this, cx| {
2164                if let Some((ix, message)) = this.user_message_mut(&user_message_id) {
2165                    if let Some(checkpoint) = message.checkpoint.as_mut() {
2166                        checkpoint.show = !equal;
2167                        cx.emit(AcpThreadEvent::EntryUpdated(ix));
2168                    }
2169                }
2170            })?;
2171
2172            Ok(())
2173        })
2174    }
2175
2176    fn last_user_message(&mut self) -> Option<(usize, &mut UserMessage)> {
2177        self.entries
2178            .iter_mut()
2179            .enumerate()
2180            .rev()
2181            .find_map(|(ix, entry)| {
2182                if let AgentThreadEntry::UserMessage(message) = entry {
2183                    Some((ix, message))
2184                } else {
2185                    None
2186                }
2187            })
2188    }
2189
2190    fn user_message_mut(&mut self, id: &UserMessageId) -> Option<(usize, &mut UserMessage)> {
2191        self.entries.iter_mut().enumerate().find_map(|(ix, entry)| {
2192            if let AgentThreadEntry::UserMessage(message) = entry {
2193                if message.id.as_ref() == Some(id) {
2194                    Some((ix, message))
2195                } else {
2196                    None
2197                }
2198            } else {
2199                None
2200            }
2201        })
2202    }
2203
2204    pub fn read_text_file(
2205        &self,
2206        path: PathBuf,
2207        line: Option<u32>,
2208        limit: Option<u32>,
2209        reuse_shared_snapshot: bool,
2210        cx: &mut Context<Self>,
2211    ) -> Task<Result<String, acp::Error>> {
2212        // Args are 1-based, move to 0-based
2213        let line = line.unwrap_or_default().saturating_sub(1);
2214        let limit = limit.unwrap_or(u32::MAX);
2215        let project = self.project.clone();
2216        let action_log = self.action_log.clone();
2217        cx.spawn(async move |this, cx| {
2218            let load = project.update(cx, |project, cx| {
2219                let path = project
2220                    .project_path_for_absolute_path(&path, cx)
2221                    .ok_or_else(|| {
2222                        acp::Error::resource_not_found(Some(path.display().to_string()))
2223                    })?;
2224                Ok::<_, acp::Error>(project.open_buffer(path, cx))
2225            })?;
2226
2227            let buffer = load.await?;
2228
2229            let snapshot = if reuse_shared_snapshot {
2230                this.read_with(cx, |this, _| {
2231                    this.shared_buffers.get(&buffer.clone()).cloned()
2232                })
2233                .log_err()
2234                .flatten()
2235            } else {
2236                None
2237            };
2238
2239            let snapshot = if let Some(snapshot) = snapshot {
2240                snapshot
2241            } else {
2242                action_log.update(cx, |action_log, cx| {
2243                    action_log.buffer_read(buffer.clone(), cx);
2244                });
2245
2246                let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
2247                this.update(cx, |this, _| {
2248                    this.shared_buffers.insert(buffer.clone(), snapshot.clone());
2249                })?;
2250                snapshot
2251            };
2252
2253            let max_point = snapshot.max_point();
2254            let start_position = Point::new(line, 0);
2255
2256            if start_position > max_point {
2257                return Err(acp::Error::invalid_params().data(format!(
2258                    "Attempting to read beyond the end of the file, line {}:{}",
2259                    max_point.row + 1,
2260                    max_point.column
2261                )));
2262            }
2263
2264            let start = snapshot.anchor_before(start_position);
2265            let end = snapshot.anchor_before(Point::new(line.saturating_add(limit), 0));
2266
2267            project.update(cx, |project, cx| {
2268                project.set_agent_location(
2269                    Some(AgentLocation {
2270                        buffer: buffer.downgrade(),
2271                        position: start,
2272                    }),
2273                    cx,
2274                );
2275            });
2276
2277            Ok(snapshot.text_for_range(start..end).collect::<String>())
2278        })
2279    }
2280
2281    pub fn write_text_file(
2282        &self,
2283        path: PathBuf,
2284        content: String,
2285        cx: &mut Context<Self>,
2286    ) -> Task<Result<()>> {
2287        let project = self.project.clone();
2288        let action_log = self.action_log.clone();
2289        cx.spawn(async move |this, cx| {
2290            let load = project.update(cx, |project, cx| {
2291                let path = project
2292                    .project_path_for_absolute_path(&path, cx)
2293                    .context("invalid path")?;
2294                anyhow::Ok(project.open_buffer(path, cx))
2295            });
2296            let buffer = load?.await?;
2297            let snapshot = this.update(cx, |this, cx| {
2298                this.shared_buffers
2299                    .get(&buffer)
2300                    .cloned()
2301                    .unwrap_or_else(|| buffer.read(cx).snapshot())
2302            })?;
2303            let edits = cx
2304                .background_executor()
2305                .spawn(async move {
2306                    let old_text = snapshot.text();
2307                    text_diff(old_text.as_str(), &content)
2308                        .into_iter()
2309                        .map(|(range, replacement)| {
2310                            (snapshot.anchor_range_between(range), replacement)
2311                        })
2312                        .collect::<Vec<_>>()
2313                })
2314                .await;
2315
2316            project.update(cx, |project, cx| {
2317                project.set_agent_location(
2318                    Some(AgentLocation {
2319                        buffer: buffer.downgrade(),
2320                        position: edits
2321                            .last()
2322                            .map(|(range, _)| range.end)
2323                            .unwrap_or(Anchor::min_for_buffer(buffer.read(cx).remote_id())),
2324                    }),
2325                    cx,
2326                );
2327            });
2328
2329            let format_on_save = cx.update(|cx| {
2330                action_log.update(cx, |action_log, cx| {
2331                    action_log.buffer_read(buffer.clone(), cx);
2332                });
2333
2334                let format_on_save = buffer.update(cx, |buffer, cx| {
2335                    buffer.edit(edits, None, cx);
2336
2337                    let settings = language::language_settings::language_settings(
2338                        buffer.language().map(|l| l.name()),
2339                        buffer.file(),
2340                        cx,
2341                    );
2342
2343                    settings.format_on_save != FormatOnSave::Off
2344                });
2345                action_log.update(cx, |action_log, cx| {
2346                    action_log.buffer_edited(buffer.clone(), cx);
2347                });
2348                format_on_save
2349            });
2350
2351            if format_on_save {
2352                let format_task = project.update(cx, |project, cx| {
2353                    project.format(
2354                        HashSet::from_iter([buffer.clone()]),
2355                        LspFormatTarget::Buffers,
2356                        false,
2357                        FormatTrigger::Save,
2358                        cx,
2359                    )
2360                });
2361                format_task.await.log_err();
2362
2363                action_log.update(cx, |action_log, cx| {
2364                    action_log.buffer_edited(buffer.clone(), cx);
2365                });
2366            }
2367
2368            project
2369                .update(cx, |project, cx| project.save_buffer(buffer, cx))
2370                .await
2371        })
2372    }
2373
2374    pub fn create_terminal(
2375        &self,
2376        command: String,
2377        args: Vec<String>,
2378        extra_env: Vec<acp::EnvVariable>,
2379        cwd: Option<PathBuf>,
2380        output_byte_limit: Option<u64>,
2381        cx: &mut Context<Self>,
2382    ) -> Task<Result<Entity<Terminal>>> {
2383        let env = match &cwd {
2384            Some(dir) => self.project.update(cx, |project, cx| {
2385                project.environment().update(cx, |env, cx| {
2386                    env.directory_environment(dir.as_path().into(), cx)
2387                })
2388            }),
2389            None => Task::ready(None).shared(),
2390        };
2391        let env = cx.spawn(async move |_, _| {
2392            let mut env = env.await.unwrap_or_default();
2393            // Disables paging for `git` and hopefully other commands
2394            env.insert("PAGER".into(), "".into());
2395            for var in extra_env {
2396                env.insert(var.name, var.value);
2397            }
2398            env
2399        });
2400
2401        let project = self.project.clone();
2402        let language_registry = project.read(cx).languages().clone();
2403        let is_windows = project.read(cx).path_style(cx).is_windows();
2404
2405        let terminal_id = acp::TerminalId::new(Uuid::new_v4().to_string());
2406        let terminal_task = cx.spawn({
2407            let terminal_id = terminal_id.clone();
2408            async move |_this, cx| {
2409                let env = env.await;
2410                let shell = project
2411                    .update(cx, |project, cx| {
2412                        project
2413                            .remote_client()
2414                            .and_then(|r| r.read(cx).default_system_shell())
2415                    })
2416                    .unwrap_or_else(|| get_default_system_shell_preferring_bash());
2417                let (task_command, task_args) =
2418                    ShellBuilder::new(&Shell::Program(shell), is_windows)
2419                        .redirect_stdin_to_dev_null()
2420                        .build(Some(command.clone()), &args);
2421                let terminal = project
2422                    .update(cx, |project, cx| {
2423                        project.create_terminal_task(
2424                            task::SpawnInTerminal {
2425                                command: Some(task_command),
2426                                args: task_args,
2427                                cwd: cwd.clone(),
2428                                env,
2429                                ..Default::default()
2430                            },
2431                            cx,
2432                        )
2433                    })
2434                    .await?;
2435
2436                anyhow::Ok(cx.new(|cx| {
2437                    Terminal::new(
2438                        terminal_id,
2439                        &format!("{} {}", command, args.join(" ")),
2440                        cwd,
2441                        output_byte_limit.map(|l| l as usize),
2442                        terminal,
2443                        language_registry,
2444                        cx,
2445                    )
2446                }))
2447            }
2448        });
2449
2450        cx.spawn(async move |this, cx| {
2451            let terminal = terminal_task.await?;
2452            this.update(cx, |this, _cx| {
2453                this.terminals.insert(terminal_id, terminal.clone());
2454                terminal
2455            })
2456        })
2457    }
2458
2459    pub fn kill_terminal(
2460        &mut self,
2461        terminal_id: acp::TerminalId,
2462        cx: &mut Context<Self>,
2463    ) -> Result<()> {
2464        self.terminals
2465            .get(&terminal_id)
2466            .context("Terminal not found")?
2467            .update(cx, |terminal, cx| {
2468                terminal.kill(cx);
2469            });
2470
2471        Ok(())
2472    }
2473
2474    pub fn release_terminal(
2475        &mut self,
2476        terminal_id: acp::TerminalId,
2477        cx: &mut Context<Self>,
2478    ) -> Result<()> {
2479        self.terminals
2480            .remove(&terminal_id)
2481            .context("Terminal not found")?
2482            .update(cx, |terminal, cx| {
2483                terminal.kill(cx);
2484            });
2485
2486        Ok(())
2487    }
2488
2489    pub fn terminal(&self, terminal_id: acp::TerminalId) -> Result<Entity<Terminal>> {
2490        self.terminals
2491            .get(&terminal_id)
2492            .context("Terminal not found")
2493            .cloned()
2494    }
2495
2496    pub fn to_markdown(&self, cx: &App) -> String {
2497        self.entries.iter().map(|e| e.to_markdown(cx)).collect()
2498    }
2499
2500    pub fn emit_load_error(&mut self, error: LoadError, cx: &mut Context<Self>) {
2501        cx.emit(AcpThreadEvent::LoadError(error));
2502    }
2503
2504    pub fn register_terminal_created(
2505        &mut self,
2506        terminal_id: acp::TerminalId,
2507        command_label: String,
2508        working_dir: Option<PathBuf>,
2509        output_byte_limit: Option<u64>,
2510        terminal: Entity<::terminal::Terminal>,
2511        cx: &mut Context<Self>,
2512    ) -> Entity<Terminal> {
2513        let language_registry = self.project.read(cx).languages().clone();
2514
2515        let entity = cx.new(|cx| {
2516            Terminal::new(
2517                terminal_id.clone(),
2518                &command_label,
2519                working_dir.clone(),
2520                output_byte_limit.map(|l| l as usize),
2521                terminal,
2522                language_registry,
2523                cx,
2524            )
2525        });
2526        self.terminals.insert(terminal_id.clone(), entity.clone());
2527        entity
2528    }
2529}
2530
2531fn markdown_for_raw_output(
2532    raw_output: &serde_json::Value,
2533    language_registry: &Arc<LanguageRegistry>,
2534    cx: &mut App,
2535) -> Option<Entity<Markdown>> {
2536    match raw_output {
2537        serde_json::Value::Null => None,
2538        serde_json::Value::Bool(value) => Some(cx.new(|cx| {
2539            Markdown::new(
2540                value.to_string().into(),
2541                Some(language_registry.clone()),
2542                None,
2543                cx,
2544            )
2545        })),
2546        serde_json::Value::Number(value) => Some(cx.new(|cx| {
2547            Markdown::new(
2548                value.to_string().into(),
2549                Some(language_registry.clone()),
2550                None,
2551                cx,
2552            )
2553        })),
2554        serde_json::Value::String(value) => Some(cx.new(|cx| {
2555            Markdown::new(
2556                value.clone().into(),
2557                Some(language_registry.clone()),
2558                None,
2559                cx,
2560            )
2561        })),
2562        value => Some(cx.new(|cx| {
2563            let pretty_json = to_string_pretty(value).unwrap_or_else(|_| value.to_string());
2564
2565            Markdown::new(
2566                format!("```json\n{}\n```", pretty_json).into(),
2567                Some(language_registry.clone()),
2568                None,
2569                cx,
2570            )
2571        })),
2572    }
2573}
2574
2575#[cfg(test)]
2576mod tests {
2577    use super::*;
2578    use anyhow::anyhow;
2579    use futures::{channel::mpsc, future::LocalBoxFuture, select};
2580    use gpui::{App, AsyncApp, TestAppContext, WeakEntity};
2581    use indoc::indoc;
2582    use project::{FakeFs, Fs};
2583    use rand::{distr, prelude::*};
2584    use serde_json::json;
2585    use settings::SettingsStore;
2586    use smol::stream::StreamExt as _;
2587    use std::{
2588        any::Any,
2589        cell::RefCell,
2590        path::Path,
2591        rc::Rc,
2592        sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst},
2593        time::Duration,
2594    };
2595    use util::path;
2596
2597    fn init_test(cx: &mut TestAppContext) {
2598        env_logger::try_init().ok();
2599        cx.update(|cx| {
2600            let settings_store = SettingsStore::test(cx);
2601            cx.set_global(settings_store);
2602        });
2603    }
2604
2605    #[gpui::test]
2606    async fn test_terminal_output_buffered_before_created_renders(cx: &mut gpui::TestAppContext) {
2607        init_test(cx);
2608
2609        let fs = FakeFs::new(cx.executor());
2610        let project = Project::test(fs, [], cx).await;
2611        let connection = Rc::new(FakeAgentConnection::new());
2612        let thread = cx
2613            .update(|cx| connection.new_session(project, std::path::Path::new(path!("/test")), cx))
2614            .await
2615            .unwrap();
2616
2617        let terminal_id = acp::TerminalId::new(uuid::Uuid::new_v4().to_string());
2618
2619        // Send Output BEFORE Created - should be buffered by acp_thread
2620        thread.update(cx, |thread, cx| {
2621            thread.on_terminal_provider_event(
2622                TerminalProviderEvent::Output {
2623                    terminal_id: terminal_id.clone(),
2624                    data: b"hello buffered".to_vec(),
2625                },
2626                cx,
2627            );
2628        });
2629
2630        // Create a display-only terminal and then send Created
2631        let lower = cx.new(|cx| {
2632            let builder = ::terminal::TerminalBuilder::new_display_only(
2633                ::terminal::terminal_settings::CursorShape::default(),
2634                ::terminal::terminal_settings::AlternateScroll::On,
2635                None,
2636                0,
2637                cx.background_executor(),
2638                PathStyle::local(),
2639            )
2640            .unwrap();
2641            builder.subscribe(cx)
2642        });
2643
2644        thread.update(cx, |thread, cx| {
2645            thread.on_terminal_provider_event(
2646                TerminalProviderEvent::Created {
2647                    terminal_id: terminal_id.clone(),
2648                    label: "Buffered Test".to_string(),
2649                    cwd: None,
2650                    output_byte_limit: None,
2651                    terminal: lower.clone(),
2652                },
2653                cx,
2654            );
2655        });
2656
2657        // After Created, buffered Output should have been flushed into the renderer
2658        let content = thread.read_with(cx, |thread, cx| {
2659            let term = thread.terminal(terminal_id.clone()).unwrap();
2660            term.read_with(cx, |t, cx| t.inner().read(cx).get_content())
2661        });
2662
2663        assert!(
2664            content.contains("hello buffered"),
2665            "expected buffered output to render, got: {content}"
2666        );
2667    }
2668
2669    #[gpui::test]
2670    async fn test_terminal_output_and_exit_buffered_before_created(cx: &mut gpui::TestAppContext) {
2671        init_test(cx);
2672
2673        let fs = FakeFs::new(cx.executor());
2674        let project = Project::test(fs, [], cx).await;
2675        let connection = Rc::new(FakeAgentConnection::new());
2676        let thread = cx
2677            .update(|cx| connection.new_session(project, std::path::Path::new(path!("/test")), cx))
2678            .await
2679            .unwrap();
2680
2681        let terminal_id = acp::TerminalId::new(uuid::Uuid::new_v4().to_string());
2682
2683        // Send Output BEFORE Created
2684        thread.update(cx, |thread, cx| {
2685            thread.on_terminal_provider_event(
2686                TerminalProviderEvent::Output {
2687                    terminal_id: terminal_id.clone(),
2688                    data: b"pre-exit data".to_vec(),
2689                },
2690                cx,
2691            );
2692        });
2693
2694        // Send Exit BEFORE Created
2695        thread.update(cx, |thread, cx| {
2696            thread.on_terminal_provider_event(
2697                TerminalProviderEvent::Exit {
2698                    terminal_id: terminal_id.clone(),
2699                    status: acp::TerminalExitStatus::new().exit_code(0),
2700                },
2701                cx,
2702            );
2703        });
2704
2705        // Now create a display-only lower-level terminal and send Created
2706        let lower = cx.new(|cx| {
2707            let builder = ::terminal::TerminalBuilder::new_display_only(
2708                ::terminal::terminal_settings::CursorShape::default(),
2709                ::terminal::terminal_settings::AlternateScroll::On,
2710                None,
2711                0,
2712                cx.background_executor(),
2713                PathStyle::local(),
2714            )
2715            .unwrap();
2716            builder.subscribe(cx)
2717        });
2718
2719        thread.update(cx, |thread, cx| {
2720            thread.on_terminal_provider_event(
2721                TerminalProviderEvent::Created {
2722                    terminal_id: terminal_id.clone(),
2723                    label: "Buffered Exit Test".to_string(),
2724                    cwd: None,
2725                    output_byte_limit: None,
2726                    terminal: lower.clone(),
2727                },
2728                cx,
2729            );
2730        });
2731
2732        // Output should be present after Created (flushed from buffer)
2733        let content = thread.read_with(cx, |thread, cx| {
2734            let term = thread.terminal(terminal_id.clone()).unwrap();
2735            term.read_with(cx, |t, cx| t.inner().read(cx).get_content())
2736        });
2737
2738        assert!(
2739            content.contains("pre-exit data"),
2740            "expected pre-exit data to render, got: {content}"
2741        );
2742    }
2743
2744    /// Test that killing a terminal via Terminal::kill properly:
2745    /// 1. Causes wait_for_exit to complete (doesn't hang forever)
2746    /// 2. The underlying terminal still has the output that was written before the kill
2747    ///
2748    /// This test verifies that the fix to kill_active_task (which now also kills
2749    /// the shell process in addition to the foreground process) properly allows
2750    /// wait_for_exit to complete instead of hanging indefinitely.
2751    #[cfg(unix)]
2752    #[gpui::test]
2753    async fn test_terminal_kill_allows_wait_for_exit_to_complete(cx: &mut gpui::TestAppContext) {
2754        use std::collections::HashMap;
2755        use task::Shell;
2756        use util::shell_builder::ShellBuilder;
2757
2758        init_test(cx);
2759        cx.executor().allow_parking();
2760
2761        let fs = FakeFs::new(cx.executor());
2762        let project = Project::test(fs, [], cx).await;
2763        let connection = Rc::new(FakeAgentConnection::new());
2764        let thread = cx
2765            .update(|cx| connection.new_session(project.clone(), Path::new(path!("/test")), cx))
2766            .await
2767            .unwrap();
2768
2769        let terminal_id = acp::TerminalId::new(uuid::Uuid::new_v4().to_string());
2770
2771        // Create a real PTY terminal that runs a command which prints output then sleeps
2772        // We use printf instead of echo and chain with && sleep to ensure proper execution
2773        let (completion_tx, _completion_rx) = smol::channel::unbounded();
2774        let (program, args) = ShellBuilder::new(&Shell::System, false).build(
2775            Some("printf 'output_before_kill\\n' && sleep 60".to_owned()),
2776            &[],
2777        );
2778
2779        let builder = cx
2780            .update(|cx| {
2781                ::terminal::TerminalBuilder::new(
2782                    None,
2783                    None,
2784                    task::Shell::WithArguments {
2785                        program,
2786                        args,
2787                        title_override: None,
2788                    },
2789                    HashMap::default(),
2790                    ::terminal::terminal_settings::CursorShape::default(),
2791                    ::terminal::terminal_settings::AlternateScroll::On,
2792                    None,
2793                    vec![],
2794                    0,
2795                    false,
2796                    0,
2797                    Some(completion_tx),
2798                    cx,
2799                    vec![],
2800                    PathStyle::local(),
2801                )
2802            })
2803            .await
2804            .unwrap();
2805
2806        let lower_terminal = cx.new(|cx| builder.subscribe(cx));
2807
2808        // Create the acp_thread Terminal wrapper
2809        thread.update(cx, |thread, cx| {
2810            thread.on_terminal_provider_event(
2811                TerminalProviderEvent::Created {
2812                    terminal_id: terminal_id.clone(),
2813                    label: "printf output_before_kill && sleep 60".to_string(),
2814                    cwd: None,
2815                    output_byte_limit: None,
2816                    terminal: lower_terminal.clone(),
2817                },
2818                cx,
2819            );
2820        });
2821
2822        // Wait for the printf command to execute and produce output
2823        // Use real time since parking is enabled
2824        cx.executor().timer(Duration::from_millis(500)).await;
2825
2826        // Get the acp_thread Terminal and kill it
2827        let wait_for_exit = thread.update(cx, |thread, cx| {
2828            let term = thread.terminals.get(&terminal_id).unwrap();
2829            let wait_for_exit = term.read(cx).wait_for_exit();
2830            term.update(cx, |term, cx| {
2831                term.kill(cx);
2832            });
2833            wait_for_exit
2834        });
2835
2836        // KEY ASSERTION: wait_for_exit should complete within a reasonable time (not hang).
2837        // Before the fix to kill_active_task, this would hang forever because
2838        // only the foreground process was killed, not the shell, so the PTY
2839        // child never exited and wait_for_completed_task never completed.
2840        let exit_result = futures::select! {
2841            result = futures::FutureExt::fuse(wait_for_exit) => Some(result),
2842            _ = futures::FutureExt::fuse(cx.background_executor.timer(Duration::from_secs(5))) => None,
2843        };
2844
2845        assert!(
2846            exit_result.is_some(),
2847            "wait_for_exit should complete after kill, but it timed out. \
2848            This indicates kill_active_task is not properly killing the shell process."
2849        );
2850
2851        // Give the system a chance to process any pending updates
2852        cx.run_until_parked();
2853
2854        // Verify that the underlying terminal still has the output that was
2855        // written before the kill. This verifies that killing doesn't lose output.
2856        let inner_content = thread.read_with(cx, |thread, cx| {
2857            let term = thread.terminals.get(&terminal_id).unwrap();
2858            term.read(cx).inner().read(cx).get_content()
2859        });
2860
2861        assert!(
2862            inner_content.contains("output_before_kill"),
2863            "Underlying terminal should contain output from before kill, got: {}",
2864            inner_content
2865        );
2866    }
2867
2868    #[gpui::test]
2869    async fn test_push_user_content_block(cx: &mut gpui::TestAppContext) {
2870        init_test(cx);
2871
2872        let fs = FakeFs::new(cx.executor());
2873        let project = Project::test(fs, [], cx).await;
2874        let connection = Rc::new(FakeAgentConnection::new());
2875        let thread = cx
2876            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
2877            .await
2878            .unwrap();
2879
2880        // Test creating a new user message
2881        thread.update(cx, |thread, cx| {
2882            thread.push_user_content_block(None, "Hello, ".into(), cx);
2883        });
2884
2885        thread.update(cx, |thread, cx| {
2886            assert_eq!(thread.entries.len(), 1);
2887            if let AgentThreadEntry::UserMessage(user_msg) = &thread.entries[0] {
2888                assert_eq!(user_msg.id, None);
2889                assert_eq!(user_msg.content.to_markdown(cx), "Hello, ");
2890            } else {
2891                panic!("Expected UserMessage");
2892            }
2893        });
2894
2895        // Test appending to existing user message
2896        let message_1_id = UserMessageId::new();
2897        thread.update(cx, |thread, cx| {
2898            thread.push_user_content_block(Some(message_1_id.clone()), "world!".into(), cx);
2899        });
2900
2901        thread.update(cx, |thread, cx| {
2902            assert_eq!(thread.entries.len(), 1);
2903            if let AgentThreadEntry::UserMessage(user_msg) = &thread.entries[0] {
2904                assert_eq!(user_msg.id, Some(message_1_id));
2905                assert_eq!(user_msg.content.to_markdown(cx), "Hello, world!");
2906            } else {
2907                panic!("Expected UserMessage");
2908            }
2909        });
2910
2911        // Test creating new user message after assistant message
2912        thread.update(cx, |thread, cx| {
2913            thread.push_assistant_content_block("Assistant response".into(), false, cx);
2914        });
2915
2916        let message_2_id = UserMessageId::new();
2917        thread.update(cx, |thread, cx| {
2918            thread.push_user_content_block(
2919                Some(message_2_id.clone()),
2920                "New user message".into(),
2921                cx,
2922            );
2923        });
2924
2925        thread.update(cx, |thread, cx| {
2926            assert_eq!(thread.entries.len(), 3);
2927            if let AgentThreadEntry::UserMessage(user_msg) = &thread.entries[2] {
2928                assert_eq!(user_msg.id, Some(message_2_id));
2929                assert_eq!(user_msg.content.to_markdown(cx), "New user message");
2930            } else {
2931                panic!("Expected UserMessage at index 2");
2932            }
2933        });
2934    }
2935
2936    #[gpui::test]
2937    async fn test_thinking_concatenation(cx: &mut gpui::TestAppContext) {
2938        init_test(cx);
2939
2940        let fs = FakeFs::new(cx.executor());
2941        let project = Project::test(fs, [], cx).await;
2942        let connection = Rc::new(FakeAgentConnection::new().on_user_message(
2943            |_, thread, mut cx| {
2944                async move {
2945                    thread.update(&mut cx, |thread, cx| {
2946                        thread
2947                            .handle_session_update(
2948                                acp::SessionUpdate::AgentThoughtChunk(acp::ContentChunk::new(
2949                                    "Thinking ".into(),
2950                                )),
2951                                cx,
2952                            )
2953                            .unwrap();
2954                        thread
2955                            .handle_session_update(
2956                                acp::SessionUpdate::AgentThoughtChunk(acp::ContentChunk::new(
2957                                    "hard!".into(),
2958                                )),
2959                                cx,
2960                            )
2961                            .unwrap();
2962                    })?;
2963                    Ok(acp::PromptResponse::new(acp::StopReason::EndTurn))
2964                }
2965                .boxed_local()
2966            },
2967        ));
2968
2969        let thread = cx
2970            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
2971            .await
2972            .unwrap();
2973
2974        thread
2975            .update(cx, |thread, cx| thread.send_raw("Hello from Zed!", cx))
2976            .await
2977            .unwrap();
2978
2979        let output = thread.read_with(cx, |thread, cx| thread.to_markdown(cx));
2980        assert_eq!(
2981            output,
2982            indoc! {r#"
2983            ## User
2984
2985            Hello from Zed!
2986
2987            ## Assistant
2988
2989            <thinking>
2990            Thinking hard!
2991            </thinking>
2992
2993            "#}
2994        );
2995    }
2996
2997    #[gpui::test]
2998    async fn test_edits_concurrently_to_user(cx: &mut TestAppContext) {
2999        init_test(cx);
3000
3001        let fs = FakeFs::new(cx.executor());
3002        fs.insert_tree(path!("/tmp"), json!({"foo": "one\ntwo\nthree\n"}))
3003            .await;
3004        let project = Project::test(fs.clone(), [], cx).await;
3005        let (read_file_tx, read_file_rx) = oneshot::channel::<()>();
3006        let read_file_tx = Rc::new(RefCell::new(Some(read_file_tx)));
3007        let connection = Rc::new(FakeAgentConnection::new().on_user_message(
3008            move |_, thread, mut cx| {
3009                let read_file_tx = read_file_tx.clone();
3010                async move {
3011                    let content = thread
3012                        .update(&mut cx, |thread, cx| {
3013                            thread.read_text_file(path!("/tmp/foo").into(), None, None, false, cx)
3014                        })
3015                        .unwrap()
3016                        .await
3017                        .unwrap();
3018                    assert_eq!(content, "one\ntwo\nthree\n");
3019                    read_file_tx.take().unwrap().send(()).unwrap();
3020                    thread
3021                        .update(&mut cx, |thread, cx| {
3022                            thread.write_text_file(
3023                                path!("/tmp/foo").into(),
3024                                "one\ntwo\nthree\nfour\nfive\n".to_string(),
3025                                cx,
3026                            )
3027                        })
3028                        .unwrap()
3029                        .await
3030                        .unwrap();
3031                    Ok(acp::PromptResponse::new(acp::StopReason::EndTurn))
3032                }
3033                .boxed_local()
3034            },
3035        ));
3036
3037        let (worktree, pathbuf) = project
3038            .update(cx, |project, cx| {
3039                project.find_or_create_worktree(path!("/tmp/foo"), true, cx)
3040            })
3041            .await
3042            .unwrap();
3043        let buffer = project
3044            .update(cx, |project, cx| {
3045                project.open_buffer((worktree.read(cx).id(), pathbuf), cx)
3046            })
3047            .await
3048            .unwrap();
3049
3050        let thread = cx
3051            .update(|cx| connection.new_session(project, Path::new(path!("/tmp")), cx))
3052            .await
3053            .unwrap();
3054
3055        let request = thread.update(cx, |thread, cx| {
3056            thread.send_raw("Extend the count in /tmp/foo", cx)
3057        });
3058        read_file_rx.await.ok();
3059        buffer.update(cx, |buffer, cx| {
3060            buffer.edit([(0..0, "zero\n".to_string())], None, cx);
3061        });
3062        cx.run_until_parked();
3063        assert_eq!(
3064            buffer.read_with(cx, |buffer, _| buffer.text()),
3065            "zero\none\ntwo\nthree\nfour\nfive\n"
3066        );
3067        assert_eq!(
3068            String::from_utf8(fs.read_file_sync(path!("/tmp/foo")).unwrap()).unwrap(),
3069            "zero\none\ntwo\nthree\nfour\nfive\n"
3070        );
3071        request.await.unwrap();
3072    }
3073
3074    #[gpui::test]
3075    async fn test_reading_from_line(cx: &mut TestAppContext) {
3076        init_test(cx);
3077
3078        let fs = FakeFs::new(cx.executor());
3079        fs.insert_tree(path!("/tmp"), json!({"foo": "one\ntwo\nthree\nfour\n"}))
3080            .await;
3081        let project = Project::test(fs.clone(), [], cx).await;
3082        project
3083            .update(cx, |project, cx| {
3084                project.find_or_create_worktree(path!("/tmp/foo"), true, cx)
3085            })
3086            .await
3087            .unwrap();
3088
3089        let connection = Rc::new(FakeAgentConnection::new());
3090
3091        let thread = cx
3092            .update(|cx| connection.new_session(project, Path::new(path!("/tmp")), cx))
3093            .await
3094            .unwrap();
3095
3096        // Whole file
3097        let content = thread
3098            .update(cx, |thread, cx| {
3099                thread.read_text_file(path!("/tmp/foo").into(), None, None, false, cx)
3100            })
3101            .await
3102            .unwrap();
3103
3104        assert_eq!(content, "one\ntwo\nthree\nfour\n");
3105
3106        // Only start line
3107        let content = thread
3108            .update(cx, |thread, cx| {
3109                thread.read_text_file(path!("/tmp/foo").into(), Some(3), None, false, cx)
3110            })
3111            .await
3112            .unwrap();
3113
3114        assert_eq!(content, "three\nfour\n");
3115
3116        // Only limit
3117        let content = thread
3118            .update(cx, |thread, cx| {
3119                thread.read_text_file(path!("/tmp/foo").into(), None, Some(2), false, cx)
3120            })
3121            .await
3122            .unwrap();
3123
3124        assert_eq!(content, "one\ntwo\n");
3125
3126        // Range
3127        let content = thread
3128            .update(cx, |thread, cx| {
3129                thread.read_text_file(path!("/tmp/foo").into(), Some(2), Some(2), false, cx)
3130            })
3131            .await
3132            .unwrap();
3133
3134        assert_eq!(content, "two\nthree\n");
3135
3136        // Invalid
3137        let err = thread
3138            .update(cx, |thread, cx| {
3139                thread.read_text_file(path!("/tmp/foo").into(), Some(6), Some(2), false, cx)
3140            })
3141            .await
3142            .unwrap_err();
3143
3144        assert_eq!(
3145            err.to_string(),
3146            "Invalid params: \"Attempting to read beyond the end of the file, line 5:0\""
3147        );
3148    }
3149
3150    #[gpui::test]
3151    async fn test_reading_empty_file(cx: &mut TestAppContext) {
3152        init_test(cx);
3153
3154        let fs = FakeFs::new(cx.executor());
3155        fs.insert_tree(path!("/tmp"), json!({"foo": ""})).await;
3156        let project = Project::test(fs.clone(), [], cx).await;
3157        project
3158            .update(cx, |project, cx| {
3159                project.find_or_create_worktree(path!("/tmp/foo"), true, cx)
3160            })
3161            .await
3162            .unwrap();
3163
3164        let connection = Rc::new(FakeAgentConnection::new());
3165
3166        let thread = cx
3167            .update(|cx| connection.new_session(project, Path::new(path!("/tmp")), cx))
3168            .await
3169            .unwrap();
3170
3171        // Whole file
3172        let content = thread
3173            .update(cx, |thread, cx| {
3174                thread.read_text_file(path!("/tmp/foo").into(), None, None, false, cx)
3175            })
3176            .await
3177            .unwrap();
3178
3179        assert_eq!(content, "");
3180
3181        // Only start line
3182        let content = thread
3183            .update(cx, |thread, cx| {
3184                thread.read_text_file(path!("/tmp/foo").into(), Some(1), None, false, cx)
3185            })
3186            .await
3187            .unwrap();
3188
3189        assert_eq!(content, "");
3190
3191        // Only limit
3192        let content = thread
3193            .update(cx, |thread, cx| {
3194                thread.read_text_file(path!("/tmp/foo").into(), None, Some(2), false, cx)
3195            })
3196            .await
3197            .unwrap();
3198
3199        assert_eq!(content, "");
3200
3201        // Range
3202        let content = thread
3203            .update(cx, |thread, cx| {
3204                thread.read_text_file(path!("/tmp/foo").into(), Some(1), Some(1), false, cx)
3205            })
3206            .await
3207            .unwrap();
3208
3209        assert_eq!(content, "");
3210
3211        // Invalid
3212        let err = thread
3213            .update(cx, |thread, cx| {
3214                thread.read_text_file(path!("/tmp/foo").into(), Some(5), Some(2), false, cx)
3215            })
3216            .await
3217            .unwrap_err();
3218
3219        assert_eq!(
3220            err.to_string(),
3221            "Invalid params: \"Attempting to read beyond the end of the file, line 1:0\""
3222        );
3223    }
3224    #[gpui::test]
3225    async fn test_reading_non_existing_file(cx: &mut TestAppContext) {
3226        init_test(cx);
3227
3228        let fs = FakeFs::new(cx.executor());
3229        fs.insert_tree(path!("/tmp"), json!({})).await;
3230        let project = Project::test(fs.clone(), [], cx).await;
3231        project
3232            .update(cx, |project, cx| {
3233                project.find_or_create_worktree(path!("/tmp"), true, cx)
3234            })
3235            .await
3236            .unwrap();
3237
3238        let connection = Rc::new(FakeAgentConnection::new());
3239
3240        let thread = cx
3241            .update(|cx| connection.new_session(project, Path::new(path!("/tmp")), cx))
3242            .await
3243            .unwrap();
3244
3245        // Out of project file
3246        let err = thread
3247            .update(cx, |thread, cx| {
3248                thread.read_text_file(path!("/foo").into(), None, None, false, cx)
3249            })
3250            .await
3251            .unwrap_err();
3252
3253        assert_eq!(err.code, acp::ErrorCode::ResourceNotFound);
3254    }
3255
3256    #[gpui::test]
3257    async fn test_succeeding_canceled_toolcall(cx: &mut TestAppContext) {
3258        init_test(cx);
3259
3260        let fs = FakeFs::new(cx.executor());
3261        let project = Project::test(fs, [], cx).await;
3262        let id = acp::ToolCallId::new("test");
3263
3264        let connection = Rc::new(FakeAgentConnection::new().on_user_message({
3265            let id = id.clone();
3266            move |_, thread, mut cx| {
3267                let id = id.clone();
3268                async move {
3269                    thread
3270                        .update(&mut cx, |thread, cx| {
3271                            thread.handle_session_update(
3272                                acp::SessionUpdate::ToolCall(
3273                                    acp::ToolCall::new(id.clone(), "Label")
3274                                        .kind(acp::ToolKind::Fetch)
3275                                        .status(acp::ToolCallStatus::InProgress),
3276                                ),
3277                                cx,
3278                            )
3279                        })
3280                        .unwrap()
3281                        .unwrap();
3282                    Ok(acp::PromptResponse::new(acp::StopReason::EndTurn))
3283                }
3284                .boxed_local()
3285            }
3286        }));
3287
3288        let thread = cx
3289            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
3290            .await
3291            .unwrap();
3292
3293        let request = thread.update(cx, |thread, cx| {
3294            thread.send_raw("Fetch https://example.com", cx)
3295        });
3296
3297        run_until_first_tool_call(&thread, cx).await;
3298
3299        thread.read_with(cx, |thread, _| {
3300            assert!(matches!(
3301                thread.entries[1],
3302                AgentThreadEntry::ToolCall(ToolCall {
3303                    status: ToolCallStatus::InProgress,
3304                    ..
3305                })
3306            ));
3307        });
3308
3309        thread.update(cx, |thread, cx| thread.cancel(cx)).await;
3310
3311        thread.read_with(cx, |thread, _| {
3312            assert!(matches!(
3313                &thread.entries[1],
3314                AgentThreadEntry::ToolCall(ToolCall {
3315                    status: ToolCallStatus::Canceled,
3316                    ..
3317                })
3318            ));
3319        });
3320
3321        thread
3322            .update(cx, |thread, cx| {
3323                thread.handle_session_update(
3324                    acp::SessionUpdate::ToolCallUpdate(acp::ToolCallUpdate::new(
3325                        id,
3326                        acp::ToolCallUpdateFields::new().status(acp::ToolCallStatus::Completed),
3327                    )),
3328                    cx,
3329                )
3330            })
3331            .unwrap();
3332
3333        request.await.unwrap();
3334
3335        thread.read_with(cx, |thread, _| {
3336            assert!(matches!(
3337                thread.entries[1],
3338                AgentThreadEntry::ToolCall(ToolCall {
3339                    status: ToolCallStatus::Completed,
3340                    ..
3341                })
3342            ));
3343        });
3344    }
3345
3346    #[gpui::test]
3347    async fn test_no_pending_edits_if_tool_calls_are_completed(cx: &mut TestAppContext) {
3348        init_test(cx);
3349        let fs = FakeFs::new(cx.background_executor.clone());
3350        fs.insert_tree(path!("/test"), json!({})).await;
3351        let project = Project::test(fs, [path!("/test").as_ref()], cx).await;
3352
3353        let connection = Rc::new(FakeAgentConnection::new().on_user_message({
3354            move |_, thread, mut cx| {
3355                async move {
3356                    thread
3357                        .update(&mut cx, |thread, cx| {
3358                            thread.handle_session_update(
3359                                acp::SessionUpdate::ToolCall(
3360                                    acp::ToolCall::new("test", "Label")
3361                                        .kind(acp::ToolKind::Edit)
3362                                        .status(acp::ToolCallStatus::Completed)
3363                                        .content(vec![acp::ToolCallContent::Diff(acp::Diff::new(
3364                                            "/test/test.txt",
3365                                            "foo",
3366                                        ))]),
3367                                ),
3368                                cx,
3369                            )
3370                        })
3371                        .unwrap()
3372                        .unwrap();
3373                    Ok(acp::PromptResponse::new(acp::StopReason::EndTurn))
3374                }
3375                .boxed_local()
3376            }
3377        }));
3378
3379        let thread = cx
3380            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
3381            .await
3382            .unwrap();
3383
3384        cx.update(|cx| thread.update(cx, |thread, cx| thread.send(vec!["Hi".into()], cx)))
3385            .await
3386            .unwrap();
3387
3388        assert!(cx.read(|cx| !thread.read(cx).has_pending_edit_tool_calls()));
3389    }
3390
3391    #[gpui::test(iterations = 10)]
3392    async fn test_checkpoints(cx: &mut TestAppContext) {
3393        init_test(cx);
3394        let fs = FakeFs::new(cx.background_executor.clone());
3395        fs.insert_tree(
3396            path!("/test"),
3397            json!({
3398                ".git": {}
3399            }),
3400        )
3401        .await;
3402        let project = Project::test(fs.clone(), [path!("/test").as_ref()], cx).await;
3403
3404        let simulate_changes = Arc::new(AtomicBool::new(true));
3405        let next_filename = Arc::new(AtomicUsize::new(0));
3406        let connection = Rc::new(FakeAgentConnection::new().on_user_message({
3407            let simulate_changes = simulate_changes.clone();
3408            let next_filename = next_filename.clone();
3409            let fs = fs.clone();
3410            move |request, thread, mut cx| {
3411                let fs = fs.clone();
3412                let simulate_changes = simulate_changes.clone();
3413                let next_filename = next_filename.clone();
3414                async move {
3415                    if simulate_changes.load(SeqCst) {
3416                        let filename = format!("/test/file-{}", next_filename.fetch_add(1, SeqCst));
3417                        fs.write(Path::new(&filename), b"").await?;
3418                    }
3419
3420                    let acp::ContentBlock::Text(content) = &request.prompt[0] else {
3421                        panic!("expected text content block");
3422                    };
3423                    thread.update(&mut cx, |thread, cx| {
3424                        thread
3425                            .handle_session_update(
3426                                acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk::new(
3427                                    content.text.to_uppercase().into(),
3428                                )),
3429                                cx,
3430                            )
3431                            .unwrap();
3432                    })?;
3433                    Ok(acp::PromptResponse::new(acp::StopReason::EndTurn))
3434                }
3435                .boxed_local()
3436            }
3437        }));
3438        let thread = cx
3439            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
3440            .await
3441            .unwrap();
3442
3443        cx.update(|cx| thread.update(cx, |thread, cx| thread.send(vec!["Lorem".into()], cx)))
3444            .await
3445            .unwrap();
3446        thread.read_with(cx, |thread, cx| {
3447            assert_eq!(
3448                thread.to_markdown(cx),
3449                indoc! {"
3450                    ## User (checkpoint)
3451
3452                    Lorem
3453
3454                    ## Assistant
3455
3456                    LOREM
3457
3458                "}
3459            );
3460        });
3461        assert_eq!(fs.files(), vec![Path::new(path!("/test/file-0"))]);
3462
3463        cx.update(|cx| thread.update(cx, |thread, cx| thread.send(vec!["ipsum".into()], cx)))
3464            .await
3465            .unwrap();
3466        thread.read_with(cx, |thread, cx| {
3467            assert_eq!(
3468                thread.to_markdown(cx),
3469                indoc! {"
3470                    ## User (checkpoint)
3471
3472                    Lorem
3473
3474                    ## Assistant
3475
3476                    LOREM
3477
3478                    ## User (checkpoint)
3479
3480                    ipsum
3481
3482                    ## Assistant
3483
3484                    IPSUM
3485
3486                "}
3487            );
3488        });
3489        assert_eq!(
3490            fs.files(),
3491            vec![
3492                Path::new(path!("/test/file-0")),
3493                Path::new(path!("/test/file-1"))
3494            ]
3495        );
3496
3497        // Checkpoint isn't stored when there are no changes.
3498        simulate_changes.store(false, SeqCst);
3499        cx.update(|cx| thread.update(cx, |thread, cx| thread.send(vec!["dolor".into()], cx)))
3500            .await
3501            .unwrap();
3502        thread.read_with(cx, |thread, cx| {
3503            assert_eq!(
3504                thread.to_markdown(cx),
3505                indoc! {"
3506                    ## User (checkpoint)
3507
3508                    Lorem
3509
3510                    ## Assistant
3511
3512                    LOREM
3513
3514                    ## User (checkpoint)
3515
3516                    ipsum
3517
3518                    ## Assistant
3519
3520                    IPSUM
3521
3522                    ## User
3523
3524                    dolor
3525
3526                    ## Assistant
3527
3528                    DOLOR
3529
3530                "}
3531            );
3532        });
3533        assert_eq!(
3534            fs.files(),
3535            vec![
3536                Path::new(path!("/test/file-0")),
3537                Path::new(path!("/test/file-1"))
3538            ]
3539        );
3540
3541        // Rewinding the conversation truncates the history and restores the checkpoint.
3542        thread
3543            .update(cx, |thread, cx| {
3544                let AgentThreadEntry::UserMessage(message) = &thread.entries[2] else {
3545                    panic!("unexpected entries {:?}", thread.entries)
3546                };
3547                thread.restore_checkpoint(message.id.clone().unwrap(), cx)
3548            })
3549            .await
3550            .unwrap();
3551        thread.read_with(cx, |thread, cx| {
3552            assert_eq!(
3553                thread.to_markdown(cx),
3554                indoc! {"
3555                    ## User (checkpoint)
3556
3557                    Lorem
3558
3559                    ## Assistant
3560
3561                    LOREM
3562
3563                "}
3564            );
3565        });
3566        assert_eq!(fs.files(), vec![Path::new(path!("/test/file-0"))]);
3567    }
3568
3569    #[gpui::test]
3570    async fn test_tool_result_refusal(cx: &mut TestAppContext) {
3571        use std::sync::atomic::AtomicUsize;
3572        init_test(cx);
3573
3574        let fs = FakeFs::new(cx.executor());
3575        let project = Project::test(fs, None, cx).await;
3576
3577        // Create a connection that simulates refusal after tool result
3578        let prompt_count = Arc::new(AtomicUsize::new(0));
3579        let connection = Rc::new(FakeAgentConnection::new().on_user_message({
3580            let prompt_count = prompt_count.clone();
3581            move |_request, thread, mut cx| {
3582                let count = prompt_count.fetch_add(1, SeqCst);
3583                async move {
3584                    if count == 0 {
3585                        // First prompt: Generate a tool call with result
3586                        thread.update(&mut cx, |thread, cx| {
3587                            thread
3588                                .handle_session_update(
3589                                    acp::SessionUpdate::ToolCall(
3590                                        acp::ToolCall::new("tool1", "Test Tool")
3591                                            .kind(acp::ToolKind::Fetch)
3592                                            .status(acp::ToolCallStatus::Completed)
3593                                            .raw_input(serde_json::json!({"query": "test"}))
3594                                            .raw_output(serde_json::json!({"result": "inappropriate content"})),
3595                                    ),
3596                                    cx,
3597                                )
3598                                .unwrap();
3599                        })?;
3600
3601                        // Now return refusal because of the tool result
3602                        Ok(acp::PromptResponse::new(acp::StopReason::Refusal))
3603                    } else {
3604                        Ok(acp::PromptResponse::new(acp::StopReason::EndTurn))
3605                    }
3606                }
3607                .boxed_local()
3608            }
3609        }));
3610
3611        let thread = cx
3612            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
3613            .await
3614            .unwrap();
3615
3616        // Track if we see a Refusal event
3617        let saw_refusal_event = Arc::new(std::sync::Mutex::new(false));
3618        let saw_refusal_event_captured = saw_refusal_event.clone();
3619        thread.update(cx, |_thread, cx| {
3620            cx.subscribe(
3621                &thread,
3622                move |_thread, _event_thread, event: &AcpThreadEvent, _cx| {
3623                    if matches!(event, AcpThreadEvent::Refusal) {
3624                        *saw_refusal_event_captured.lock().unwrap() = true;
3625                    }
3626                },
3627            )
3628            .detach();
3629        });
3630
3631        // Send a user message - this will trigger tool call and then refusal
3632        let send_task = thread.update(cx, |thread, cx| thread.send(vec!["Hello".into()], cx));
3633        cx.background_executor.spawn(send_task).detach();
3634        cx.run_until_parked();
3635
3636        // Verify that:
3637        // 1. A Refusal event WAS emitted (because it's a tool result refusal, not user prompt)
3638        // 2. The user message was NOT truncated
3639        assert!(
3640            *saw_refusal_event.lock().unwrap(),
3641            "Refusal event should be emitted for tool result refusals"
3642        );
3643
3644        thread.read_with(cx, |thread, _| {
3645            let entries = thread.entries();
3646            assert!(entries.len() >= 2, "Should have user message and tool call");
3647
3648            // Verify user message is still there
3649            assert!(
3650                matches!(entries[0], AgentThreadEntry::UserMessage(_)),
3651                "User message should not be truncated"
3652            );
3653
3654            // Verify tool call is there with result
3655            if let AgentThreadEntry::ToolCall(tool_call) = &entries[1] {
3656                assert!(
3657                    tool_call.raw_output.is_some(),
3658                    "Tool call should have output"
3659                );
3660            } else {
3661                panic!("Expected tool call at index 1");
3662            }
3663        });
3664    }
3665
3666    #[gpui::test]
3667    async fn test_user_prompt_refusal_emits_event(cx: &mut TestAppContext) {
3668        init_test(cx);
3669
3670        let fs = FakeFs::new(cx.executor());
3671        let project = Project::test(fs, None, cx).await;
3672
3673        let refuse_next = Arc::new(AtomicBool::new(false));
3674        let connection = Rc::new(FakeAgentConnection::new().on_user_message({
3675            let refuse_next = refuse_next.clone();
3676            move |_request, _thread, _cx| {
3677                if refuse_next.load(SeqCst) {
3678                    async move { Ok(acp::PromptResponse::new(acp::StopReason::Refusal)) }
3679                        .boxed_local()
3680                } else {
3681                    async move { Ok(acp::PromptResponse::new(acp::StopReason::EndTurn)) }
3682                        .boxed_local()
3683                }
3684            }
3685        }));
3686
3687        let thread = cx
3688            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
3689            .await
3690            .unwrap();
3691
3692        // Track if we see a Refusal event
3693        let saw_refusal_event = Arc::new(std::sync::Mutex::new(false));
3694        let saw_refusal_event_captured = saw_refusal_event.clone();
3695        thread.update(cx, |_thread, cx| {
3696            cx.subscribe(
3697                &thread,
3698                move |_thread, _event_thread, event: &AcpThreadEvent, _cx| {
3699                    if matches!(event, AcpThreadEvent::Refusal) {
3700                        *saw_refusal_event_captured.lock().unwrap() = true;
3701                    }
3702                },
3703            )
3704            .detach();
3705        });
3706
3707        // Send a message that will be refused
3708        refuse_next.store(true, SeqCst);
3709        cx.update(|cx| thread.update(cx, |thread, cx| thread.send(vec!["hello".into()], cx)))
3710            .await
3711            .unwrap();
3712
3713        // Verify that a Refusal event WAS emitted for user prompt refusal
3714        assert!(
3715            *saw_refusal_event.lock().unwrap(),
3716            "Refusal event should be emitted for user prompt refusals"
3717        );
3718
3719        // Verify the message was truncated (user prompt refusal)
3720        thread.read_with(cx, |thread, cx| {
3721            assert_eq!(thread.to_markdown(cx), "");
3722        });
3723    }
3724
3725    #[gpui::test]
3726    async fn test_refusal(cx: &mut TestAppContext) {
3727        init_test(cx);
3728        let fs = FakeFs::new(cx.background_executor.clone());
3729        fs.insert_tree(path!("/"), json!({})).await;
3730        let project = Project::test(fs.clone(), [path!("/").as_ref()], cx).await;
3731
3732        let refuse_next = Arc::new(AtomicBool::new(false));
3733        let connection = Rc::new(FakeAgentConnection::new().on_user_message({
3734            let refuse_next = refuse_next.clone();
3735            move |request, thread, mut cx| {
3736                let refuse_next = refuse_next.clone();
3737                async move {
3738                    if refuse_next.load(SeqCst) {
3739                        return Ok(acp::PromptResponse::new(acp::StopReason::Refusal));
3740                    }
3741
3742                    let acp::ContentBlock::Text(content) = &request.prompt[0] else {
3743                        panic!("expected text content block");
3744                    };
3745                    thread.update(&mut cx, |thread, cx| {
3746                        thread
3747                            .handle_session_update(
3748                                acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk::new(
3749                                    content.text.to_uppercase().into(),
3750                                )),
3751                                cx,
3752                            )
3753                            .unwrap();
3754                    })?;
3755                    Ok(acp::PromptResponse::new(acp::StopReason::EndTurn))
3756                }
3757                .boxed_local()
3758            }
3759        }));
3760        let thread = cx
3761            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
3762            .await
3763            .unwrap();
3764
3765        cx.update(|cx| thread.update(cx, |thread, cx| thread.send(vec!["hello".into()], cx)))
3766            .await
3767            .unwrap();
3768        thread.read_with(cx, |thread, cx| {
3769            assert_eq!(
3770                thread.to_markdown(cx),
3771                indoc! {"
3772                    ## User
3773
3774                    hello
3775
3776                    ## Assistant
3777
3778                    HELLO
3779
3780                "}
3781            );
3782        });
3783
3784        // Simulate refusing the second message. The message should be truncated
3785        // when a user prompt is refused.
3786        refuse_next.store(true, SeqCst);
3787        cx.update(|cx| thread.update(cx, |thread, cx| thread.send(vec!["world".into()], cx)))
3788            .await
3789            .unwrap();
3790        thread.read_with(cx, |thread, cx| {
3791            assert_eq!(
3792                thread.to_markdown(cx),
3793                indoc! {"
3794                    ## User
3795
3796                    hello
3797
3798                    ## Assistant
3799
3800                    HELLO
3801
3802                "}
3803            );
3804        });
3805    }
3806
3807    async fn run_until_first_tool_call(
3808        thread: &Entity<AcpThread>,
3809        cx: &mut TestAppContext,
3810    ) -> usize {
3811        let (mut tx, mut rx) = mpsc::channel::<usize>(1);
3812
3813        let subscription = cx.update(|cx| {
3814            cx.subscribe(thread, move |thread, _, cx| {
3815                for (ix, entry) in thread.read(cx).entries.iter().enumerate() {
3816                    if matches!(entry, AgentThreadEntry::ToolCall(_)) {
3817                        return tx.try_send(ix).unwrap();
3818                    }
3819                }
3820            })
3821        });
3822
3823        select! {
3824            _ = futures::FutureExt::fuse(cx.background_executor.timer(Duration::from_secs(10))) => {
3825                panic!("Timeout waiting for tool call")
3826            }
3827            ix = rx.next().fuse() => {
3828                drop(subscription);
3829                ix.unwrap()
3830            }
3831        }
3832    }
3833
3834    #[derive(Clone, Default)]
3835    struct FakeAgentConnection {
3836        auth_methods: Vec<acp::AuthMethod>,
3837        sessions: Arc<parking_lot::Mutex<HashMap<acp::SessionId, WeakEntity<AcpThread>>>>,
3838        on_user_message: Option<
3839            Rc<
3840                dyn Fn(
3841                        acp::PromptRequest,
3842                        WeakEntity<AcpThread>,
3843                        AsyncApp,
3844                    ) -> LocalBoxFuture<'static, Result<acp::PromptResponse>>
3845                    + 'static,
3846            >,
3847        >,
3848    }
3849
3850    impl FakeAgentConnection {
3851        fn new() -> Self {
3852            Self {
3853                auth_methods: Vec::new(),
3854                on_user_message: None,
3855                sessions: Arc::default(),
3856            }
3857        }
3858
3859        #[expect(unused)]
3860        fn with_auth_methods(mut self, auth_methods: Vec<acp::AuthMethod>) -> Self {
3861            self.auth_methods = auth_methods;
3862            self
3863        }
3864
3865        fn on_user_message(
3866            mut self,
3867            handler: impl Fn(
3868                acp::PromptRequest,
3869                WeakEntity<AcpThread>,
3870                AsyncApp,
3871            ) -> LocalBoxFuture<'static, Result<acp::PromptResponse>>
3872            + 'static,
3873        ) -> Self {
3874            self.on_user_message.replace(Rc::new(handler));
3875            self
3876        }
3877    }
3878
3879    impl AgentConnection for FakeAgentConnection {
3880        fn telemetry_id(&self) -> SharedString {
3881            "fake".into()
3882        }
3883
3884        fn auth_methods(&self) -> &[acp::AuthMethod] {
3885            &self.auth_methods
3886        }
3887
3888        fn new_session(
3889            self: Rc<Self>,
3890            project: Entity<Project>,
3891            _cwd: &Path,
3892            cx: &mut App,
3893        ) -> Task<gpui::Result<Entity<AcpThread>>> {
3894            let session_id = acp::SessionId::new(
3895                rand::rng()
3896                    .sample_iter(&distr::Alphanumeric)
3897                    .take(7)
3898                    .map(char::from)
3899                    .collect::<String>(),
3900            );
3901            let action_log = cx.new(|_| ActionLog::new(project.clone()));
3902            let thread = cx.new(|cx| {
3903                AcpThread::new(
3904                    None,
3905                    "Test",
3906                    self.clone(),
3907                    project,
3908                    action_log,
3909                    session_id.clone(),
3910                    watch::Receiver::constant(
3911                        acp::PromptCapabilities::new()
3912                            .image(true)
3913                            .audio(true)
3914                            .embedded_context(true),
3915                    ),
3916                    cx,
3917                )
3918            });
3919            self.sessions.lock().insert(session_id, thread.downgrade());
3920            Task::ready(Ok(thread))
3921        }
3922
3923        fn authenticate(&self, method: acp::AuthMethodId, _cx: &mut App) -> Task<gpui::Result<()>> {
3924            if self.auth_methods().iter().any(|m| m.id == method) {
3925                Task::ready(Ok(()))
3926            } else {
3927                Task::ready(Err(anyhow!("Invalid Auth Method")))
3928            }
3929        }
3930
3931        fn prompt(
3932            &self,
3933            _id: Option<UserMessageId>,
3934            params: acp::PromptRequest,
3935            cx: &mut App,
3936        ) -> Task<gpui::Result<acp::PromptResponse>> {
3937            let sessions = self.sessions.lock();
3938            let thread = sessions.get(&params.session_id).unwrap();
3939            if let Some(handler) = &self.on_user_message {
3940                let handler = handler.clone();
3941                let thread = thread.clone();
3942                cx.spawn(async move |cx| handler(params, thread, cx.clone()).await)
3943            } else {
3944                Task::ready(Ok(acp::PromptResponse::new(acp::StopReason::EndTurn)))
3945            }
3946        }
3947
3948        fn cancel(&self, session_id: &acp::SessionId, cx: &mut App) {
3949            let sessions = self.sessions.lock();
3950            let thread = sessions.get(session_id).unwrap().clone();
3951
3952            cx.spawn(async move |cx| {
3953                thread
3954                    .update(cx, |thread, cx| thread.cancel(cx))
3955                    .unwrap()
3956                    .await
3957            })
3958            .detach();
3959        }
3960
3961        fn truncate(
3962            &self,
3963            session_id: &acp::SessionId,
3964            _cx: &App,
3965        ) -> Option<Rc<dyn AgentSessionTruncate>> {
3966            Some(Rc::new(FakeAgentSessionEditor {
3967                _session_id: session_id.clone(),
3968            }))
3969        }
3970
3971        fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
3972            self
3973        }
3974    }
3975
3976    struct FakeAgentSessionEditor {
3977        _session_id: acp::SessionId,
3978    }
3979
3980    impl AgentSessionTruncate for FakeAgentSessionEditor {
3981        fn run(&self, _message_id: UserMessageId, _cx: &mut App) -> Task<Result<()>> {
3982            Task::ready(Ok(()))
3983        }
3984    }
3985
3986    #[gpui::test]
3987    async fn test_tool_call_not_found_creates_failed_entry(cx: &mut TestAppContext) {
3988        init_test(cx);
3989
3990        let fs = FakeFs::new(cx.executor());
3991        let project = Project::test(fs, [], cx).await;
3992        let connection = Rc::new(FakeAgentConnection::new());
3993        let thread = cx
3994            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
3995            .await
3996            .unwrap();
3997
3998        // Try to update a tool call that doesn't exist
3999        let nonexistent_id = acp::ToolCallId::new("nonexistent-tool-call");
4000        thread.update(cx, |thread, cx| {
4001            let result = thread.handle_session_update(
4002                acp::SessionUpdate::ToolCallUpdate(acp::ToolCallUpdate::new(
4003                    nonexistent_id.clone(),
4004                    acp::ToolCallUpdateFields::new().status(acp::ToolCallStatus::Completed),
4005                )),
4006                cx,
4007            );
4008
4009            // The update should succeed (not return an error)
4010            assert!(result.is_ok());
4011
4012            // There should now be exactly one entry in the thread
4013            assert_eq!(thread.entries.len(), 1);
4014
4015            // The entry should be a failed tool call
4016            if let AgentThreadEntry::ToolCall(tool_call) = &thread.entries[0] {
4017                assert_eq!(tool_call.id, nonexistent_id);
4018                assert!(matches!(tool_call.status, ToolCallStatus::Failed));
4019                assert_eq!(tool_call.kind, acp::ToolKind::Fetch);
4020
4021                // Check that the content contains the error message
4022                assert_eq!(tool_call.content.len(), 1);
4023                if let ToolCallContent::ContentBlock(content_block) = &tool_call.content[0] {
4024                    match content_block {
4025                        ContentBlock::Markdown { markdown } => {
4026                            let markdown_text = markdown.read(cx).source();
4027                            assert!(markdown_text.contains("Tool call not found"));
4028                        }
4029                        ContentBlock::Empty => panic!("Expected markdown content, got empty"),
4030                        ContentBlock::ResourceLink { .. } => {
4031                            panic!("Expected markdown content, got resource link")
4032                        }
4033                        ContentBlock::Image { .. } => {
4034                            panic!("Expected markdown content, got image")
4035                        }
4036                    }
4037                } else {
4038                    panic!("Expected ContentBlock, got: {:?}", tool_call.content[0]);
4039                }
4040            } else {
4041                panic!("Expected ToolCall entry, got: {:?}", thread.entries[0]);
4042            }
4043        });
4044    }
4045
4046    /// Tests that restoring a checkpoint properly cleans up terminals that were
4047    /// created after that checkpoint, and cancels any in-progress generation.
4048    ///
4049    /// Reproduces issue #35142: When a checkpoint is restored, any terminal processes
4050    /// that were started after that checkpoint should be terminated, and any in-progress
4051    /// AI generation should be canceled.
4052    #[gpui::test]
4053    async fn test_restore_checkpoint_kills_terminal(cx: &mut TestAppContext) {
4054        init_test(cx);
4055
4056        let fs = FakeFs::new(cx.executor());
4057        let project = Project::test(fs, [], cx).await;
4058        let connection = Rc::new(FakeAgentConnection::new());
4059        let thread = cx
4060            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
4061            .await
4062            .unwrap();
4063
4064        // Send first user message to create a checkpoint
4065        cx.update(|cx| {
4066            thread.update(cx, |thread, cx| {
4067                thread.send(vec!["first message".into()], cx)
4068            })
4069        })
4070        .await
4071        .unwrap();
4072
4073        // Send second message (creates another checkpoint) - we'll restore to this one
4074        cx.update(|cx| {
4075            thread.update(cx, |thread, cx| {
4076                thread.send(vec!["second message".into()], cx)
4077            })
4078        })
4079        .await
4080        .unwrap();
4081
4082        // Create 2 terminals BEFORE the checkpoint that have completed running
4083        let terminal_id_1 = acp::TerminalId::new(uuid::Uuid::new_v4().to_string());
4084        let mock_terminal_1 = cx.new(|cx| {
4085            let builder = ::terminal::TerminalBuilder::new_display_only(
4086                ::terminal::terminal_settings::CursorShape::default(),
4087                ::terminal::terminal_settings::AlternateScroll::On,
4088                None,
4089                0,
4090                cx.background_executor(),
4091                PathStyle::local(),
4092            )
4093            .unwrap();
4094            builder.subscribe(cx)
4095        });
4096
4097        thread.update(cx, |thread, cx| {
4098            thread.on_terminal_provider_event(
4099                TerminalProviderEvent::Created {
4100                    terminal_id: terminal_id_1.clone(),
4101                    label: "echo 'first'".to_string(),
4102                    cwd: Some(PathBuf::from("/test")),
4103                    output_byte_limit: None,
4104                    terminal: mock_terminal_1.clone(),
4105                },
4106                cx,
4107            );
4108        });
4109
4110        thread.update(cx, |thread, cx| {
4111            thread.on_terminal_provider_event(
4112                TerminalProviderEvent::Output {
4113                    terminal_id: terminal_id_1.clone(),
4114                    data: b"first\n".to_vec(),
4115                },
4116                cx,
4117            );
4118        });
4119
4120        thread.update(cx, |thread, cx| {
4121            thread.on_terminal_provider_event(
4122                TerminalProviderEvent::Exit {
4123                    terminal_id: terminal_id_1.clone(),
4124                    status: acp::TerminalExitStatus::new().exit_code(0),
4125                },
4126                cx,
4127            );
4128        });
4129
4130        let terminal_id_2 = acp::TerminalId::new(uuid::Uuid::new_v4().to_string());
4131        let mock_terminal_2 = cx.new(|cx| {
4132            let builder = ::terminal::TerminalBuilder::new_display_only(
4133                ::terminal::terminal_settings::CursorShape::default(),
4134                ::terminal::terminal_settings::AlternateScroll::On,
4135                None,
4136                0,
4137                cx.background_executor(),
4138                PathStyle::local(),
4139            )
4140            .unwrap();
4141            builder.subscribe(cx)
4142        });
4143
4144        thread.update(cx, |thread, cx| {
4145            thread.on_terminal_provider_event(
4146                TerminalProviderEvent::Created {
4147                    terminal_id: terminal_id_2.clone(),
4148                    label: "echo 'second'".to_string(),
4149                    cwd: Some(PathBuf::from("/test")),
4150                    output_byte_limit: None,
4151                    terminal: mock_terminal_2.clone(),
4152                },
4153                cx,
4154            );
4155        });
4156
4157        thread.update(cx, |thread, cx| {
4158            thread.on_terminal_provider_event(
4159                TerminalProviderEvent::Output {
4160                    terminal_id: terminal_id_2.clone(),
4161                    data: b"second\n".to_vec(),
4162                },
4163                cx,
4164            );
4165        });
4166
4167        thread.update(cx, |thread, cx| {
4168            thread.on_terminal_provider_event(
4169                TerminalProviderEvent::Exit {
4170                    terminal_id: terminal_id_2.clone(),
4171                    status: acp::TerminalExitStatus::new().exit_code(0),
4172                },
4173                cx,
4174            );
4175        });
4176
4177        // Get the second message ID to restore to
4178        let second_message_id = thread.read_with(cx, |thread, _| {
4179            // At this point we have:
4180            // - Index 0: First user message (with checkpoint)
4181            // - Index 1: Second user message (with checkpoint)
4182            // No assistant responses because FakeAgentConnection just returns EndTurn
4183            let AgentThreadEntry::UserMessage(message) = &thread.entries[1] else {
4184                panic!("expected user message at index 1");
4185            };
4186            message.id.clone().unwrap()
4187        });
4188
4189        // Create a terminal AFTER the checkpoint we'll restore to.
4190        // This simulates the AI agent starting a long-running terminal command.
4191        let terminal_id = acp::TerminalId::new(uuid::Uuid::new_v4().to_string());
4192        let mock_terminal = cx.new(|cx| {
4193            let builder = ::terminal::TerminalBuilder::new_display_only(
4194                ::terminal::terminal_settings::CursorShape::default(),
4195                ::terminal::terminal_settings::AlternateScroll::On,
4196                None,
4197                0,
4198                cx.background_executor(),
4199                PathStyle::local(),
4200            )
4201            .unwrap();
4202            builder.subscribe(cx)
4203        });
4204
4205        // Register the terminal as created
4206        thread.update(cx, |thread, cx| {
4207            thread.on_terminal_provider_event(
4208                TerminalProviderEvent::Created {
4209                    terminal_id: terminal_id.clone(),
4210                    label: "sleep 1000".to_string(),
4211                    cwd: Some(PathBuf::from("/test")),
4212                    output_byte_limit: None,
4213                    terminal: mock_terminal.clone(),
4214                },
4215                cx,
4216            );
4217        });
4218
4219        // Simulate the terminal producing output (still running)
4220        thread.update(cx, |thread, cx| {
4221            thread.on_terminal_provider_event(
4222                TerminalProviderEvent::Output {
4223                    terminal_id: terminal_id.clone(),
4224                    data: b"terminal is running...\n".to_vec(),
4225                },
4226                cx,
4227            );
4228        });
4229
4230        // Create a tool call entry that references this terminal
4231        // This represents the agent requesting a terminal command
4232        thread.update(cx, |thread, cx| {
4233            thread
4234                .handle_session_update(
4235                    acp::SessionUpdate::ToolCall(
4236                        acp::ToolCall::new("terminal-tool-1", "Running command")
4237                            .kind(acp::ToolKind::Execute)
4238                            .status(acp::ToolCallStatus::InProgress)
4239                            .content(vec![acp::ToolCallContent::Terminal(acp::Terminal::new(
4240                                terminal_id.clone(),
4241                            ))])
4242                            .raw_input(serde_json::json!({"command": "sleep 1000", "cd": "/test"})),
4243                    ),
4244                    cx,
4245                )
4246                .unwrap();
4247        });
4248
4249        // Verify terminal exists and is in the thread
4250        let terminal_exists_before =
4251            thread.read_with(cx, |thread, _| thread.terminals.contains_key(&terminal_id));
4252        assert!(
4253            terminal_exists_before,
4254            "Terminal should exist before checkpoint restore"
4255        );
4256
4257        // Verify the terminal's underlying task is still running (not completed)
4258        let terminal_running_before = thread.read_with(cx, |thread, _cx| {
4259            let terminal_entity = thread.terminals.get(&terminal_id).unwrap();
4260            terminal_entity.read_with(cx, |term, _cx| {
4261                term.output().is_none() // output is None means it's still running
4262            })
4263        });
4264        assert!(
4265            terminal_running_before,
4266            "Terminal should be running before checkpoint restore"
4267        );
4268
4269        // Verify we have the expected entries before restore
4270        let entry_count_before = thread.read_with(cx, |thread, _| thread.entries.len());
4271        assert!(
4272            entry_count_before > 1,
4273            "Should have multiple entries before restore"
4274        );
4275
4276        // Restore the checkpoint to the second message.
4277        // This should:
4278        // 1. Cancel any in-progress generation (via the cancel() call)
4279        // 2. Remove the terminal that was created after that point
4280        thread
4281            .update(cx, |thread, cx| {
4282                thread.restore_checkpoint(second_message_id, cx)
4283            })
4284            .await
4285            .unwrap();
4286
4287        // Verify that no send_task is in progress after restore
4288        // (cancel() clears the send_task)
4289        let has_send_task_after = thread.read_with(cx, |thread, _| thread.send_task.is_some());
4290        assert!(
4291            !has_send_task_after,
4292            "Should not have a send_task after restore (cancel should have cleared it)"
4293        );
4294
4295        // Verify the entries were truncated (restoring to index 1 truncates at 1, keeping only index 0)
4296        let entry_count = thread.read_with(cx, |thread, _| thread.entries.len());
4297        assert_eq!(
4298            entry_count, 1,
4299            "Should have 1 entry after restore (only the first user message)"
4300        );
4301
4302        // Verify the 2 completed terminals from before the checkpoint still exist
4303        let terminal_1_exists = thread.read_with(cx, |thread, _| {
4304            thread.terminals.contains_key(&terminal_id_1)
4305        });
4306        assert!(
4307            terminal_1_exists,
4308            "Terminal 1 (from before checkpoint) should still exist"
4309        );
4310
4311        let terminal_2_exists = thread.read_with(cx, |thread, _| {
4312            thread.terminals.contains_key(&terminal_id_2)
4313        });
4314        assert!(
4315            terminal_2_exists,
4316            "Terminal 2 (from before checkpoint) should still exist"
4317        );
4318
4319        // Verify they're still in completed state
4320        let terminal_1_completed = thread.read_with(cx, |thread, _cx| {
4321            let terminal_entity = thread.terminals.get(&terminal_id_1).unwrap();
4322            terminal_entity.read_with(cx, |term, _cx| term.output().is_some())
4323        });
4324        assert!(terminal_1_completed, "Terminal 1 should still be completed");
4325
4326        let terminal_2_completed = thread.read_with(cx, |thread, _cx| {
4327            let terminal_entity = thread.terminals.get(&terminal_id_2).unwrap();
4328            terminal_entity.read_with(cx, |term, _cx| term.output().is_some())
4329        });
4330        assert!(terminal_2_completed, "Terminal 2 should still be completed");
4331
4332        // Verify the running terminal (created after checkpoint) was removed
4333        let terminal_3_exists =
4334            thread.read_with(cx, |thread, _| thread.terminals.contains_key(&terminal_id));
4335        assert!(
4336            !terminal_3_exists,
4337            "Terminal 3 (created after checkpoint) should have been removed"
4338        );
4339
4340        // Verify total count is 2 (the two from before the checkpoint)
4341        let terminal_count = thread.read_with(cx, |thread, _| thread.terminals.len());
4342        assert_eq!(
4343            terminal_count, 2,
4344            "Should have exactly 2 terminals (the completed ones from before checkpoint)"
4345        );
4346    }
4347
4348    /// Tests that update_last_checkpoint correctly updates the original message's checkpoint
4349    /// even when a new user message is added while the async checkpoint comparison is in progress.
4350    ///
4351    /// This is a regression test for a bug where update_last_checkpoint would fail with
4352    /// "no checkpoint" if a new user message (without a checkpoint) was added between when
4353    /// update_last_checkpoint started and when its async closure ran.
4354    #[gpui::test]
4355    async fn test_update_last_checkpoint_with_new_message_added(cx: &mut TestAppContext) {
4356        init_test(cx);
4357
4358        let fs = FakeFs::new(cx.executor());
4359        fs.insert_tree(path!("/test"), json!({".git": {}, "file.txt": "content"}))
4360            .await;
4361        let project = Project::test(fs.clone(), [Path::new(path!("/test"))], cx).await;
4362
4363        let handler_done = Arc::new(AtomicBool::new(false));
4364        let handler_done_clone = handler_done.clone();
4365        let connection = Rc::new(FakeAgentConnection::new().on_user_message(
4366            move |_, _thread, _cx| {
4367                handler_done_clone.store(true, SeqCst);
4368                async move { Ok(acp::PromptResponse::new(acp::StopReason::EndTurn)) }.boxed_local()
4369            },
4370        ));
4371
4372        let thread = cx
4373            .update(|cx| connection.new_session(project, Path::new(path!("/test")), cx))
4374            .await
4375            .unwrap();
4376
4377        let send_future = thread.update(cx, |thread, cx| thread.send_raw("First message", cx));
4378        let send_task = cx.background_executor.spawn(send_future);
4379
4380        // Tick until handler completes, then a few more to let update_last_checkpoint start
4381        while !handler_done.load(SeqCst) {
4382            cx.executor().tick();
4383        }
4384        for _ in 0..5 {
4385            cx.executor().tick();
4386        }
4387
4388        thread.update(cx, |thread, cx| {
4389            thread.push_entry(
4390                AgentThreadEntry::UserMessage(UserMessage {
4391                    id: Some(UserMessageId::new()),
4392                    content: ContentBlock::Empty,
4393                    chunks: vec!["Injected message (no checkpoint)".into()],
4394                    checkpoint: None,
4395                    indented: false,
4396                }),
4397                cx,
4398            );
4399        });
4400
4401        cx.run_until_parked();
4402        let result = send_task.await;
4403
4404        assert!(
4405            result.is_ok(),
4406            "send should succeed even when new message added during update_last_checkpoint: {:?}",
4407            result.err()
4408        );
4409    }
4410}