Files
zed/crates/agent
Nikhil Sood 06999050d6 agent: Stream thread saves into zstd instead of buffering the JSON (#66683)
## Summary

Closes #65622

`ThreadsDatabase::save_thread_sync` serialized the whole thread into a
JSON `String` and then compressed it with `zstd::encode_all` into a
second buffer, so every save held the full uncompressed JSON and the
compressed output at the same time. For long threads with large tool
outputs that's tens of MB per save.

This change serializes through a `BufWriter` straight into a
`zstd::stream::Encoder`, so the uncompressed JSON is never held in
memory. The `BufWriter` is flushed explicitly so write errors propagate.
Serialization and compression now both happen before taking the database
lock (serialization already did), so the lock is only held for the
insert. The stored format is unchanged: same compression level, same
JSON including the flattened `version` field, so existing rows and the
load path are unaffected.

I measured it with a standalone program (not part of this PR) on a
synthetic thread with about 54 MB of JSON, 5 runs each:

| | extra peak heap during save | serialize + compress (median) |
| --- | --- | --- |
| before | 64.2 MiB | 42.6 ms |
| after | 0.23 MiB | 31.1 ms |

The synthetic data compresses very well, so on real threads the saving
is roughly the size of the uncompressed JSON per save. The `BufWriter`
matters: without it, streaming was about 2x slower than before because
`serde_json` makes many tiny writes.

Not in this PR: the save on scroll (`ui_scroll_position` and
`draft_prompt` are part of `DbThread`, so scrolling still triggers a
full save). That seems like a separate design question. I'm happy to
look at it as a follow-up. This change also doesn't touch save ordering.
Saves for a session still go through the save worker one at a time, and
#62698 is the separate fix for stale saves.

## Testing

- Added `test_save_thread_stores_versioned_json`, which saves a thread
with a long summary (quotes, backslashes, newlines, Unicode), reads the
raw row, decompresses it, checks the `version` field, and checks that
`DbThread::from_json` restores the same value. It guards the on-disk
format, so it passes before and after the change.
- `cargo test -p agent db::` (22 tests), `./script/clippy -p agent` and
`cargo fmt --check -p agent` pass. I ran these on Linux (arm64). I
haven't tested it manually in the app.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist). (No UI
changes.)
- [x] Tests cover the new or changed behavior.

Release Notes:

- Improved memory usage when saving long agent threads.

Co-authored-by: Nikhil Sood <206686452+founderinaction@users.noreply.github.com>
2026-11-14 00:21:14 +00:00
..