Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

agent: Release native command work with its session (#65502)

Ben Brandt725573ec14a2
diff --git a/crates/agent/src/agent.rs b/crates/agent/src/agent.rs
index b0042fbdf95..4e02992ecd2 100644
--- a/crates/agent/src/agent.rs
+++ b/crates/agent/src/agent.rs
@@ -1800,6 +1800,10 @@ impl NativeAgent {
return;
}
+ session
+ .thread
+ .update(cx, |thread, cx| thread.cancel(cx))
+ .detach();
self.enqueue_save(session_id, draft_prompt, cx);
let Some(session) = self.sessions.remove(session_id) else {
return;
@@ -1980,11 +1984,7 @@ impl NativeAgent {
.sessions
.get(&session_id)
.context("Failed to get session")?;
- let acp_thread = session
- .acp_thread
- .upgrade()
- .context("Session was released")?;
- anyhow::Ok((acp_thread, session.thread.clone()))
+ anyhow::Ok((session.acp_thread.clone(), session.thread.clone()))
})??;
let mut last_is_user = true;
@@ -2014,7 +2014,7 @@ impl NativeAgent {
true,
cx,
);
- });
+ })?;
thread.update(cx, |thread, cx| {
thread.push_acp_user_block(id, [block], path_style, cx)
@@ -2028,7 +2028,7 @@ impl NativeAgent {
true,
cx,
);
- });
+ })?;
thread.update(cx, |thread, cx| thread.push_acp_agent_block(block, cx))?;
}
@@ -2050,7 +2050,7 @@ impl NativeAgent {
cx.update(|cx| {
NativeAgentConnection::handle_thread_events(
response_stream,
- acp_thread.downgrade(),
+ acp_thread,
connection,
cx,
)
@@ -2073,24 +2073,20 @@ impl NativeAgent {
.sessions
.get(&session_id)
.context("Failed to get session")?;
- let acp_thread = session
- .acp_thread
- .upgrade()
- .context("Session was released")?;
- anyhow::Ok((acp_thread, session.thread.clone()))
+ anyhow::Ok((session.acp_thread.clone(), session.thread.clone()))
})??;
let response_stream =
thread.update(cx, |thread, cx| thread.compact(client_user_message_id, cx))?;
acp_thread.update(cx, |acp_thread, cx| {
acp_thread.update_token_usage(None, cx);
- });
+ })?;
let connection = this.upgrade().map(NativeAgentConnection);
cx.update(|cx| {
NativeAgentConnection::handle_thread_events(
response_stream,
- acp_thread.downgrade(),
+ acp_thread,
connection,
cx,
)
@@ -2128,11 +2124,7 @@ impl NativeAgent {
.sessions
.get(&session_id)
.context("Failed to get session")?;
- let acp_thread = session
- .acp_thread
- .upgrade()
- .context("Session was released")?;
- anyhow::Ok((acp_thread, session.thread.clone()))
+ anyhow::Ok((session.acp_thread.clone(), session.thread.clone()))
})??;
// Build the model-context message: skill envelope first, then
@@ -2181,7 +2173,7 @@ impl NativeAgent {
true,
cx,
);
- });
+ })?;
// Model context: a single user message containing the skill
// envelope followed by the user's appended content.
@@ -2199,7 +2191,7 @@ impl NativeAgent {
cx.update(|cx| {
NativeAgentConnection::handle_thread_events(
response_stream,
- acp_thread.downgrade(),
+ acp_thread,
connection,
cx,
)
@@ -4275,6 +4267,74 @@ mod internal_tests {
.collect()
}
+ #[gpui::test]
+ async fn test_native_skill_command_releases_while_waiting_for_permission(
+ cx: &mut TestAppContext,
+ ) {
+ use language_model::{LanguageModelToolUse, LanguageModelToolUseInput};
+
+ let fake = init_test(cx);
+ let (_connection, agent, project, acp_thread) = setup_native_agent_session(cx).await;
+ cx.run_until_parked();
+ let session_id = acp_thread.read_with(cx, |thread, _| thread.session_id().clone());
+ let thread = cx.update(|cx| native_thread_for_session(&agent, &session_id, cx));
+ let fs = project.read_with(cx, |project, _| project.fs().clone());
+ fs.write(Path::new("/a/file.txt"), b"retained")
+ .await
+ .expect("test file");
+ let model = fake.model("fake");
+ cx.update(|cx| {
+ let mut settings = agent_settings::AgentSettings::get_global(cx).clone();
+ settings.tool_permissions.default = settings::ToolPermissionMode::Confirm;
+ settings.tool_permissions.tools.clear();
+ agent_settings::AgentSettings::override_global(settings, cx);
+ });
+ thread.update(cx, |thread, cx| thread.set_model(model.clone(), cx));
+ let mut skill = make_global_skill("release-skill", "Release skill");
+ skill.embedded_body = Some("Do the requested work");
+ agent.update(cx, |agent, _| {
+ agent
+ .projects
+ .get_mut(&project.entity_id())
+ .expect("project state")
+ .skills = Arc::new(vec![skill]);
+ });
+ let send = acp_thread.update(cx, |thread, cx| {
+ thread.send(vec!["/:release-skill".into()], cx)
+ });
+ let send = cx.foreground_executor().spawn(send);
+ cx.run_until_parked();
+ let request = fake
+ .pending_completions_for(&model)
+ .pop()
+ .expect("skill reaches model");
+ let input = json!({"path": "a/file.txt"});
+ fake.send_event(
+ &model,
+ &request,
+ LanguageModelCompletionEvent::ToolUse(LanguageModelToolUse {
+ id: "delete".into(),
+ name: DeletePathTool::NAME.into(),
+ raw_input: input.to_string(),
+ input: LanguageModelToolUseInput::Json(input),
+ is_input_complete: true,
+ thought_signature: None,
+ }),
+ );
+ fake.end_stream(&model, &request);
+ cx.run_until_parked();
+ assert!(acp_thread.read_with(cx, |thread, _| thread.is_waiting_for_confirmation()));
+ drop(acp_thread);
+ release_dropped_entities(cx);
+ assert!(agent.read_with(cx, |agent, _| !agent.sessions.contains_key(&session_id)));
+ assert!(
+ thread.read_with(cx, |thread, _| thread.is_turn_complete()),
+ "release cancels model even with retained native Thread"
+ );
+ send.await
+ .expect_err("release cancels the pending completion");
+ }
+
#[gpui::test]
async fn test_native_prompt_rejects_unknown_content_before_dispatch(cx: &mut TestAppContext) {
let fake = init_test(cx);