Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(agent): assert worker overlap structurally, not by substring

M3gA-Mind23baecc5979b
diff --git a/tests/agent_harness_e2e.rs b/tests/agent_harness_e2e.rs
index 098f8cb659..82ccb274ad 100644
--- a/tests/agent_harness_e2e.rs
+++ b/tests/agent_harness_e2e.rs
@@ -163,6 +163,101 @@ fn error_completion(status: u16, message: &str) -> Value {
json!({ "status": status, "error": message })
}
+// ─── Fan-out overlap barrier ────────────────────────────────────────────────
+//
+// Only `parallel_subagent_fanout` arms this; every other test leaves it empty
+// and the handler's fast path is a single `is_empty()` check.
+//
+// The problem it solves: "both workers eventually issued a request" is
+// satisfied by strictly serial execution, so a deadline-based assertion cannot
+// tell a fan-out from a fast sequence. This barrier makes overlap the only way
+// through — each armed worker's response is withheld until a *second* armed
+// worker has also arrived. Serial execution parks on the first one until the
+// wait expires and never sets [`CANARY_OVERLAP`].
+
+/// Canary substrings whose worker requests must overlap. Empty = disarmed.
+static CANARY_BARRIER: OnceLock<Mutex<Vec<String>>> = OnceLock::new();
+/// Worker requests currently parked at the barrier.
+static CANARY_IN_FLIGHT: OnceLock<Mutex<std::collections::HashSet<String>>> = OnceLock::new();
+/// Set once two armed workers were parked simultaneously.
+static CANARY_OVERLAP: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+
+/// How long a parked worker waits for a peer before giving up. Generous — it is
+/// only ever reached when the property under test is already violated, so it
+/// costs nothing on a passing run and bounds a failing one.
+const CANARY_BARRIER_WAIT: Duration = Duration::from_secs(20);
+
+fn canary_barrier() -> &'static Mutex<Vec<String>> {
+ CANARY_BARRIER.get_or_init(|| Mutex::new(Vec::new()))
+}
+
+fn canary_in_flight() -> &'static Mutex<std::collections::HashSet<String>> {
+ CANARY_IN_FLIGHT.get_or_init(|| Mutex::new(std::collections::HashSet::new()))
+}
+
+fn lock_or_recover<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
+ match m.lock() {
+ Ok(g) => g,
+ Err(p) => p.into_inner(),
+ }
+}
+
+/// Arms the barrier for `canaries` and clears any previous state.
+fn arm_canary_barrier(canaries: &[&str]) {
+ *lock_or_recover(canary_barrier()) = canaries.iter().map(|c| (*c).to_string()).collect();
+ lock_or_recover(canary_in_flight()).clear();
+ CANARY_OVERLAP.store(false, std::sync::atomic::Ordering::SeqCst);
+}
+
+fn disarm_canary_barrier() {
+ lock_or_recover(canary_barrier()).clear();
+ lock_or_recover(canary_in_flight()).clear();
+}
+
+fn canary_overlap_observed() -> bool {
+ CANARY_OVERLAP.load(std::sync::atomic::Ordering::SeqCst)
+}
+
+/// Which armed canary this request is a **worker's own** request for, if any.
+///
+/// Structural, not a substring search over the serialized body, and that
+/// distinction is the whole point. Every captured request carries its full
+/// conversation history, so after the orchestrator's spawn turn its *own*
+/// follow-up request also contains both canary strings — inside the prior
+/// assistant message's `tool_calls`. Matching anywhere in the body would let
+/// that follow-up stand in for a worker that never ran.
+///
+/// A worker's own request ends with the `user` message carrying its prompt.
+/// The orchestrator's follow-up ends with a `tool` result. So: last message,
+/// `role == "user"`, content contains the canary.
+fn canary_worker_request(body: &Value) -> Option<String> {
+ let last = body.get("messages").and_then(Value::as_array)?.last()?;
+ if last.get("role").and_then(Value::as_str)? != "user" {
+ return None;
+ }
+ let content = last.get("content").and_then(Value::as_str)?;
+ lock_or_recover(canary_barrier())
+ .iter()
+ .find(|canary| content.contains(canary.as_str()))
+ .cloned()
+}
+
+/// Parks an armed worker until a second one joins it, or the wait expires.
+async fn hold_for_canary_peer(canary: String) {
+ {
+ let mut in_flight = lock_or_recover(canary_in_flight());
+ in_flight.insert(canary.clone());
+ if in_flight.len() >= 2 {
+ CANARY_OVERLAP.store(true, std::sync::atomic::Ordering::SeqCst);
+ }
+ }
+ let deadline = std::time::Instant::now() + CANARY_BARRIER_WAIT;
+ while !canary_overlap_observed() && std::time::Instant::now() < deadline {
+ tokio::time::sleep(Duration::from_millis(20)).await;
+ }
+ lock_or_recover(canary_in_flight()).remove(&canary);
+}
+
async fn scripted_chat_completions(
uri: Uri,
_headers: HeaderMap,
@@ -180,6 +275,15 @@ async fn scripted_chat_completions(
}))
});
+ // Park an armed fan-out worker before it is answered, so a peer has a
+ // chance to arrive. Before the queue pop, not after: holding the popped
+ // entry would serialize the FIFO itself and deadlock the peer.
+ if !lock_or_recover(canary_barrier()).is_empty() {
+ if let Some(canary) = canary_worker_request(&body) {
+ hold_for_canary_peer(canary).await;
+ }
+ }
+
let next = with_scripted(|q| q.pop_front());
let Some(entry) = next else {
let message = json!({ "role": "assistant", "content": "default scripted completion" });
@@ -1970,6 +2074,10 @@ fn parallel_subagent_fanout() {
async fn parallel_subagent_fanout_inner() {
let _lock = env_lock();
+ // Arm the overlap barrier before the stack boots: each worker's response is
+ // withheld until a second worker has also arrived, so a serial
+ // implementation parks and never reaches `canary_overlap_observed`.
+ arm_canary_barrier(&["PARALLEL_ALPHA_CANARY", "PARALLEL_BETA_CANARY"]);
// ONE assistant message carrying TWO spawns — the "issued together" shape.
// Both children are single-turn (text only, no inner tool loop), so the
// remaining entries are: the orchestrator's own reply plus one completion
@@ -2016,57 +2124,52 @@ async fn parallel_subagent_fanout_inner() {
// The children are detached, so the turn can end before they have issued
// their upstream calls. Poll rather than assert immediately — a bare
// assertion here would be a race, and a sleep would be a guess.
- let both_dispatched = |reqs: &[Value]| {
- let all = serde_json::to_string(reqs).unwrap_or_default();
- all.contains("Find PARALLEL_ALPHA_CANARY") && all.contains("Find PARALLEL_BETA_CANARY")
+ let worker_canaries = |reqs: &[Value]| -> std::collections::HashSet<String> {
+ reqs.iter()
+ .filter_map(|r| canary_worker_request(r.get("body")?))
+ .collect()
};
let deadline = std::time::Instant::now() + Duration::from_secs(60);
loop {
- if with_captured(|c| both_dispatched(c)) {
+ if with_captured(|c| worker_canaries(c).len()) >= 2 {
break;
}
assert!(
std::time::Instant::now() < deadline,
- "both workers must be dispatched; captured requests: {}",
+ "both workers must issue their own request; captured: {}",
serde_json::to_string_pretty(&with_captured(|c| c.clone())).unwrap_or_default()
);
tokio::time::sleep(Duration::from_millis(100)).await;
}
let requests = with_captured(|c| c.clone());
+ let found = worker_canaries(&requests);
+ disarm_canary_barrier();
+
+ // Each worker issued its OWN request — identified by request shape, not by
+ // a substring hit anywhere in the serialized body. Every captured request
+ // carries its full history, so after the spawn turn the orchestrator's own
+ // follow-up also contains both canary strings inside the prior assistant
+ // message's `tool_calls`; matching on those would let this pass with one
+ // worker dropped on the floor.
+ for canary in ["PARALLEL_ALPHA_CANARY", "PARALLEL_BETA_CANARY"] {
+ assert!(
+ found.contains(canary),
+ "worker `{canary}` never issued its own request (found: {found:?}); \
+ requests: {}",
+ serde_json::to_string_pretty(&requests).unwrap_or_default()
+ );
+ }
- // Both prompts reached the upstream, so both workers really ran — not one
- // worker twice, and not one worker while the other was dropped on the
- // floor. This is the claim that justifies retiring `spawn_parallel_agents`.
- let alpha = requests
- .iter()
- .filter(|r| {
- serde_json::to_string(r)
- .unwrap_or_default()
- .contains("Find PARALLEL_ALPHA_CANARY")
- })
- .count();
- let beta = requests
- .iter()
- .filter(|r| {
- serde_json::to_string(r)
- .unwrap_or_default()
- .contains("Find PARALLEL_BETA_CANARY")
- })
- .count();
- assert!(
- alpha >= 1 && beta >= 1,
- "each worker must be dispatched at least once (alpha={alpha}, beta={beta}); \
- requests: {}",
- serde_json::to_string_pretty(&requests).unwrap_or_default()
- );
-
- // ≥3 upstream calls: the orchestrator's spawn turn, its own reply, and at
- // least the two children. Fewer means a child never reached the model.
+ // ...and they were in flight at the same time. This is the assertion that
+ // makes the test about concurrency rather than eventual dispatch: the
+ // barrier only releases when a second worker joins the first, so strictly
+ // serial execution cannot reach here — it parks, times out, and leaves the
+ // flag clear. It is the claim that justified retiring `spawn_parallel_agents`.
assert!(
- requests.len() >= 3,
- "expected ≥3 upstream requests (orchestrator + 2 workers), got {};\nrequests: {}",
- requests.len(),
+ canary_overlap_observed(),
+ "the two workers never overlapped — fan-out ran serially, which is the \
+ regression #4754 measured (145-200s gaps); requests: {}",
serde_json::to_string_pretty(&requests).unwrap_or_default()
);
diff --git a/tests/orchestrator_parallel_fanout_routing.rs b/tests/orchestrator_parallel_fanout_routing.rs
index dec68767df..90a5c1bb35 100644
--- a/tests/orchestrator_parallel_fanout_routing.rs
+++ b/tests/orchestrator_parallel_fanout_routing.rs
@@ -24,9 +24,6 @@
const ORCHESTRATOR_PROMPT: &str =
include_str!("../src/openhuman/agent/registry/agents/orchestrator/prompt.md");
-const SPAWN_SUBAGENT_SRC: &str =
- include_str!("../src/openhuman/agent/orchestration/tools/spawn_subagent.rs");
-
#[test]
fn prompt_routes_fanout_to_concurrent_async_spawns() {
let prompt = ORCHESTRATOR_PROMPT.to_lowercase();
@@ -83,19 +80,23 @@ fn prompt_does_not_teach_a_retired_subagent_tool() {
}
}
-#[test]
-fn spawn_subagent_description_redirects_fanout_to_parallel() {
- // The tool description is what the model reads while picking a tool, so the
- // redirect has to live there, not only in the system prompt. Anchor on the
- // description() body to avoid matching an unrelated mention elsewhere.
- let desc_start = SPAWN_SUBAGENT_SRC
- .find("fn description(&self)")
- .expect("spawn_subagent must have a description()");
- let desc = &SPAWN_SUBAGENT_SRC[desc_start..];
- let desc_body = &desc[..desc.find("fn parameters_schema").unwrap_or(desc.len())];
- assert!(
- desc_body.contains("spawn_parallel_agents"),
- "spawn_subagent's description must redirect concurrent fan-out to \
- `spawn_parallel_agents` so the model picks the parallel tool (#4754)"
- );
-}
+// `spawn_subagent_description_redirects_fanout_to_parallel` was removed here.
+//
+// It required `spawn_subagent`'s description to redirect fan-out to
+// `spawn_parallel_agents` — a tool #5757 retired. It still passed, because the
+// description still says it, which made it the same orphan as the two above:
+// a test pinning guidance for a tool nothing can call.
+//
+// It is deleted rather than re-anchored because there is no correct name to
+// re-anchor it to. `spawn_subagent` survives in exactly one agent —
+// `trigger_reactor/agent.toml` — and that agent's tool list is
+// `spawn_subagent` alone: no `spawn_parallel_agents`, and no
+// `spawn_async_subagent` either. So the redirect is dangling for its only
+// caller, and pointing it at the new tool would leave it just as dangling.
+//
+// The underlying defect is in `src/`, not in a test: `spawn_subagent`'s
+// description sends its only caller to a tool that caller does not have.
+// Deciding what it should say instead — give `trigger_reactor` a fan-out
+// affordance, or drop the redirect — is a product call for #5757's author, so
+// it is reported rather than guessed at here. Encoding a guess as an assertion
+// is what left this file asserting against a retired tool in the first place.