Skip to main content

infinity_daemon/session/
mod.rs

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
33/// The daemon's view of active threads: a thread is active while its driver
34/// is live, and stays active after the driver idles for as long as it holds
35/// active subscriptions (its RAP servers must stay up to deliver the
36/// events). Maintained by the session activity watcher.
37pub type ActiveThreadSet = Arc<std::sync::Mutex<std::collections::HashSet<ThreadId>>>;
38
39pub type SessionStoreHandle = Arc<tokio::sync::Mutex<session_store::SessionStore>>;
40
41/// Shared handle to the [`SessionManager`]. `Rc` rather than `Arc`: the
42/// manager (and the whole agent system) lives on one tokio [`LocalSet`]
43/// thread, and it holds `!Send` per-session toolsets.
44///
45/// [`LocalSet`]: tokio::task::LocalSet
46pub type SharedSessionManager = std::rc::Rc<tokio::sync::Mutex<SessionManager>>;
47
48/// Configuration for building a [`SessionManager`]. The non-generic
49/// constructor ([`SessionManager::new`]) fills in the home-directory
50/// defaults; tests can point everything at temp dirs for hermetic runs.
51pub struct SessionManagerConfig {
52    /// Directory for persisted daemon state (`sessions.json`, thread
53    /// history, tool state).
54    pub state_dir: PathBuf,
55    /// Base URL for the RAP callback server.
56    pub callback_url: String,
57    /// Path to the user-level RAP config merged into every session
58    /// (`~/.infinity/rap.json` in production). `None` disables user-level
59    /// RAP servers, so sessions only boot servers from their cwd config.
60    pub user_rap_config: Option<PathBuf>,
61    /// Source of session/thread ids. Production uses random UUIDs
62    /// ([`crate::ids::UuidIdSource`]); tests use
63    /// [`crate::ids::SequentialIdSource`] so ids rendered in UIs are
64    /// deterministic. Shared (`Arc`) between the manager and the
65    /// conversation store so all ids come from one sequence.
66    pub id_source: Arc<dyn IdSource>,
67}
68
69/// Manages all sessions on top of a single, daemon-lifetime agent system.
70///
71/// The agent system itself never shuts down: threads idle out individually
72/// and respawn on demand, so message delivery never races a teardown. What
73/// starts and stops with session activity are the session's RAP tool servers,
74/// managed lazily by [`SessionRapManager`] — "shutting down" a session just
75/// means stopping its servers (they reboot transparently on the next tool
76/// invocation) and flagging it in the session store.
77pub struct SessionManager {
78    pub session_store: SessionStoreHandle,
79    conversation_store: PersistentConversationStore,
80    state_store: PersistentStateStore,
81    /// Registered model providers and their available models.
82    pub catalog: Arc<ModelCatalog>,
83    /// Source of new session ids (thread ids come from the conversation
84    /// store, which shares the same source).
85    id_source: Arc<dyn IdSource>,
86    /// Spawned model provider processes. Held so the providers stay alive
87    /// for the daemon's lifetime (the processes are killed on drop).
88    _provider_processes: Vec<tokio::process::Child>,
89    /// Connected clients that receive broadcast updates.
90    broadcast_clients: Arc<std::sync::Mutex<Vec<mpsc::UnboundedSender<DaemonMessage>>>>,
91    /// Remote daemon connections.
92    pub remote_daemons: Option<crate::remote::RemoteDaemons>,
93
94    // ── Handles into the always-running agent system ──
95    /// Delivers input messages to their threads.
96    sender: ChannelSender,
97    /// Attaches subscribers to threads (resolves once installed).
98    subscribe: SubscribeHandle<SubscribeRequest>,
99    /// Threads that are live or waiting on subscription events (see
100    /// [`ActiveThreadSet`]).
101    active_threads: ActiveThreadSet,
102    /// Per-thread subscriber lists for broadcasting display events.
103    pub subscriber_map: SubscriberMap,
104    /// Per-session RAP servers and toolsets (lazily booted).
105    pub rap_manager: SessionRapManager,
106    /// Requests an idle re-evaluation for a session (e.g. after a client
107    /// disconnect). Consumed by the session activity watcher alongside the
108    /// core thread lifecycle events.
109    idle_eval_tx: mpsc::UnboundedSender<ThreadId>,
110}
111
112impl SessionManager {
113    /// Build a manager with the production defaults: providers are spawned
114    /// from `~/.infinity/providers.json` and the user-level RAP config is
115    /// `~/.infinity/rap.json`.
116    pub async fn new(state_dir: PathBuf, callback_url: String) -> Result<Self, BoxError> {
117        // Spawn the model providers configured in `~/.infinity/providers.json`
118        // (each runs as a separate process serving a Unix socket) and register
119        // them. Provider ids are the config keys; the first model of the
120        // first provider is the global default.
121        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    /// Build a manager from explicit `(provider_id, provider)` pairs instead
138    /// of spawning provider processes from the on-disk config. This is the
139    /// generic constructor used by tests (e.g. with an in-process mock
140    /// provider); `provider_processes` are held for the manager's lifetime
141    /// and killed on drop.
142    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        // Task: listen for session store changes and broadcast to clients
183        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        // ── The daemon-lifetime agent system ──
223        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        // Per-thread observer factory: each new thread driver gets a
246        // subscriber list seeded with its parent thread's subscribers (so
247        // clients watching a parent automatically see its children).
248        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        // ── Session activity watcher ──
277        //
278        // The single owner of the session store's idle flag and of the
279        // daemon's view of thread activity. Every driver spawn (`Live`)
280        // marks the thread active and its session non-idle, covering user
281        // input and RAP callbacks alike. Every driver exit (`Idle`) asks the
282        // state store whether the thread still holds active subscriptions:
283        // if it does, the thread stays active (its subscription events will
284        // respawn the driver, and its RAP servers must stay up to deliver
285        // them); if not, the thread is retired and the session is evaluated
286        // for teardown. Stopping servers is always safe: they reboot lazily
287        // on the next tool invocation, so there is no teardown/wakeup race
288        // to coordinate.
289        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                                // A client disconnect pings for re-evaluation
312                                // without a driver transition.
313                                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                                // A thread waiting on subscription events is
343                                // still active even though its driver exited.
344                                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    /// Initialize remote daemon connections from config.
402    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    /// Broadcast a message to all connected clients.
421    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    /// Handle a view_update RAP callback: persist the view and broadcast to subscribers.
429    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    /// Create a brand new session with the given working directory and model.
447    /// The model is not validated here; if it is no longer available when the
448    /// agent runs, the thread worker falls back to the default model.
449    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        // Ensure the root thread metadata exists before setting last_updated,
462        // otherwise set_last_updated is a no-op and the session broadcasts with
463        // an empty timestamp (sorting it to the bottom of the session list).
464        self.conversation_store
465            .ensure_root_thread(&session_id)
466            .await
467            .map_err(|e| format!("failed to ensure root thread: {e}"))?;
468        // Persist the selected model on the root thread so restarts keep it.
469        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        // Nothing to start: the agent system is always running, and the
474        // session's RAP servers boot lazily on its first step.
475        Ok(session_id)
476    }
477
478    /// Resume a persisted session, recovering its cwd from the session store.
479    /// Does NOT boot the agent loop — that happens lazily on first user input
480    /// via `send_input`. This just emits `Connected` so the client can attach.
481    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        // Resolve the thread's own selected model (falling back to the global
497        // default if it is no longer available).
498        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    /// Attach a client's message sender to a thread for receiving display
512    /// events. The request is routed to the thread's driver (spawning one if
513    /// none is live) and resolves once the subscriber is installed — its
514    /// replay sent and its registration completed — so input sent afterwards
515    /// is guaranteed to be observed by the client.
516    ///
517    /// `keeps_session_alive`: when `false`, this subscriber will not prevent
518    /// the session from going idle (and its RAP servers being stopped).
519    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            // Only possible while the daemon itself is shutting down.
539            tracing::warn!("failed to attach client to thread {thread_id}");
540        }
541    }
542
543    /// Send an input message to the agent system.
544    ///
545    /// All input surfaces enqueue blindly. The router drops event-style input
546    /// for unknown or stopped threads, while user text may create or resume a
547    /// thread. A resulting `Live` lifecycle transition clears the root
548    /// session's idle and stopped flags, including when a child thread wakes.
549    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    /// The agent system's input sender, used by the RAP callback bridge to
559    /// forward converted callbacks directly into the input queue. The router
560    /// owns thread-existence and stopped-thread admission.
561    pub fn input_sender(&self) -> ChannelSender {
562        self.sender.clone()
563    }
564
565    /// Switch the model used for future requests on a specific thread. The
566    /// switch affects only `thread_id` — it does not propagate to child
567    /// threads.
568    ///
569    /// The selection is validated against the catalog and persisted in the
570    /// conversation store; every completion round resolves the persisted
571    /// selection when it starts (via [`CatalogModelSource`]), so the switch
572    /// takes effect on the thread's next round — an in-flight completion
573    /// finishes on the model it started with.
574    ///
575    /// The [`DaemonMessage::ModelSwitched`] confirmation is broadcast to the
576    /// thread's subscribers, and delivered to `requester` as well when the
577    /// requester is not already among those subscribers — so the requesting
578    /// client sees the confirmation exactly once whether or not it is
579    /// attached.
580    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        // Persist so future rounds (and daemon restarts) resolve the new
597        // selection.
598        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        // Broadcast the confirmation to the thread's subscribers; deliver
609        // directly to the requester only if the broadcast did not already
610        // reach it, so it sees the confirmation exactly once.
611        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    /// List all sessions — active ones plus persisted ones from the cache.
620    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        // Include remote sessions
650        if let Some(ref rd) = self.remote_daemons {
651            result.extend(rd.all_remote_sessions());
652        }
653
654        result
655    }
656
657    /// Shut down a session: stop its RAP servers, drop its cached toolset
658    /// (so a later restart re-reads the RAP config), clear pending choices,
659    /// and mark it shut down in the store.
660    ///
661    /// The agent system keeps running — a thread that is mid-completion
662    /// finishes and flushes its turn, then parks. Sending new user input
663    /// clears the flag and picks the session back up (its servers reboot
664    /// lazily); RAP callbacks arriving while shut down are ignored.
665    #[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    /// Returns true if the session has no active threads: none with a live
686    /// driver and none waiting on subscription events.
687    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    /// Request an idle re-evaluation for a session (e.g. after a client
692    /// disconnect): if none of its threads are live and no keep-alive client
693    /// remains, its RAP servers are stopped.
694    pub fn send_idle_ping(&self, session_id: &ThreadId<str>) {
695        let _ = self.idle_eval_tx.send(session_id.to_owned());
696    }
697}
698
699/// Whether any active thread belongs to `session_id` (live driver or waiting
700/// on subscription events). The single definition of "session activity",
701/// shared by [`SessionManager::is_session_idle`] and
702/// [`evaluate_session_idle`] so the two can never disagree.
703fn 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
715/// Decide whether `session_id` has gone idle and release its resources.
716///
717/// Runs on every thread-driver exit and on explicit idle pings. "Idle" means
718/// the session has no active threads: none with a live driver and none
719/// waiting on subscription events. The session is then flagged idle in the
720/// store, and, if no keep-alive client is attached, its RAP servers are
721/// stopped. Stopping is always safe (never a race): a message that arrives a
722/// moment later simply respawns a driver, and the first tool interaction
723/// boots the servers back up.
724async 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    // Mark idle in the store so listing shows Idle status.
737    {
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    // If a keep-alive client is attached to any of the session's threads,
747    // keep its servers warm.
748    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}