Files
codex/codex-rs/tui/src/app/backend_events.rs
T
Benjamin Carlsson b7056f3408 Keep agent overview refresh state scoped to each backend (#61596)
## Why

Switching backends while an overview refresh is pending can lose queued pagination or let a late page consume another backend's refill budget.

## What changed

- Track queued refreshes, “Show more” requests, refill counts, and refresh failures per backend.
- Preserve pending work across backend switches and use registered live request handles to refresh the owning backend.
- Cancel stale reads for the affected backend after mutations, event lag, or disconnects, and hide “Show more” when its backend is disconnected.

## Testing

Add regression coverage for pagination across focus changes, backend-specific refills, pin completion, stale reads after event lag, and connection registration during reconnect.

GitOrigin-RevId: 3429b2b3ac93156b4dc4234ad042431d26c84525
2026-11-15 01:50:45 +00:00

87 lines
3.7 KiB
Rust

//! Checks incoming event ownership before consulting native-thread caches or UI state.
//! Connection-wide lifecycle and account events may only update the displayed conversation's UI.
use super::App;
use super::app_server_event_targets::ServerNotificationThreadTarget;
use super::app_server_event_targets::server_notification_thread_target;
use crate::app_server_session::AppServerSession;
use crate::app_server_session::AppServerSessionEvent;
use crate::backend_id::BackendId;
use crate::chatwidget::ThreadInputState;
use codex_app_server_client::AppServerEvent;
use codex_app_server_protocol::ServerNotification;
use codex_protocol::ThreadId;
use std::collections::HashMap;
impl App {
pub(super) async fn handle_polled_backend_event(
&mut self,
app_server: &AppServerSession,
displayed_owner: BackendId,
parked_inputs: &mut HashMap<ThreadId, ThreadInputState>,
event: AppServerSessionEvent,
) {
if displayed_owner != event.backend_id {
if event.backend_id != app_server.backend_id() {
return;
}
let invalidates_discovery = match &event.event {
AppServerEvent::Lagged { .. } | AppServerEvent::Disconnected { .. } => true,
AppServerEvent::ServerNotification(notification) => matches!(
notification.as_ref(),
ServerNotification::AccountUpdated(_)
| ServerNotification::ThreadArchived(_)
| ServerNotification::ThreadDeleted(_)
| ServerNotification::ThreadUnarchived(_)
),
_ => false,
};
if invalidates_discovery {
self.cancel_agents_overview_refresh(event.backend_id);
self.reset_agents_overview_discovery(event.backend_id);
}
if let AppServerEvent::ServerNotification(notification) = &event.event
&& let ServerNotificationThreadTarget::Thread(thread_id) =
server_notification_thread_target(notification)
{
if matches!(
notification.as_ref(),
ServerNotification::ThreadArchived(_)
| ServerNotification::ThreadDeleted(_)
| ServerNotification::ThreadUnarchived(_)
) && self.backend_registry.owner(thread_id) == Some(event.backend_id)
{
self.track_agents_overview_notification(notification);
self.repaint_agents_overview();
}
if let ServerNotification::ThreadSettingsUpdated(settings) = notification.as_ref()
&& self.backend_registry.owner(thread_id) == Some(event.backend_id)
&& self
.update_retained_blank_settings(thread_id, settings)
.await
&& let Some(input) = parked_inputs.get_mut(&thread_id)
{
input.pending_thread_settings = Some(settings.clone());
}
if self.voice_owner_thread_id() == Some(thread_id) {
self.deliver_background_voice_notification(thread_id, notification);
}
}
return;
}
self.handle_backend_event(app_server, event).await;
}
pub(super) async fn handle_backend_event(
&mut self,
app_server: &AppServerSession,
event: AppServerSessionEvent,
) {
if event.backend_id != app_server.backend_id() {
tracing::warn!("ignoring server event from a different backend");
return;
}
self.handle_app_server_event(app_server, event.event).await;
}
}