Bound TUI thread replay buffers by delta size (#39081)

## Why

The per-thread replay buffer limited its event count, but streamed agent-message
deltas could still retain an unbounded amount of text while a thread was
inactive.

## What changed

- Coalesce adjacent deltas for the same thread, turn, and item into chunks of up
  to 4 KiB.
- Limit buffered agent-message deltas to 256 KiB, discard a single delta larger
  than that limit, and evict the oldest events when either buffer limit is
  exceeded.
- Keep pending input and approval state in sync when eviction removes the
  corresponding request, including clearing side-thread status indicators.

## Testing

Added coverage for delta coalescing, byte-based eviction, oversized deltas,
replaying only retained text, and side-thread status updates after eviction.

GitOrigin-RevId: 88d85353a124467d467822d2fd9854312b68d547
This commit is contained in:
Tamir Duberstein authored and copyberry committed 2026-08-17 21:03:06 +00:00
1 parent 8ef139667f
commit 9c099e94a2
7 files changed
+496 -43

No files matched your search

+6 -12
View File
@@ -982,13 +982,14 @@ impl App {
let channel = self.ensure_thread_channel(thread_id);
(channel.sender.clone(), Arc::clone(&channel.store))
};
let (notification, pending_status, turn_stopped) = {
let (notification, previous_pending_status, pending_status, turn_stopped) = {
let mut guard = store.lock().await;
if guard.session.is_none()
&& let Some(session) = inferred_session
{
guard.session = Some(session);
}
let previous_pending_status = guard.side_parent_pending_status();
let turn_stopped = match &notification {
ServerNotification::TurnCompleted(notification) => {
guard.active_turn_id() == Some(notification.turn.id.as_str())
@@ -1005,6 +1006,7 @@ impl App {
};
(
notification,
previous_pending_status,
guard.side_parent_pending_status(),
turn_stopped,
)
@@ -1034,6 +1036,8 @@ impl App {
self.set_side_parent_status(thread_id, Some(status));
} else if let Some(change) = notification_status_change {
self.apply_side_parent_status_change(thread_id, change);
} else if previous_pending_status.is_some() {
self.clear_side_parent_action_status(thread_id);
}
self.refresh_pending_thread_approvals().await;
Ok(())
@@ -1184,17 +1188,7 @@ impl App {
// retaining another deep copy for thread replay only accumulates already-delivered
// history data. Inactive responses still need the buffer because they are not sent.
if !should_send || !matches!(&event, HistoryLookupResponse::Batch { .. }) {
guard
.buffer
.push_back(ThreadBufferedEvent::HistoryEntryResponse(event.clone()));
if guard.buffer.len() > guard.capacity
&& let Some(removed) = guard.buffer.pop_front()
&& let ThreadBufferedEvent::Request(request) = &removed
{
guard
.pending_interactive_replay
.note_evicted_server_request(request.as_ref());
}
guard.push_buffered_event(ThreadBufferedEvent::HistoryEntryResponse(event.clone()));
}
should_send
};