From ea7777862967cb5a5b6e20829550c299db66b3b6 Mon Sep 17 00:00:00 2001 From: Friel Date: Fri, 3 Apr 2026 22:27:58 +0000 Subject: [PATCH] Cover watchdog self-close routing and final messages --- .../src/tools/handlers/multi_agents_tests.rs | 129 ++++++++++++++++-- codex-rs/features/src/lib.rs | 3 + codex-rs/features/src/tests.rs | 15 ++ 3 files changed, 135 insertions(+), 12 deletions(-) diff --git a/codex-rs/core/src/tools/handlers/multi_agents_tests.rs b/codex-rs/core/src/tools/handlers/multi_agents_tests.rs index 4d1a5430e4..35d73d2762 100644 --- a/codex-rs/core/src/tools/handlers/multi_agents_tests.rs +++ b/codex-rs/core/src/tools/handlers/multi_agents_tests.rs @@ -1,6 +1,7 @@ use super::*; use crate::AuthManager; use crate::CodexAuth; +use crate::CodexThread; use crate::ThreadManager; use crate::agent::WatchdogRegistration; use crate::built_in_model_providers; @@ -92,7 +93,7 @@ async fn attach_watchdog_helper_for_tests( manager: &ThreadManager, agent_control: &crate::agent::AgentControl, config: &crate::config::Config, -) -> ThreadId { +) -> (ThreadId, ThreadId, ThreadId) { let owner = manager .start_thread(config.clone()) .await @@ -126,7 +127,24 @@ async fn attach_watchdog_helper_for_tests( Some(owner.thread_id), "watchdog helper should be registered for owner" ); - helper.thread_id + (owner.thread_id, target, helper.thread_id) +} + +async fn wait_for_owner_close_end_event( + owner_thread: &CodexThread, + target_thread_id: ThreadId, +) -> CollabCloseEndEvent { + loop { + let event = timeout(Duration::from_secs(2), owner_thread.next_event()) + .await + .expect("owner close event should arrive") + .expect("owner event should be readable"); + if let EventMsg::CollabCloseEnd(close_end) = event.msg + && close_end.receiver_thread_id == target_thread_id + { + return close_end; + } + } } fn history_contains_inter_agent_communication( @@ -2495,15 +2513,19 @@ async fn watchdog_self_close_rejects_non_watchdog_thread() { } #[tokio::test] -async fn watchdog_self_close_closes_watchdog_helper_and_returns_previous_status() { +async fn watchdog_self_close_closes_watchdog_handle_and_returns_previous_status() { let (mut session, turn) = make_session_and_context().await; let manager = thread_manager(); let agent_control = manager.agent_control(); session.services.agent_control = agent_control.clone(); - let helper_thread_id = + let (owner_thread_id, target_thread_id, helper_thread_id) = attach_watchdog_helper_for_tests(&manager, &agent_control, turn.config.as_ref()).await; + let owner_thread = manager + .get_thread(owner_thread_id) + .await + .expect("owner thread should exist"); session.conversation_id = helper_thread_id; - let status_before = agent_control.get_status(helper_thread_id).await; + let status_before = agent_control.get_status(target_thread_id).await; let output = WatchdogSelfCloseHandler .handle(invocation( @@ -2521,18 +2543,89 @@ async fn watchdog_self_close_closes_watchdog_helper_and_returns_previous_status( assert_eq!( result, WatchdogSelfCloseResult { - previous_status: status_before, + previous_status: status_before.clone(), } ); assert_eq!(success, Some(true)); assert_eq!( - agent_control.get_status(helper_thread_id).await, + agent_control.get_status(target_thread_id).await, AgentStatus::NotFound ); + assert_eq!( + agent_control.get_status(helper_thread_id).await, + AgentStatus::NotFound, + ); + let close_end = wait_for_owner_close_end_event(owner_thread.as_ref(), target_thread_id).await; + assert_eq!(close_end.sender_thread_id, helper_thread_id); + assert_eq!(close_end.receiver_thread_id, target_thread_id); + assert_eq!(close_end.status, status_before); } #[tokio::test] -async fn multi_agent_v2_watchdog_self_close_closes_watchdog_helper_and_returns_previous_status() { +async fn watchdog_self_close_sends_final_message_to_owner_before_closing_handle() { + let (mut session, turn) = make_session_and_context().await; + let manager = thread_manager(); + let agent_control = manager.agent_control(); + session.services.agent_control = agent_control.clone(); + let (owner_thread_id, target_thread_id, helper_thread_id) = + attach_watchdog_helper_for_tests(&manager, &agent_control, turn.config.as_ref()).await; + let owner_thread = manager + .get_thread(owner_thread_id) + .await + .expect("owner thread should exist"); + session.conversation_id = helper_thread_id; + + let output = WatchdogSelfCloseHandler + .handle(invocation( + Arc::new(session), + Arc::new(turn), + "watchdog_self_close", + function_payload(json!({"message": "watchdog done"})), + )) + .await + .expect("watchdog helper should self-close and notify owner"); + let (_content, success) = expect_text_output(output); + + assert_eq!(success, Some(true)); + assert_eq!( + agent_control.get_status(target_thread_id).await, + AgentStatus::NotFound, + ); + let captured_ops = manager.captured_ops(); + let owner_wakeup_items = captured_ops + .iter() + .into_iter() + .find_map(|(thread_id, op)| match op { + Op::InjectResponseItems { items } if *thread_id == owner_thread_id => { + Some(items.clone()) + } + _ => None, + }) + .unwrap_or_else(|| panic!("owner should receive watchdog final message: {captured_ops:?}")); + let output = owner_wakeup_items + .iter() + .find_map(|item| match item { + ResponseInputItem::FunctionCallOutput { output, .. } => Some(output), + ResponseInputItem::Message { .. } + | ResponseInputItem::FunctionCall { .. } + | ResponseInputItem::McpToolCallOutput { .. } + | ResponseInputItem::CustomToolCallOutput { .. } + | ResponseInputItem::ToolSearchOutput { .. } => None, + }) + .expect("watchdog final message should be forwarded through the owner inbox"); + let FunctionCallOutputBody::Text(output_text) = &output.body else { + panic!("watchdog final inbox payload should be text"); + }; + let payload: serde_json::Value = + serde_json::from_str(output_text).expect("watchdog inbox payload should be json"); + assert_eq!(payload["message"], "watchdog done"); + let close_end = wait_for_owner_close_end_event(owner_thread.as_ref(), target_thread_id).await; + assert_eq!(close_end.sender_thread_id, helper_thread_id); + assert_eq!(close_end.receiver_thread_id, target_thread_id); +} + +#[tokio::test] +async fn multi_agent_v2_watchdog_self_close_closes_watchdog_handle_and_returns_previous_status() { let (mut session, mut turn) = make_session_and_context().await; let manager = thread_manager(); let agent_control = manager.agent_control(); @@ -2542,11 +2635,15 @@ async fn multi_agent_v2_watchdog_self_close_closes_watchdog_helper_and_returns_p .features .enable(Feature::MultiAgentV2) .expect("test config should allow feature update"); - let helper_thread_id = + let (owner_thread_id, target_thread_id, helper_thread_id) = attach_watchdog_helper_for_tests(&manager, &agent_control, &config).await; + let owner_thread = manager + .get_thread(owner_thread_id) + .await + .expect("owner thread should exist"); session.conversation_id = helper_thread_id; turn.config = Arc::new(config); - let status_before = agent_control.get_status(helper_thread_id).await; + let status_before = agent_control.get_status(target_thread_id).await; let output = WatchdogSelfCloseHandlerV2 .handle(invocation( @@ -2564,14 +2661,22 @@ async fn multi_agent_v2_watchdog_self_close_closes_watchdog_helper_and_returns_p assert_eq!( result, WatchdogSelfCloseResult { - previous_status: status_before, + previous_status: status_before.clone(), } ); assert_eq!(success, Some(true)); assert_eq!( - agent_control.get_status(helper_thread_id).await, + agent_control.get_status(target_thread_id).await, AgentStatus::NotFound ); + assert_eq!( + agent_control.get_status(helper_thread_id).await, + AgentStatus::NotFound, + ); + let close_end = wait_for_owner_close_end_event(owner_thread.as_ref(), target_thread_id).await; + assert_eq!(close_end.sender_thread_id, helper_thread_id); + assert_eq!(close_end.receiver_thread_id, target_thread_id); + assert_eq!(close_end.status, status_before); } #[tokio::test] diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index 3e8336307d..d5c2f6451e 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -435,6 +435,9 @@ impl Features { if self.enabled(Feature::SpawnCsv) && !self.enabled(Feature::Collab) { self.enable(Feature::Collab); } + if self.enabled(Feature::AgentWatchdog) && !self.enabled(Feature::ToolSearch) { + self.enable(Feature::ToolSearch); + } if self.enabled(Feature::CodeModeOnly) && !self.enabled(Feature::CodeMode) { self.enable(Feature::CodeMode); } diff --git a/codex-rs/features/src/tests.rs b/codex-rs/features/src/tests.rs index 5fdb03a772..8e44ae0fc4 100644 --- a/codex-rs/features/src/tests.rs +++ b/codex-rs/features/src/tests.rs @@ -192,6 +192,21 @@ fn enable_fanout_normalization_enables_multi_agent_one_way() { assert_eq!(collab_features.enabled(Feature::SpawnCsv), false); } +#[test] +fn agent_watchdog_normalization_enables_tool_search_one_way() { + let mut watchdog_features = Features::with_defaults(); + watchdog_features.enable(Feature::AgentWatchdog); + watchdog_features.normalize_dependencies(); + assert_eq!(watchdog_features.enabled(Feature::AgentWatchdog), true); + assert_eq!(watchdog_features.enabled(Feature::ToolSearch), true); + + let mut tool_search_features = Features::with_defaults(); + tool_search_features.enable(Feature::ToolSearch); + tool_search_features.normalize_dependencies(); + assert_eq!(tool_search_features.enabled(Feature::ToolSearch), true); + assert_eq!(tool_search_features.enabled(Feature::AgentWatchdog), false); +} + #[test] fn apps_require_feature_flag_and_chatgpt_auth() { let mut features = Features::with_defaults();