# agent: Coalesce thread saves into a single worker \(\#63053\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/ea701d40f3a594d4277ad2038afcccd6547e44a0)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: ea701d40f3a594d4277ad2038afcccd6547e44a0

Requested commit: ea701d40f3a594d4277ad2038afcccd6547e44a0

Commit: ea701d40f3a594d4277ad2038afcccd6547e44a0

Tree: f2e225a7a7b0bda149e0b9bb07087ed27c3f5ee0

Author: Prohect

Committer: GitHub

## Message

```
agent: Coalesce thread saves into a single worker (#63053)

# Objective

- Reduce excessive RAM usage during long agent sessions. Previously
every
`Thread::notify` triggered a full serialization of the entire
conversation
(including any embedded base64 image) to JSON, so streaming bursts
queued
many large, redundant saves and ballooned memory into the multi-GB
range.
- Related to #57126.
- Related to #62133.

## Solution

- Replace the per-notification save task with a single coalescing save
worker
  per session in `crates/agent/src/agent.rs`.
- `save_thread` now writes the latest snapshot into an in-place buffer
and wakes
  the worker via a `watch` channel, instead of spawning a fresh
  full-conversation save on every notification.
- The worker drains the buffer in a loop, saving only the latest
snapshot per
burst (latest-wins coalescing), and preserves save-on-close by draining
the
  final snapshot before exiting.

## Testing

- `cargo check -p agent`
- `cargo test -p agent test_close_session_saves_thread`
- `cargo test -p agent test_save_load_thread`
- `cargo test -p agent test_threads_flushed_to_database_on_app_quit`

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards (no UI change)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

---

Release Notes:

- Improved memory usage during long agent sessions.

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
```

## Parents

- [34241d5fc8c90c2e1959698e0a3a11712330f115](https://git.cafe/gitcafe/zed/commit/34241d5fc8c90c2e1959698e0a3a11712330f115?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/ea701d40f3a594d4277ad2038afcccd6547e44a0?format=markdown)
