Fix acp generating status after stop (#35852)

Release Notes:

- N/A
This commit is contained in:
Agus Zubiaga 2025-08-08 02:26:53 -03:00 committed by GitHub
parent 3bee803b51
commit edef1f1470
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,7 +6,6 @@ use anyhow::{Context as _, Result};
use assistant_tool::ActionLog; use assistant_tool::ActionLog;
use buffer_diff::BufferDiff; use buffer_diff::BufferDiff;
use editor::{Bias, MultiBuffer, PathKey}; use editor::{Bias, MultiBuffer, PathKey};
use futures::future::{Fuse, FusedFuture};
use futures::{FutureExt, channel::oneshot, future::BoxFuture}; use futures::{FutureExt, channel::oneshot, future::BoxFuture};
use gpui::{AppContext, Context, Entity, EventEmitter, SharedString, Task}; use gpui::{AppContext, Context, Entity, EventEmitter, SharedString, Task};
use itertools::Itertools; use itertools::Itertools;
@ -580,7 +579,7 @@ pub struct AcpThread {
project: Entity<Project>, project: Entity<Project>,
action_log: Entity<ActionLog>, action_log: Entity<ActionLog>,
shared_buffers: HashMap<Entity<Buffer>, BufferSnapshot>, shared_buffers: HashMap<Entity<Buffer>, BufferSnapshot>,
send_task: Option<Fuse<Task<()>>>, send_task: Option<Task<()>>,
connection: Rc<dyn AgentConnection>, connection: Rc<dyn AgentConnection>,
session_id: acp::SessionId, session_id: acp::SessionId,
} }
@ -670,11 +669,7 @@ impl AcpThread {
} }
pub fn status(&self) -> ThreadStatus { pub fn status(&self) -> ThreadStatus {
if self if self.send_task.is_some() {
.send_task
.as_ref()
.map_or(false, |t| !t.is_terminated())
{
if self.waiting_for_tool_confirmation() { if self.waiting_for_tool_confirmation() {
ThreadStatus::WaitingForToolConfirmation ThreadStatus::WaitingForToolConfirmation
} else { } else {
@ -1049,8 +1044,7 @@ impl AcpThread {
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let cancel_task = self.cancel(cx); let cancel_task = self.cancel(cx);
self.send_task = Some( self.send_task = Some(cx.spawn(async move |this, cx| {
cx.spawn(async move |this, cx| {
async { async {
cancel_task.await; cancel_task.await;
@ -1067,13 +1061,12 @@ impl AcpThread {
.await; .await;
tx.send(result).log_err(); tx.send(result).log_err();
anyhow::Ok(()) anyhow::Ok(())
} }
.await .await
.log_err(); .log_err();
}) }));
.fuse(),
);
cx.spawn(async move |this, cx| match rx.await { cx.spawn(async move |this, cx| match rx.await {
Ok(Err(e)) => { Ok(Err(e)) => {
@ -1081,7 +1074,23 @@ impl AcpThread {
.log_err(); .log_err();
Err(e)? Err(e)?
} }
_ => { result => {
let cancelled = matches!(
result,
Ok(Ok(acp::PromptResponse {
stop_reason: acp::StopReason::Cancelled
}))
);
// We only take the task if the current prompt wasn't cancelled.
//
// This prompt may have been cancelled because another one was sent
// while it was still generating. In these cases, dropping `send_task`
// would cause the next generation to be cancelled.
if !cancelled {
this.update(cx, |this, _cx| this.send_task.take()).ok();
}
this.update(cx, |_, cx| cx.emit(AcpThreadEvent::Stopped)) this.update(cx, |_, cx| cx.emit(AcpThreadEvent::Stopped))
.log_err(); .log_err();
Ok(()) Ok(())