From c56750cf382302726cfadf8e8048108a132de532 Mon Sep 17 00:00:00 2001 From: "Will.hou" <1205157517@qq.com> Date: Thu, 21 May 2026 20:05:36 +0800 Subject: [PATCH 1/3] fix: aggregate `is_sleeping` and `reset_prefix_cache` across DP engines Both utility methods previously returned only the first engine's result, masking divergence under data parallel. `is_sleeping` now errors with `InconsistentUtilityResults` when engines disagree, and `reset_prefix_cache` AND-aggregates so it reports success only when every engine confirms the reset. Co-Authored-By: Claude Opus 4.7 (1M context) Signed-off-by: Will.hou <1205157517@qq.com> --- rust/src/engine-core-client/src/client.rs | 27 ++- rust/src/engine-core-client/src/error.rs | 2 + .../engine-core-client/src/tests/client.rs | 168 ++++++++++++++++++ 3 files changed, 192 insertions(+), 5 deletions(-) diff --git a/rust/src/engine-core-client/src/client.rs b/rust/src/engine-core-client/src/client.rs index 4e7140525353..e0ffc5da0a7d 100644 --- a/rust/src/engine-core-client/src/client.rs +++ b/rust/src/engine-core-client/src/client.rs @@ -595,9 +595,23 @@ impl EngineCoreClient { } /// Return whether the engine is currently sleeping at any level. + /// + /// Under data parallel, all engines should agree on the sleep state: a + /// divergence signals a control-plane bug. Returns + /// `Error::InconsistentUtilityResults` if engines disagree. pub async fn is_sleeping(&self) -> Result { - // TODO: we only return the result of the first engine here. - Ok(self.call_utility("is_sleeping", ()).await?[0]) + let results: Vec = self.call_utility("is_sleeping", ()).await?; + // `engine_count >= 1` is enforced during startup handshake, so `results` + // is non-empty. + let first = results[0]; + if results.iter().all(|&v| v == first) { + Ok(first) + } else { + Err(Error::InconsistentUtilityResults { + method: "is_sleeping".to_string(), + values: format!("{results:?}"), + }) + } } /// Reset the multi-modal cache. @@ -613,18 +627,21 @@ impl EngineCoreClient { } /// Reset the prefix cache and optionally the external connector cache. + /// + /// Under data parallel, returns `true` only when every engine confirms the + /// reset (AND aggregation). pub async fn reset_prefix_cache( &self, reset_running_requests: bool, reset_connector: bool, ) -> Result { - // TODO: we only return the result of the first engine here. - Ok(self + let results: Vec = self .call_utility( "reset_prefix_cache", (reset_running_requests, reset_connector), ) - .await?[0]) + .await?; + Ok(results.into_iter().all(|ok| ok)) } /// Put the engine to sleep. diff --git a/rust/src/engine-core-client/src/error.rs b/rust/src/engine-core-client/src/error.rs index 1ce03c407869..0493732b03f9 100644 --- a/rust/src/engine-core-client/src/error.rs +++ b/rust/src/engine-core-client/src/error.rs @@ -83,6 +83,8 @@ pub enum Error { }, #[error("utility call `{method}` closed unexpectedly (call_id={call_id})")] UtilityCallClosed { method: String, call_id: u64 }, + #[error("utility call `{method}` returned inconsistent results across engines: {values}")] + InconsistentUtilityResults { method: String, values: String }, /// A special variant to allow cloning the same error. #[error(transparent)] diff --git a/rust/src/engine-core-client/src/tests/client.rs b/rust/src/engine-core-client/src/tests/client.rs index 54128330bb02..0b46d9a43b72 100644 --- a/rust/src/engine-core-client/src/tests/client.rs +++ b/rust/src/engine-core-client/src/tests/client.rs @@ -2116,6 +2116,174 @@ async fn collective_rpc_flattens_results_from_all_engines() { client.shutdown().await.unwrap(); } +/// Spawn a mock engine that handles a single utility call, asserts the method +/// name matches, and replies with `result`. +fn spawn_mock_utility_engine( + handshake_address: String, + engine_id: Vec, + expected_method: &'static str, + result: bool, +) -> (tokio::sync::oneshot::Sender<()>, tokio::task::JoinHandle<()>) { + spawn_mock_engine_task(handshake_address, engine_id, move |dealer, push| { + Box::pin(async move { + let utility = recv_engine_message(dealer).await; + assert_eq!(utility[0].as_ref(), &[0x03]); + let payload = decode_value(&utility[1]); + let array = match payload { + Value::Array(array) => array, + other => panic!("expected utility payload array, got {other:?}"), + }; + let call_id = array[1].as_u64().expect("call_id"); + assert_eq!(array[2], Value::from(expected_method)); + send_outputs( + push, + EngineCoreOutputs { + utility_output: Some(UtilityOutput { + call_id: call_id.into(), + failure_message: None, + result: Some(utility_result_value(result)), + }), + ..Default::default() + }, + ) + .await; + }) + }) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn is_sleeping_returns_error_when_engines_disagree() { + init_tracing(); + let ipc = IpcNamespace::new().unwrap(); + let handshake_address = ipc.handshake_endpoint(); + + let (shutdown_tx_0, engine_task_0) = spawn_mock_utility_engine( + handshake_address.clone(), + b"engine-0".to_vec(), + "is_sleeping", + true, + ); + tokio::time::sleep(Duration::from_millis(50)).await; + let (shutdown_tx_1, engine_task_1) = spawn_mock_utility_engine( + handshake_address.clone(), + b"engine-1".to_vec(), + "is_sleeping", + false, + ); + + let client = connect_client_with_ipc( + handshake_test_config( + handshake_address, + 2, + "test-model", + Duration::from_secs(2), + 5, + None, + ), + &ipc, + ) + .await; + + let error = client.is_sleeping().await.unwrap_err(); + assert!( + matches!( + &error, + Error::InconsistentUtilityResults { method, .. } if method == "is_sleeping" + ), + "expected InconsistentUtilityResults, got {error:?}", + ); + + let _ = shutdown_tx_0.send(()); + let _ = shutdown_tx_1.send(()); + engine_task_0.await.unwrap(); + engine_task_1.await.unwrap(); + client.shutdown().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn reset_prefix_cache_returns_true_when_all_engines_succeed() { + init_tracing(); + let ipc = IpcNamespace::new().unwrap(); + let handshake_address = ipc.handshake_endpoint(); + + let (shutdown_tx_0, engine_task_0) = spawn_mock_utility_engine( + handshake_address.clone(), + b"engine-0".to_vec(), + "reset_prefix_cache", + true, + ); + tokio::time::sleep(Duration::from_millis(50)).await; + let (shutdown_tx_1, engine_task_1) = spawn_mock_utility_engine( + handshake_address.clone(), + b"engine-1".to_vec(), + "reset_prefix_cache", + true, + ); + + let client = connect_client_with_ipc( + handshake_test_config( + handshake_address, + 2, + "test-model", + Duration::from_secs(2), + 5, + None, + ), + &ipc, + ) + .await; + + assert!(client.reset_prefix_cache(false, false).await.unwrap()); + + let _ = shutdown_tx_0.send(()); + let _ = shutdown_tx_1.send(()); + engine_task_0.await.unwrap(); + engine_task_1.await.unwrap(); + client.shutdown().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn reset_prefix_cache_returns_false_when_any_engine_fails() { + init_tracing(); + let ipc = IpcNamespace::new().unwrap(); + let handshake_address = ipc.handshake_endpoint(); + + let (shutdown_tx_0, engine_task_0) = spawn_mock_utility_engine( + handshake_address.clone(), + b"engine-0".to_vec(), + "reset_prefix_cache", + true, + ); + tokio::time::sleep(Duration::from_millis(50)).await; + let (shutdown_tx_1, engine_task_1) = spawn_mock_utility_engine( + handshake_address.clone(), + b"engine-1".to_vec(), + "reset_prefix_cache", + false, + ); + + let client = connect_client_with_ipc( + handshake_test_config( + handshake_address, + 2, + "test-model", + Duration::from_secs(2), + 5, + None, + ), + &ipc, + ) + .await; + + assert!(!client.reset_prefix_cache(false, false).await.unwrap()); + + let _ = shutdown_tx_0.send(()); + let _ = shutdown_tx_1.send(()); + engine_task_0.await.unwrap(); + engine_task_1.await.unwrap(); + client.shutdown().await.unwrap(); +} + #[test] fn python_msgpack_fixtures_match_rust_encoding() { init_tracing(); From ff22b41bc88913c5843da1ebaa868eaa3558b1b5 Mon Sep 17 00:00:00 2001 From: "Will.hou" <1205157517@qq.com> Date: Thu, 21 May 2026 21:23:04 +0800 Subject: [PATCH 2/3] style: rustfmt `spawn_mock_utility_engine` return type Co-Authored-By: Claude Opus 4.7 (1M context) Signed-off-by: Will.hou <1205157517@qq.com> --- rust/src/engine-core-client/src/tests/client.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rust/src/engine-core-client/src/tests/client.rs b/rust/src/engine-core-client/src/tests/client.rs index 0b46d9a43b72..12c4a139968e 100644 --- a/rust/src/engine-core-client/src/tests/client.rs +++ b/rust/src/engine-core-client/src/tests/client.rs @@ -2123,7 +2123,10 @@ fn spawn_mock_utility_engine( engine_id: Vec, expected_method: &'static str, result: bool, -) -> (tokio::sync::oneshot::Sender<()>, tokio::task::JoinHandle<()>) { +) -> ( + tokio::sync::oneshot::Sender<()>, + tokio::task::JoinHandle<()>, +) { spawn_mock_engine_task(handshake_address, engine_id, move |dealer, push| { Box::pin(async move { let utility = recv_engine_message(dealer).await; From 47c8458c359c7ab52334ca1ddbb66804c3118011 Mon Sep 17 00:00:00 2001 From: "Will.hou" <1205157517@qq.com> Date: Tue, 26 May 2026 14:49:37 +0800 Subject: [PATCH 3/3] [rust] address review: drop test sleeps, guard empty aggregation results Remove the `sleep(50ms)` between spawning the two mock engines in the new utility tests. Engine ordering is content-based (BTreeMap over engine_id in connect_handshake, "startup order does not matter"), and the handshake loop already blocks until every engine completes HELLO/READY/registration, so the sleep gated nothing. Per rust/AGENTS.md, prefer deterministic synchronization over sleep-based timing. Guard both `is_sleeping` and `reset_prefix_cache` against an empty results vector: `is_sleeping` used `[0]` indexing, and `reset_prefix_cache` would report a vacuous `[].all() == true` success. Both now fail loud rather than relying on the startup `engine_count >= 1` invariant. Strengthen the utility tests: assert the serialized request args in the mock engine handler, and add an `is_sleeping` success case covering the all-engines-agree path (previously only disagreement was tested). Co-authored-by: Claude Signed-off-by: Will.hou <1205157517@qq.com> --- rust/src/engine-core-client/src/client.rs | 17 +++++- .../engine-core-client/src/tests/client.rs | 57 +++++++++++++++++-- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/rust/src/engine-core-client/src/client.rs b/rust/src/engine-core-client/src/client.rs index e0ffc5da0a7d..dd3000f3c6ab 100644 --- a/rust/src/engine-core-client/src/client.rs +++ b/rust/src/engine-core-client/src/client.rs @@ -602,8 +602,12 @@ impl EngineCoreClient { pub async fn is_sleeping(&self) -> Result { let results: Vec = self.call_utility("is_sleeping", ()).await?; // `engine_count >= 1` is enforced during startup handshake, so `results` - // is non-empty. - let first = results[0]; + // is normally non-empty; fall back to a fail-loud error rather than + // indexing in case that invariant is ever bypassed. + let first = *results.first().ok_or_else(|| Error::InconsistentUtilityResults { + method: "is_sleeping".to_string(), + values: "[]".to_string(), + })?; if results.iter().all(|&v| v == first) { Ok(first) } else { @@ -641,6 +645,15 @@ impl EngineCoreClient { (reset_running_requests, reset_connector), ) .await?; + // `engine_count >= 1` is enforced during startup handshake, so `results` + // is normally non-empty; fail loud rather than reporting a vacuous + // success (`[].all() == true`) in case that invariant is ever bypassed. + if results.is_empty() { + return Err(Error::InconsistentUtilityResults { + method: "reset_prefix_cache".to_string(), + values: "[]".to_string(), + }); + } Ok(results.into_iter().all(|ok| ok)) } diff --git a/rust/src/engine-core-client/src/tests/client.rs b/rust/src/engine-core-client/src/tests/client.rs index 12c4a139968e..c32e33a11197 100644 --- a/rust/src/engine-core-client/src/tests/client.rs +++ b/rust/src/engine-core-client/src/tests/client.rs @@ -2117,11 +2117,12 @@ async fn collective_rpc_flattens_results_from_all_engines() { } /// Spawn a mock engine that handles a single utility call, asserts the method -/// name matches, and replies with `result`. +/// name and serialized args match, and replies with `result`. fn spawn_mock_utility_engine( handshake_address: String, engine_id: Vec, expected_method: &'static str, + expected_args: Value, result: bool, ) -> ( tokio::sync::oneshot::Sender<()>, @@ -2136,8 +2137,10 @@ fn spawn_mock_utility_engine( Value::Array(array) => array, other => panic!("expected utility payload array, got {other:?}"), }; + // Utility requests serialize as `(client_index, call_id, method, args)`. let call_id = array[1].as_u64().expect("call_id"); assert_eq!(array[2], Value::from(expected_method)); + assert_eq!(array[3], expected_args, "unexpected utility args"); send_outputs( push, EngineCoreOutputs { @@ -2164,13 +2167,14 @@ async fn is_sleeping_returns_error_when_engines_disagree() { handshake_address.clone(), b"engine-0".to_vec(), "is_sleeping", + Value::Array(vec![]), true, ); - tokio::time::sleep(Duration::from_millis(50)).await; let (shutdown_tx_1, engine_task_1) = spawn_mock_utility_engine( handshake_address.clone(), b"engine-1".to_vec(), "is_sleeping", + Value::Array(vec![]), false, ); @@ -2203,6 +2207,49 @@ async fn is_sleeping_returns_error_when_engines_disagree() { client.shutdown().await.unwrap(); } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn is_sleeping_returns_value_when_all_engines_agree() { + init_tracing(); + let ipc = IpcNamespace::new().unwrap(); + let handshake_address = ipc.handshake_endpoint(); + + let (shutdown_tx_0, engine_task_0) = spawn_mock_utility_engine( + handshake_address.clone(), + b"engine-0".to_vec(), + "is_sleeping", + Value::Array(vec![]), + true, + ); + let (shutdown_tx_1, engine_task_1) = spawn_mock_utility_engine( + handshake_address.clone(), + b"engine-1".to_vec(), + "is_sleeping", + Value::Array(vec![]), + true, + ); + + let client = connect_client_with_ipc( + handshake_test_config( + handshake_address, + 2, + "test-model", + Duration::from_secs(2), + 5, + None, + ), + &ipc, + ) + .await; + + assert!(client.is_sleeping().await.unwrap()); + + let _ = shutdown_tx_0.send(()); + let _ = shutdown_tx_1.send(()); + engine_task_0.await.unwrap(); + engine_task_1.await.unwrap(); + client.shutdown().await.unwrap(); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn reset_prefix_cache_returns_true_when_all_engines_succeed() { init_tracing(); @@ -2213,13 +2260,14 @@ async fn reset_prefix_cache_returns_true_when_all_engines_succeed() { handshake_address.clone(), b"engine-0".to_vec(), "reset_prefix_cache", + Value::Array(vec![Value::from(false), Value::from(false)]), true, ); - tokio::time::sleep(Duration::from_millis(50)).await; let (shutdown_tx_1, engine_task_1) = spawn_mock_utility_engine( handshake_address.clone(), b"engine-1".to_vec(), "reset_prefix_cache", + Value::Array(vec![Value::from(false), Value::from(false)]), true, ); @@ -2255,13 +2303,14 @@ async fn reset_prefix_cache_returns_false_when_any_engine_fails() { handshake_address.clone(), b"engine-0".to_vec(), "reset_prefix_cache", + Value::Array(vec![Value::from(false), Value::from(false)]), true, ); - tokio::time::sleep(Duration::from_millis(50)).await; let (shutdown_tx_1, engine_task_1) = spawn_mock_utility_engine( handshake_address.clone(), b"engine-1".to_vec(), "reset_prefix_cache", + Value::Array(vec![Value::from(false), Value::from(false)]), false, );