1use std::collections::HashMap;
2use std::path::{Path, PathBuf};
3use std::sync::Arc;
4
5use infinity_agent_core::ThreadId;
6use infinity_agent_core::message::InputMessage;
7use infinity_agent_core::system::AgentSystemBuilder;
8use infinity_agent_core::system::local::{
9 ChannelSender, SubscribeHandle, ThreadLifecycleEvent, ThreadLifecycleState,
10};
11use infinity_agent_core::traits::{ConversationStore, InputSender, StateStore};
12use infinity_protocol::{DaemonMessage, ModelRef, SessionInfo};
13use tokio::sync::mpsc;
14
15use crate::config;
16use crate::ids::IdSource;
17use crate::memory_store::{PersistentConversationStore, PersistentStateStore};
18use crate::models::{self, CatalogModelSource, ModelCatalog};
19use crate::rap_servers::SessionRapManager;
20use crate::session_store;
21
22pub mod display;
23pub mod observer;
24#[cfg(test)]
25mod tests;
26
27pub use observer::{
28 DaemonObserver, SubscribeRequest, Subscriber, SubscriberMap, ThreadSubscribers,
29};
30
31type BoxError = Box<dyn std::error::Error + Send + Sync>;
32
33pub type ActiveThreadSet = Arc<std::sync::Mutex<std::collections::HashSet<ThreadId>>>;
38
39pub type SessionStoreHandle = Arc<tokio::sync::Mutex<session_store::SessionStore>>;
40
41pub type SharedSessionManager = std::rc::Rc<tokio::sync::Mutex<SessionManager>>;
47
48pub struct SessionManagerConfig {
52 pub state_dir: PathBuf,
55 pub callback_url: String,
57 pub user_rap_config: Option<PathBuf>,
61 pub id_source: Arc<dyn IdSource>,
67}
68
69pub struct SessionManager {
78 pub session_store: SessionStoreHandle,
79 conversation_store: PersistentConversationStore,
80 state_store: PersistentStateStore,
81 pub catalog: Arc<ModelCatalog>,
83 id_source: Arc<dyn IdSource>,
86 _provider_processes: Vec<tokio::process::Child>,
89 broadcast_clients: Arc<std::sync::Mutex<Vec<mpsc::UnboundedSender<DaemonMessage>>>>,
91 pub remote_daemons: Option<crate::remote::RemoteDaemons>,
93
94 sender: ChannelSender,
97 subscribe: SubscribeHandle<SubscribeRequest>,
99 active_threads: ActiveThreadSet,
102 pub subscriber_map: SubscriberMap,
104 pub rap_manager: SessionRapManager,
106 idle_eval_tx: mpsc::UnboundedSender<ThreadId>,
110}
111
112impl SessionManager {
113 pub async fn new(state_dir: PathBuf, callback_url: String) -> Result<Self, BoxError> {
117 let providers_config = models::load_providers_config(&config::providers_config_path()?)?;
122 let (providers, provider_processes) =
123 models::spawn_configured_providers(&providers_config).await?;
124 Self::with_providers(
125 SessionManagerConfig {
126 state_dir,
127 callback_url,
128 user_rap_config: Some(config::user_config_path()?),
129 id_source: Arc::new(crate::ids::UuidIdSource),
130 },
131 providers,
132 provider_processes,
133 )
134 .await
135 }
136
137 pub async fn with_providers(
143 config: SessionManagerConfig,
144 providers: Vec<(String, Arc<dyn infinity_provider_protocol::ModelProvider>)>,
145 provider_processes: Vec<tokio::process::Child>,
146 ) -> Result<Self, BoxError> {
147 let SessionManagerConfig {
148 state_dir,
149 callback_url,
150 user_rap_config,
151 id_source,
152 } = config;
153 let catalog = Arc::new(ModelCatalog::new(providers).await?);
154
155 std::fs::create_dir_all(&state_dir).ok();
156 let sessions_path = state_dir.join("sessions.json");
157 let (change_tx, mut change_rx) = mpsc::unbounded_channel::<ThreadId>();
158 let change_tx_for_conv = change_tx.clone();
159 let session_store = Arc::new(tokio::sync::Mutex::new(session_store::SessionStore::load(
160 &sessions_path.to_string_lossy(),
161 change_tx,
162 )));
163 let broadcast_clients: Arc<std::sync::Mutex<Vec<mpsc::UnboundedSender<DaemonMessage>>>> =
164 Arc::new(std::sync::Mutex::new(Vec::new()));
165
166 let threads_dir = state_dir.join("threads");
167 std::fs::create_dir_all(&threads_dir).ok();
168 let mut conversation_store = PersistentConversationStore::new_with_dir(
169 &threads_dir,
170 catalog.default_ref().clone(),
171 id_source.clone(),
172 );
173 conversation_store.set_change_tx(change_tx_for_conv);
174 let state_store = PersistentStateStore::new(
175 state_dir.join("state"),
176 conversation_store.clone(),
177 session_store.clone(),
178 );
179 let subscription_state = state_store.clone();
180 let state_store_for_updates = state_store.clone();
181
182 let bc = broadcast_clients.clone();
184 let ss = session_store.clone();
185 let cs = conversation_store.clone();
186 tokio::task::spawn_local(rap_protocol::log_panic(
187 "session_change_broadcaster",
188 async move {
189 while let Some(session_id) = change_rx.recv().await {
190 let store = ss.lock().await;
191 let info = match store.sessions.get(&session_id) {
192 Some(e) => {
193 let threads = cs.get_open_subthreads(&session_id);
194 SessionInfo {
195 title: cs.get_thread_title(&session_id),
196 last_updated: cs.get_last_updated(&session_id),
197 total_tokens_used: cs.get_total_tokens_used(&session_id),
198 status: e.status(
199 state_store_for_updates
200 .has_pending_choices_for_session(&session_id),
201 ),
202 threads,
203 remote: None,
204 }
205 }
206 None => continue,
207 };
208 drop(store);
209 let mut sessions = HashMap::new();
210 sessions.insert(
211 infinity_protocol::ThreadRef::local(session_id.to_owned()),
212 info,
213 );
214 let msg = DaemonMessage::SessionsUpdated { sessions };
215 bc.lock()
216 .expect("bug: mutex poisoned")
217 .retain(|tx| tx.send(msg.clone()).is_ok());
218 }
219 },
220 ));
221
222 let subscriber_map: SubscriberMap = Default::default();
224 let rap_manager = SessionRapManager::new(
225 conversation_store.clone(),
226 session_store.clone(),
227 user_rap_config,
228 subscriber_map.clone(),
229 );
230
231 let state_store_for_system = state_store.clone();
232 let system = AgentSystemBuilder::new_local(
233 conversation_store.clone(),
234 state_store_for_system,
235 CatalogModelSource {
236 catalog: catalog.clone(),
237 conversation_store: conversation_store.clone(),
238 },
239 )
240 .callback_url(callback_url.clone())
241 .thread_config(rap_manager.clone())
242 .with_tokio_sleep_tools()
243 .build_local();
244
245 let make_observer = {
249 let subscriber_map = subscriber_map.clone();
250 let conversation_store = conversation_store.clone();
251 move |thread_id: &ThreadId<str>| {
252 let parent_subs = {
253 let parent_id = conversation_store.get_thread_parent_id(thread_id);
254 let smap = subscriber_map.lock().expect("bug: mutex poisoned");
255 let source = parent_id.as_deref().unwrap_or(thread_id);
256 smap.get(source)
257 .map(|arc| arc.lock().expect("bug: mutex poisoned").clone())
258 .unwrap_or_default()
259 };
260 let subscribers = subscriber_map
261 .lock()
262 .expect("bug: mutex poisoned")
263 .entry(thread_id.to_owned())
264 .or_insert_with(|| Arc::new(std::sync::Mutex::new(parent_subs)))
265 .clone();
266 DaemonObserver {
267 subscribers,
268 conversation_store: conversation_store.clone(),
269 }
270 }
271 };
272 let mut running = system.start_with_observer(make_observer);
273 let sender = running.sender();
274 let subscribe = running.subscribe_handle();
275
276 let active_threads: ActiveThreadSet = Default::default();
290 let (idle_eval_tx, mut idle_eval_rx) = mpsc::unbounded_channel::<ThreadId>();
291 {
292 let conversation_store = conversation_store.clone();
293 let session_store = session_store.clone();
294 let subscriber_map = subscriber_map.clone();
295 let active_threads = active_threads.clone();
296 let rap_manager = rap_manager.clone();
297 tokio::task::spawn_local(rap_protocol::log_panic(
298 "session_activity_watcher",
299 async move {
300 loop {
301 enum Activity {
302 Lifecycle(ThreadLifecycleEvent),
303 Reevaluate(ThreadId),
304 }
305 let event = tokio::select! {
306 event = running.next_lifecycle_event() => match event {
307 Some(event) => Activity::Lifecycle(event),
308 None => break,
309 },
310 eval = idle_eval_rx.recv() => match eval {
311 Some(session_id) => Activity::Reevaluate(session_id),
314 None => break,
315 },
316 };
317 match event {
318 Activity::Lifecycle(ThreadLifecycleEvent {
319 thread_id,
320 state: ThreadLifecycleState::Live,
321 }) => {
322 let session_id = conversation_store.get_root_thread_id(&thread_id);
323 active_threads
324 .lock()
325 .expect("bug: mutex poisoned")
326 .insert(thread_id);
327 let mut store = session_store.lock().await;
328 let changed = store.clear_idle(&session_id)
329 | store.clear_shut_down(&session_id);
330 if changed && let Err(error) = store.save() {
331 tracing::warn!(
332 %session_id,
333 %error,
334 "failed to persist reactivated session",
335 );
336 }
337 }
338 Activity::Lifecycle(ThreadLifecycleEvent {
339 thread_id,
340 state: ThreadLifecycleState::Idle,
341 }) => {
342 let subscribed = subscription_state
345 .get_active_subscriptions(&thread_id)
346 .await
347 .map(|subscriptions| !subscriptions.is_empty())
348 .unwrap_or(false);
349 if !subscribed {
350 active_threads
351 .lock()
352 .expect("bug: mutex poisoned")
353 .remove(&thread_id);
354 }
355 let session_id = conversation_store.get_root_thread_id(&thread_id);
356 evaluate_session_idle(
357 &session_id,
358 &conversation_store,
359 &session_store,
360 &subscriber_map,
361 &active_threads,
362 &rap_manager,
363 )
364 .await;
365 }
366 Activity::Reevaluate(session_id) => {
367 evaluate_session_idle(
368 &session_id,
369 &conversation_store,
370 &session_store,
371 &subscriber_map,
372 &active_threads,
373 &rap_manager,
374 )
375 .await;
376 }
377 }
378 }
379 },
380 ));
381 }
382
383 Ok(Self {
384 session_store,
385 conversation_store,
386 state_store,
387 catalog,
388 id_source,
389 _provider_processes: provider_processes,
390 broadcast_clients,
391 remote_daemons: None,
392 sender,
393 subscribe,
394 active_threads,
395 subscriber_map,
396 rap_manager,
397 idle_eval_tx,
398 })
399 }
400
401 pub fn init_remotes(&mut self, configs: Vec<crate::remote::RemoteConfig>) {
403 if configs.is_empty() {
404 return;
405 }
406 self.remote_daemons = Some(crate::remote::RemoteDaemons::new(
407 configs,
408 self.broadcast_clients.clone(),
409 ));
410 }
411
412 pub fn conversation_store(&self) -> &PersistentConversationStore {
413 &self.conversation_store
414 }
415
416 pub fn state_store(&self) -> &PersistentStateStore {
417 &self.state_store
418 }
419
420 pub fn broadcast(&self, msg: DaemonMessage) {
422 self.broadcast_clients
423 .lock()
424 .expect("bug: mutex poisoned")
425 .retain(|tx| tx.send(msg.clone()).is_ok());
426 }
427
428 pub fn handle_view_update(
430 &self,
431 group_id: &ThreadId<str>,
432 view_type: &str,
433 content: serde_json::Value,
434 ) {
435 self.conversation_store
436 .set_view(group_id, view_type, content.clone());
437
438 let msg = DaemonMessage::ViewUpdate {
439 thread_id: Some(infinity_protocol::ThreadRef::local(group_id.to_owned())),
440 view_type: view_type.to_owned(),
441 content,
442 };
443 observer::broadcast_to_thread(&self.subscriber_map, group_id, &msg, None);
444 }
445
446 pub async fn create_session(
450 &self,
451 cwd: &Path,
452 model: ModelRef,
453 emit: &mut impl AsyncFnMut(DaemonMessage),
454 ) -> Result<ThreadId, BoxError> {
455 let session_id = self.id_source.generate();
456 {
457 let mut store = self.session_store.lock().await;
458 store.create(&session_id, cwd.to_path_buf());
459 let _ = store.save();
460 }
461 self.conversation_store
465 .ensure_root_thread(&session_id)
466 .await
467 .map_err(|e| format!("failed to ensure root thread: {e}"))?;
468 self.conversation_store.set_thread_model(&session_id, model);
470 self.conversation_store
471 .set_last_updated(&session_id, &chrono::Utc::now().to_rfc3339());
472 emit(self.build_connected(&session_id, &session_id)).await;
473 Ok(session_id)
476 }
477
478 pub async fn resume_session(
482 &self,
483 session_id: &ThreadId<str>,
484 thread_id: &ThreadId<str>,
485 emit: &mut impl AsyncFnMut(DaemonMessage),
486 ) -> Result<(), BoxError> {
487 emit(self.build_connected(session_id, thread_id)).await;
488 Ok(())
489 }
490
491 fn build_connected(
492 &self,
493 session_id: &ThreadId<str>,
494 thread_id: &ThreadId<str>,
495 ) -> DaemonMessage {
496 let selected = self.conversation_store.get_thread_model(thread_id);
499 let (model_ref, entry, _) = self.catalog.resolve(&selected);
500 DaemonMessage::Connected {
501 root_thread_id: infinity_protocol::ThreadRef::local(session_id.to_owned()),
502 thread_id: infinity_protocol::ThreadRef::local(thread_id.to_owned()),
503 model_name: entry.display_name.clone(),
504 context_window: entry.context_window,
505 title: self.conversation_store.get_thread_title(session_id),
506 total_tokens_used: self.conversation_store.get_total_tokens_used(session_id),
507 provider_id: model_ref.provider_id,
508 }
509 }
510
511 pub async fn attach_client(
520 &self,
521 thread_id: &ThreadId<str>,
522 tx: mpsc::UnboundedSender<DaemonMessage>,
523 wants_replay: bool,
524 keeps_session_alive: bool,
525 ) {
526 let installed = self
527 .subscribe
528 .subscribe(
529 thread_id,
530 SubscribeRequest {
531 tx: tx.clone(),
532 wants_replay,
533 keeps_session_alive,
534 },
535 )
536 .await;
537 if !installed {
538 tracing::warn!("failed to attach client to thread {thread_id}");
540 }
541 }
542
543 pub async fn send_input(&self, msg: (InputMessage, Option<String>)) -> bool {
550 let (input, dedup) = msg;
551 let dedup_id = dedup.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
552 self.sender
553 .send_to_input_queue(input, &dedup_id)
554 .await
555 .is_ok()
556 }
557
558 pub fn input_sender(&self) -> ChannelSender {
562 self.sender.clone()
563 }
564
565 pub fn switch_model(
581 &self,
582 thread_id: &ThreadId<str>,
583 model: ModelRef,
584 requester: Option<&mpsc::UnboundedSender<DaemonMessage>>,
585 ) -> Result<(), String> {
586 let Some(entry) = self.catalog.find(&model) else {
587 return Err(format!(
588 "unknown model {}/{}",
589 model.provider_id, model.model_id
590 ));
591 };
592 if !self.conversation_store.has_thread(thread_id) {
593 return Err(format!("thread {thread_id} not found"));
594 }
595
596 self.conversation_store
599 .set_thread_model(thread_id, model.clone());
600
601 let msg = DaemonMessage::ModelSwitched {
602 thread_id: infinity_protocol::ThreadRef::local(thread_id.to_owned()),
603 model_name: entry.display_name.clone(),
604 context_window: entry.context_window,
605 provider_id: model.provider_id.clone(),
606 };
607
608 let requester_reached =
612 observer::broadcast_to_thread(&self.subscriber_map, thread_id, &msg, requester);
613 if !requester_reached && let Some(requester) = requester {
614 let _ = requester.send(msg);
615 }
616 Ok(())
617 }
618
619 pub async fn list_sessions(
621 &self,
622 subscribe: Option<mpsc::UnboundedSender<DaemonMessage>>,
623 ) -> HashMap<infinity_protocol::ThreadRef, SessionInfo> {
624 if let Some(tx) = subscribe {
625 self.broadcast_clients
626 .lock()
627 .expect("bug: mutex poisoned")
628 .push(tx);
629 }
630
631 let store = self.session_store.lock().await;
632 let mut result: HashMap<infinity_protocol::ThreadRef, SessionInfo> = HashMap::new();
633
634 for (id, entry) in &store.sessions {
635 let threads = self.conversation_store.get_open_subthreads(id);
636 result.insert(
637 infinity_protocol::ThreadRef::local(id.to_owned()),
638 SessionInfo {
639 title: self.conversation_store.get_thread_title(id),
640 last_updated: self.conversation_store.get_last_updated(id),
641 total_tokens_used: self.conversation_store.get_total_tokens_used(id),
642 status: entry.status(self.state_store.has_pending_choices_for_session(id)),
643 threads,
644 remote: None,
645 },
646 );
647 }
648
649 if let Some(ref rd) = self.remote_daemons {
651 result.extend(rd.all_remote_sessions());
652 }
653
654 result
655 }
656
657 #[tracing::instrument(skip(self))]
666 pub async fn cleanup_session(&self, session_id: &ThreadId<str>) {
667 if let Err(error) = self
668 .state_store
669 .clear_pending_choices_for_session(session_id)
670 .await
671 {
672 tracing::error!(%error, "failed to clear pending choices for {session_id}");
673 }
674 self.rap_manager.evict_session(session_id).await;
675 let mut store = self.session_store.lock().await;
676 if store.sessions.contains_key(session_id) {
677 store.mark_shut_down(session_id);
678 let _ = store.save();
679 tracing::info!("Cleanup complete");
680 } else {
681 tracing::warn!("Session not found");
682 }
683 }
684
685 pub fn is_session_idle(&self, session_id: &ThreadId<str>) -> bool {
688 !session_has_active_threads(&self.active_threads, &self.conversation_store, session_id)
689 }
690
691 pub fn send_idle_ping(&self, session_id: &ThreadId<str>) {
695 let _ = self.idle_eval_tx.send(session_id.to_owned());
696 }
697}
698
699fn session_has_active_threads(
704 active_threads: &ActiveThreadSet,
705 conversation_store: &PersistentConversationStore,
706 session_id: &ThreadId<str>,
707) -> bool {
708 active_threads
709 .lock()
710 .expect("bug: mutex poisoned")
711 .iter()
712 .any(|thread_id| conversation_store.get_root_thread_id(thread_id) == *session_id)
713}
714
715async fn evaluate_session_idle(
725 session_id: &ThreadId<str>,
726 conversation_store: &PersistentConversationStore,
727 session_store: &SessionStoreHandle,
728 subscriber_map: &SubscriberMap,
729 active_threads: &ActiveThreadSet,
730 rap_manager: &SessionRapManager,
731) {
732 if session_has_active_threads(active_threads, conversation_store, session_id) {
733 return;
734 }
735
736 {
738 let mut store = session_store.lock().await;
739 if !store.sessions.contains_key(session_id) {
740 return;
741 }
742 store.mark_idle(session_id);
743 let _ = store.save();
744 }
745
746 let has_clients = {
749 let smap = subscriber_map.lock().expect("bug: mutex poisoned");
750 smap.iter()
751 .filter(|(thread_id, _)| {
752 conversation_store.get_root_thread_id(thread_id) == *session_id
753 })
754 .any(|(_, subs)| {
755 subs.lock()
756 .expect("bug: mutex poisoned")
757 .iter()
758 .any(|sub| !sub.tx.is_closed() && sub.keeps_session_alive)
759 })
760 };
761 if !has_clients {
762 tracing::info!(
763 "Session {session_id} is idle with no keep-alive clients; stopping its RAP servers"
764 );
765 rap_manager.shutdown_session(session_id).await;
766 } else {
767 tracing::info!("Session {session_id} is idle but a client is still connected");
768 }
769}