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_servers: Route v2 configuration and usage updates (#65556)

Ben Brandt36682547bea1
diff --git a/crates/acp_thread/src/acp_thread.rs b/crates/acp_thread/src/acp_thread.rs
index 5d0bf7d0e7d..0d4c316fa89 100644
--- a/crates/acp_thread/src/acp_thread.rs
+++ b/crates/acp_thread/src/acp_thread.rs
@@ -3334,6 +3334,7 @@ pub struct TokenUsage {
pub struct SessionCost {
pub amount: f64,
pub currency: SharedString,
+ pub meta: Option<acp_v2::Meta>,
}
pub const TOKEN_USAGE_WARNING_THRESHOLD: f32 = 0.8;
@@ -4454,16 +4455,14 @@ impl AcpThread {
config_options::from_v1(config_options).map_err(acp_v1::Error::from)?,
)),
acp_v1::SessionUpdate::UsageUpdate(update) => {
- let usage = self.token_usage.get_or_insert_with(Default::default);
- usage.max_tokens = update.size;
- usage.used_tokens = update.used;
- if let Some(cost) = update.cost {
- self.cost = Some(SessionCost {
- amount: cost.amount,
- currency: cost.currency.into(),
- });
- }
- cx.emit(AcpThreadEvent::TokenUsageUpdated);
+ self.update_usage(
+ acp_v2::UsageUpdate::new(update.used, update.size)
+ .cost(update.cost.map(|cost| {
+ acp_v2::Cost::new(cost.amount, cost.currency).meta(cost.meta)
+ }))
+ .meta(update.meta),
+ cx,
+ );
}
_ => {}
}
@@ -5305,6 +5304,21 @@ impl AcpThread {
cx.emit(AcpThreadEvent::TokenUsageUpdated);
}
+ pub fn update_usage(&mut self, update: acp_v2::UsageUpdate, cx: &mut Context<Self>) {
+ let usage = self.token_usage.get_or_insert_with(Default::default);
+ usage.max_tokens = update.size;
+ usage.used_tokens = update.used;
+ // The schema uses Option for cost, so omission and null both mean no new report.
+ if let Some(cost) = update.cost {
+ self.cost = Some(SessionCost {
+ amount: cost.amount,
+ currency: cost.currency.into(),
+ meta: cost.meta,
+ });
+ }
+ cx.emit(AcpThreadEvent::TokenUsageUpdated);
+ }
+
pub fn update_retry_status(&mut self, status: RetryStatus, cx: &mut Context<Self>) {
cx.emit(AcpThreadEvent::Retry(status));
}
diff --git a/crates/agent_servers/src/acp.rs b/crates/agent_servers/src/acp.rs
index c26818382dc..82ff97c7446 100644
--- a/crates/agent_servers/src/acp.rs
+++ b/crates/agent_servers/src/acp.rs
@@ -3462,6 +3462,206 @@ mod tests {
assert!(harness.fallback.lock().expect("fallback mutex").is_empty());
}
+ #[gpui::test]
+ async fn v2_config_sdk_replaces_owner_and_notifies_subscribers(cx: &mut gpui::TestAppContext) {
+ use acp_thread::AcpThreadEvent;
+ use serde_json::json;
+
+ let harness = V2SessionHarness::new(cx).await;
+ let session_id = harness
+ .thread
+ .read_with(cx, |thread, _| thread.session_id().clone());
+ assert!(
+ cx.read(|cx| harness.connection.session_config_options(&session_id, cx))
+ .is_none()
+ );
+ let events = Rc::new(RefCell::new(Vec::new()));
+ let _subscription = cx.update(|cx| {
+ cx.subscribe(&harness.thread, {
+ let events = events.clone();
+ let connection = harness.connection.clone();
+ let session_id = session_id.clone();
+ move |_, event, cx| {
+ if let AcpThreadEvent::ConfigOptionsUpdated(options) = event {
+ let owner = connection
+ .session_config_options(&session_id, cx)
+ .expect("config owner installed before the event");
+ assert_eq!(owner.config_options(), *options);
+ events.borrow_mut().push(options.clone());
+ }
+ }
+ })
+ });
+ let first = json!([
+ {"configId": "opaque/手動", "name": "Mode", "description": "Select a mode",
+ "category": "mode", "type": "select", "currentValue": "custom",
+ "options": [{"value": "custom", "name": "Custom", "description": "Offered",
+ "_meta": {"scope": "value", "retain": [null, 7]}}],
+ "_meta": {"scope": "option"}},
+ {"configId": "future", "name": "Future", "type": "_future",
+ "payload": {"retain": [false, null]}, "_meta": {"scope": "future"}}
+ ]);
+ let mut expected_events = Vec::new();
+ harness.send(
+ json!({"sessionUpdate": "config_option_update", "configOptions": first,
+ "_meta": {"scope": "update"}}),
+ Some(v2_terminal_meta("delivery")),
+ );
+ harness.drain(cx).await;
+ expected_events.push(
+ serde_json::from_value::<Vec<acp_v2::SessionConfigOption>>(first.clone())
+ .expect("initial options"),
+ );
+ let owner = cx
+ .read(|cx| harness.connection.session_config_options(&session_id, cx))
+ .expect("config owner");
+ assert_eq!(owner.config_options(), expected_events[0]);
+ let mut updates = cx.update(|cx| owner.watch(cx).expect("config subscription"));
+ drop(updates.borrow());
+ for options in [
+ json!([{"configId": "enabled", "name": "Enabled", "type": "boolean",
+ "currentValue": true, "_meta": {"scope": "replacement"}}]),
+ json!([]),
+ ] {
+ let expected: Vec<acp_v2::SessionConfigOption> =
+ serde_json::from_value(options.clone()).expect("replacement options");
+ harness.send(
+ json!({"sessionUpdate": "config_option_update", "configOptions": options}),
+ None,
+ );
+ harness.drain(cx).await;
+ updates
+ .changed()
+ .now_or_never()
+ .expect("config replacement notification")
+ .expect("live config subscription");
+ assert_eq!(owner.config_options(), expected);
+ expected_events.push(expected);
+ }
+ harness.send_to(
+ "unknown-session",
+ json!({"sessionUpdate": "config_option_update", "configOptions": first}),
+ None,
+ );
+ harness.drain(cx).await;
+ assert!(updates.changed().now_or_never().is_none());
+ assert!(owner.config_options().is_empty());
+ assert!(
+ !harness
+ .connection
+ .sessions
+ .borrow()
+ .contains_key(&"unknown-session".into())
+ );
+ harness.thread.read_with(cx, |thread, _| {
+ assert!(thread.entries().is_empty());
+ });
+ cx.run_until_parked();
+ assert_eq!(*events.borrow(), expected_events);
+ assert!(harness.fallback.lock().expect("fallback mutex").is_empty());
+ }
+
+ #[gpui::test]
+ async fn v2_usage_sdk_preserves_counters_and_optional_cost(cx: &mut gpui::TestAppContext) {
+ use acp_thread::{AcpThreadEvent, TokenUsage};
+ use serde_json::json;
+
+ let harness = V2SessionHarness::new(cx).await;
+ let events = Rc::new(RefCell::new(0));
+ let _subscription = cx.update(|cx| {
+ cx.subscribe(&harness.thread, {
+ let events = events.clone();
+ move |_, event, _| {
+ if matches!(event, AcpThreadEvent::TokenUsageUpdated) {
+ *events.borrow_mut() += 1;
+ }
+ assert!(!matches!(event, AcpThreadEvent::Error));
+ }
+ })
+ });
+ harness.send(
+ json!({"sessionUpdate": "usage_update", "used": 0, "size": 10000}),
+ None,
+ );
+ harness.drain(cx).await;
+ harness.thread.update(cx, |thread, cx| {
+ assert_eq!(
+ thread.token_usage().expect("initial usage").max_tokens,
+ 10000
+ );
+ assert!(thread.cost().is_none());
+ thread.update_token_usage(
+ Some(TokenUsage {
+ max_tokens: 10000,
+ used_tokens: 0,
+ input_tokens: 123,
+ output_tokens: 456,
+ max_output_tokens: Some(789),
+ }),
+ cx,
+ );
+ });
+ for (update, expected_amount, expected_currency) in [
+ (
+ json!({"used": 1000, "size": 10000,
+ "cost": {"amount": 0.42, "currency": "USD", "_meta": {"scope": "cost"}}}),
+ 0.42,
+ "USD",
+ ),
+ (json!({"used": 2000, "size": 20000}), 0.42, "USD"),
+ (json!({"used": 0, "size": 0, "cost": null}), 0.42, "USD"),
+ (
+ json!({"used": 3000, "size": 30000, "cost": {"amount": 0, "currency": "EUR"}}),
+ 0.0,
+ "EUR",
+ ),
+ ] {
+ let expected: acp_v2::UsageUpdate =
+ serde_json::from_value(update.clone()).expect("usage source");
+ let mut update = update;
+ let object = update.as_object_mut().expect("usage object");
+ object.insert("sessionUpdate".into(), json!("usage_update"));
+ object.insert("_meta".into(), json!({"scope": "update"}));
+ harness.send(update, Some(v2_terminal_meta("delivery")));
+ harness.drain(cx).await;
+ harness.thread.read_with(cx, |thread, _| {
+ assert_eq!(
+ thread.token_usage(),
+ Some(&TokenUsage {
+ max_tokens: expected.size,
+ used_tokens: expected.used,
+ input_tokens: 123,
+ output_tokens: 456,
+ max_output_tokens: Some(789),
+ })
+ );
+ let cost = thread.cost().expect("retained cost");
+ assert_eq!(cost.amount, expected_amount);
+ assert_eq!(cost.currency.as_ref(), expected_currency);
+ assert_eq!(
+ cost.meta,
+ (expected_currency == "USD").then(|| v2_terminal_meta("cost"))
+ );
+ assert!(thread.entries().is_empty());
+ assert!(!thread.had_error());
+ });
+ }
+ harness.send_to(
+ "unknown-session",
+ json!({"sessionUpdate": "usage_update", "used": 999, "size": 999,
+ "cost": {"amount": 999, "currency": "USD"}}),
+ None,
+ );
+ harness.drain(cx).await;
+ harness.thread.read_with(cx, |thread, _| {
+ assert_eq!(thread.token_usage().expect("usage").used_tokens, 3000);
+ assert_eq!(thread.cost().expect("cost").amount, 0.0);
+ });
+ cx.run_until_parked();
+ assert_eq!(*events.borrow(), 6);
+ assert!(harness.fallback.lock().expect("fallback mutex").is_empty());
+ }
+
#[gpui::test]
async fn v2_tool_sdk_patches_chunks_and_display_references(cx: &mut gpui::TestAppContext) {
use acp_thread::{ToolCallContent, ToolCallStatus};
@@ -3849,6 +4049,18 @@ mod tests {
"output": {"data": encoded_output}, "exitStatus": {"exitCode": 0}}),
None,
);
+ harness.send_to(
+ session_id,
+ json!({"sessionUpdate": "config_option_update", "configOptions": [
+ {"configId": "shared", "name": text, "type": "boolean", "currentValue": true}
+ ]}),
+ None,
+ );
+ harness.send_to(
+ session_id,
+ json!({"sessionUpdate": "usage_update", "used": text.len(), "size": 10000}),
+ None,
+ );
}
harness.drain(cx).await;
for (thread, expected) in [(&harness.thread, "main"), (&child, "child")] {
@@ -3857,6 +4069,19 @@ mod tests {
panic!("one message in the routed session");
};
assert_eq!(user.content.to_markdown(cx), expected);
+ assert_eq!(
+ thread.token_usage().expect("routed usage").used_tokens,
+ expected.len() as u64
+ );
+ let config = harness
+ .connection
+ .session_config_options(thread.session_id(), cx)
+ .expect("routed config");
+ assert_eq!(config.config_options().len(), 1);
+ assert_eq!(
+ config.config_options().first().expect("config option").name,
+ expected
+ );
let terminal = thread
.terminal("shared-display".into())
.expect("routed terminal");
@@ -3874,6 +4099,10 @@ mod tests {
let child_terminal = child
.read_with(cx, |thread, _| thread.terminal("shared-display".into()))
.expect("child terminal");
+ let child_config = cx
+ .read(|cx| harness.connection.session_config_options(&child_id, cx))
+ .expect("child config owner");
+ let child_options = child_config.config_options();
assert_eq!(harness.connection.sessions.borrow().len(), 2);
let released = cx.update(|cx| {
harness
@@ -3903,6 +4132,8 @@ mod tests {
"exitStatus": {"exitCode": 7}}),
json!({"sessionUpdate": "terminal_output_chunk", "terminalId": "shared-display",
"data": "bGF0ZQ=="}),
+ json!({"sessionUpdate": "config_option_update", "configOptions": []}),
+ json!({"sessionUpdate": "usage_update", "used": 999, "size": 999}),
] {
harness.send_to("child-session", update, None);
}
@@ -3913,7 +4144,9 @@ mod tests {
panic!("released owner's retained message");
};
assert_eq!(user.content.to_markdown(cx), "child");
+ assert_eq!(thread.token_usage().expect("retained usage").used_tokens, 5);
});
+ assert_eq!(child_config.config_options(), child_options);
child_terminal.read_with(cx, |terminal, _| {
let output = terminal.output().expect("retained child output");
assert_eq!(output.content.trim_end(), "child");
@@ -3931,6 +4164,10 @@ mod tests {
let _transport_tasks = (_client_task, _agent_task, _legacy_agent_task);
let weak_thread = thread.downgrade();
let session_id = thread.read_with(cx, |thread, _| thread.session_id().clone());
+ let config = cx
+ .read(|cx| connection.session_config_options(&session_id, cx))
+ .expect("config owner before drop");
+ let options = config.config_options();
let notification =
InboundSessionNotification::try_from(acp_v2::UpdateSessionNotification::new(
session_id.clone(),
@@ -3941,7 +4178,7 @@ mod tests {
.expect("message notification");
let terminal_notification =
terminal_updates::decode_terminal_notification(acp_v2::UpdateSessionNotification::new(
- session_id,
+ session_id.clone(),
acp_v2::SessionUpdate::TerminalOutputChunk(acp_v2::TerminalOutputChunk::new(
"shared-display",
"bGF0ZQ==",
@@ -3960,12 +4197,29 @@ mod tests {
terminal_notification.into(),
handle_v2_session_notification,
);
+ for update in [
+ acp_v2::SessionUpdate::ConfigOptionUpdate(acp_v2::ConfigOptionUpdate::new(
+ Vec::new(),
+ )),
+ acp_v2::SessionUpdate::UsageUpdate(acp_v2::UsageUpdate::new(999, 999)),
+ ] {
+ enqueue_notification(
+ &connection.dispatch_tx,
+ InboundSessionNotification::try_from(acp_v2::UpdateSessionNotification::new(
+ session_id.clone(),
+ update,
+ ))
+ .expect("queued config/usage notification"),
+ handle_v2_session_notification,
+ );
+ }
drop(thread);
});
drain_foreground_queue(&connection.dispatch_tx)
.await
.expect("apply queued work after owner drop");
assert!(weak_thread.upgrade().is_none());
+ assert_eq!(config.config_options(), options);
}
#[gpui::test]
@@ -4469,7 +4723,7 @@ mod tests {
"sessionId": "v2-terminal-session", "_meta": {"scope": "known envelope"},
"futureOuter": {"preserve": [1, null, true]},
"update": {
- "sessionUpdate": "config_option_update", "configOptions": [],
+ "sessionUpdate": "state_update", "state": "running",
"_meta": {"scope": "known update"}, "futureKnownField": {"nested": "keep"}
}
}),
@@ -8632,14 +8886,31 @@ fn handle_v2_session_notification(
return;
}
};
+ let Some(thread) = thread.upgrade() else {
+ log::warn!("Received ACP v2 update for a released session");
+ return;
+ };
if let InboundSessionUpdate::SessionInfo(update) = &notification.update
&& let Some(session_list) = context.session_list.borrow().as_ref()
{
session_list.send_info_update(notification.session_id.clone(), update.clone());
}
+ if let InboundSessionUpdate::ConfigOptions(update) = &notification.update {
+ let config_options = {
+ let mut sessions = context.sessions.borrow_mut();
+ let Some(session) = sessions.get_mut(&notification.session_id) else {
+ return;
+ };
+ session
+ .config_options
+ .get_or_insert_with(|| ConfigOptions::new(Rc::new(RefCell::new(Vec::new()))))
+ .clone()
+ };
+ *config_options.config_options.borrow_mut() = update.config_options.clone();
+ config_options.tx.borrow_mut().send(()).log_err();
+ }
thread
.update(cx, |thread, cx| notification.apply(thread, cx))
- .and_then(|result| result)
.log_err();
}
diff --git a/crates/agent_servers/src/acp/session_updates.rs b/crates/agent_servers/src/acp/session_updates.rs
index 7826e6ea2f6..27ea5c181ea 100644
--- a/crates/agent_servers/src/acp/session_updates.rs
+++ b/crates/agent_servers/src/acp/session_updates.rs
@@ -29,6 +29,8 @@ pub enum InboundSessionUpdate {
Terminal(DecodedTerminalUpdate),
SessionInfo(acp_v2::SessionInfoUpdate),
AvailableCommands(acp_v2::AvailableCommandsUpdate),
+ ConfigOptions(acp_v2::ConfigOptionUpdate),
+ Usage(acp_v2::UsageUpdate),
Notice(acp_v2::Notice),
Compaction(acp_v2::CompactionUpdate),
CompactionSummary(acp_v2::CompactionSummaryChunk),
@@ -93,6 +95,10 @@ impl TryFrom<acp_v2::UpdateSessionNotification> for InboundSessionNotification {
acp_v2::SessionUpdate::AvailableCommandsUpdate(update) => {
InboundSessionUpdate::AvailableCommands(update)
}
+ acp_v2::SessionUpdate::ConfigOptionUpdate(update) => {
+ InboundSessionUpdate::ConfigOptions(update)
+ }
+ acp_v2::SessionUpdate::UsageUpdate(update) => InboundSessionUpdate::Usage(update),
acp_v2::SessionUpdate::Notice(notice) => InboundSessionUpdate::Notice(notice),
acp_v2::SessionUpdate::CompactionUpdate(update) => {
InboundSessionUpdate::Compaction(update)
@@ -164,6 +170,16 @@ impl InboundSessionNotification {
thread.update_available_commands(update.available_commands, cx);
Ok(())
}
+ InboundSessionUpdate::ConfigOptions(update) => {
+ cx.emit(acp_thread::AcpThreadEvent::ConfigOptionsUpdated(
+ update.config_options,
+ ));
+ Ok(())
+ }
+ InboundSessionUpdate::Usage(update) => {
+ thread.update_usage(update, cx);
+ Ok(())
+ }
InboundSessionUpdate::Notice(notice) => {
thread.push_notice(notice, cx);
Ok(())