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        let effect_host = Arc::clone(&env.core.control.effect_host);
271        // Lazily spawn the default process work runner (Decision 3: deferred to
272        // the first open so a tokio runtime is guaranteed; idempotent via the
273        // shared once-guard) and thread its poke onto this session's host so the
274        // process control seam can wake the runner after a successful start.
275        env.process_work_poke = self.core.process_work_runner.poke().await;
276        let runtime = LashRuntime::from_environment(&env, policy, state, store).await?;
277        let handle = RuntimeHandle::with_live_replay_store(
278            runtime,
279            Arc::clone(&self.core.live_replay_store),
280        );
281        Ok(LashSession {
282            runtime: handle,
283            effect_host,
284            mode,
285            parent_session_id: self.parent_session_id,
286            active_plugins: self.active_plugins,
287        })
288    }
289
290    async fn create_store(
291        &self,
292        policy: &SessionPolicy,
293    ) -> Result<Option<Arc<dyn RuntimePersistence>>> {
294        if let Some(store) = self.store.as_ref() {
295            return Ok(Some(Arc::clone(store)));
296        }
297        let Some(factory) = self.core.store_factory.as_ref() else {
298            return Ok(None);
299        };
300        let request = SessionStoreCreateRequest {
301            session_id: self.session_id.clone(),
302            relation: self
303                .parent_session_id
304                .as_ref()
305                .map(|parent_session_id| lash_core::SessionRelation::Child {
306                    parent_session_id: parent_session_id.clone(),
307                    caused_by: None,
308                })
309                .unwrap_or_default(),
310            policy: policy.clone(),
311        };
312        factory
313            .create_store(&request)
314            .await
315            .map(Some)
316            .map_err(|message| EmbedError::StoreFactory {
317                session_id: self.session_id.clone(),
318                message,
319            })
320    }
321}
322
323impl PromptLayerSink for SessionBuilder {
324    fn prompt_layer_mut(&mut self) -> &mut PromptLayer {
325        self.spec.prompt.get_or_insert_with(PromptLayer::new)
326    }
327}
328
329#[derive(Clone)]
330pub struct LashSession {
331    pub(crate) runtime: RuntimeHandle,
332    pub(crate) effect_host: Arc<dyn EffectHost>,
333    pub(crate) mode: ModeId,
334    pub(crate) parent_session_id: Option<String>,
335    pub(crate) active_plugins: Vec<ActivePluginBinding>,
336}
337
338#[derive(Clone, Debug, Default)]
339pub struct SessionConfigPatch {
340    pub provider: Option<ProviderHandle>,
341    pub model: Option<ModelSpec>,
342    pub prompt: Option<PromptLayer>,
343}
344
345impl LashSession {
346    pub async fn close(self) -> Result<()> {
347        let runtime = self.runtime.writer();
348        let runtime = runtime.lock().await;
349        runtime.unregister_plugin_session()?;
350        Ok(())
351    }
352
353    pub fn session_id(&self) -> String {
354        self.runtime.observe().session_id().to_string()
355    }
356
357    pub fn policy_snapshot(&self) -> SessionPolicy {
358        self.runtime.observe().policy.clone()
359    }
360
361    pub fn observe(&self) -> ObservableSession {
362        ObservableSession {
363            runtime: self.runtime.clone(),
364        }
365    }
366
367    pub fn mode(&self) -> &ModeId {
368        &self.mode
369    }
370
371    pub fn parent_session_id(&self) -> Option<&str> {
372        self.parent_session_id.as_deref()
373    }
374
375    pub async fn effect_host(&self) -> Arc<dyn EffectHost> {
376        Arc::clone(&self.effect_host)
377    }
378
379    pub fn turn(&self, input: TurnInput) -> TurnBuilder {
380        TurnBuilder {
381            runtime: self.runtime.clone(),
382            effect_host: Arc::clone(&self.effect_host),
383            active_plugins: self.active_plugins.clone(),
384            input,
385            cancel: CancellationToken::new(),
386            protocol_turn_options: None,
387            provider: None,
388            model: None,
389            turn_id: None,
390        }
391    }
392
393    pub fn queued_turn(&self) -> QueuedTurnBuilder {
394        QueuedTurnBuilder {
395            runtime: self.runtime.clone(),
396            effect_host: Arc::clone(&self.effect_host),
397            cancel: CancellationToken::new(),
398            batch_ids: Vec::new(),
399            drain_id: None,
400        }
401    }
402
403    pub fn control(&self) -> SessionControl {
404        SessionControl {
405            runtime: self.runtime.clone(),
406        }
407    }
408
409    pub async fn configure(&self, patch: SessionConfigPatch) -> Result<()> {
410        self.control().config().update(patch).await
411    }
412
413    pub fn tools(&self) -> ToolsControl {
414        ToolsControl::new(self.control())
415    }
416
417    pub fn commands(&self) -> SessionCommandsControl {
418        self.control().commands()
419    }
420
421    pub fn triggers(&self) -> TriggersControl {
422        self.control().triggers()
423    }
424
425    pub fn process_control(&self) -> ProcessControl {
426        ProcessControl::new(self.control())
427    }
428
429    pub fn plugin_actions(&self) -> PluginActions {
430        PluginActions {
431            control: self.control(),
432        }
433    }
434
435    pub fn enqueue(&self, input: TurnInput) -> EnqueueTurnBuilder<'_> {
436        EnqueueTurnBuilder {
437            session: self,
438            input,
439            id: None,
440            delivery_policy: DeliveryPolicy::AfterCurrentTurnCommit,
441            slot_policy: SlotPolicy::Exclusive,
442        }
443    }
444
445    pub async fn queued_work(&self) -> Result<Vec<QueuedWorkBatch>> {
446        let observation = self.runtime.observe();
447        let store = observation.queue_store.as_ref().ok_or_else(|| {
448            EmbedError::Runtime(lash_core::RuntimeError::new(
449                lash_core::RuntimeErrorCode::StoreCommitFailed,
450                "queued work inspection requires a persistent runtime store",
451            ))
452        })?;
453        store
454            .list_pending_queued_work(observation.session_id())
455            .await
456            .map_err(|err| {
457                EmbedError::Runtime(lash_core::RuntimeError::new(
458                    lash_core::RuntimeErrorCode::StoreCommitFailed,
459                    err.to_string(),
460                ))
461            })
462    }
463
464    pub async fn cancel_queued_work_batch(
465        &self,
466        batch_id: &str,
467    ) -> Result<Option<QueuedWorkBatch>> {
468        let session_id = self.session_id();
469        self.runtime
470            .cancel_queued_work_batch(&session_id, batch_id)
471            .await
472            .map_err(EmbedError::Runtime)
473    }
474
475    pub fn read_view(&self) -> SessionReadView {
476        self.runtime.observe().read_view.clone()
477    }
478
479    pub fn usage_report(&self) -> SessionUsageReport {
480        self.runtime.observe().usage_report.clone()
481    }
482
483    pub async fn set_turn_phase_probe(
484        &self,
485        probe: Arc<dyn lash_core::runtime::RuntimeTurnPhaseProbe>,
486    ) {
487        let writer = self.runtime.writer();
488        let mut runtime = writer.lock().await;
489        runtime.set_turn_phase_probe(probe);
490        self.runtime.publish_from(&runtime);
491    }
492}
493
494#[derive(Clone)]
495pub struct ObservableSession {
496    pub(crate) runtime: RuntimeHandle,
497}
498
499impl ObservableSession {
500    fn snapshot(&self) -> Arc<RuntimeObservation> {
501        self.runtime.observe()
502    }
503
504    pub fn current_observation(&self) -> SessionObservation {
505        self.runtime.current_session_observation()
506    }
507
508    pub fn resume_from_cursor(&self, cursor: &SessionCursor) -> Result<SessionResume> {
509        self.runtime
510            .resume_session_observation(cursor)
511            .map_err(live_replay_error)
512    }
513
514    pub fn subscribe_from_cursor(
515        &self,
516        cursor: &SessionCursor,
517    ) -> Result<SessionObservationSubscription> {
518        self.runtime
519            .subscribe_session_observation(cursor)
520            .map_err(live_replay_error)
521    }
522
523    pub fn session_id(&self) -> String {
524        self.snapshot().session_id().to_string()
525    }
526
527    pub fn policy_snapshot(&self) -> SessionPolicy {
528        self.snapshot().policy.clone()
529    }
530
531    pub fn read_view(&self) -> SessionReadView {
532        self.snapshot().read_view.clone()
533    }
534
535    pub fn usage_report(&self) -> SessionUsageReport {
536        self.snapshot().usage_report.clone()
537    }
538
539    pub fn tool_state(&self) -> Option<ToolState> {
540        self.snapshot().tool_state.clone()
541    }
542
543    pub fn active_tool_definitions(&self) -> Vec<ToolManifest> {
544        self.snapshot()
545            .tool_state
546            .as_ref()
547            .map(ToolState::tool_manifests)
548            .unwrap_or_default()
549    }
550
551    pub async fn list_process_handles(&self) -> Vec<ProcessHandleSummary> {
552        self.snapshot().list_process_handles().await
553    }
554
555    pub async fn list_all_process_handles(&self) -> Vec<ProcessHandleSummary> {
556        self.snapshot().list_all_process_handles().await
557    }
558
559    pub fn process_scope(&self) -> ProcessScope {
560        self.snapshot().process_scope()
561    }
562}
563
564fn live_replay_error(err: lash_core::LiveReplayStoreError) -> EmbedError {
565    EmbedError::Runtime(lash_core::RuntimeError::new(
566        RuntimeErrorCode::Other("live_replay".to_string()),
567        err.to_string(),
568    ))
569}
570
571pub struct EnqueueTurnBuilder<'a> {
572    session: &'a LashSession,
573    input: TurnInput,
574    id: Option<String>,
575    delivery_policy: DeliveryPolicy,
576    slot_policy: SlotPolicy,
577}
578
579impl<'a> EnqueueTurnBuilder<'a> {
580    pub fn id(mut self, id: impl Into<String>) -> Self {
581        self.id = Some(id.into());
582        self
583    }
584
585    pub fn delivery_policy(mut self, policy: DeliveryPolicy) -> Self {
586        self.delivery_policy = policy;
587        self
588    }
589
590    pub fn slot_policy(mut self, policy: SlotPolicy) -> Self {
591        self.slot_policy = policy;
592        self
593    }
594
595    pub async fn send(self) -> Result<QueuedWorkBatch> {
596        let source_key = self.id.map(|id| format!("host:{id}"));
597        self.session
598            .runtime
599            .enqueue_turn_input(
600                self.input,
601                self.delivery_policy,
602                self.slot_policy,
603                source_key,
604            )
605            .await
606            .map_err(EmbedError::Runtime)
607    }
608}
609
610impl<'a> std::future::IntoFuture for EnqueueTurnBuilder<'a> {
611    type Output = Result<QueuedWorkBatch>;
612    type IntoFuture =
613        std::pin::Pin<Box<dyn std::future::Future<Output = Result<QueuedWorkBatch>> + 'a>>;
614
615    fn into_future(self) -> Self::IntoFuture {
616        Box::pin(self.send())
617    }
618}