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.
This commit is contained in:
Mani Rash Ahmadi 2025-05-26 03:51:00 -04:00 committed by GitHub
parent 2f274b2a89
commit 51b25b5c22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 5 additions and 4 deletions

View file

@ -1481,7 +1481,7 @@ impl ActiveThread {
_window: &mut Window,
cx: &mut Context<Self>,
) {
self.context_store.update(cx, |store, _cx| store.clear());
self.context_store.update(cx, |store, cx| store.clear(cx));
cx.notify();
}

View file

@ -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>) {
self.context_set.clear();
self.context_thread_ids.clear();
cx.notify();
}
pub fn new_context_for_thread(

View file

@ -371,7 +371,7 @@ impl<T: 'static> PromptEditor<T> {
_window: &mut Window,
cx: &mut Context<Self>,
) {
self.context_store.update(cx, |store, _cx| store.clear());
self.context_store.update(cx, |store, cx| store.clear(cx));
cx.notify();
}

View file

@ -279,7 +279,7 @@ impl MessageEditor {
_window: &mut Window,
cx: &mut Context<Self>,
) {
self.context_store.update(cx, |store, _cx| store.clear());
self.context_store.update(cx, |store, cx| store.clear(cx));
cx.notify();
}