use rap_protocol::ThreadId;
use std::collections::HashSet;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use tokio::sync::{mpsc, oneshot};
use crate::message::{InputMessage, InputMessageContent, SyntheticKind, TaggedSyntheticKind};
use crate::traits::{ConversationStore, StateStore};
use infinity_provider_protocol::message::UserContent;
use rap_client::http::HttpClient;
use super::sender::ChannelSender;
use crate::system::builder::SystemInner;
use crate::system::defer::InMemoryDeferQueue;
use crate::system::events::AgentEvent;
use crate::system::observer::ThreadObserver;
use crate::system::thread::{StepOutcome, Thread, is_user_text_input};
type BoxError = Box<dyn std::error::Error + Send + Sync>;
type StepFuture<'a> = Pin<Box<dyn Future<Output = Result<StepOutcome, BoxError>> + 'a>>;
struct InFlightStep<'a> {
fut: StepFuture<'a>,
cancel_tx: oneshot::Sender<()>,
}
impl InFlightStep<'_> {
async fn cancel(self) -> Result<StepOutcome, BoxError> {
let _ = self.cancel_tx.send(());
self.fut.await
}
}
pub type ActiveThreads = Arc<Mutex<HashSet<ThreadId>>>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThreadLifecycleEvent {
pub thread_id: ThreadId,
pub state: ThreadLifecycleState,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ThreadLifecycleState {
Live,
Idle,
}
fn is_compaction_complete(msg: &InputMessage) -> bool {
msg.synthetic
.as_ref()
.is_some_and(SyntheticKind::is_compaction_complete)
}
pub(crate) async fn drive_thread<C, S, H, O>(
inner: Rc<SystemInner<C, S, ChannelSender, H>>,
thread_id: ThreadId,
mut rx: mpsc::UnboundedReceiver<(InputMessage, String)>,
mut subscribe_rx: mpsc::UnboundedReceiver<(O::SubscribeRequest, oneshot::Sender<()>)>,
observer: O,
active_threads: ActiveThreads,
lifecycle_tx: mpsc::UnboundedSender<ThreadLifecycleEvent>,
) where
C: ConversationStore + 'static,
S: StateStore + 'static,
H: HttpClient + 'static,
O: ThreadObserver + 'static,
{
active_threads
.lock()
.expect("bug: mutex poisoned")
.insert(thread_id.to_owned());
let _ = lifecycle_tx.send(ThreadLifecycleEvent {
thread_id: thread_id.clone(),
state: ThreadLifecycleState::Live,
});
let _guard = DriverGuard {
thread_id: thread_id.clone(),
active_threads,
lifecycle_tx,
};
let thread = match Thread::load(inner, thread_id.clone()).await {
Ok(t) => t,
Err(e) => {
observer.on_event(
&thread_id,
&AgentEvent::Info {
text: format!("Error: {e}"),
},
);
return;
}
};
let mut defer = InMemoryDeferQueue::new();
let mut pending_batch: Vec<(InputMessage, String)> = Vec::new();
let mut compaction_triggered = false;
let mut total_tokens: u64 = 0;
let mut subscribe_closed = false;
let mut in_flight: Option<InFlightStep<'_>> = None;
loop {
let new_inputs: Vec<(InputMessage, String)> = if let Some(flight) = in_flight.as_mut() {
tokio::select! {
biased;
res = &mut flight.fut => {
in_flight = None;
handle_step_result(
res,
&observer,
&thread_id,
&mut total_tokens,
&mut compaction_triggered,
&mut pending_batch,
);
continue;
},
first = rx.recv() => {
let Some(first) = first else {
let _ = in_flight
.take()
.expect("bug: in-flight step missing during shutdown")
.cancel()
.await;
return;
};
let mut batch = vec![first];
while let Ok(item) = rx.try_recv() {
batch.push(item);
}
if batch.iter().any(|(msg, _)| is_user_text_input(msg)) {
let res = in_flight
.take()
.expect("bug: in-flight step missing during interrupt")
.cancel()
.await;
handle_step_result(
res,
&observer,
&thread_id,
&mut total_tokens,
&mut compaction_triggered,
&mut pending_batch,
);
let (mut user_inputs, non_user_inputs): (Vec<_>, Vec<_>) = batch
.into_iter()
.partition(|(msg, _)| is_user_text_input(msg));
if let InputMessageContent::User(UserContent::Text(text)) =
&mut user_inputs[0].0.content
{
text.text = format!("<interrupt>{}", text.text);
} else {
panic!("bug: user_inputs should only contain user text");
}
pending_batch.extend(non_user_inputs);
user_inputs
} else {
pending_batch.extend(batch);
continue;
}
},
req = subscribe_rx.recv(), if !subscribe_closed => {
match req {
Some((req, ack)) => {
observer.on_subscribe(&thread_id, req, thread.replay_snapshot());
let _ = ack.send(());
}
None => subscribe_closed = true,
}
continue;
}
}
} else {
let mut batch: Vec<(InputMessage, String)> = std::mem::take(&mut pending_batch);
while let Ok(item) = rx.try_recv() {
batch.push(item);
}
let defer_ready = !defer.is_empty() && thread.pending_active_tool_call().is_none();
if batch.is_empty() && !defer_ready {
match check_before_idle(
&mut batch,
&mut rx,
&mut subscribe_rx,
&observer,
&thread,
&thread_id,
) {
IdleDecision::Continue => {
}
IdleDecision::Exit => {
tracing::info!("Thread {} going idle", thread_id);
return;
}
IdleDecision::AwaitInput => {
loop {
tokio::select! {
biased;
msg = rx.recv() => {
match msg {
Some(m) => {
batch.push(m);
break;
}
None => return,
}
}
req = subscribe_rx.recv(), if !subscribe_closed => {
match req {
Some((req, ack)) => {
observer.on_subscribe(&thread_id, req, thread.replay_snapshot());
let _ = ack.send(());
}
None => subscribe_closed = true,
}
}
}
}
while let Ok(item) = rx.try_recv() {
batch.push(item);
}
}
}
}
batch
};
for (msg, _) in &new_inputs {
if is_compaction_complete(msg) {
compaction_triggered = false;
total_tokens = 0;
}
}
let batch = match thread.filter_deferrable(new_inputs, &mut defer).await {
Ok(batch) => batch,
Err(e) => {
observer.on_event(
&thread_id,
&AgentEvent::Info {
text: format!("Error: {e}"),
},
);
continue;
}
};
if batch.is_empty() {
continue;
}
let (cancel_tx, cancel_rx) = oneshot::channel::<()>();
in_flight = Some(InFlightStep {
fut: Box::pin(thread.step_no_defer(batch, &observer, cancel_rx)),
cancel_tx,
});
}
}
enum IdleDecision {
Continue,
Exit,
AwaitInput,
}
fn check_before_idle<C, S, H, O>(
batch: &mut Vec<(InputMessage, String)>,
rx: &mut mpsc::UnboundedReceiver<(InputMessage, String)>,
subscribe_rx: &mut mpsc::UnboundedReceiver<(O::SubscribeRequest, oneshot::Sender<()>)>,
observer: &O,
thread: &Thread<C, S, ChannelSender, H>,
thread_id: &ThreadId<str>,
) -> IdleDecision
where
C: ConversationStore + 'static,
S: StateStore + 'static,
H: HttpClient + 'static,
O: ThreadObserver,
{
while let Ok((request, ack)) = subscribe_rx.try_recv() {
observer.on_subscribe(thread_id, request, thread.replay_snapshot());
let _ = ack.send(());
}
if let Ok(item) = rx.try_recv() {
batch.push(item);
while let Ok(item) = rx.try_recv() {
batch.push(item);
}
IdleDecision::Continue
} else if thread.awaiting_tool_result() {
IdleDecision::AwaitInput
} else {
IdleDecision::Exit
}
}
fn handle_step_result(
res: Result<StepOutcome, BoxError>,
observer: &impl ThreadObserver,
thread_id: &ThreadId<str>,
total_tokens: &mut u64,
compaction_triggered: &mut bool,
pending_batch: &mut Vec<(InputMessage, String)>,
) {
match res {
Ok(StepOutcome::Completed {
usage,
context_window,
}) => {
if let Some(u) = usage {
*total_tokens = u.total_tokens;
}
if !*compaction_triggered
&& context_window > 0
&& *total_tokens as usize > context_window * 3 / 4
{
*compaction_triggered = true;
tracing::info!(
"Auto-compaction for thread {}: {} total tokens > 75% of {} context window",
thread_id,
total_tokens,
context_window
);
observer.on_event(
thread_id,
&AgentEvent::Info {
text: "✦ Auto-compaction triggered (context > 75%)".to_owned(),
},
);
pending_batch.push((
InputMessage {
content: InputMessageContent::User(UserContent::text("")),
group_id: thread_id.to_owned(),
metadata: None,
synthetic: Some(SyntheticKind::Tagged(TaggedSyntheticKind::Compaction)),
display_as: None,
subscription: false,
},
uuid::Uuid::new_v4().to_string(),
));
}
}
Ok(StepOutcome::Skipped) => {}
Err(e) => {
observer.on_event(
thread_id,
&AgentEvent::Info {
text: format!("Error: {e}"),
},
);
}
}
}
struct DriverGuard {
thread_id: ThreadId,
active_threads: ActiveThreads,
lifecycle_tx: mpsc::UnboundedSender<ThreadLifecycleEvent>,
}
impl Drop for DriverGuard {
fn drop(&mut self) {
self.active_threads
.lock()
.expect("bug: mutex poisoned")
.remove(&self.thread_id);
let _ = self.lifecycle_tx.send(ThreadLifecycleEvent {
thread_id: self.thread_id.clone(),
state: ThreadLifecycleState::Idle,
});
}
}
#[cfg(test)]
mod tests {
use crate::system::events::AgentEvent;
use crate::system::test_support::*;
#[tokio::test(flavor = "current_thread")]
async fn subscribed_thread_idles_and_event_respawns_it() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut rx, mut ctrl, _conv) =
start_system(vec![Box::new(SubscribeTool)], None);
running
.send_user_text(rap_protocol::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("subscribed");
ctrl.finish();
collect_until_finished(&mut rx).await;
wait_idle(&mut running).await;
assert!(
running.is_idle(),
"an active subscription must not keep the driver resident"
);
running
.send(
subscription_event_input("t1", "tc-sub", "late event").0,
"ev-late",
)
.await;
let child_req = ctrl.next_request().await;
assert!(
tool_result_texts(&child_req)
.iter()
.any(|t| t.contains("late event")),
"the event must reach a respawned driver"
);
ctrl.send_text("handled");
ctrl.finish();
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn auto_compaction_triggers_and_applies() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut running, mut rx, mut ctrl, _conv) =
start_system(vec![], Some(small_context_entry()));
running
.send_user_text(rap_protocol::ThreadId::from_ref("t1"), "hello")
.await;
let _req = ctrl.next_request().await;
ctrl.send_text("hi there");
ctrl.finish_with_usage(high_usage());
collect_until_finished(&mut rx).await;
let creq = ctrl.next_request().await;
assert!(is_compaction_req(&creq));
handle_compaction_child(&mut ctrl, &creq, "summary of everything");
loop {
if let Evt::E(AgentEvent::CompactionApplied) = next_evt(&mut rx).await {
break;
}
}
wait_idle(&mut running).await;
})
.await;
}
}