Reject TUI events from a different backend (#55304)

## Why

Events from another backend must not mutate the current backend's thread or connection state, even when native thread IDs collide.

## What changed

Attach the session's `BackendId` to incoming app-server events and check it before dispatching to thread caches or UI handlers. Apply the same check when waiting for a thread revert notification.

## Testing

Add regression tests covering ignored foreign notifications, approval requests, lag and disconnect events, accepted events from the current backend, and preservation of backend identity on transport events.

GitOrigin-RevId: 2577ab78e0592726f5f05e3426772899e6cb7c7e
This commit is contained in:
Benjamin Carlsson
2026-10-19 15:20:42 +00:00
committed by copyberry
parent f6e03d4eac
commit e4568a3538
26 changed files with 197 additions and 44 deletions
@@ -0,0 +1,10 @@
//! Retains the logical backend that produced a session event before UI dispatch.
//! Backend identity survives reconnects; it is not a transport generation or wire ID.
use crate::backend_id::BackendId;
use codex_app_server_client::AppServerEvent;
pub(crate) struct AppServerSessionEvent {
pub(crate) backend_id: BackendId,
pub(crate) event: AppServerEvent,
}
@@ -3,6 +3,7 @@
use super::*;
use crate::external_agent_config_migration::flow::ExternalAgentConfigMigrationFlowOutcome;
use crate::external_agent_config_migration::flow::handle_external_agent_config_migration_prompt;
use codex_app_server_client::AppServerEvent;
use codex_app_server_protocol::JSONRPCMessage;
use codex_app_server_protocol::ServerNotification;
use futures::SinkExt;
@@ -103,7 +104,9 @@ async fn external_agent_config_migration_over_shared_transport() -> Result<()> {
for (import_id, consumed) in [("other-client", false), ("this-client", true)] {
let event =
tokio::time::timeout(Duration::from_secs(/*secs*/ 5), session.next_event()).await?;
let Some(AppServerEvent::ServerNotification(notification)) = event else {
let Some(AppServerEvent::ServerNotification(notification)) =
event.map(|event| event.event)
else {
panic!("expected import completion");
};
let ServerNotification::ExternalAgentConfigImportCompleted(notification) =
@@ -2,6 +2,7 @@
use super::*;
use crate::legacy_core::config::ConfigBuilder;
use codex_app_server_client::AppServerEvent;
use codex_app_server_protocol::ServerNotification;
use core_test_support::responses;
use pretty_assertions::assert_eq;
@@ -155,7 +156,7 @@ stream_max_retries = 0
.await?;
tokio::time::timeout(std::time::Duration::from_secs(/*secs*/ 30), async {
while let Some(event) = app_server.next_event().await {
if let AppServerEvent::ServerNotification(notification) = event
if let AppServerEvent::ServerNotification(notification) = event.event
&& let ServerNotification::TurnCompleted(completed) = *notification
{
assert_eq!(
@@ -83,7 +83,7 @@ async fn remote_resume_restores_saved_server_profile_without_permission_override
tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
if let Some(codex_app_server_client::AppServerEvent::ServerNotification(notification)) =
server.next_event().await
server.next_event().await.map(|event| event.event)
&& let codex_app_server_protocol::ServerNotification::ThreadSettingsUpdated(
settings,
) = *notification
@@ -3,6 +3,7 @@
use super::*;
use crate::legacy_core::config::ConfigBuilder;
use crate::legacy_core::config::ConfigOverrides;
use codex_app_server_client::AppServerEvent;
use codex_app_server_protocol::ServerNotification;
use core_test_support::responses;
use pretty_assertions::assert_eq;
@@ -112,7 +113,7 @@ stream_max_retries = 0
.await?;
tokio::time::timeout(std::time::Duration::from_secs(/*secs*/ 30), async {
while let Some(event) = app_server.next_event().await {
if let AppServerEvent::ServerNotification(notification) = event
if let AppServerEvent::ServerNotification(notification) = event.event
&& let ServerNotification::TurnCompleted(completed) = *notification
{
assert_eq!(