mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
Let idle child threads unload while the message board is connected (#63621)
## Why The message board connects to the app server to inject messages. Automatically subscribing it to new threads keeps idle child threads loaded after their last other subscriber leaves. ## What changed Skip automatic subscriptions to newly created threads for clients that initialize with the shared `codex_message_board` client name. ## Testing Add a WebSocket regression test that unsubscribes the parent thread's client from a completed child thread, checks for `thread/closed`, and confirms through the original message board connection that only the parent remains loaded. GitOrigin-RevId: 73af9e370b90c59f984c4bc804140febb0308d9b
This commit is contained in:
@@ -18,6 +18,9 @@ use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use strum_macros::Display;
|
||||
|
||||
/// The message board's `clientInfo.name` during app-server initialization.
|
||||
pub const MESSAGE_BOARD_CLIENT_NAME: &str = "codex_message_board";
|
||||
|
||||
/// Authentication mode for OpenAI-backed providers.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Display, JsonSchema, TS)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
|
||||
@@ -51,6 +51,7 @@ use crate::transport::start_websocket_acceptor;
|
||||
use codex_analytics::AppServerRpcTransport;
|
||||
use codex_app_server_protocol::ConfigWarningNotification;
|
||||
use codex_app_server_protocol::JSONRPCMessage;
|
||||
use codex_app_server_protocol::MESSAGE_BOARD_CLIENT_NAME;
|
||||
use codex_app_server_protocol::ServerNotification;
|
||||
use codex_app_server_protocol::TextPosition as AppTextPosition;
|
||||
use codex_app_server_protocol::TextRange as AppTextRange;
|
||||
@@ -1355,20 +1356,22 @@ pub async fn run_main_with_transport_options(
|
||||
created = thread_created_rx.recv(), if listen_for_threads && !ready_to_exit => {
|
||||
match created {
|
||||
Ok(thread_id) => {
|
||||
let mut initialized_connection_ids = Vec::new();
|
||||
let mut subscriber_ids = Vec::new();
|
||||
for (connection_id, connection_state) in &connections {
|
||||
if connection_state.session.initialized() {
|
||||
initialized_connection_ids.push(*connection_id);
|
||||
// The board only injects mail. Subscribing it keeps idle children loaded.
|
||||
if connection_state
|
||||
.session
|
||||
.app_server_client_name()
|
||||
.is_some_and(|name| name != MESSAGE_BOARD_CLIENT_NAME)
|
||||
{
|
||||
subscriber_ids.push(*connection_id);
|
||||
}
|
||||
}
|
||||
let processor = Arc::clone(&processor);
|
||||
// Attachment can wait on snapshot locks; keep force shutdown responsive.
|
||||
thread_listener_tasks.spawn(async move {
|
||||
processor
|
||||
.try_attach_thread_listener(
|
||||
thread_id,
|
||||
initialized_connection_ids,
|
||||
)
|
||||
.try_attach_thread_listener(thread_id, subscriber_ids)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use anyhow::bail;
|
||||
use app_test_support::DEFAULT_APP_SERVER_TEST_ARGS;
|
||||
use app_test_support::MockResponsesConfig;
|
||||
use app_test_support::create_mock_responses_server_sequence_unchecked;
|
||||
use app_test_support::to_response;
|
||||
use base64::Engine;
|
||||
@@ -14,17 +15,20 @@ use codex_app_server_protocol::JSONRPCMessage;
|
||||
use codex_app_server_protocol::JSONRPCNotification;
|
||||
use codex_app_server_protocol::JSONRPCRequest;
|
||||
use codex_app_server_protocol::JSONRPCResponse;
|
||||
use codex_app_server_protocol::MESSAGE_BOARD_CLIENT_NAME;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::ThreadLoadedListParams;
|
||||
use codex_app_server_protocol::ThreadLoadedListResponse;
|
||||
use codex_app_server_protocol::ThreadStartParams;
|
||||
use codex_app_server_protocol::ThreadStartResponse;
|
||||
use codex_core::config::set_project_trust_level;
|
||||
use codex_features::Feature;
|
||||
use codex_http_client::HttpClient;
|
||||
use codex_http_client::HttpClientBuilder;
|
||||
use codex_http_client::HttpResponse;
|
||||
use codex_protocol::config_types::TrustLevel;
|
||||
use core_test_support::responses::ev_completed;
|
||||
use core_test_support::responses::ev_function_call_with_namespace;
|
||||
use core_test_support::responses::sse;
|
||||
use core_test_support::streaming_sse::StreamingSseChunk;
|
||||
use core_test_support::streaming_sse::start_streaming_sse_server;
|
||||
@@ -33,6 +37,7 @@ use futures::StreamExt;
|
||||
use hmac::Hmac;
|
||||
use hmac::Mac;
|
||||
use http::StatusCode;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
use sha2::Sha256;
|
||||
use std::net::SocketAddr;
|
||||
@@ -559,6 +564,75 @@ async fn websocket_disconnect_keeps_last_subscribed_thread_loaded_until_idle_tim
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn idle_child_unloads_while_message_board_stays_connected() -> Result<()> {
|
||||
let done = app_test_support::create_final_assistant_message_sse_response("Ready.")?;
|
||||
let server = create_mock_responses_server_sequence_unchecked(vec![
|
||||
sse(vec![
|
||||
ev_function_call_with_namespace(
|
||||
"spawn-worker",
|
||||
"collaboration",
|
||||
"spawn_agent",
|
||||
r#"{"task_name":"worker","message":"Say ready.","fork_turns":"none"}"#,
|
||||
),
|
||||
ev_completed("spawn"),
|
||||
]),
|
||||
done.clone(),
|
||||
done,
|
||||
])
|
||||
.await;
|
||||
let codex_home = TempDir::new()?;
|
||||
MockResponsesConfig::new(&server.uri())
|
||||
.enable_feature(Feature::MultiAgentV2)
|
||||
.disable_feature(Feature::CodeMode)
|
||||
.disable_feature(Feature::CodeModeOnly)
|
||||
.with_root_config("thread_unload_delay_secs = 0")
|
||||
.write(codex_home.path())?;
|
||||
let (mut process, address) = spawn_websocket_server(codex_home.path()).await?;
|
||||
let mut owner = connect_websocket(address).await?;
|
||||
send_initialize_request(&mut owner, /*id*/ 1, "ws_thread_owner").await?;
|
||||
read_response_for_id(&mut owner, /*id*/ 1).await?;
|
||||
let parent = start_thread(&mut owner, /*id*/ 2).await?;
|
||||
let mut board = connect_websocket(address).await?;
|
||||
send_initialize_request(&mut board, /*id*/ 1, MESSAGE_BOARD_CLIENT_NAME).await?;
|
||||
read_response_for_id(&mut board, /*id*/ 1).await?;
|
||||
|
||||
send_request(
|
||||
&mut owner,
|
||||
"turn/start",
|
||||
/*id*/ 3,
|
||||
Some(json!({"threadId": parent, "input": [{"type": "text", "text": "Spawn a worker."}]})),
|
||||
)
|
||||
.await?;
|
||||
let mut child = None;
|
||||
for _ in 0..2 {
|
||||
let completed = read_notification_for_method(&mut owner, "turn/completed").await?;
|
||||
let params = completed.params.context("turn completion")?;
|
||||
assert_eq!(params["turn"]["status"], "completed");
|
||||
if params["threadId"] != parent {
|
||||
child = Some(params["threadId"].as_str().context("child ID")?.to_owned());
|
||||
}
|
||||
}
|
||||
let child = child.context("child turn must complete")?;
|
||||
send_request(
|
||||
&mut owner,
|
||||
"thread/unsubscribe",
|
||||
/*id*/ 4,
|
||||
Some(json!({"threadId": child})),
|
||||
)
|
||||
.await?;
|
||||
let (reply, closed) =
|
||||
read_response_and_notification_for_method(&mut owner, /*id*/ 4, "thread/closed")
|
||||
.await
|
||||
.context("idle child should unload with the board still connected")?;
|
||||
assert_eq!(reply.result, json!({"status": "unsubscribed"}));
|
||||
assert_eq!(closed.params, Some(json!({"threadId": child})));
|
||||
// Query over the original board socket to rule out a disconnect causing the unload.
|
||||
assert_loaded_threads(&mut board, /*id*/ 2, &[&parent]).await?;
|
||||
process.kill().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) async fn spawn_websocket_server(codex_home: &Path) -> Result<(Child, SocketAddr)> {
|
||||
spawn_websocket_server_with_args(codex_home, "ws://127.0.0.1:0", &[]).await
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
use anyhow::Context;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
use codex_app_server_protocol::MESSAGE_BOARD_CLIENT_NAME;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::ThreadInjectItemsParams;
|
||||
use codex_app_server_remote_client::AppServerEvent;
|
||||
@@ -122,7 +123,7 @@ async fn connect(url: &Url) -> anyhow::Result<Connection> {
|
||||
websocket_url: url.to_string(),
|
||||
auth_token: None,
|
||||
},
|
||||
client_name: "codex_message_board".into(),
|
||||
client_name: MESSAGE_BOARD_CLIENT_NAME.into(),
|
||||
client_version: env!("CARGO_PKG_VERSION").into(),
|
||||
experimental_api: true,
|
||||
mcp_server_openai_form_elicitation: false,
|
||||
|
||||
Reference in New Issue
Block a user