Avoid rollout reads for configured TUI sessions (#39731)

## Why

A newly started thread may not have materialized its rollout before the TUI
receives `ThreadStarted`. Trying to infer session state from that path can wait
through rollout reader retries even when a lifecycle response already provided
the authoritative session.

## What changed

- Preserve session state already stored for a known thread instead of inferring
  it again from `ThreadStarted`.
- Continue updating agent-picker metadata from the notification.
- Restrict fallback session inference to newly observed `ThreadStarted`
  notifications that do not already have session state.

## Testing

Added a startup test that verifies a known thread routes `ThreadStarted`
immediately, retains its configured session, buffers the notification, and
updates agent metadata when the rollout does not exist yet.

GitOrigin-RevId: 7ed98fb46df17566c4a61ac677f60fc8d94f7321
This commit is contained in:
Charlie Marsh authored and copyberry committed 2026-08-20 15:42:40 +00:00
1 parent 9bf673718a
commit bf2aee99c5
2 files changed
+109 -15

No files matched your search

+30 -15
View File
@@ -8,6 +8,7 @@ use super::session_lifecycle::ThreadAttachPresentation;
use super::*;
use crate::chatwidget::ThreadInputStateRestoreMode;
use crate::session_resume::read_session_model;
use codex_app_server_protocol::ThreadStartedNotification;
use codex_app_server_protocol::TurnInterruptParams;
use codex_app_server_protocol::TurnInterruptResponse;
use codex_app_server_protocol::WarningNotification;
@@ -988,9 +989,32 @@ impl App {
self.apply_thread_settings_to_cached_session(thread_id, &notification.thread_settings)
.await;
}
let inferred_session = self
.infer_session_for_thread_notification(thread_id, &notification)
.await;
let inferred_session = if let ServerNotification::ThreadStarted(started) = &notification
&& self.primary_session_configured.is_some()
{
self.upsert_agent_picker_thread(
thread_id,
started.thread.agent_nickname.clone(),
started.thread.agent_role.clone(),
/*is_closed*/ false,
);
// Lifecycle responses already contain authoritative session state. Their rollout may
// not exist until the first turn, so inferring it again can wait through reader retries.
let already_has_session = match self.thread_event_channels.get(&thread_id) {
Some(channel) => channel.store.lock().await.session.is_some(),
None => false,
};
if already_has_session {
None
} else {
self.infer_session_for_started_thread(thread_id, started)
.await
}
} else {
None
};
let is_turn_started = matches!(notification, ServerNotification::TurnStarted(_));
let notification_status_change = SideParentStatusChange::for_notification(&notification);
let (sender, store) = {
@@ -1104,14 +1128,11 @@ impl App {
}
}
pub(super) async fn infer_session_for_thread_notification(
&mut self,
async fn infer_session_for_started_thread(
&self,
thread_id: ThreadId,
notification: &ServerNotification,
notification: &ThreadStartedNotification,
) -> Option<ThreadSessionState> {
let ServerNotification::ThreadStarted(notification) = notification else {
return None;
};
let mut session = self.primary_session_configured.clone()?;
session.thread_id = thread_id;
session.thread_name = notification.thread.name.clone();
@@ -1128,12 +1149,6 @@ impl App {
}
session.message_history = None;
session.rollout_path = rollout_path;
self.upsert_agent_picker_thread(
thread_id,
notification.thread.agent_nickname.clone(),
notification.thread.agent_role.clone(),
/*is_closed*/ false,
);
Some(session)
}