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(session_id.to_string(), info);
211                    let msg = DaemonMessage::SessionsUpdated { sessions };
212                    bc.lock()
213                        .expect("bug: mutex poisoned")
214                        .retain(|tx| tx.send(msg.clone()).is_ok());
215                }
216            },
217        ));
218
219        // ── The daemon-lifetime agent system ──
220        let subscriber_map: SubscriberMap = Default::default();
221        let rap_manager = SessionRapManager::new(
222            conversation_store.clone(),
223            session_store.clone(),
224            user_rap_config,
225            subscriber_map.clone(),
226        );
227
228        let state_store_for_system = state_store.clone();
229        let system = AgentSystemBuilder::new_local(
230            conversation_store.clone(),
231            state_store_for_system,
232            CatalogModelSource {
233                catalog: catalog.clone(),
234                conversation_store: conversation_store.clone(),
235            },
236        )
237        .callback_url(callback_url.clone())
238        .thread_config(rap_manager.clone())
239        .with_tokio_sleep_tools()
240        .build_local();
241
242        // Per-thread observer factory: each new thread driver gets a
243        // subscriber list seeded with its parent thread's subscribers (so
244        // clients watching a parent automatically see its children).
245        let make_observer = {
246            let subscriber_map = subscriber_map.clone();
247            let conversation_store = conversation_store.clone();
248            move |thread_id: &ThreadId<str>| {
249                let parent_subs = {
250                    let parent_id = conversation_store.get_thread_parent_id(thread_id);
251                    let smap = subscriber_map.lock().expect("bug: mutex poisoned");
252                    let source = parent_id.as_deref().unwrap_or(thread_id);
253                    smap.get(source)
254                        .map(|arc| arc.lock().expect("bug: mutex poisoned").clone())
255                        .unwrap_or_default()
256                };
257                let subscribers = subscriber_map
258                    .lock()
259                    .expect("bug: mutex poisoned")
260                    .entry(thread_id.to_owned())
261                    .or_insert_with(|| Arc::new(std::sync::Mutex::new(parent_subs)))
262                    .clone();
263                DaemonObserver {
264                    subscribers,
265                    conversation_store: conversation_store.clone(),
266                }
267            }
268        };
269        let mut running = system.start_with_observer(make_observer);
270        let sender = running.sender();
271        let subscribe = running.subscribe_handle();
272
273        // ── Session activity watcher ──
274        //
275        // The single owner of the session store's idle flag and of the
276        // daemon's view of thread activity. Every driver spawn (`Live`)
277        // marks the thread active and its session non-idle, covering user
278        // input and RAP callbacks alike. Every driver exit (`Idle`) asks the
279        // state store whether the thread still holds active subscriptions:
280        // if it does, the thread stays active (its subscription events will
281        // respawn the driver, and its RAP servers must stay up to deliver
282        // them); if not, the thread is retired and the session is evaluated
283        // for teardown. Stopping servers is always safe: they reboot lazily
284        // on the next tool invocation, so there is no teardown/wakeup race
285        // to coordinate.
286        let active_threads: ActiveThreadSet = Default::default();
287        let (idle_eval_tx, mut idle_eval_rx) = mpsc::unbounded_channel::<ThreadId>();
288        {
289            let conversation_store = conversation_store.clone();
290            let session_store = session_store.clone();
291            let subscriber_map = subscriber_map.clone();
292            let active_threads = active_threads.clone();
293            let rap_manager = rap_manager.clone();
294            tokio::task::spawn_local(rap_protocol::log_panic(
295                "session_activity_watcher",
296                async move {
297                    loop {
298                        enum Activity {
299                            Lifecycle(ThreadLifecycleEvent),
300                            Reevaluate(ThreadId),
301                        }
302                        let event = tokio::select! {
303                            event = running.next_lifecycle_event() => match event {
304                                Some(event) => Activity::Lifecycle(event),
305                                None => break,
306                            },
307                            eval = idle_eval_rx.recv() => match eval {
308                                // A client disconnect pings for re-evaluation
309                                // without a driver transition.
310                                Some(session_id) => Activity::Reevaluate(session_id),
311                                None => break,
312                            },
313                        };
314                        match event {
315                            Activity::Lifecycle(ThreadLifecycleEvent {
316                                thread_id,
317                                state: ThreadLifecycleState::Live,
318                            }) => {
319                                let session_id = conversation_store.get_root_thread_id(&thread_id);
320                                active_threads
321                                    .lock()
322                                    .expect("bug: mutex poisoned")
323                                    .insert(thread_id);
324                                let mut store = session_store.lock().await;
325                                let changed = store.clear_idle(&session_id)
326                                    | store.clear_shut_down(&session_id);
327                                if changed && let Err(error) = store.save() {
328                                    tracing::warn!(
329                                        %session_id,
330                                        %error,
331                                        "failed to persist reactivated session",
332                                    );
333                                }
334                            }
335                            Activity::Lifecycle(ThreadLifecycleEvent {
336                                thread_id,
337                                state: ThreadLifecycleState::Idle,
338                            }) => {
339                                // A thread waiting on subscription events is
340                                // still active even though its driver exited.
341                                let subscribed = subscription_state
342                                    .get_active_subscriptions(&thread_id)
343                                    .await
344                                    .map(|subscriptions| !subscriptions.is_empty())
345                                    .unwrap_or(false);
346                                if !subscribed {
347                                    active_threads
348                                        .lock()
349                                        .expect("bug: mutex poisoned")
350                                        .remove(&thread_id);
351                                }
352                                let session_id = conversation_store.get_root_thread_id(&thread_id);
353                                evaluate_session_idle(
354                                    &session_id,
355                                    &conversation_store,
356                                    &session_store,
357                                    &subscriber_map,
358                                    &active_threads,
359                                    &rap_manager,
360                                )
361                                .await;
362                            }
363                            Activity::Reevaluate(session_id) => {
364                                evaluate_session_idle(
365                                    &session_id,
366                                    &conversation_store,
367                                    &session_store,
368                                    &subscriber_map,
369                                    &active_threads,
370                                    &rap_manager,
371                                )
372                                .await;
373                            }
374                        }
375                    }
376                },
377            ));
378        }
379
380        Ok(Self {
381            session_store,
382            conversation_store,
383            state_store,
384            catalog,
385            id_source,
386            _provider_processes: provider_processes,
387            broadcast_clients,
388            remote_daemons: None,
389            sender,
390            subscribe,
391            active_threads,
392            subscriber_map,
393            rap_manager,
394            idle_eval_tx,
395        })
396    }
397
398    /// Initialize remote daemon connections from config.
399    pub fn init_remotes(&mut self, configs: Vec<crate::remote::RemoteConfig>) {
400        if configs.is_empty() {
401            return;
402        }
403        self.remote_daemons = Some(crate::remote::RemoteDaemons::new(
404            configs,
405            self.broadcast_clients.clone(),
406        ));
407    }
408
409    pub fn conversation_store(&self) -> &PersistentConversationStore {
410        &self.conversation_store
411    }
412
413    pub fn state_store(&self) -> &PersistentStateStore {
414        &self.state_store
415    }
416
417    /// Broadcast a message to all connected clients.
418    pub fn broadcast(&self, msg: DaemonMessage) {
419        self.broadcast_clients
420            .lock()
421            .expect("bug: mutex poisoned")
422            .retain(|tx| tx.send(msg.clone()).is_ok());
423    }
424
425    /// Handle a view_update RAP callback: persist the view and broadcast to subscribers.
426    pub fn handle_view_update(
427        &self,
428        group_id: &ThreadId<str>,
429        view_type: &str,
430        content: serde_json::Value,
431    ) {
432        self.conversation_store
433            .set_view(group_id, view_type, content.clone());
434
435        let msg = DaemonMessage::ViewUpdate {
436            thread_id: Some(group_id.to_string()),
437            view_type: view_type.to_owned(),
438            content,
439        };
440        observer::broadcast_to_thread(&self.subscriber_map, group_id, &msg, None);
441    }
442
443    /// Create a brand new session with the given working directory and model.
444    /// The model is not validated here; if it is no longer available when the
445    /// agent runs, the thread worker falls back to the default model.
446    pub async fn create_session(
447        &self,
448        cwd: &Path,
449        model: ModelRef,
450        emit: &mut impl AsyncFnMut(DaemonMessage),
451    ) -> Result<ThreadId, BoxError> {
452        let session_id = ThreadId::from(self.id_source.generate());
453        {
454            let mut store = self.session_store.lock().await;
455            store.create(&session_id, cwd.to_path_buf());
456            let _ = store.save();
457        }
458        // Ensure the root thread metadata exists before setting last_updated,
459        // otherwise set_last_updated is a no-op and the session broadcasts with
460        // an empty timestamp (sorting it to the bottom of the session list).
461        self.conversation_store
462            .ensure_root_thread(&session_id)
463            .await
464            .map_err(|e| format!("failed to ensure root thread: {e}"))?;
465        // Persist the selected model on the root thread so restarts keep it.
466        self.conversation_store.set_thread_model(&session_id, model);
467        self.conversation_store
468            .set_last_updated(&session_id, &chrono::Utc::now().to_rfc3339());
469        emit(self.build_connected(&session_id, &session_id)).await;
470        // Nothing to start: the agent system is always running, and the
471        // session's RAP servers boot lazily on its first step.
472        Ok(session_id)
473    }
474
475    /// Resume a persisted session, recovering its cwd from the session store.
476    /// Does NOT boot the agent loop — that happens lazily on first user input
477    /// via `send_input`. This just emits `Connected` so the client can attach.
478    pub async fn resume_session(
479        &self,
480        session_id: &ThreadId<str>,
481        thread_id: &ThreadId<str>,
482        emit: &mut impl AsyncFnMut(DaemonMessage),
483    ) -> Result<(), BoxError> {
484        emit(self.build_connected(session_id, thread_id)).await;
485        Ok(())
486    }
487
488    fn build_connected(
489        &self,
490        session_id: &ThreadId<str>,
491        thread_id: &ThreadId<str>,
492    ) -> DaemonMessage {
493        // Resolve the thread's own selected model (falling back to the global
494        // default if it is no longer available).
495        let selected = self.conversation_store.get_thread_model(thread_id);
496        let (model_ref, entry, _) = self.catalog.resolve(&selected);
497        DaemonMessage::Connected {
498            session_id: session_id.to_string(),
499            thread_id: thread_id.to_string(),
500            model_name: entry.display_name.clone(),
501            context_window: entry.context_window,
502            title: self.conversation_store.get_thread_title(session_id),
503            total_tokens_used: self.conversation_store.get_total_tokens_used(session_id),
504            provider_id: model_ref.provider_id,
505        }
506    }
507
508    /// Attach a client's message sender to a thread for receiving display
509    /// events. The request is routed to the thread's driver (spawning one if
510    /// none is live) and resolves once the subscriber is installed — its
511    /// replay sent and its registration completed — so input sent afterwards
512    /// is guaranteed to be observed by the client.
513    ///
514    /// `keeps_session_alive`: when `false`, this subscriber will not prevent
515    /// the session from going idle (and its RAP servers being stopped).
516    pub async fn attach_client(
517        &self,
518        thread_id: &ThreadId<str>,
519        tx: mpsc::UnboundedSender<DaemonMessage>,
520        wants_replay: bool,
521        keeps_session_alive: bool,
522    ) {
523        let installed = self
524            .subscribe
525            .subscribe(
526                thread_id,
527                SubscribeRequest {
528                    tx: tx.clone(),
529                    wants_replay,
530                    keeps_session_alive,
531                },
532            )
533            .await;
534        if !installed {
535            // Only possible while the daemon itself is shutting down.
536            tracing::warn!("failed to attach client to thread {thread_id}");
537        }
538    }
539
540    /// Send an input message to the agent system.
541    ///
542    /// All input surfaces enqueue blindly. The router drops event-style input
543    /// for unknown or stopped threads, while user text may create or resume a
544    /// thread. A resulting `Live` lifecycle transition clears the root
545    /// session's idle and stopped flags, including when a child thread wakes.
546    pub async fn send_input(&self, msg: (InputMessage, Option<String>)) -> bool {
547        let (input, dedup) = msg;
548        let dedup_id = dedup.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
549        self.sender
550            .send_to_input_queue(input, &dedup_id)
551            .await
552            .is_ok()
553    }
554
555    /// The agent system's input sender, used by the RAP callback bridge to
556    /// forward converted callbacks directly into the input queue. The router
557    /// owns thread-existence and stopped-thread admission.
558    pub fn input_sender(&self) -> ChannelSender {
559        self.sender.clone()
560    }
561
562    /// Switch the model used for future requests on a specific thread. The
563    /// switch affects only `thread_id` — it does not propagate to child
564    /// threads.
565    ///
566    /// The selection is validated against the catalog and persisted in the
567    /// conversation store; every completion round resolves the persisted
568    /// selection when it starts (via [`CatalogModelSource`]), so the switch
569    /// takes effect on the thread's next round — an in-flight completion
570    /// finishes on the model it started with.
571    ///
572    /// The [`DaemonMessage::ModelSwitched`] confirmation is broadcast to the
573    /// thread's subscribers, and delivered to `requester` as well when the
574    /// requester is not already among those subscribers — so the requesting
575    /// client sees the confirmation exactly once whether or not it is
576    /// attached.
577    pub fn switch_model(
578        &self,
579        thread_id: &ThreadId<str>,
580        model: ModelRef,
581        requester: Option<&mpsc::UnboundedSender<DaemonMessage>>,
582    ) -> Result<(), String> {
583        let Some(entry) = self.catalog.find(&model) else {
584            return Err(format!(
585                "unknown model {}/{}",
586                model.provider_id, model.model_id
587            ));
588        };
589        if !self.conversation_store.has_thread(thread_id) {
590            return Err(format!("thread {thread_id} not found"));
591        }
592
593        // Persist so future rounds (and daemon restarts) resolve the new
594        // selection.
595        self.conversation_store
596            .set_thread_model(thread_id, model.clone());
597
598        let msg = DaemonMessage::ModelSwitched {
599            thread_id: thread_id.to_string(),
600            model_name: entry.display_name.clone(),
601            context_window: entry.context_window,
602            provider_id: model.provider_id.clone(),
603        };
604
605        // Broadcast the confirmation to the thread's subscribers; deliver
606        // directly to the requester only if the broadcast did not already
607        // reach it, so it sees the confirmation exactly once.
608        let requester_reached =
609            observer::broadcast_to_thread(&self.subscriber_map, thread_id, &msg, requester);
610        if !requester_reached && let Some(requester) = requester {
611            let _ = requester.send(msg);
612        }
613        Ok(())
614    }
615
616    /// List all sessions — active ones plus persisted ones from the cache.
617    pub async fn list_sessions(
618        &self,
619        subscribe: Option<mpsc::UnboundedSender<DaemonMessage>>,
620    ) -> HashMap<String, SessionInfo> {
621        if let Some(tx) = subscribe {
622            self.broadcast_clients
623                .lock()
624                .expect("bug: mutex poisoned")
625                .push(tx);
626        }
627
628        let store = self.session_store.lock().await;
629        let mut result: HashMap<String, SessionInfo> = HashMap::new();
630
631        for (id, entry) in &store.sessions {
632            let threads = self.conversation_store.get_open_subthreads(id);
633            result.insert(
634                id.to_string(),
635                SessionInfo {
636                    title: self.conversation_store.get_thread_title(id),
637                    last_updated: self.conversation_store.get_last_updated(id),
638                    total_tokens_used: self.conversation_store.get_total_tokens_used(id),
639                    status: entry.status(self.state_store.has_pending_choices_for_session(id)),
640                    threads,
641                    remote: None,
642                },
643            );
644        }
645
646        // Include remote sessions
647        if let Some(ref rd) = self.remote_daemons {
648            result.extend(rd.all_remote_sessions());
649        }
650
651        result
652    }
653
654    /// Shut down a session: stop its RAP servers, drop its cached toolset
655    /// (so a later restart re-reads the RAP config), clear pending choices,
656    /// and mark it shut down in the store.
657    ///
658    /// The agent system keeps running — a thread that is mid-completion
659    /// finishes and flushes its turn, then parks. Sending new user input
660    /// clears the flag and picks the session back up (its servers reboot
661    /// lazily); RAP callbacks arriving while shut down are ignored.
662    #[tracing::instrument(skip(self))]
663    pub async fn cleanup_session(&self, session_id: &ThreadId<str>) {
664        if let Err(error) = self
665            .state_store
666            .clear_pending_choices_for_session(session_id)
667            .await
668        {
669            tracing::error!(%error, "failed to clear pending choices for {session_id}");
670        }
671        self.rap_manager.evict_session(session_id).await;
672        let mut store = self.session_store.lock().await;
673        if store.sessions.contains_key(session_id) {
674            store.mark_shut_down(session_id);
675            let _ = store.save();
676            tracing::info!("Cleanup complete");
677        } else {
678            tracing::warn!("Session not found");
679        }
680    }
681
682    /// Returns true if the session has no active threads: none with a live
683    /// driver and none waiting on subscription events.
684    pub fn is_session_idle(&self, session_id: &ThreadId<str>) -> bool {
685        !session_has_active_threads(&self.active_threads, &self.conversation_store, session_id)
686    }
687
688    /// Request an idle re-evaluation for a session (e.g. after a client
689    /// disconnect): if none of its threads are live and no keep-alive client
690    /// remains, its RAP servers are stopped.
691    pub fn send_idle_ping(&self, session_id: &ThreadId<str>) {
692        let _ = self.idle_eval_tx.send(session_id.to_owned());
693    }
694}
695
696/// Whether any active thread belongs to `session_id` (live driver or waiting
697/// on subscription events). The single definition of "session activity",
698/// shared by [`SessionManager::is_session_idle`] and
699/// [`evaluate_session_idle`] so the two can never disagree.
700fn session_has_active_threads(
701    active_threads: &ActiveThreadSet,
702    conversation_store: &PersistentConversationStore,
703    session_id: &ThreadId<str>,
704) -> bool {
705    active_threads
706        .lock()
707        .expect("bug: mutex poisoned")
708        .iter()
709        .any(|thread_id| conversation_store.get_root_thread_id(thread_id) == *session_id)
710}
711
712/// Decide whether `session_id` has gone idle and release its resources.
713///
714/// Runs on every thread-driver exit and on explicit idle pings. "Idle" means
715/// the session has no active threads: none with a live driver and none
716/// waiting on subscription events. The session is then flagged idle in the
717/// store, and, if no keep-alive client is attached, its RAP servers are
718/// stopped. Stopping is always safe (never a race): a message that arrives a
719/// moment later simply respawns a driver, and the first tool interaction
720/// boots the servers back up.
721async fn evaluate_session_idle(
722    session_id: &ThreadId<str>,
723    conversation_store: &PersistentConversationStore,
724    session_store: &SessionStoreHandle,
725    subscriber_map: &SubscriberMap,
726    active_threads: &ActiveThreadSet,
727    rap_manager: &SessionRapManager,
728) {
729    if session_has_active_threads(active_threads, conversation_store, session_id) {
730        return;
731    }
732
733    // Mark idle in the store so listing shows Idle status.
734    {
735        let mut store = session_store.lock().await;
736        if !store.sessions.contains_key(session_id) {
737            return;
738        }
739        store.mark_idle(session_id);
740        let _ = store.save();
741    }
742
743    // If a keep-alive client is attached to any of the session's threads,
744    // keep its servers warm.
745    let has_clients = {
746        let smap = subscriber_map.lock().expect("bug: mutex poisoned");
747        smap.iter()
748            .filter(|(thread_id, _)| {
749                conversation_store.get_root_thread_id(thread_id) == *session_id
750            })
751            .any(|(_, subs)| {
752                subs.lock()
753                    .expect("bug: mutex poisoned")
754                    .iter()
755                    .any(|sub| !sub.tx.is_closed() && sub.keeps_session_alive)
756            })
757    };
758    if !has_clients {
759        tracing::info!(
760            "Session {session_id} is idle with no keep-alive clients; stopping its RAP servers"
761        );
762        rap_manager.shutdown_session(session_id).await;
763    } else {
764        tracing::info!("Session {session_id} is idle but a client is still connected");
765    }
766}