From 51b25b5c223aebe6bb707121697612604565cbaf Mon Sep 17 00:00:00 2001 From: Mani Rash Ahmadi <66619933+captaindpt@users.noreply.github.com> Date: Mon, 26 May 2025 03:51:00 -0400 Subject: [PATCH] agent: Ensure context meter updates when context is cleared (#30320) Addresses an issue where the agent context token meter in the panel toolbar (showing usage like "X / Y tokens") failed to update its count after the user cleared the current context via the context editor UI. While the meter updated correctly when adding items, clearing them left the display showing the old count. The root cause was traced to the `ContextStore::clear` method in `crates/agent/src/context_store.rs`. This method correctly cleared the internal data structures holding the context items but neglected to call `cx.notify()` to inform listeners of the state change. Consequently, the UI components responsible for displaying the token count were not triggered to re-render with the new (presumably lower) count. This PR fixes the issue by adding the missing `cx.notify()` call to the `ContextStore::clear` method. This ensures listeners are notified when the context set is cleared, allowing the token meter UI to update correctly. Release Notes: - Fixed an issue where the agent context token meter did not update when the context was cleared. --- crates/agent/src/active_thread.rs | 2 +- crates/agent/src/context_store.rs | 3 ++- crates/agent/src/inline_prompt_editor.rs | 2 +- crates/agent/src/message_editor.rs | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/agent/src/active_thread.rs b/crates/agent/src/active_thread.rs index afa774168a359bda1ae61f8abc2ee9270135d47e..094530129e1d4400a48039778fa57fffde94f68f 100644 --- a/crates/agent/src/active_thread.rs +++ b/crates/agent/src/active_thread.rs @@ -1481,7 +1481,7 @@ impl ActiveThread { _window: &mut Window, cx: &mut Context, ) { - self.context_store.update(cx, |store, _cx| store.clear()); + self.context_store.update(cx, |store, cx| store.clear(cx)); cx.notify(); } diff --git a/crates/agent/src/context_store.rs b/crates/agent/src/context_store.rs index dd5b42f596ac6de2413a2ab62ba0e24e98addbc4..110db59864ccd12f81cb11161747bbfef0bd1ee2 100644 --- a/crates/agent/src/context_store.rs +++ b/crates/agent/src/context_store.rs @@ -58,9 +58,10 @@ impl ContextStore { self.context_set.iter().map(|entry| entry.as_ref()) } - pub fn clear(&mut self) { + pub fn clear(&mut self, cx: &mut Context) { self.context_set.clear(); self.context_thread_ids.clear(); + cx.notify(); } pub fn new_context_for_thread( diff --git a/crates/agent/src/inline_prompt_editor.rs b/crates/agent/src/inline_prompt_editor.rs index b47f9ac01a88c4b35aa84c558030f1af94614267..c086541f2db030cc0993f9870d2c3ed599ba9214 100644 --- a/crates/agent/src/inline_prompt_editor.rs +++ b/crates/agent/src/inline_prompt_editor.rs @@ -371,7 +371,7 @@ impl PromptEditor { _window: &mut Window, cx: &mut Context, ) { - self.context_store.update(cx, |store, _cx| store.clear()); + self.context_store.update(cx, |store, cx| store.clear(cx)); cx.notify(); } diff --git a/crates/agent/src/message_editor.rs b/crates/agent/src/message_editor.rs index c4306baedf502a72a2cc2700130910bd70b4fcc6..6d2d17b20c2e8d7e7db903861f8b886d13224efc 100644 --- a/crates/agent/src/message_editor.rs +++ b/crates/agent/src/message_editor.rs @@ -279,7 +279,7 @@ impl MessageEditor { _window: &mut Window, cx: &mut Context, ) { - self.context_store.update(cx, |store, _cx| store.clear()); + self.context_store.update(cx, |store, cx| store.clear(cx)); cx.notify(); }