mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Scope TUI app directory state to the active context (#38743)
## Why App directory data and in-flight requests can outlive the account, workspace, or thread that produced them, allowing stale apps to appear in the current TUI context. ## What changed - Invalidate cached app data, dismiss the app picker, and start a fresh fetch when the account, workspace, or thread changes. - Tag app directory fetches with their originating thread, workspace, and scope generation, and ignore queued requests or results that no longer match. - Treat `AppListUpdated` notifications as revalidation signals instead of directly adopting their unscoped contents, with deduplication and a bounded error retry. ## Testing Added coverage for stale request and result rejection, context invalidation, picker dismissal, and notification revalidation. GitOrigin-RevId: f20930a0598a6abbdfb3c7826ec955346fe2da7a
This commit is contained in:
1 parent
85fc4def35
commit
3c7ae4a812
13 files changed
+505
-28
No files matched your search
@@ -6,7 +6,6 @@ use super::app_server_event_targets::server_notification_thread_target;
|
||||
use super::app_server_event_targets::server_request_thread_id;
|
||||
use crate::app_command::AppCommand;
|
||||
use crate::app_event::AppEvent;
|
||||
use crate::app_event::ConnectorsSnapshot;
|
||||
use crate::app_info::app_info_from_api;
|
||||
use crate::app_server_session::AppServerSession;
|
||||
use crate::app_server_session::status_account_display_from_auth_mode;
|
||||
@@ -162,17 +161,17 @@ impl App {
|
||||
return;
|
||||
}
|
||||
ServerNotification::AppListUpdated(notification) => {
|
||||
self.chat_widget.on_connectors_loaded(
|
||||
Ok(ConnectorsSnapshot {
|
||||
connectors: notification
|
||||
.data
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(app_info_from_api)
|
||||
.collect(),
|
||||
}),
|
||||
/*is_final*/ false,
|
||||
);
|
||||
if self.current_displayed_thread_id().is_some() {
|
||||
self.chat_widget
|
||||
.refresh_connector_directory_after_notification(
|
||||
notification
|
||||
.data
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(app_info_from_api)
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -246,14 +246,21 @@ impl App {
|
||||
) {
|
||||
let request_handle = app_server.request_handle();
|
||||
let app_event_tx = self.app_event_tx.clone();
|
||||
let thread_id = self
|
||||
.current_displayed_thread_id()
|
||||
.map(|thread_id| thread_id.to_string());
|
||||
let thread_id = self.current_displayed_thread_id();
|
||||
let cwd = self.chat_widget.config_ref().cwd.to_path_buf();
|
||||
let generation = self.chat_widget.connector_scope_generation();
|
||||
tokio::spawn(async move {
|
||||
let result = fetch_connectors_list(request_handle, force_refetch, thread_id)
|
||||
.await
|
||||
.map_err(|err| err.to_string());
|
||||
let result = fetch_connectors_list(
|
||||
request_handle,
|
||||
force_refetch,
|
||||
thread_id.map(|thread_id| thread_id.to_string()),
|
||||
)
|
||||
.await
|
||||
.map_err(|err| err.to_string());
|
||||
app_event_tx.send(AppEvent::ConnectorsLoaded {
|
||||
thread_id,
|
||||
cwd,
|
||||
generation,
|
||||
result,
|
||||
is_final: true,
|
||||
});
|
||||
|
||||
@@ -765,8 +765,13 @@ impl App {
|
||||
AppEvent::RefreshConnectors { force_refetch } => {
|
||||
self.chat_widget.refresh_connectors(force_refetch);
|
||||
}
|
||||
AppEvent::FetchConnectorsList { force_refetch } => {
|
||||
self.fetch_connectors_list(app_server, force_refetch);
|
||||
AppEvent::FetchConnectorsList {
|
||||
force_refetch,
|
||||
generation,
|
||||
} => {
|
||||
if generation == self.chat_widget.connector_scope_generation() {
|
||||
self.fetch_connectors_list(app_server, force_refetch);
|
||||
}
|
||||
}
|
||||
AppEvent::PluginInstallAuthAdvance { refresh_connectors } => {
|
||||
if refresh_connectors {
|
||||
@@ -1285,8 +1290,19 @@ impl App {
|
||||
AppEvent::CommitPendingUsageOutputAfterStreamShutdown => {
|
||||
self.insert_pending_usage_output_after_stream_shutdown(tui);
|
||||
}
|
||||
AppEvent::ConnectorsLoaded { result, is_final } => {
|
||||
self.chat_widget.on_connectors_loaded(result, is_final);
|
||||
AppEvent::ConnectorsLoaded {
|
||||
thread_id,
|
||||
cwd,
|
||||
generation,
|
||||
result,
|
||||
is_final,
|
||||
} => {
|
||||
if thread_id == self.current_displayed_thread_id()
|
||||
&& cwd.as_path() == self.chat_widget.config_ref().cwd.as_path()
|
||||
&& generation == self.chat_widget.connector_scope_generation()
|
||||
{
|
||||
self.chat_widget.on_connectors_loaded(result, is_final);
|
||||
}
|
||||
}
|
||||
AppEvent::UpdateReasoningEffort(effort) => {
|
||||
self.on_update_reasoning_effort(effort.clone());
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
mod advanced_reasoning_tests;
|
||||
#[path = "tests/background_exit_tests.rs"]
|
||||
mod background_exit_tests;
|
||||
#[path = "tests/connector_policy.rs"]
|
||||
mod connector_policy;
|
||||
#[path = "tests/key_chords.rs"]
|
||||
mod key_chords;
|
||||
#[path = "tests/mcp_startup.rs"]
|
||||
|
||||
@@ -0,0 +1,253 @@
|
||||
use super::*;
|
||||
use crate::app_event::ConnectorsSnapshot;
|
||||
use codex_app_server_client::AppServerEvent;
|
||||
use codex_app_server_protocol::AppListUpdatedNotification;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[tokio::test]
|
||||
async fn connector_directory_results_ignore_stale_thread_workspace_and_account() -> Result<()> {
|
||||
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
app.chat_widget
|
||||
.set_feature_enabled(Feature::Apps, /*enabled*/ true);
|
||||
let thread_id = ThreadId::new();
|
||||
let cwd = app.chat_widget.config_ref().cwd.to_path_buf();
|
||||
app.chat_widget
|
||||
.handle_thread_session(test_thread_session(thread_id, cwd.clone()));
|
||||
let previous_generation = app.chat_widget.connector_scope_generation();
|
||||
app.chat_widget.update_account_state(
|
||||
/*status_account_display*/ None, /*plan_type*/ None,
|
||||
/*has_chatgpt_account*/ true, /*has_codex_backend_auth*/ true,
|
||||
);
|
||||
app.chat_widget.insert_str("$");
|
||||
|
||||
let generation = app.chat_widget.connector_scope_generation();
|
||||
let snapshot = ConnectorsSnapshot {
|
||||
connectors: vec![serde_json::from_str(
|
||||
r#"{"id":"scoped-app","name":"Scoped App","isAccessible":true}"#,
|
||||
)?],
|
||||
};
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let mut app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
|
||||
for (candidate_thread, candidate_cwd, candidate_generation, is_current) in [
|
||||
(Some(ThreadId::new()), cwd.clone(), generation, false),
|
||||
(
|
||||
Some(thread_id),
|
||||
cwd.join("other-workspace"),
|
||||
generation,
|
||||
false,
|
||||
),
|
||||
(Some(thread_id), cwd.clone(), previous_generation, false),
|
||||
(Some(thread_id), cwd.clone(), generation, true),
|
||||
] {
|
||||
app.handle_event(
|
||||
&mut tui,
|
||||
&mut app_server,
|
||||
AppEvent::ConnectorsLoaded {
|
||||
thread_id: candidate_thread,
|
||||
cwd: candidate_cwd,
|
||||
generation: candidate_generation,
|
||||
result: Ok(snapshot.clone()),
|
||||
is_final: true,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(
|
||||
render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("Scoped App"),
|
||||
is_current
|
||||
);
|
||||
}
|
||||
|
||||
app_server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn queued_connector_fetches_are_bound_to_their_original_account() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
let cwd = app.chat_widget.config_ref().cwd.to_path_buf();
|
||||
app.chat_widget
|
||||
.handle_thread_session(test_thread_session(ThreadId::new(), cwd));
|
||||
while app_event_rx.try_recv().is_ok() {}
|
||||
|
||||
app.chat_widget.update_account_state(
|
||||
/*status_account_display*/ None, /*plan_type*/ None,
|
||||
/*has_chatgpt_account*/ true, /*has_codex_backend_auth*/ true,
|
||||
);
|
||||
let stale_fetch = app_event_rx.try_recv()?;
|
||||
app.chat_widget.update_account_state(
|
||||
/*status_account_display*/ None, /*plan_type*/ None,
|
||||
/*has_chatgpt_account*/ true, /*has_codex_backend_auth*/ true,
|
||||
);
|
||||
let current_fetch = app_event_rx.try_recv()?;
|
||||
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let mut app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
let metrics = tokio::runtime::Handle::current().metrics();
|
||||
let initial_tasks = metrics.num_alive_tasks();
|
||||
|
||||
app.handle_event(&mut tui, &mut app_server, stale_fetch)
|
||||
.await?;
|
||||
assert_eq!(metrics.num_alive_tasks(), initial_tasks);
|
||||
app.handle_event(&mut tui, &mut app_server, current_fetch)
|
||||
.await?;
|
||||
assert_eq!(metrics.num_alive_tasks(), initial_tasks + 1);
|
||||
|
||||
app_server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unscoped_app_list_notifications_only_revalidate_the_current_directory() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
let thread_id = ThreadId::new();
|
||||
let cwd = app.chat_widget.config_ref().cwd.to_path_buf();
|
||||
app.chat_widget
|
||||
.handle_thread_session(test_thread_session(thread_id, cwd.clone()));
|
||||
app.chat_widget.update_account_state(
|
||||
/*status_account_display*/ None, /*plan_type*/ None,
|
||||
/*has_chatgpt_account*/ true, /*has_codex_backend_auth*/ true,
|
||||
);
|
||||
let scoped_snapshot = ConnectorsSnapshot {
|
||||
connectors: vec![serde_json::from_str(
|
||||
r#"{"id":"scoped-app","name":"Scoped App","isAccessible":true}"#,
|
||||
)?],
|
||||
};
|
||||
app.chat_widget
|
||||
.on_connectors_loaded(Ok(scoped_snapshot.clone()), /*is_final*/ true);
|
||||
app.chat_widget.insert_str("$");
|
||||
while app_event_rx.try_recv().is_ok() {}
|
||||
|
||||
let mut app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
for (id, name, should_refresh) in [
|
||||
("scoped-app", "Scoped App", false),
|
||||
("old-workspace", "Old Workspace", true),
|
||||
("old-workspace", "Old Workspace", false),
|
||||
] {
|
||||
let notification = ServerNotification::AppListUpdated(AppListUpdatedNotification {
|
||||
data: vec![serde_json::from_value(serde_json::json!({
|
||||
"id": id, "name": name, "isAccessible": true
|
||||
}))?],
|
||||
});
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
AppServerEvent::ServerNotification(Box::new(notification)),
|
||||
)
|
||||
.await;
|
||||
|
||||
let refresh_requested = matches!(
|
||||
app_event_rx.try_recv(),
|
||||
Ok(AppEvent::FetchConnectorsList {
|
||||
force_refetch: false,
|
||||
..
|
||||
})
|
||||
);
|
||||
assert_eq!(refresh_requested, should_refresh);
|
||||
let popup = render_bottom_popup(&app.chat_widget, /*width*/ 80);
|
||||
assert!(popup.contains("Scoped App"));
|
||||
assert!(!popup.contains("Old Workspace"));
|
||||
}
|
||||
|
||||
let ready_app = r#"{"id":"newly-ready","name":"Newly Ready","isAccessible":true}"#;
|
||||
let ready_notification = ServerNotification::AppListUpdated(AppListUpdatedNotification {
|
||||
data: vec![serde_json::from_str(ready_app)?],
|
||||
});
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
AppServerEvent::ServerNotification(Box::new(ready_notification.clone())),
|
||||
)
|
||||
.await;
|
||||
assert!(app_event_rx.try_recv().is_err());
|
||||
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("Newly Ready"));
|
||||
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let generation = app.chat_widget.connector_scope_generation();
|
||||
for should_retry in [true, false, true, false] {
|
||||
app.handle_event(
|
||||
&mut tui,
|
||||
&mut app_server,
|
||||
AppEvent::ConnectorsLoaded {
|
||||
thread_id: Some(thread_id),
|
||||
cwd: cwd.clone(),
|
||||
generation,
|
||||
result: Err("app directory failed".to_string()),
|
||||
is_final: true,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(
|
||||
matches!(
|
||||
app_event_rx.try_recv(),
|
||||
Ok(AppEvent::FetchConnectorsList {
|
||||
force_refetch: false,
|
||||
..
|
||||
})
|
||||
),
|
||||
should_retry
|
||||
);
|
||||
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("Newly Ready"));
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
AppServerEvent::ServerNotification(Box::new(ready_notification.clone())),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(app_event_rx.try_recv().is_ok(), !should_retry);
|
||||
if !should_retry {
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
AppServerEvent::ServerNotification(Box::new(ready_notification.clone())),
|
||||
)
|
||||
.await;
|
||||
assert!(app_event_rx.try_recv().is_err());
|
||||
}
|
||||
}
|
||||
|
||||
for (snapshot, should_revalidate) in [
|
||||
(scoped_snapshot, true),
|
||||
(
|
||||
ConnectorsSnapshot {
|
||||
connectors: vec![serde_json::from_str(ready_app)?],
|
||||
},
|
||||
false,
|
||||
),
|
||||
] {
|
||||
app.handle_event(
|
||||
&mut tui,
|
||||
&mut app_server,
|
||||
AppEvent::ConnectorsLoaded {
|
||||
thread_id: Some(thread_id),
|
||||
cwd: cwd.clone(),
|
||||
generation,
|
||||
result: Ok(snapshot),
|
||||
is_final: true,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(
|
||||
matches!(
|
||||
app_event_rx.try_recv(),
|
||||
Ok(AppEvent::FetchConnectorsList {
|
||||
force_refetch: false,
|
||||
..
|
||||
})
|
||||
),
|
||||
should_revalidate
|
||||
);
|
||||
if should_revalidate {
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
AppServerEvent::ServerNotification(Box::new(ready_notification.clone())),
|
||||
)
|
||||
.await;
|
||||
assert!(app_event_rx.try_recv().is_err());
|
||||
}
|
||||
}
|
||||
app.chat_widget.add_connectors_output();
|
||||
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("Newly Ready"));
|
||||
|
||||
app_server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user