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:
Charlie Marsh authored and copyberry committed 2026-08-15 10:57:15 +00:00
1 parent 85fc4def35
commit 3c7ae4a812
13 files changed
+505 -28

No files matched your search

@@ -342,9 +342,23 @@ async fn invalid_url_elicitation_is_declined() {
#[tokio::test]
async fn thread_settings_updated_updates_visible_state_without_transcript() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(Some("gpt-5.2")).await;
set_chatgpt_auth(&mut chat);
set_fast_mode_test_catalog(&mut chat);
let thread_id = ThreadId::new();
chat.handle_thread_session(configured_thread_session(thread_id));
let mut session = configured_thread_session(thread_id);
session.cwd = test_path_buf("/tmp/original-workspace").abs();
chat.handle_thread_session(session);
let generation = chat.connector_scope_generation();
chat.on_connectors_loaded(
Ok(crate::app_event::ConnectorsSnapshot {
connectors: vec![
serde_json::from_str(r#"{"id":"old","name":"Old","isAccessible":true}"#)
.expect("valid app"),
],
}),
/*is_final*/ true,
);
chat.prefetch_connectors();
let _ = drain_insert_history(&mut rx);
chat.handle_server_notification(
@@ -352,6 +366,9 @@ async fn thread_settings_updated_updates_visible_state_without_transcript() {
/*replay_kind*/ None,
);
assert_ne!(chat.connector_scope_generation(), generation);
assert!(chat.connectors_for_mentions().is_none());
assert!(chat.connectors.prefetch_in_flight);
assert_eq!(chat.current_model(), "gpt-5.4");
assert_eq!(
chat.current_reasoning_effort(),
@@ -2260,6 +2260,48 @@ async fn apps_notification_update_excludes_inaccessible_apps_from_mentions() {
);
}
#[tokio::test]
async fn account_changes_revoke_app_directory_and_restart_scoped_requests() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.thread_id = Some(ThreadId::new());
set_chatgpt_auth(&mut chat);
chat.on_connectors_loaded(
Ok(ConnectorsSnapshot {
connectors: vec![
serde_json::from_str(
r#"{"id":"old-account","name":"Old Account","isAccessible":true}"#,
)
.expect("valid app"),
],
}),
/*is_final*/ true,
);
let generation = chat.connector_scope_generation();
chat.update_account_state(
/*status_account_display*/ None, /*plan_type*/ None,
/*has_chatgpt_account*/ true, /*has_codex_backend_auth*/ true,
);
assert_ne!(chat.connector_scope_generation(), generation);
assert!(chat.connectors_for_mentions().is_none());
assert!(chat.connectors.prefetch_in_flight);
assert_matches!(
rx.try_recv(),
Ok(AppEvent::FetchConnectorsList {
force_refetch: false,
generation
}) if generation == chat.connector_scope_generation()
);
let (mut replacement, _, _) = make_chatwidget_manual(/*model_override*/ None).await;
replacement.invalidate_connector_scope();
assert_ne!(
chat.connector_scope_generation(),
replacement.connector_scope_generation()
);
}
#[tokio::test]
async fn apps_refresh_failure_keeps_existing_full_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
@@ -3797,3 +3839,31 @@ async fn reasoning_popup_escape_returns_to_model_popup() {
assert!(after_escape.contains("Select Model"));
assert!(!after_escape.contains("Select Reasoning Level"));
}
#[tokio::test]
async fn account_change_dismisses_the_previous_app_directory_snapshot() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.thread_id = Some(ThreadId::new());
set_chatgpt_auth(&mut chat);
chat.on_connectors_loaded(
Ok(ConnectorsSnapshot {
connectors: vec![serde_json::from_str(
r#"{"id":"previous-account","name":"Previous Account App","isAccessible":true}"#,
)
.expect("valid app")],
}),
/*is_final*/ true,
);
chat.add_connectors_output();
let before = render_bottom_popup(&chat, /*width*/ 80);
chat.update_account_state(
/*status_account_display*/ None, /*plan_type*/ None,
/*has_chatgpt_account*/ true, /*has_codex_backend_auth*/ true,
);
let after = normalize_snapshot_paths(render_bottom_popup(&chat, /*width*/ 80));
assert_chatwidget_snapshot!(
"connector_scope_invalidation",
format!("Before account change:\n{before}\n\nAfter account change:\n{after}")
);
}