Skip to main content

lash/
session.rs

1use crate::support::*;
2use lash_core::runtime::{DeliveryPolicy, QueuedWorkBatch, SlotPolicy};
3
4pub struct SessionBuilder {
5    pub(crate) core: LashCore,
6    pub(crate) session_id: String,
7    pub(crate) spec: SessionSpec,
8    pub(crate) mode: Option<ModeId>,
9    pub(crate) parent_session_id: Option<String>,
10    pub(crate) store: Option<Arc<dyn RuntimePersistence>>,
11    pub(crate) provider: Option<ProviderHandle>,
12    pub(crate) active_plugins: Vec<ActivePluginBinding>,
13    pub(crate) plugin_factories: Vec<Arc<dyn PluginFactory>>,
14    pub(crate) rlm_final_answer_format: Option<lash_rlm_types::RlmFinalAnswerFormat>,
15}
16
17impl SessionBuilder {
18    pub fn standard(mut self) -> Self {
19        self.mode = Some(ModeId::standard());
20        self
21    }
22
23    pub fn rlm(mut self) -> Self {
24        self.mode = Some(ModeId::rlm());
25        self
26    }
27
28    pub fn mode(mut self, mode: ModeId) -> Self {
29        self.mode = Some(mode);
30        self
31    }
32
33    pub fn provider(mut self, provider: ProviderHandle) -> Self {
34        self.spec = self.spec.provider_id(provider.kind());
35        self.provider = Some(provider);
36        self
37    }
38
39    pub fn session_spec(mut self, spec: SessionSpec) -> Self {
40        self.spec = spec;
41        self
42    }
43
44    pub fn parent(mut self, parent_session_id: impl Into<String>) -> Self {
45        self.parent_session_id = Some(parent_session_id.into());
46        self
47    }
48
49    /// Use a specific persistence store for this root session.
50    ///
51    /// This is the right API for a host-owned, pre-opened session database.
52    /// Managed child sessions never reuse this store; configure
53    /// `LashCoreBuilder::child_store_factory` when child sessions should also
54    /// persist.
55    pub fn store(mut self, store: Arc<dyn RuntimePersistence>) -> Self {
56        self.store = Some(store);
57        self
58    }
59
60    pub fn plugin<P: PluginBinding>(mut self, config: P::SessionConfig) -> Self {
61        self.active_plugins.push(ActivePluginBinding {
62            id: P::ID,
63            requires_turn_input: P::requires_turn_input(&config),
64        });
65        self.plugin_factories.push(P::factory(&config));
66        self
67    }
68
69    pub async fn open(self) -> Result<LashSession> {
70        let (policy, mode) = self.session_policy()?;
71        let store = self.create_store(&policy).await?;
72        let mut state = self
73            .load_or_default_state(&policy, store.as_deref())
74            .await?;
75        self.apply_rlm_session_options(&mode, &mut state)?;
76        self.open_resolved(policy, mode, state, store).await
77    }
78
79    /// Open this session with a fresh resident graph, ignoring any persisted
80    /// session graph/checkpoint state that may already exist for the same
81    /// session id.
82    ///
83    /// The next successful commit writes a full replacement graph, so normal
84    /// embedders can use this to start over without manually calling
85    /// `load_persisted_session_state` or constructing a `RuntimeSessionState`.
86    /// Use [`Self::open`] for resume and [`Self::open_with_state`] only when
87    /// restoring explicit host-owned state.
88    pub async fn open_fresh(self) -> Result<LashSession> {
89        let (policy, mode) = self.session_policy()?;
90        let store = self.create_store(&policy).await?;
91        let mut state = RuntimeSessionState {
92            session_id: self.session_id.clone(),
93            policy: policy.clone(),
94            graph_replace_required: true,
95            ..RuntimeSessionState::default()
96        };
97        self.apply_rlm_session_options(&mode, &mut state)?;
98        self.open_resolved(policy, mode, state, store).await
99    }
100
101    /// Open with an explicitly supplied runtime state.
102    ///
103    /// This is for advanced hosts that already own a complete state snapshot.
104    /// Normal embedders should use [`Self::open`] to resume according to Lash's
105    /// residency policy or [`Self::open_fresh`] to start over and replace prior
106    /// persisted state on the next commit.
107    pub async fn open_with_state(self, mut state: RuntimeSessionState) -> Result<LashSession> {
108        let (policy, mode) = self.session_policy()?;
109        let store = self.create_store(&policy).await?;
110        if state.session_id != self.session_id {
111            return Err(EmbedError::StoreSessionMismatch {
112                loaded: state.session_id,
113                requested: self.session_id,
114            });
115        }
116        let recorded_provider_id = state.policy.recorded_provider_id().to_string();
117        state.policy = policy.clone();
118        state.policy.provider_id = recorded_provider_id;
119        self.apply_rlm_session_options(&mode, &mut state)?;
120        self.open_resolved(policy, mode, state, store).await
121    }
122
123    fn session_policy(&self) -> Result<(SessionPolicy, ModeId)> {
124        let mode = self
125            .mode
126            .clone()
127            .unwrap_or_else(|| self.core.default_mode.clone());
128        if !self.core.modes.contains_key(&mode) {
129            return Err(EmbedError::ModeNotInstalled { mode });
130        }
131        let mut policy = self.spec.resolve_against(&self.core.policy);
132        policy.session_id = Some(self.session_id.clone());
133        Ok((policy, mode))
134    }
135
136    async fn load_or_default_state(
137        &self,
138        policy: &SessionPolicy,
139        store: Option<&dyn RuntimePersistence>,
140    ) -> Result<RuntimeSessionState> {
141        let state = match store {
142            Some(store) => {
143                let loaded = self.load_persisted_state_for_residency(store).await?;
144                let mut state = loaded.unwrap_or_else(|| RuntimeSessionState {
145                    session_id: self.session_id.clone(),
146                    policy: policy.clone(),
147                    ..RuntimeSessionState::default()
148                });
149                if state.session_id != self.session_id {
150                    return Err(EmbedError::StoreSessionMismatch {
151                        loaded: state.session_id,
152                        requested: self.session_id.clone(),
153                    });
154                }
155                let recorded_provider_id = state.policy.recorded_provider_id().to_string();
156                state.policy = policy.clone();
157                state.policy.provider_id = recorded_provider_id;
158                state
159            }
160            None => RuntimeSessionState {
161                session_id: self.session_id.clone(),
162                policy: policy.clone(),
163                ..RuntimeSessionState::default()
164            },
165        };
166        Ok(state)
167    }
168
169    async fn load_persisted_state_for_residency(
170        &self,
171        store: &dyn RuntimePersistence,
172    ) -> Result<Option<RuntimeSessionState>> {
173        match self.core.env.residency {
174            Residency::KeepAll => {
175                let loaded = lash_core::store::load_persisted_session_state(store)
176                    .await
177                    .map_err(|err| {
178                        SessionError::Protocol(format!("failed to load store: {err}"))
179                    })?;
180                Ok(loaded)
181            }
182            Residency::ActivePathOnly => {
183                let active =
184                    lash_core::store::load_persisted_session_state_active_path(store, None)
185                        .await
186                        .map_err(|err| {
187                            SessionError::Protocol(format!(
188                                "failed to load active-path store: {err}"
189                            ))
190                        })?;
191                if active
192                    .as_ref()
193                    .is_some_and(|state| state.session_graph.nodes.is_empty())
194                {
195                    let mut full = lash_core::store::load_persisted_session_state(store)
196                        .await
197                        .map_err(|err| {
198                            SessionError::Protocol(format!(
199                                "failed to heal active-path store from full graph: {err}"
200                            ))
201                        })?;
202                    if let Some(state) = full.as_mut() {
203                        state.graph_replace_required = true;
204                    }
205                    return Ok(full);
206                }
207                Ok(active)
208            }
209        }
210    }
211
212    fn apply_rlm_session_options(
213        &self,
214        mode: &ModeId,
215        state: &mut RuntimeSessionState,
216    ) -> Result<()> {
217        let Some(final_answer_format) = self.rlm_session_final_answer_format(mode) else {
218            return Ok(());
219        };
220        let mut extras = if state.protocol_turn_options.is_empty() {
221            lash_rlm_types::RlmCreateExtras::default()
222        } else {
223            state.protocol_turn_options.decode()?
224        };
225        extras.final_answer_format = Some(final_answer_format);
226        let options = ProtocolTurnOptions::typed(extras)?;
227        state.protocol_turn_options = options.clone();
228        for frame in &mut state.agent_frames {
229            frame.protocol_turn_options = options.clone();
230        }
231        Ok(())
232    }
233
234    fn rlm_session_final_answer_format(
235        &self,
236        mode: &ModeId,
237    ) -> Option<lash_rlm_types::RlmFinalAnswerFormat> {
238        if mode != &ModeId::rlm() {
239            return None;
240        }
241        self.rlm_final_answer_format.clone().or_else(|| {
242            if self.parent_session_id.is_none() {
243                Some(lash_rlm_types::RlmFinalAnswerFormat::Markdown)
244            } else {
245                Some(lash_rlm_types::RlmFinalAnswerFormat::RawSubmitValue)
246            }
247        })
248    }
249
250    async fn open_resolved(
251        self,
252        policy: SessionPolicy,
253        mode: ModeId,
254        state: RuntimeSessionState,
255        store: Option<Arc<dyn RuntimePersistence>>,
256    ) -> Result<LashSession> {
257        let mut env = self.core.env.clone();
258        if let Some(provider) = self.provider.clone().or_else(|| self.core.provider.clone()) {
259            env.core.providers.provider_resolver =
260                Arc::new(lash_core::SingleProviderResolver::new(provider));
261        }
262        let plugin_host = build_plugin_host_for_mode(
263            &self.core.modes,
264            &mode,
265            self.core.plugin_factories.as_ref(),
266            self.plugin_factories,
267            env.process_registry.is_some(),
268        )?;
269        env.plugin_host = Some(Arc::new(plugin_host));
270        // Lazily spawn the default process work runner (Decision 3: deferred to
271        // the first open so a tokio runtime is guaranteed; idempotent via the
272        // shared once-guard) and thread its poke onto this session's host so the
273        // process control seam can wake the runner after a successful start.
274        env.process_work_poke = self.core.process_work_runner.poke().await;
275        let runtime = LashRuntime::from_environment(&env, policy, state, store).await?;
276        let handle = RuntimeHandle::new(runtime);
277        Ok(LashSession {
278            runtime: handle,
279            mode,
280            parent_session_id: self.parent_session_id,
281            active_plugins: self.active_plugins,
282        })
283    }
284
285    async fn create_store(
286        &self,
287        policy: &SessionPolicy,
288    ) -> Result<Option<Arc<dyn RuntimePersistence>>> {
289        if let Some(store) = self.store.as_ref() {
290            return Ok(Some(Arc::clone(store)));
291        }
292        let Some(factory) = self.core.store_factory.as_ref() else {
293            return Ok(None);
294        };
295        let request = SessionStoreCreateRequest {
296            session_id: self.session_id.clone(),
297            relation: self
298                .parent_session_id
299                .as_ref()
300                .map(|parent_session_id| lash_core::SessionRelation::Child {
301                    parent_session_id: parent_session_id.clone(),
302                    caused_by: None,
303                })
304                .unwrap_or_default(),
305            policy: policy.clone(),
306        };
307        factory
308            .create_store(&request)
309            .await
310            .map(Some)
311            .map_err(|message| EmbedError::StoreFactory {
312                session_id: self.session_id.clone(),
313                message,
314            })
315    }
316}
317
318impl PromptLayerSink for SessionBuilder {
319    fn prompt_layer_mut(&mut self) -> &mut PromptLayer {
320        self.spec.prompt.get_or_insert_with(PromptLayer::new)
321    }
322}
323
324#[derive(Clone)]
325pub struct LashSession {
326    pub(crate) runtime: RuntimeHandle,
327    pub(crate) mode: ModeId,
328    pub(crate) parent_session_id: Option<String>,
329    pub(crate) active_plugins: Vec<ActivePluginBinding>,
330}
331
332#[derive(Clone, Debug, Default)]
333pub struct SessionConfigPatch {
334    pub provider: Option<ProviderHandle>,
335    pub model: Option<lash_core::ModelSpec>,
336    pub prompt: Option<PromptLayer>,
337}
338
339impl LashSession {
340    pub async fn close(self) -> Result<()> {
341        let runtime = self.runtime.writer();
342        let runtime = runtime.lock().await;
343        runtime.unregister_plugin_session()?;
344        Ok(())
345    }
346
347    pub fn session_id(&self) -> String {
348        self.runtime.observe().session_id().to_string()
349    }
350
351    pub fn policy_snapshot(&self) -> SessionPolicy {
352        self.runtime.observe().policy.clone()
353    }
354
355    pub fn observe(&self) -> ObservableSession {
356        ObservableSession {
357            runtime: self.runtime.clone(),
358        }
359    }
360
361    pub fn mode(&self) -> &ModeId {
362        &self.mode
363    }
364
365    pub fn parent_session_id(&self) -> Option<&str> {
366        self.parent_session_id.as_deref()
367    }
368
369    pub async fn effect_host(&self) -> Arc<dyn EffectHost> {
370        self.runtime.writer().lock().await.effect_host()
371    }
372
373    pub async fn run(
374        &self,
375        input: TurnInput,
376        scoped_effect_controller: ScopedEffectController<'_>,
377    ) -> Result<TurnOutput> {
378        self.turn(input).run(scoped_effect_controller).await
379    }
380
381    pub fn turn(&self, input: TurnInput) -> TurnBuilder {
382        TurnBuilder {
383            runtime: self.runtime.clone(),
384            active_plugins: self.active_plugins.clone(),
385            input,
386            cancel: CancellationToken::new(),
387            protocol_turn_options: None,
388            provider: None,
389            model: None,
390        }
391    }
392
393    pub fn next_queued_turn(&self) -> QueuedTurnBuilder {
394        QueuedTurnBuilder {
395            runtime: self.runtime.clone(),
396            cancel: CancellationToken::new(),
397            batch_ids: Vec::new(),
398        }
399    }
400
401    pub fn control(&self) -> SessionControl {
402        SessionControl {
403            runtime: self.runtime.clone(),
404        }
405    }
406
407    pub async fn configure(&self, patch: SessionConfigPatch) -> Result<()> {
408        self.control().config().update(patch).await
409    }
410
411    pub fn tools(&self) -> ToolsControl {
412        ToolsControl::new(self.control())
413    }
414
415    pub fn commands(&self) -> SessionCommandsControl {
416        self.control().commands()
417    }
418
419    pub fn triggers(&self) -> TriggersControl {
420        self.control().triggers()
421    }
422
423    pub fn process_control(&self) -> ProcessControl {
424        ProcessControl::new(self.control())
425    }
426
427    pub fn plugin_actions(&self) -> PluginActions {
428        PluginActions {
429            control: self.control(),
430        }
431    }
432
433    pub fn queue(&self, input: TurnInput) -> QueueInputBuilder<'_> {
434        QueueInputBuilder {
435            session: self,
436            input,
437            id: None,
438            delivery_policy: DeliveryPolicy::AfterCurrentTurnCommit,
439            slot_policy: SlotPolicy::Exclusive,
440        }
441    }
442
443    pub async fn queued_work(&self) -> Result<Vec<QueuedWorkBatch>> {
444        let observation = self.runtime.observe();
445        let store = observation.queue_store.as_ref().ok_or_else(|| {
446            EmbedError::Runtime(lash_core::RuntimeError::new(
447                lash_core::RuntimeErrorCode::StoreCommitFailed,
448                "queued work inspection requires a persistent runtime store",
449            ))
450        })?;
451        store
452            .list_pending_queued_work(observation.session_id())
453            .await
454            .map_err(|err| {
455                EmbedError::Runtime(lash_core::RuntimeError::new(
456                    lash_core::RuntimeErrorCode::StoreCommitFailed,
457                    err.to_string(),
458                ))
459            })
460    }
461
462    pub async fn cancel_queued_work_batch(
463        &self,
464        batch_id: &str,
465    ) -> Result<Option<QueuedWorkBatch>> {
466        let session_id = self.session_id();
467        self.runtime
468            .cancel_queued_work_batch(&session_id, batch_id)
469            .await
470            .map_err(EmbedError::Runtime)
471    }
472
473    pub fn read_view(&self) -> SessionReadView {
474        self.runtime.observe().read_view.clone()
475    }
476
477    pub fn usage_report(&self) -> SessionUsageReport {
478        self.runtime.observe().usage_report.clone()
479    }
480
481    pub async fn set_turn_phase_probe(
482        &self,
483        probe: Arc<dyn lash_core::runtime::RuntimeTurnPhaseProbe>,
484    ) {
485        let writer = self.runtime.writer();
486        let mut runtime = writer.lock().await;
487        runtime.set_turn_phase_probe(probe);
488        self.runtime.publish_from(&runtime);
489    }
490}
491
492#[derive(Clone)]
493pub struct ObservableSession {
494    pub(crate) runtime: RuntimeHandle,
495}
496
497impl ObservableSession {
498    fn snapshot(&self) -> Arc<RuntimeObservation> {
499        self.runtime.observe()
500    }
501
502    pub fn session_id(&self) -> String {
503        self.snapshot().session_id().to_string()
504    }
505
506    pub fn policy_snapshot(&self) -> SessionPolicy {
507        self.snapshot().policy.clone()
508    }
509
510    pub fn read_view(&self) -> SessionReadView {
511        self.snapshot().read_view.clone()
512    }
513
514    pub fn usage_report(&self) -> SessionUsageReport {
515        self.snapshot().usage_report.clone()
516    }
517
518    pub fn tool_state(&self) -> Option<ToolState> {
519        self.snapshot().tool_state.clone()
520    }
521
522    pub fn active_tool_definitions(&self) -> Vec<ToolManifest> {
523        self.snapshot()
524            .tool_state
525            .as_ref()
526            .map(ToolState::tool_manifests)
527            .unwrap_or_default()
528    }
529
530    pub async fn list_process_handles(&self) -> Vec<lash_core::ProcessHandleSummary> {
531        self.snapshot().list_process_handles().await
532    }
533
534    pub async fn list_all_process_handles(&self) -> Vec<lash_core::ProcessHandleSummary> {
535        self.snapshot().list_all_process_handles().await
536    }
537
538    pub fn process_scope(&self) -> ProcessScope {
539        self.snapshot().process_scope()
540    }
541}
542pub struct QueueInputBuilder<'a> {
543    session: &'a LashSession,
544    input: TurnInput,
545    id: Option<String>,
546    delivery_policy: DeliveryPolicy,
547    slot_policy: SlotPolicy,
548}
549
550impl<'a> QueueInputBuilder<'a> {
551    pub fn id(mut self, id: impl Into<String>) -> Self {
552        self.id = Some(id.into());
553        self
554    }
555
556    pub fn delivery_policy(mut self, policy: DeliveryPolicy) -> Self {
557        self.delivery_policy = policy;
558        self
559    }
560
561    pub fn slot_policy(mut self, policy: SlotPolicy) -> Self {
562        self.slot_policy = policy;
563        self
564    }
565
566    pub async fn send(self) -> Result<()> {
567        let source_key = self.id.map(|id| format!("host:{id}"));
568        self.session
569            .runtime
570            .enqueue_turn_input(
571                self.input,
572                self.delivery_policy,
573                self.slot_policy,
574                source_key,
575            )
576            .await
577            .map(|_| ())
578            .map_err(EmbedError::Runtime)
579    }
580}
581
582impl<'a> std::future::IntoFuture for QueueInputBuilder<'a> {
583    type Output = Result<()>;
584    type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>>;
585
586    fn into_future(self) -> Self::IntoFuture {
587        Box::pin(self.send())
588    }
589}