mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
Keep Cloud threads current in the Agents overview (#62858)
## Why The Agents overview can go stale when Cloud threads change from another client, especially when the TUI has no conversation stream attached. ## What changed - Use Cloud stream notifications to update thread status and refresh the thread list. Poll every 10 seconds while the overview is visible and no stream is attached. - Refresh the history already shown while preserving the `Show more` cursor. Use fresh list metadata for thread names, recency, and unread state, and restore rows when a refresh catches a missed unarchive. ## Testing Cover Cloud inventory notifications, polling while detached, pagination across refreshes, and recovery from a missed unarchive. GitOrigin-RevId: 63c98438b6ddd2e47dd2d1f419c2a676508c6d91
This commit is contained in:
committed by
copyberry
parent
941e1dcade
commit
beac2ef8fc
@@ -21,6 +21,8 @@ use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::MutexGuard;
|
||||
use std::sync::PoisonError;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::oneshot;
|
||||
use tokio::time::Instant;
|
||||
@@ -121,6 +123,7 @@ pub(super) enum IdleReconnect {
|
||||
#[derive(Clone)]
|
||||
pub struct CloudRequestHandle {
|
||||
pub(super) reconnect: tokio::sync::watch::Sender<IdleReconnect>,
|
||||
pub(super) attached: Arc<AtomicBool>,
|
||||
commands: mpsc::Sender<CloudCommand>,
|
||||
native: Option<Arc<codex_cloud_client::Client>>,
|
||||
title_client: Option<Arc<CloudTitleClient>>,
|
||||
@@ -209,6 +212,7 @@ impl CloudClient {
|
||||
handle: CloudRequestHandle {
|
||||
commands,
|
||||
reconnect: Default::default(),
|
||||
attached: Default::default(),
|
||||
native: native.map(Arc::new),
|
||||
title_client: title_client.map(Arc::new),
|
||||
},
|
||||
@@ -281,6 +285,11 @@ impl CloudClient {
|
||||
}
|
||||
|
||||
impl CloudRequestHandle {
|
||||
/// Whether the worker has an installed reply stream that can deliver inventory updates.
|
||||
pub fn has_attachment(&self) -> bool {
|
||||
self.attached.load(Ordering::Relaxed) && !self.commands.is_closed()
|
||||
}
|
||||
|
||||
/// Generate metadata with Cloud's own account, outside its conversation command queue.
|
||||
pub async fn generate_thread_title(
|
||||
&self,
|
||||
@@ -462,6 +471,7 @@ impl CloudRequestHandle {
|
||||
Self {
|
||||
commands,
|
||||
reconnect: self.reconnect.clone(),
|
||||
attached: self.attached.clone(),
|
||||
native: None,
|
||||
title_client: None,
|
||||
},
|
||||
|
||||
@@ -837,8 +837,12 @@ async fn prepared_resume_keeps_the_previous_stream_until_handoff() -> Result<()>
|
||||
if outcome == "burst" {
|
||||
// One partial-history warning plus the entire live burst, including its unread tail.
|
||||
for _ in 0..PREPARED_EVENT_LIMIT + 2 {
|
||||
let mut event = client.next_event().await;
|
||||
while matches!(event, Some(AppServerEvent::CloudInventoryChanged(Some(_)))) {
|
||||
event = client.next_event().await;
|
||||
}
|
||||
assert!(
|
||||
matches!(client.next_event().await, Some(AppServerEvent::ServerNotification(notification))
|
||||
matches!(event, Some(AppServerEvent::ServerNotification(notification))
|
||||
if matches!(*notification, protocol::ServerNotification::Warning(_)))
|
||||
);
|
||||
}
|
||||
@@ -982,12 +986,20 @@ async fn accepted_idle_reconnect_reports_changed_login() -> Result<()> {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn attachment_status_follows_its_server_notifications() -> Result<()> {
|
||||
async fn attachment_tracks_its_status_and_reports_account_inventory_changes() -> Result<()> {
|
||||
let (output, mut events) = mpsc::unbounded_channel();
|
||||
let mut replies = Replies::default();
|
||||
let notifications: [&[u8]; 3] = [
|
||||
let (client, _commands, _events) = CloudClient::channel(/*capacity*/ 1);
|
||||
let handle = client.request_handle();
|
||||
let mut replies = Replies {
|
||||
attached: handle.attached.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
let notifications: [&[u8]; 6] = [
|
||||
b"\x1a\x0d\x12\x0b\x0a\x06\x22\x04\x0a\x02\x08\x01\x12\x01t", // t is waiting
|
||||
b"\xaa\x01\x0c\x12\x0a\x0a\x01x\x12\x05\x22\x01u\x40\x04", // x starts a turn
|
||||
b"\x1a\x09\x12\x07\x0a\x02\x12\x00\x12\x01x", // x is idle
|
||||
b"\x22\x05\x12\x03\x0a\x01x", // x is archived
|
||||
b"\x32\x05\x12\x03\x0a\x01x", // x is unarchived
|
||||
b"\x1a\x09\x12\x07\x0a\x02\x12\x00\x12\x01t", // t is idle
|
||||
];
|
||||
let stream = stream::iter(
|
||||
@@ -1007,13 +1019,27 @@ async fn attachment_status_follows_its_server_notifications() -> Result<()> {
|
||||
let waiting = cloud::ThreadStatus::Active {
|
||||
active_flags: vec![cloud::ThreadActiveFlag::WaitingOnApproval],
|
||||
};
|
||||
assert_eq!(replies.thread_status("t"), Some(&waiting));
|
||||
replies.forward_next(&output).await;
|
||||
assert_eq!(replies.thread_status("t"), Some(&waiting));
|
||||
let waiting = serde_json::json!(waiting);
|
||||
let status_change = |status: &Value| serde_json::json!({"method": "thread/status/changed", "params": {"threadId": "t", "status": status}});
|
||||
for expected in [
|
||||
status_change(&waiting),
|
||||
Value::Null,
|
||||
Value::Null,
|
||||
serde_json::json!({"method": "thread/archived", "params": {"threadId": "x"}}),
|
||||
serde_json::json!({"method": "thread/unarchived", "params": {"threadId": "x"}}),
|
||||
status_change(&serde_json::json!(cloud::ThreadStatus::Idle)),
|
||||
] {
|
||||
let AppServerEvent::CloudInventoryChanged(lifecycle) = events.try_recv()? else {
|
||||
panic!("expected inventory change");
|
||||
};
|
||||
assert_eq!(serde_json::to_value(lifecycle)?, expected);
|
||||
let status = expected.pointer("/params/status").unwrap_or(&waiting);
|
||||
assert_eq!(&serde_json::to_value(replies.thread_status("t"))?, status);
|
||||
assert!(handle.has_attachment());
|
||||
replies.forward_next(&output).await;
|
||||
}
|
||||
|
||||
replies.forward_next(&output).await;
|
||||
assert_eq!(replies.thread_status("t"), Some(&cloud::ThreadStatus::Idle));
|
||||
replies.forward_next(&output).await;
|
||||
assert!(!handle.has_attachment());
|
||||
assert_eq!(replies.thread_status("t"), Some(&cloud::ThreadStatus::Idle));
|
||||
let AppServerEvent::ThreadStreamDisconnected(error) = events.try_recv()? else {
|
||||
panic!("expected a Cloud stream loss");
|
||||
@@ -1030,5 +1056,8 @@ async fn attachment_status_follows_its_server_notifications() -> Result<()> {
|
||||
.replace("t", async { Ok(stream::pending().boxed()) }, &output)
|
||||
.await?;
|
||||
assert_eq!(replies.thread_status("t"), None);
|
||||
assert!(handle.has_attachment());
|
||||
replies.detach("t");
|
||||
assert!(!handle.has_attachment());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ use futures::StreamExt;
|
||||
use futures::stream::BoxStream;
|
||||
use serde_json::Value;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
@@ -70,6 +72,7 @@ struct Attachment {
|
||||
struct Replies {
|
||||
idle: Option<idle::Recovery>,
|
||||
active: Option<Attachment>,
|
||||
attached: Arc<AtomicBool>,
|
||||
generation: u64,
|
||||
stream_losses: CloudStreamLosses,
|
||||
// Keep the last observed status through EOF until this conversation is unloaded.
|
||||
@@ -77,6 +80,12 @@ struct Replies {
|
||||
}
|
||||
|
||||
impl Replies {
|
||||
fn set_active(&mut self, active: Option<Attachment>) {
|
||||
self.active = active;
|
||||
self.attached
|
||||
.store(self.active.is_some(), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
async fn next_event(&mut self, output: &mpsc::UnboundedSender<AppServerEvent>) {
|
||||
if let Some(recovery) = &mut self.idle
|
||||
&& let Some((root, _, open)) = &mut recovery.held
|
||||
@@ -158,7 +167,7 @@ impl Replies {
|
||||
ended => {
|
||||
let id = active.thread_id.clone();
|
||||
let error = ended.and_then(Result::err);
|
||||
self.active = None;
|
||||
self.set_active(/*active*/ None);
|
||||
if let Some(recovery) = &mut self.idle {
|
||||
recovery
|
||||
.state
|
||||
@@ -259,10 +268,10 @@ impl Replies {
|
||||
output: &mpsc::UnboundedSender<AppServerEvent>,
|
||||
) {
|
||||
self.generation = self.generation.wrapping_add(1);
|
||||
self.active = Some(Attachment {
|
||||
self.set_active(Some(Attachment {
|
||||
thread_id: id.to_owned(),
|
||||
stream,
|
||||
});
|
||||
}));
|
||||
if let Some(event) = first_event {
|
||||
forward(
|
||||
id,
|
||||
@@ -303,7 +312,7 @@ impl Replies {
|
||||
.as_ref()
|
||||
.is_some_and(|active| active.thread_id == id)
|
||||
{
|
||||
self.active = None;
|
||||
self.set_active(/*active*/ None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,12 +359,10 @@ fn start_worker(
|
||||
loaded: None,
|
||||
warned_partial_history: None,
|
||||
};
|
||||
let handle = app_client.request_handle();
|
||||
let mut replies = Replies {
|
||||
idle: Some(idle::Recovery::new(
|
||||
app_client.request_handle().reconnect,
|
||||
client,
|
||||
login,
|
||||
)),
|
||||
attached: handle.attached,
|
||||
idle: Some(idle::Recovery::new(handle.reconnect, client, login)),
|
||||
stream_losses: app_client.stream_losses.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
@@ -917,6 +924,15 @@ fn forward(
|
||||
recovery.observe_busy();
|
||||
}
|
||||
}
|
||||
if let Ok(status) = serde_json::from_value(serde_json::json!(&status)) {
|
||||
let change = protocol::ThreadStatusChangedNotification {
|
||||
thread_id: id.to_owned(),
|
||||
status,
|
||||
};
|
||||
let _ = output.send(AppServerEvent::CloudInventoryChanged(Some(Box::new(
|
||||
protocol::ServerNotification::ThreadStatusChanged(change),
|
||||
))));
|
||||
}
|
||||
*latest_status = Some((id.to_owned(), status));
|
||||
return;
|
||||
}
|
||||
@@ -944,7 +960,24 @@ fn forward(
|
||||
thread_id: Some(id.to_owned()),
|
||||
message: "This cloud turn requested an unsupported interaction. Open it in another client to continue.".into(),
|
||||
}),
|
||||
cloud::Event::Notification(_) => return,
|
||||
event @ cloud::Event::Notification(_) => {
|
||||
let lifecycle = match event.inventory_change() {
|
||||
Some(cloud::InventoryChange::Archived(thread_id)) => Some(
|
||||
protocol::ServerNotification::ThreadArchived(
|
||||
protocol::ThreadArchivedNotification { thread_id },
|
||||
),
|
||||
),
|
||||
Some(cloud::InventoryChange::Unarchived(thread_id)) => Some(
|
||||
protocol::ServerNotification::ThreadUnarchived(
|
||||
protocol::ThreadUnarchivedNotification { thread_id },
|
||||
),
|
||||
),
|
||||
Some(cloud::InventoryChange::Refresh) => None,
|
||||
None => return,
|
||||
};
|
||||
let _ = output.send(AppServerEvent::CloudInventoryChanged(lifecycle.map(Box::new)));
|
||||
return;
|
||||
}
|
||||
};
|
||||
send_notification(output, notification);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user