use async_trait::async_trait;
use infinity_provider_protocol::StreamChunk;
use infinity_provider_protocol::message::UserContent;
use rap_protocol::ThreadId;
use tokio::sync::mpsc;
use super::events::AgentEvent;
use super::local::{ChannelSender, ThreadLifecycleEvent, ThreadLifecycleState};
use super::test_support::*;
use crate::message::{
InputMessage, InputMessageContent, SyntheticKind, TaggedSyntheticKind, UserChoiceRequired,
};
use crate::tools::{Tool, ToolContext};
#[tokio::test(flavor = "current_thread")]
async fn pending_choices_are_thread_local_and_replayed() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
use super::{AgentSystemBuilder, EventCollector, NoDeferral};
use crate::stores::{InMemoryConversationStore, InMemoryStateStore};
use crate::traits::{ConversationStore, StateStore};
let (model, _ctrl) = model_source(None);
let conv = InMemoryConversationStore::new();
conv.ensure_root_thread(ThreadId::from_ref("t1"))
.await
.expect("ensure root");
conv.ensure_root_thread(ThreadId::from_ref("t2"))
.await
.expect("ensure root");
let state = InMemoryStateStore::new();
let sender = ChannelSender::new_pair().0;
let mut system = AgentSystemBuilder::new(conv, state.clone(), model, sender).build();
let choice_input = |thread_id: &str, choice_id: &str| {
(
InputMessage {
content: InputMessageContent::UserChoice(UserChoiceRequired {
content_type: "user_choice_required".to_owned(),
id: choice_id.into(),
call_id: None,
prompt: "Pick one".to_owned(),
choices: vec!["A".to_owned(), "B".to_owned()],
default: 0,
response_url: "http://example.test/choice".to_owned(),
}),
group_id: thread_id.into(),
metadata: None,
synthetic: None,
display_as: None,
subscription: false,
},
format!("message-{choice_id}"),
)
};
let collector = EventCollector::new();
system
.step(
vec![
choice_input("t1", "choice-1"),
choice_input("t2", "choice-2"),
],
&collector,
&mut NoDeferral,
)
.await
.expect("persist choices");
assert_eq!(
state
.get_pending_user_choices(ThreadId::from_ref("t1"))
.await
.expect("load first thread choices")[0]
.id
.as_str(),
"choice-1"
);
assert_eq!(
state
.get_pending_user_choices(ThreadId::from_ref("t2"))
.await
.expect("load second thread choices")[0]
.id
.as_str(),
"choice-2"
);
let (sub_tx, mut sub_rx) = mpsc::unbounded_channel();
let replay_conv = InMemoryConversationStore::new();
replay_conv
.ensure_root_thread(ThreadId::from_ref("t1"))
.await
.expect("ensure replay root");
let running = AgentSystemBuilder::new_local(replay_conv, state, model_source(None).0)
.start_with_observer(|_| TestObserver {
tx: mpsc::unbounded_channel().0,
});
running.subscribe(ThreadId::from_ref("t1"), sub_tx).await;
let Evt::Replay(snapshot) = next_evt(&mut sub_rx).await else {
panic!("expected replay");
};
assert_eq!(snapshot.pending_choices.len(), 1);
assert_eq!(snapshot.pending_choices[0].id.as_str(), "choice-1");
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn driver_idles_after_text_response() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("t1"), "hello")
.await;
let _req = ctrl.next_request().await;
ctrl.send_text("hi there");
ctrl.finish();
let texts = collect_until_finished(&mut rx).await;
assert_eq!(texts, vec!["hi there"]);
wait_idle(&mut running).await;
assert!(running.is_idle());
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn driver_stays_alive_waiting_for_tool_result() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut rx, mut ctrl, _conv) =
start_system(vec![Box::new(AsyncTool)], None);
running
.send_user_text(ThreadId::from_ref("t1"), "use tool")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
ctrl.finish();
collect_until_finished(&mut rx).await;
assert!(
!matches!(
running.try_next_lifecycle_event(),
Ok(ThreadLifecycleEvent {
state: ThreadLifecycleState::Idle,
..
})
),
"should not idle while a tool call is pending"
);
assert!(!running.is_idle());
let (sub_tx, mut sub_rx) = mpsc::unbounded_channel();
running.subscribe(ThreadId::from_ref("t1"), sub_tx).await;
match next_evt(&mut sub_rx).await {
Evt::Replay(snapshot) => {
assert!(!snapshot.in_progress);
assert!(matches!(
snapshot.history.last(),
Some(crate::message::InfinityMessage::ToolCall { call, .. })
if call.function.name == "async_tool"
));
}
other => panic!("expected replay, got {other:?}"),
}
running
.send(tool_result_input("t1", "tc-1", "tool done").0, "res-1")
.await;
let _req2 = ctrl.next_request().await;
ctrl.send_text("ok");
ctrl.finish();
collect_until_finished(&mut rx).await;
wait_idle(&mut running).await;
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn user_text_interrupts_active_completion() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("t1"), "first")
.await;
let _req = ctrl.next_request().await;
ctrl.send_text("partial...");
loop {
if let Evt::E(AgentEvent::TextChunk { .. }) = next_evt(&mut rx).await {
break;
}
}
running
.send_user_text(ThreadId::from_ref("t1"), "stop that")
.await;
let req2 = ctrl.next_request().await;
let has_interrupt = req2.chat_history.into_iter().any(|m| {
if let infinity_provider_protocol::message::Message::User { content } = &m
&& let Some(UserContent::Text(t)) = content.first()
{
return t.text.contains("<interrupt>");
}
false
});
assert!(has_interrupt, "interrupting input should carry the marker");
ctrl.send_text("ok stopped");
ctrl.finish();
collect_until_finished(&mut rx).await;
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn non_user_text_during_completion_does_not_interrupt() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, _conv) = start_system(vec![Box::new(AsyncTool)], None);
running
.send_user_text(ThreadId::from_ref("t1"), "do stuff")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
ctrl.finish();
collect_until_finished(&mut rx).await;
running
.send(tool_result_input("t1", "tc-1", "tool output").0, "res-1")
.await;
let _req2 = ctrl.next_request().await;
ctrl.send_text("processing...");
loop {
if let Evt::E(AgentEvent::TextChunk { .. }) = next_evt(&mut rx).await {
break;
}
}
running
.send(
tool_result_input("t1", "tc-other", "stale event").0,
"res-stale",
)
.await;
ctrl.send_text(" done");
ctrl.finish();
let texts = collect_until_finished(&mut rx).await;
assert!(
texts.join("").contains("done"),
"should not have been interrupted"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn subscription_event_queued_during_completion_processed_after() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, _conv) =
start_system(vec![Box::new(SubscribeTool)], None);
running
.send_user_text(ThreadId::from_ref("t1"), "subscribe")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-sub", "subscribe_tool", serde_json::json!({}));
ctrl.finish();
collect_until_finished(&mut rx).await;
let _req2 = ctrl.next_request().await;
ctrl.send_text("got subscription...");
loop {
if let Evt::E(AgentEvent::TextChunk { .. }) = next_evt(&mut rx).await {
break;
}
}
running
.send(
subscription_event_input("t1", "tc-sub", "event payload xyz").0,
"ev-1",
)
.await;
ctrl.send_text(" all good");
ctrl.finish();
let texts = collect_until_finished(&mut rx).await;
assert!(
texts.join("").contains("all good"),
"should not have been interrupted"
);
let req3 = ctrl.next_request().await;
assert!(
tool_result_texts(&req3)
.iter()
.any(|t| t.contains("event payload xyz")),
"queued event should appear in next round"
);
ctrl.send_text("processed");
ctrl.finish();
collect_until_finished(&mut rx).await;
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn driver_idles_after_close_thread_tool_call() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
struct CloseThreadStub;
#[async_trait]
impl Tool<ChannelSender> for CloseThreadStub {
fn name(&self) -> &str {
"close_thread"
}
fn description(&self) -> &str {
"close"
}
fn parameters(&self) -> serde_json::Value {
serde_json::json!({"type":"object","properties":{}})
}
async fn execute(
&self,
_: serde_json::Value,
_: rap_protocol::ToolCallId,
_: Option<rap_protocol::ProviderCallId>,
_: &ToolContext<ChannelSender>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Ok(())
}
}
let (mut running, mut rx, mut ctrl, _conv) =
start_system_with(vec![Box::new(CloseThreadStub)], None, false);
running
.send_user_text(ThreadId::from_ref("t1"), "close")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-1",
"close_thread",
serde_json::json!({"thread_id": "t1"}),
);
ctrl.finish();
collect_until_finished(&mut rx).await;
wait_idle(&mut running).await;
assert!(running.is_idle());
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn thread_report_deferred_during_async_tool_wait() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, _conv) = start_system(vec![Box::new(AsyncTool)], None);
running
.send_user_text(ThreadId::from_ref("t1"), "do async")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-async", "async_tool", serde_json::json!({}));
ctrl.finish();
collect_until_finished(&mut rx).await;
running
.send(
InputMessage {
content: InputMessageContent::User(UserContent::ToolResult(
infinity_provider_protocol::message::ToolResult {
id: String::new(),
call_id: None,
content: vec![
infinity_provider_protocol::message::ToolResultContent::Text(
infinity_provider_protocol::message::Text {
text: "Report from child thread: progress update"
.into(),
},
),
],
},
)),
group_id: "t1".into(),
metadata: None,
synthetic: Some(SyntheticKind::Tagged(TaggedSyntheticKind::ThreadReport {
tool_call_id: "tc-async".into(),
child_thread_id: "child-1".into(),
})),
display_as: None,
subscription: false,
},
"report-1",
)
.await;
for _ in 0..4 {
tokio::task::yield_now().await;
}
assert!(
ctrl.try_next_request().is_none(),
"thread report should not trigger completion while waiting for async tool"
);
running
.send(tool_result_input("t1", "tc-async", "tool done").0, "res-1")
.await;
let req2 = ctrl.next_request().await;
ctrl.send_text("all processed");
ctrl.finish();
collect_until_finished(&mut rx).await;
let texts = tool_result_texts(&req2);
assert!(texts.iter().any(|t| t.contains("tool done")));
assert!(texts.iter().any(|t| t.contains("progress update")));
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn shutdown_persists_model_validated_tool_result() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, conv) = start_system(vec![Box::new(AsyncTool)], None);
running
.send_user_text(ThreadId::from_ref("t1"), "do something")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
ctrl.finish();
collect_until_finished(&mut rx).await;
running
.send(
tool_result_input("t1", "tc-1", "tool execution result").0,
"res-1",
)
.await;
let _req2 = ctrl.next_request().await;
ctrl.send_text("partial reply");
loop {
if let Evt::E(AgentEvent::TextChunk { .. }) = next_evt(&mut rx).await {
break;
}
}
let active_threads = running.active_threads();
running.shutdown().await;
assert!(
active_threads
.lock()
.expect("bug: active_threads mutex poisoned")
.is_empty(),
"no thread drivers should remain after shutdown"
);
let stored = persisted(&conv, "t1");
assert!(
stored.contains("tool execution result"),
"validated in-flight tool result should be persisted on shutdown; history: {stored}"
);
assert!(
stored.contains("partial reply"),
"partial model output should be persisted on shutdown; history: {stored}"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn shutdown_does_not_persist_unvalidated_tool_result() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, conv) = start_system(vec![Box::new(AsyncTool)], None);
running
.send_user_text(ThreadId::from_ref("t1"), "do something")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
ctrl.finish();
collect_until_finished(&mut rx).await;
running
.send(
tool_result_input("t1", "tc-1", "tool execution result").0,
"res-1",
)
.await;
let _req2 = ctrl.next_request().await;
running.shutdown().await;
let stored = persisted(&conv, "t1");
assert!(
stored.contains("tc-1"),
"the tool call itself is model output and must be persisted; history: {stored}"
);
assert!(
!stored.contains("tool execution result"),
"an unvalidated tool result must not be persisted on shutdown; history: {stored}"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn subscription_event_deferred_during_async_tool_wait() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, _conv) = start_system(vec![Box::new(AsyncTool)], None);
running
.send_user_text(ThreadId::from_ref("t1"), "do async")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-async", "async_tool", serde_json::json!({}));
ctrl.finish();
collect_until_finished(&mut rx).await;
running
.send(
subscription_event_input("t1", "tc-async", "sub event data").0,
"sub-1",
)
.await;
for _ in 0..4 {
tokio::task::yield_now().await;
}
assert!(
ctrl.try_next_request().is_none(),
"deferred event must not trigger a completion"
);
running
.send(tool_result_input("t1", "tc-async", "tool done").0, "res-1")
.await;
let req2 = ctrl.next_request().await;
ctrl.send_text("all processed");
ctrl.finish();
collect_until_finished(&mut rx).await;
let texts = tool_result_texts(&req2);
assert!(texts.iter().any(|t| t.contains("tool done")));
assert!(texts.iter().any(|t| t.contains("sub event data")));
assert!(
!texts.iter().any(|t| t.contains("interrupted")),
"in-flight tool call must not have been interrupted: {texts:?}"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn stale_result_does_not_flush_deferred_events_during_async_tool_wait() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, _conv) = start_system(vec![Box::new(AsyncTool)], None);
running
.send_user_text(ThreadId::from_ref("t1"), "do async")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-async", "async_tool", serde_json::json!({}));
ctrl.finish();
collect_until_finished(&mut rx).await;
running
.send(
subscription_event_input("t1", "tc-async", "sub event data").0,
"sub-1",
)
.await;
running
.send(
tool_result_input("t1", "tc-stale", "stale result").0,
"res-stale",
)
.await;
for _ in 0..6 {
tokio::task::yield_now().await;
}
assert!(
ctrl.try_next_request().is_none(),
"stale result must not flush the deferred event"
);
running
.send(tool_result_input("t1", "tc-async", "tool done").0, "res-1")
.await;
let req2 = ctrl.next_request().await;
ctrl.send_text("all processed");
ctrl.finish();
collect_until_finished(&mut rx).await;
let texts = tool_result_texts(&req2);
assert!(texts.iter().any(|t| t.contains("tool done")));
assert!(texts.iter().any(|t| t.contains("sub event data")));
assert!(
!texts.iter().any(|t| t.contains("interrupted")),
"in-flight tool call must not have been interrupted: {texts:?}"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn subscribe_mid_thinking_replays_current_thinking() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("t1"), "think hard")
.await;
let _req = ctrl.next_request().await;
ctrl.send_chunk(StreamChunk::ReasoningDelta {
id: None,
text: "deep ".into(),
});
ctrl.send_chunk(StreamChunk::ReasoningDelta {
id: None,
text: "thought".into(),
});
let mut seen = String::new();
while seen != "deep thought" {
if let Evt::E(AgentEvent::ThinkingChunk { text }) = next_evt(&mut rx).await {
seen.push_str(&text);
}
}
let (sub_tx, mut sub_rx) = mpsc::unbounded_channel();
running.subscribe(ThreadId::from_ref("t1"), sub_tx).await;
match next_evt(&mut sub_rx).await {
Evt::Replay(snapshot) => {
assert!(snapshot.in_progress, "completion is in flight");
assert_eq!(snapshot.current_thinking.as_deref(), Some("deep thought"));
}
other => panic!("expected replay, got {other:?}"),
}
ctrl.send_text("the answer");
loop {
if let Evt::E(AgentEvent::TextChunk { .. }) = next_evt(&mut rx).await {
break;
}
}
let (sub_tx2, mut sub_rx2) = mpsc::unbounded_channel();
running.subscribe(ThreadId::from_ref("t1"), sub_tx2).await;
match next_evt(&mut sub_rx2).await {
Evt::Replay(snapshot) => {
assert!(snapshot.in_progress);
assert!(snapshot.current_thinking.is_none());
assert!(matches!(
snapshot.history.last(),
Some(crate::message::InfinityMessage::Assistant { .. })
));
}
other => panic!("expected replay, got {other:?}"),
}
ctrl.finish();
collect_until_finished(&mut rx).await;
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn routes_to_separate_threads() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("t1"), "one")
.await;
let _r1 = ctrl.next_request().await;
ctrl.send_text("first");
ctrl.finish();
collect_until_finished(&mut rx).await;
running
.send_user_text(ThreadId::from_ref("t2"), "two")
.await;
let _r2 = ctrl.next_request().await;
ctrl.send_text("second");
ctrl.finish();
collect_until_finished(&mut rx).await;
wait_idle(&mut running).await;
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn respawns_driver_after_idle() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("t1"), "hello")
.await;
let _req = ctrl.next_request().await;
ctrl.send_text("hi");
ctrl.finish();
collect_until_finished(&mut rx).await;
wait_idle(&mut running).await;
assert!(running.is_idle());
running
.send_user_text(ThreadId::from_ref("t1"), "again")
.await;
let req2 = ctrl.next_request().await;
assert!(req2.chat_history.into_iter().any(|m| {
if let infinity_provider_protocol::message::Message::User { content } = &m
&& let Some(UserContent::Text(t)) = content.first()
{
return t.text.contains("hello");
}
false
}));
ctrl.send_text("welcome back");
ctrl.finish();
collect_until_finished(&mut rx).await;
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn compaction_during_tool_call_corrupts_history() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut _rx, mut ctrl, _conv) =
start_system(vec![Box::new(AsyncTool)], Some(small_context_entry()));
running
.send_user_text(ThreadId::from_ref("t1"), "do something")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
ctrl.finish_with_usage(high_usage());
running
.send(
tool_result_input("t1", "tc-1", "tool execution result").0,
"res-1",
)
.await;
let req2 = ctrl.next_request().await;
if is_compaction_req(&req2) {
handle_compaction_child(&mut ctrl, &req2, "Summary of conversation so far");
let _req3 = ctrl.next_request().await;
ctrl.send_text("processed tool result");
ctrl.finish();
} else {
ctrl.send_text("processed tool result");
ctrl.finish();
let compaction_req = ctrl.next_request().await;
handle_compaction_child(&mut ctrl, &compaction_req, "Summary of conversation so far");
}
running
.send_user_text(ThreadId::from_ref("t1"), "what happened?")
.await;
let req_final =
tokio::time::timeout(std::time::Duration::from_secs(5), ctrl.next_request())
.await
.expect("timed out waiting for final model request");
let history: Vec<_> = req_final.chat_history.into_iter().collect();
let has_orphaned_tool_result = history.iter().enumerate().any(|(i, m)| {
if let infinity_provider_protocol::message::Message::User { content } = m
&& let Some(UserContent::ToolResult(r)) = content.first()
&& let Some(infinity_provider_protocol::message::ToolResultContent::Text(t)) = r.content.first()
&& t.text.contains("tool execution result")
{
!history[..i].iter().any(|prev| {
if let infinity_provider_protocol::message::Message::Assistant { content, .. } = prev {
content.iter().any(|c| {
matches!(c, infinity_provider_protocol::message::AssistantContent::ToolCall(tc) if tc.id == "tc-1")
})
} else {
false
}
})
} else {
false
}
});
assert!(
!has_orphaned_tool_result,
"History is corrupted: tool result has no matching tool_call after compaction. History: {history:#?}"
);
ctrl.send_text("all good");
ctrl.finish();
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn compaction_does_not_retrigger_after_applied() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut _rx, mut ctrl, _conv) =
start_system(vec![Box::new(AsyncTool)], Some(small_context_entry()));
running
.send_user_text(ThreadId::from_ref("t1"), "do something")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
ctrl.finish_with_usage(high_usage());
let compaction_req = ctrl.next_request().await;
assert!(is_compaction_req(&compaction_req));
handle_compaction_child(&mut ctrl, &compaction_req, "Summary of conversation so far");
running
.send(
tool_result_input("t1", "tc-1", "tool execution result").0,
"res-1",
)
.await;
let req = ctrl.next_request().await;
assert!(
!is_compaction_req(&req),
"compaction must not re-trigger after being applied"
);
ctrl.send_text("processed tool result");
ctrl.finish();
wait_idle(&mut running).await;
for _ in 0..100 {
tokio::task::yield_now().await;
}
assert!(
ctrl.try_next_request().is_none(),
"no further model requests should be made after compaction is applied"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn second_compaction_during_tool_call_after_prior_compaction() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut _rx, mut ctrl, _conv) =
start_system(vec![Box::new(AsyncTool)], Some(small_context_entry()));
running
.send_user_text(ThreadId::from_ref("t1"), "first message")
.await;
let _req1 = ctrl.next_request().await;
ctrl.send_text("first response");
ctrl.finish_with_usage(high_usage());
let compaction_req1 = ctrl.next_request().await;
assert!(is_compaction_req(&compaction_req1));
handle_compaction_child(&mut ctrl, &compaction_req1, "Summary of first round");
running
.send_user_text(ThreadId::from_ref("t1"), "second message")
.await;
let _req2 = ctrl.next_request().await;
ctrl.send_text("second response");
ctrl.finish();
running
.send_user_text(ThreadId::from_ref("t1"), "third message")
.await;
let _req3 = ctrl.next_request().await;
ctrl.send_text("third response");
ctrl.finish();
running
.send_user_text(ThreadId::from_ref("t1"), "fourth message")
.await;
let _req4 = ctrl.next_request().await;
ctrl.send_tool_call("tc-2", "async_tool", serde_json::json!({}));
ctrl.finish_with_usage(high_usage());
running
.send(
tool_result_input("t1", "tc-2", "second tool result").0,
"res-2",
)
.await;
let req_c = ctrl.next_request().await;
let compaction_child_req = if is_compaction_req(&req_c) {
let r = req_c;
handle_compaction_child(&mut ctrl, &r, "Summary of second round");
let _req_d = ctrl.next_request().await;
ctrl.send_text("processed second tool");
ctrl.finish();
r
} else {
ctrl.send_text("processed second tool");
ctrl.finish();
let req_d = ctrl.next_request().await;
handle_compaction_child(&mut ctrl, &req_d, "Summary of second round");
req_d
};
insta::assert_json_snapshot!(
"second_compaction_child_history",
compaction_child_req.chat_history,
{
"[].content[].id" => "[id]",
"[].content[].content[].text" => insta::dynamic_redaction(|value, _| {
let s = value.as_str().unwrap_or("");
if s.contains("compaction thread") {
insta::internals::Content::String("[compaction_instructions]".into())
} else {
value
}
})
}
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn compaction_inside_child_thread_does_not_panic() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut _rx, mut ctrl, _conv) =
start_system(vec![Box::new(AsyncTool)], Some(small_context_entry()));
running
.send_user_text(ThreadId::from_ref("root"), "root message one")
.await;
let _req = ctrl.next_request().await;
ctrl.send_text("root response one");
ctrl.finish();
running
.send_user_text(ThreadId::from_ref("root"), "root message two")
.await;
let _req = ctrl.next_request().await;
ctrl.send_text("root response two");
ctrl.finish();
running
.send_user_text(ThreadId::from_ref("root"), "spawn a child")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-spawn",
"spawn_thread",
serde_json::json!({
"instructions": "do child work",
"child_of": ["root"]
}),
);
ctrl.finish();
let parent_followup = ctrl.next_request().await;
let child_thread_id = tool_result_texts(&parent_followup)
.iter()
.find_map(|t| {
let after =
t.strip_prefix("Child thread is successfully spawned and has ID: ")?;
Some(after.split('.').next()?.to_owned())
})
.expect("should find child thread ID in spawn result");
ctrl.send_text("ok, child spawned");
ctrl.finish();
let _child_req = ctrl.next_request().await;
ctrl.send_text("child first response");
ctrl.finish();
running
.send_user_text(ThreadId::from_ref(&child_thread_id), "child follow-up")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call("tc-child", "async_tool", serde_json::json!({}));
ctrl.finish_with_usage(high_usage());
running
.send(
tool_result_input(&child_thread_id, "tc-child", "async tool result").0,
"res-child",
)
.await;
let req_a = ctrl.next_request().await;
let compaction_req = if is_compaction_req(&req_a) {
handle_compaction_child(&mut ctrl, &req_a, "Summary of child work");
let _req_b = ctrl.next_request().await;
ctrl.send_text("processed tool result");
ctrl.finish();
req_a
} else {
ctrl.send_text("processed tool result");
ctrl.finish();
let req_b = ctrl.next_request().await;
handle_compaction_child(&mut ctrl, &req_b, "Summary of child work");
req_b
};
insta::assert_json_snapshot!(
"issue31_compaction_child_history",
compaction_req.chat_history,
{
"[].content[].id" => "[id]",
"[].content[].content[].text" => insta::dynamic_redaction(|value, _| {
let s = value.as_str().unwrap_or("");
if s.contains("compaction thread") {
insta::internals::Content::String("[compaction_instructions]".into())
} else if s.contains("INSIDE the thread") {
insta::internals::Content::String("[spawn_instructions]".into())
} else {
value
}
})
}
);
running
.send_user_text(
ThreadId::from_ref(&child_thread_id),
"message after compaction",
)
.await;
let post_compaction_req =
tokio::time::timeout(std::time::Duration::from_secs(5), ctrl.next_request())
.await
.expect("child should respond after compaction");
insta::assert_json_snapshot!(
"issue31_post_compaction_history",
post_compaction_req.chat_history,
{
"[].content[].id" => "[id]",
}
);
ctrl.send_text("done");
ctrl.finish();
})
.await;
}
fn persisted(conv: &crate::stores::InMemoryConversationStore, thread_id: &str) -> String {
format!(
"{:?}",
conv.thread_messages(ThreadId::from_ref(thread_id))
.unwrap_or_default()
)
}
#[tokio::test(flavor = "current_thread")]
async fn interrupt_before_output_persists_nothing_until_model_output() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut rx, mut ctrl, conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("t1"), "first message")
.await;
let _req1 = ctrl.next_request().await;
running
.send_user_text(ThreadId::from_ref("t1"), "second message")
.await;
let req2 = ctrl.next_request().await;
let stored = persisted(&conv, "t1");
assert!(
!stored.contains("first message"),
"interrupted input must not be persisted before model output; store: {stored}"
);
let history = format!("{:?}", req2.chat_history);
assert!(
history.contains("first message") && history.contains("second message"),
"the retry must include both inputs; history: {history}"
);
ctrl.send_text("model reply");
ctrl.finish();
collect_until_finished(&mut rx).await; collect_until_finished(&mut rx).await; wait_idle(&mut running).await;
let stored = persisted(&conv, "t1");
assert!(
stored.contains("first message")
&& stored.contains("second message")
&& stored.contains("model reply"),
"validated inputs and output must be persisted; store: {stored}"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn oversized_tool_result_never_wedges_the_thread() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
use infinity_provider_protocol::{CompletionError, ErrorClass};
let (mut running, mut rx, mut ctrl, conv) =
start_system(vec![Box::new(AsyncTool)], None);
running
.send_user_text(ThreadId::from_ref("t1"), "use the tool")
.await;
let _req1 = ctrl.next_request().await;
ctrl.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
ctrl.finish();
collect_until_finished(&mut rx).await;
running
.send(
tool_result_input("t1", "tc-1", "HUGE TOOL RESULT").0,
"res-1",
)
.await;
let _req2 = ctrl.next_request().await;
ctrl.send_error(CompletionError::provider(
ErrorClass::ContextOverflow,
"input is too long for the model",
));
ctrl.drop_stream();
let req3 = tokio::time::timeout(std::time::Duration::from_secs(5), ctrl.next_request())
.await
.expect("core should retry with a placeholder tool result");
let history = format!("{:?}", req3.chat_history);
assert!(
!history.contains("HUGE TOOL RESULT"),
"retry must not resend the oversized result; history: {history}"
);
assert!(
history.contains(crate::event_processor::TOOL_RESULT_TOO_LARGE_PLACEHOLDER),
"retry must answer the call with the placeholder; history: {history}"
);
ctrl.send_error(CompletionError::provider(
ErrorClass::ContextOverflow,
"input is too long for the model",
));
ctrl.drop_stream();
collect_until_finished(&mut rx).await;
let stored = persisted(&conv, "t1");
assert!(
!stored.contains("HUGE TOOL RESULT"),
"the oversized result must not be persisted; store: {stored}"
);
running
.send_user_text(ThreadId::from_ref("t1"), "are you alive?")
.await;
let req4 = tokio::time::timeout(std::time::Duration::from_secs(5), ctrl.next_request())
.await
.expect("a later user input must reach the model (no permanent hang)");
let history = format!("{:?}", req4.chat_history);
assert!(
history.contains(crate::event_processor::TOOL_CALL_INTERRUPTED_TEXT),
"the call must stay answered (settled as interrupted); history: {history}"
);
assert!(
!history.contains(crate::event_processor::TOOL_RESULT_TOO_LARGE_PLACEHOLDER),
"a 'too large' note would mislead the model; history: {history}"
);
assert!(
!history.contains("HUGE TOOL RESULT"),
"the poison result must be gone from the model-facing history"
);
ctrl.send_text("alive and well");
ctrl.finish();
let texts = collect_until_finished(&mut rx).await;
assert_eq!(texts, vec!["alive and well"]);
wait_idle(&mut running).await;
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn rebooted_session_settles_persisted_unanswered_tool_call() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
use super::AgentSystemBuilder;
use crate::stores::{InMemoryConversationStore, InMemoryStateStore};
let conv = InMemoryConversationStore::new();
let state = InMemoryStateStore::new();
let (model1, mut ctrl1) = model_source(None);
let (tx1, mut rx1) = mpsc::unbounded_channel();
let running1 = AgentSystemBuilder::new_local(conv.clone(), state.clone(), model1)
.tools(vec![Box::new(AsyncTool) as Box<dyn Tool<ChannelSender>>])
.start_with_observer(move |_thread_id| TestObserver { tx: tx1.clone() });
running1
.send_user_text(ThreadId::from_ref("t1"), "do something")
.await;
let _req1 = ctrl1.next_request().await;
ctrl1.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
ctrl1.finish();
collect_until_finished(&mut rx1).await;
running1
.send(
tool_result_input("t1", "tc-1", "HUGE TOOL RESULT").0,
"res-1",
)
.await;
let _req2 = ctrl1.next_request().await;
running1.shutdown().await;
let stored = persisted(&conv, "t1");
assert!(
stored.contains("tc-1") && !stored.contains("HUGE TOOL RESULT"),
"store must end on the unanswered call; store: {stored}"
);
let (model2, mut ctrl2) = model_source(None);
let (tx2, mut rx2) = mpsc::unbounded_channel();
let mut running2 = AgentSystemBuilder::new_local(conv.clone(), state.clone(), model2)
.tools(vec![Box::new(AsyncTool) as Box<dyn Tool<ChannelSender>>])
.start_with_observer(move |_thread_id| TestObserver { tx: tx2.clone() });
running2
.send_user_text(ThreadId::from_ref("t1"), "hello again")
.await;
let req = ctrl2.next_request().await;
let history = format!("{:?}", req.chat_history);
assert!(
history.contains("Tool call interrupted by user"),
"reboot + user input must settle the stranded call; history: {history}"
);
assert!(history.contains("hello again"), "history: {history}");
ctrl2.send_text("back online");
ctrl2.finish();
let texts = collect_until_finished(&mut rx2).await;
assert_eq!(texts, vec!["back online"]);
wait_idle(&mut running2).await;
let stored = persisted(&conv, "t1");
assert!(
stored.contains("Tool call interrupted by user")
&& stored.contains("hello again")
&& stored.contains("back online"),
"recovered round must persist; store: {stored}"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn overflow_after_interrupt_drops_user_inputs_and_stops() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
use infinity_provider_protocol::{CompletionError, ErrorClass};
let (mut running, mut rx, mut ctrl, conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("t1"), "first message")
.await;
let _req1 = ctrl.next_request().await;
running
.send_user_text(ThreadId::from_ref("t1"), "second message")
.await;
let req2 = ctrl.next_request().await;
let history = format!("{:?}", req2.chat_history);
assert!(history.contains("first message") && history.contains("second message"));
ctrl.send_error(CompletionError::provider(
ErrorClass::ContextOverflow,
"input is too long for the model",
));
ctrl.drop_stream();
let mut saw_drop_info = false;
let mut finished_rounds = 0;
while finished_rounds < 2 {
match next_evt(&mut rx).await {
Evt::E(AgentEvent::Info { text }) if text.contains("too large") => {
saw_drop_info = true;
}
Evt::E(AgentEvent::CompletionFinished { .. }) => finished_rounds += 1,
_ => {}
}
}
assert!(
saw_drop_info,
"the dropped input must be surfaced to the user"
);
wait_idle(&mut running).await;
assert!(
ctrl.try_next_request().is_none(),
"dropping user input must stop the round, not retry it"
);
let stored = persisted(&conv, "t1");
assert!(
!stored.contains("first message") && !stored.contains("second message"),
"dropped inputs must not be persisted; store: {stored}"
);
running
.send_user_text(ThreadId::from_ref("t1"), "third message")
.await;
let req3 = ctrl.next_request().await;
let history = format!("{:?}", req3.chat_history);
assert!(
history.contains("third message")
&& !history.contains("first message")
&& !history.contains("second message"),
"dropped inputs must not be resent; history: {history}"
);
ctrl.send_text("hello!");
ctrl.finish();
let texts = collect_until_finished(&mut rx).await;
assert_eq!(texts, vec!["hello!"]);
wait_idle(&mut running).await;
let stored = persisted(&conv, "t1");
assert!(
stored.contains("third message") && stored.contains("hello!"),
"the recovered round must persist; store: {stored}"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn fresh_context_spawn_has_no_parent_history() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut _rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("root"), "root secret message")
.await;
let _req = ctrl.next_request().await;
ctrl.send_text("root response");
ctrl.finish();
running
.send_user_text(ThreadId::from_ref("root"), "spawn a fresh child")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-spawn-fresh",
"spawn_thread",
serde_json::json!({
"instructions": "summarize the file docs/overview.md",
"child_of": ["root"],
"fresh_context": true
}),
);
ctrl.finish();
let parent_followup = ctrl.next_request().await;
assert!(
tool_result_texts(&parent_followup)
.iter()
.any(|t| t.contains("Child thread is successfully spawned")),
"spawn result present in parent follow-up"
);
ctrl.send_text("ok, spawned");
ctrl.finish();
let child_req = ctrl.next_request().await;
let history_json =
serde_json::to_string(&child_req.chat_history).expect("serialize history");
assert!(
!history_json.contains("root secret message"),
"fresh child must not inherit parent history: {history_json}"
);
assert!(
history_json.contains("summarize the file docs/overview.md"),
"fresh child sees its instructions: {history_json}"
);
assert!(
history_json.contains("FRESH context"),
"fresh child gets the fresh-context preamble: {history_json}"
);
ctrl.send_text("child done");
ctrl.finish();
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn nested_spawn_inherits_only_fresh_parent_context() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut _rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("t1"), "t1 private user message")
.await;
let _req = ctrl.next_request().await;
ctrl.send_text("t1 private assistant response");
ctrl.finish();
wait_idle(&mut running).await;
running
.send_user_text(ThreadId::from_ref("t1"), "spawn a fresh child")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-spawn-t2",
"spawn_thread",
serde_json::json!({
"instructions": "t2 independent task",
"child_of": ["t1"],
"fresh_context": true
}),
);
ctrl.finish();
let root_followup = ctrl.next_request().await;
let t2 = tool_result_texts(&root_followup)
.iter()
.find_map(|text| {
let after =
text.strip_prefix("Child thread is successfully spawned and has ID: ")?;
Some(after.split('.').next()?.to_owned())
})
.expect("should find t2 ID in spawn result");
ctrl.send_text("t1 spawned t2");
ctrl.finish();
let _child_req = ctrl.next_request().await;
ctrl.send_text("t2 findings to share with descendants");
ctrl.finish();
wait_idle(&mut running).await;
running
.send_user_text(
ThreadId::from_ref(&t2),
"t2 follow-up: delegate the next step",
)
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-spawn-t3",
"spawn_thread",
serde_json::json!({
"instructions": "t3 continue using t2 findings",
"child_of": ["t1", t2],
"fresh_context": false
}),
);
ctrl.finish();
let _child_followup = ctrl.next_request().await;
ctrl.send_text("t2 spawned t3");
ctrl.finish();
let grandchild_req = ctrl.next_request().await;
let history =
serde_json::to_string(&grandchild_req.chat_history).expect("serialize t3 history");
assert!(
!history.contains("t1 private user message")
&& !history.contains("t1 private assistant response"),
"t3 must not inherit t1 context across the fresh boundary: {history}"
);
assert!(
history.contains("t2 independent task")
&& history.contains("t2 findings to share with descendants")
&& history.contains("t2 follow-up: delegate the next step"),
"t3 must inherit t2 instructions and conversation: {history}"
);
assert!(
tool_result_texts(&grandchild_req)
.iter()
.any(|text| text.contains("t3 continue using t2 findings")),
"t3 must receive its own spawn instructions: {history}"
);
ctrl.send_text("t3 done");
ctrl.finish();
wait_idle(&mut running).await;
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn parent_message_after_fatal_error_still_reaches_sleeping_child() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut _rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("root"), "spawn a child")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-spawn",
"spawn_thread",
serde_json::json!({
"instructions": "wait for my messages",
"child_of": ["root"]
}),
);
ctrl.finish();
let parent_followup = ctrl.next_request().await;
let child_thread_id = tool_result_texts(&parent_followup)
.iter()
.find_map(|t| {
let after =
t.strip_prefix("Child thread is successfully spawned and has ID: ")?;
Some(after.split('.').next()?.to_owned())
})
.expect("should find child thread ID in spawn result");
ctrl.send_text("ok, child spawned");
ctrl.finish();
let child_req = ctrl.next_request().await;
assert!(
format!("{:?}", child_req.chat_history).contains("INSIDE the thread"),
"expected the child's seed round"
);
ctrl.send_tool_call(
"tc-sleep",
"sleep_until_event_or_input",
serde_json::json!({}),
);
ctrl.finish();
running
.send_user_text(ThreadId::from_ref("root"), "message the child")
.await;
let _parent_req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-msg1",
"send_message_to_child",
serde_json::json!({
"thread_id": child_thread_id,
"message": "hello child"
}),
);
ctrl.finish();
for _ in 0..2 {
let req = ctrl.next_request().await;
let history = format!("{:?}", req.chat_history);
if history.contains("Message from parent thread: hello child") {
ctrl.send_error(infinity_provider_protocol::CompletionError::provider(
infinity_provider_protocol::ErrorClass::Fatal,
"auth expired",
));
ctrl.drop_stream();
} else {
ctrl.send_text("message sent");
ctrl.finish();
}
}
wait_idle(&mut running).await;
running
.send_user_text(ThreadId::from_ref("root"), "message the child again")
.await;
let _parent_req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-msg2",
"send_message_to_child",
serde_json::json!({
"thread_id": child_thread_id,
"message": "hello again"
}),
);
ctrl.finish();
let mut child_got_message = false;
for _ in 0..2 {
let req =
tokio::time::timeout(std::time::Duration::from_secs(5), ctrl.next_request())
.await
.expect("timed out waiting for a model round after provider recovery");
let history = format!("{:?}", req.chat_history);
if history.contains("Message from parent thread: hello again") {
child_got_message = true;
ctrl.send_text("got it");
ctrl.finish();
} else {
ctrl.send_text("message sent");
ctrl.finish();
}
}
assert!(
child_got_message,
"the child must receive parent messages after the provider recovers"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
#[should_panic(expected = "the child must receive parent messages after the provider recovers")]
async fn parent_message_after_fatal_error_during_active_tool_call() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut _rx, mut ctrl, _conv) =
start_system(vec![Box::new(AsyncTool)], None);
running
.send_user_text(ThreadId::from_ref("root"), "spawn a child")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-spawn",
"spawn_thread",
serde_json::json!({
"instructions": "work then wait",
"child_of": ["root"]
}),
);
ctrl.finish();
let parent_followup = ctrl.next_request().await;
let child_thread_id = tool_result_texts(&parent_followup)
.iter()
.find_map(|t| {
let after =
t.strip_prefix("Child thread is successfully spawned and has ID: ")?;
Some(after.split('.').next()?.to_owned())
})
.expect("should find child thread ID in spawn result");
ctrl.send_text("ok, child spawned");
ctrl.finish();
let _child_req = ctrl.next_request().await;
ctrl.send_tool_call("tc-work", "async_tool", serde_json::json!({}));
ctrl.finish();
running
.send_user_text(ThreadId::from_ref("root"), "message the child")
.await;
let _parent_req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-msg1",
"send_message_to_child",
serde_json::json!({
"thread_id": child_thread_id,
"message": "hello child"
}),
);
ctrl.finish();
let parent_req = ctrl.next_request().await;
assert!(
format!("{:?}", parent_req.chat_history).contains("Message sent to child thread"),
"expected the parent's follow-up round"
);
ctrl.send_text("message sent");
ctrl.finish();
running
.send(
tool_result_input(&child_thread_id, "tc-work", "work finished").0,
"res-work",
)
.await;
let child_req = ctrl.next_request().await;
let history = format!("{:?}", child_req.chat_history);
assert!(
history.contains("work finished")
&& history.contains("Message from parent thread: hello child"),
"the child round must include the tool result and the drained parent message; got {history}"
);
ctrl.send_error(infinity_provider_protocol::CompletionError::provider(
infinity_provider_protocol::ErrorClass::Fatal,
"auth expired",
));
ctrl.drop_stream();
wait_idle(&mut running).await;
running
.send_user_text(ThreadId::from_ref("root"), "message the child again")
.await;
let _parent_req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-msg2",
"send_message_to_child",
serde_json::json!({
"thread_id": child_thread_id,
"message": "hello again"
}),
);
ctrl.finish();
let mut child_got_message = false;
for _ in 0..2 {
let Ok(req) = tokio::time::timeout(
std::time::Duration::from_secs(5),
ctrl.next_request(),
)
.await
else {
break;
};
let history = format!("{:?}", req.chat_history);
if history.contains("Message from parent thread: hello again") {
assert!(
history.contains("work finished")
&& history.contains("Message from parent thread: hello child"),
"inputs consumed during the fatal error must be retried, not lost; got {history}"
);
child_got_message = true;
ctrl.send_text("got it");
ctrl.finish();
} else {
ctrl.send_text("message sent");
ctrl.finish();
}
}
assert!(
child_got_message,
"the child must receive parent messages after the provider recovers"
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn child_close_report_wakes_sleeping_parent_after_compaction() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("root"), "spawn a child")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-spawn",
"spawn_thread",
serde_json::json!({
"instructions": "work, then close when asked",
"child_of": ["root"]
}),
);
ctrl.finish();
let parent_followup = ctrl.next_request().await;
let child_thread_id = tool_result_texts(&parent_followup)
.iter()
.find_map(|t| {
let after =
t.strip_prefix("Child thread is successfully spawned and has ID: ")?;
Some(after.split('.').next()?.to_owned())
})
.expect("should find child thread ID in spawn result");
ctrl.send_tool_call(
"tc-sleep",
"sleep_until_event_or_input",
serde_json::json!({}),
);
ctrl.finish();
let child_req = ctrl.next_request().await;
assert!(
format!("{:?}", child_req.chat_history).contains("INSIDE the thread"),
"expected the child's seed round"
);
ctrl.send_text("child ready");
ctrl.finish();
running
.send(
InputMessage {
content: InputMessageContent::User(UserContent::text("")),
group_id: "root".into(),
metadata: None,
synthetic: Some(SyntheticKind::Tagged(TaggedSyntheticKind::Compaction)),
display_as: None,
subscription: false,
},
"compact-1",
)
.await;
let creq = ctrl.next_request().await;
assert!(is_compaction_req(&creq), "expected the compaction child");
handle_compaction_child(&mut ctrl, &creq, "parent spawned a child and went to sleep");
loop {
if let Evt::E(AgentEvent::CompactionApplied) = next_evt(&mut rx).await {
break;
}
}
running
.send_user_text(ThreadId::from_ref(&child_thread_id), "close now")
.await;
let _child_req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-close",
"close_thread",
serde_json::json!({
"thread_id": child_thread_id,
"report_to_parent": "child finished its work",
}),
);
ctrl.finish();
let parent_req =
tokio::time::timeout(std::time::Duration::from_secs(5), ctrl.next_request())
.await
.expect("the sleeping parent must be woken by the child's close report");
let history = format!("{:?}", parent_req.chat_history);
assert!(
history.contains("child finished its work"),
"the parent's wake-up round must include the child's report; got {history}"
);
assert!(
history.contains("[Compacted conversation summary]"),
"the parent's history must be the compacted view; got {history}"
);
ctrl.send_text("resumed");
ctrl.finish();
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn parent_message_reaches_child_after_child_compaction() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (running, mut rx, mut ctrl, _conv) = start_system(vec![], None);
running
.send_user_text(ThreadId::from_ref("root"), "spawn a child")
.await;
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-spawn",
"spawn_thread",
serde_json::json!({
"instructions": "wait for my messages",
"child_of": ["root"]
}),
);
ctrl.finish();
let parent_followup = ctrl.next_request().await;
let child_thread_id = tool_result_texts(&parent_followup)
.iter()
.find_map(|t| {
let after =
t.strip_prefix("Child thread is successfully spawned and has ID: ")?;
Some(after.split('.').next()?.to_owned())
})
.expect("should find child thread ID in spawn result");
ctrl.send_text("ok, child spawned");
ctrl.finish();
let _child_req = ctrl.next_request().await;
ctrl.send_text("child ready");
ctrl.finish();
running
.send(
InputMessage {
content: InputMessageContent::User(UserContent::text("")),
group_id: child_thread_id.as_str().into(),
metadata: None,
synthetic: Some(SyntheticKind::Tagged(TaggedSyntheticKind::Compaction)),
display_as: None,
subscription: false,
},
"compact-child-1",
)
.await;
let creq = ctrl.next_request().await;
assert!(is_compaction_req(&creq), "expected the compaction child");
handle_compaction_child(&mut ctrl, &creq, "child was spawned and is waiting");
loop {
if let Evt::E(AgentEvent::CompactionApplied) = next_evt(&mut rx).await {
break;
}
}
running
.send_user_text(ThreadId::from_ref("root"), "message the child")
.await;
let _parent_req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-msg",
"send_message_to_child",
serde_json::json!({
"thread_id": child_thread_id,
"message": "hello child"
}),
);
ctrl.finish();
let mut child_got_message = false;
for _ in 0..2 {
let req =
tokio::time::timeout(std::time::Duration::from_secs(5), ctrl.next_request())
.await
.expect("the compacted child must receive the parent's message");
let history = format!("{:?}", req.chat_history);
if history.contains("Message from parent thread: hello child") {
assert!(
history.contains("[Compacted conversation summary]"),
"the child's history must be the compacted view; got {history}"
);
child_got_message = true;
ctrl.send_text("got it");
ctrl.finish();
} else {
ctrl.send_text("message sent");
ctrl.finish();
}
}
assert!(
child_got_message,
"the parent's message must reach the child after the child compacts"
);
})
.await;
}