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            turn_cancels: crate::turn::TurnCancelRegistry::default(),
288        })
289    }
290
291    async fn create_store(
292        &self,
293        policy: &SessionPolicy,
294    ) -> Result<Option<Arc<dyn RuntimePersistence>>> {
295        if let Some(store) = self.store.as_ref() {
296            return Ok(Some(Arc::clone(store)));
297        }
298        let Some(factory) = self.core.store_factory.as_ref() else {
299            return Ok(None);
300        };
301        let request = SessionStoreCreateRequest {
302            session_id: self.session_id.clone(),
303            relation: self
304                .parent_session_id
305                .as_ref()
306                .map(|parent_session_id| lash_core::SessionRelation::Child {
307                    parent_session_id: parent_session_id.clone(),
308                    caused_by: None,
309                })
310                .unwrap_or_default(),
311            policy: policy.clone(),
312        };
313        factory
314            .create_store(&request)
315            .await
316            .map(Some)
317            .map_err(|message| EmbedError::StoreFactory {
318                session_id: self.session_id.clone(),
319                message,
320            })
321    }
322}
323
324impl PromptLayerSink for SessionBuilder {
325    fn prompt_layer_mut(&mut self) -> &mut PromptLayer {
326        self.spec.prompt.get_or_insert_with(PromptLayer::new)
327    }
328}
329
330#[derive(Clone)]
331pub struct LashSession {
332    pub(crate) runtime: RuntimeHandle,
333    pub(crate) effect_host: Arc<dyn EffectHost>,
334    pub(crate) mode: ModeId,
335    pub(crate) parent_session_id: Option<String>,
336    pub(crate) active_plugins: Vec<ActivePluginBinding>,
337    pub(crate) turn_cancels: crate::turn::TurnCancelRegistry,
338}
339
340#[derive(Clone, Debug, Default)]
341pub struct SessionConfigPatch {
342    pub provider: Option<ProviderHandle>,
343    pub model: Option<ModelSpec>,
344    pub prompt: Option<PromptLayer>,
345}
346
347impl LashSession {
348    pub async fn close(self) -> Result<()> {
349        let runtime = self.runtime.writer();
350        let runtime = runtime.lock().await;
351        runtime.unregister_plugin_session()?;
352        Ok(())
353    }
354
355    pub fn session_id(&self) -> String {
356        self.runtime.observe().session_id().to_string()
357    }
358
359    pub fn policy_snapshot(&self) -> SessionPolicy {
360        self.runtime.observe().policy.clone()
361    }
362
363    pub fn observe(&self) -> ObservableSession {
364        ObservableSession {
365            runtime: self.runtime.clone(),
366        }
367    }
368
369    pub fn mode(&self) -> &ModeId {
370        &self.mode
371    }
372
373    pub fn parent_session_id(&self) -> Option<&str> {
374        self.parent_session_id.as_deref()
375    }
376
377    pub fn effect_host(&self) -> Arc<dyn EffectHost> {
378        Arc::clone(&self.effect_host)
379    }
380
381    pub fn turn(&self, input: TurnInput) -> TurnBuilder {
382        TurnBuilder {
383            runtime: self.runtime.clone(),
384            effect_host: Arc::clone(&self.effect_host),
385            active_plugins: self.active_plugins.clone(),
386            input,
387            cancel: CancellationToken::new(),
388            cancels: self.turn_cancels.clone(),
389            protocol_turn_options: None,
390            provider: None,
391            model: None,
392            turn_id: None,
393        }
394    }
395
396    pub fn queued_turn(&self) -> QueuedTurnBuilder {
397        QueuedTurnBuilder {
398            runtime: self.runtime.clone(),
399            effect_host: Arc::clone(&self.effect_host),
400            cancel: CancellationToken::new(),
401            cancels: self.turn_cancels.clone(),
402            batch_ids: Vec::new(),
403            drain_id: None,
404        }
405    }
406
407    /// Cancel every turn currently executing through this opened session
408    /// (including its clones) and report how many were signalled.
409    ///
410    /// This is the affordance behind a UI "stop" control: hold a clone of the
411    /// session wherever the stop arrives and call this, instead of threading a
412    /// [`CancellationToken`](crate::CancellationToken) into every turn call
413    /// ([`TurnBuilder::cancel`](crate::TurnBuilder::cancel) remains the
414    /// per-turn hook when you need one). A cancelled turn finishes with
415    /// `TurnOutcome::Stopped(TurnStop::Cancelled)` and commits like any other
416    /// turn; the session stays usable.
417    ///
418    /// Scope: turns started from this `LashSession` instance and its clones.
419    /// A handle opened separately for the same session id has its own
420    /// registry and is not reached.
421    pub fn cancel_running_turns(&self) -> usize {
422        self.turn_cancels.cancel_all()
423    }
424
425    pub fn control(&self) -> SessionControl {
426        SessionControl {
427            runtime: self.runtime.clone(),
428        }
429    }
430
431    pub async fn configure(&self, patch: SessionConfigPatch) -> Result<()> {
432        self.control().config().update(patch).await
433    }
434
435    pub fn tools(&self) -> ToolsControl {
436        ToolsControl::new(self.control())
437    }
438
439    pub fn commands(&self) -> SessionCommandsControl {
440        self.control().commands()
441    }
442
443    pub fn triggers(&self) -> TriggersControl {
444        self.control().triggers()
445    }
446
447    pub fn process_control(&self) -> ProcessControl {
448        ProcessControl::new(self.control())
449    }
450
451    pub fn plugin_actions(&self) -> PluginActions {
452        PluginActions {
453            control: self.control(),
454        }
455    }
456
457    pub fn enqueue(&self, input: TurnInput) -> EnqueueTurnBuilder<'_> {
458        EnqueueTurnBuilder {
459            session: self,
460            input,
461            id: None,
462            delivery_policy: DeliveryPolicy::AfterCurrentTurnCommit,
463            slot_policy: SlotPolicy::Exclusive,
464        }
465    }
466
467    pub async fn queued_work(&self) -> Result<Vec<QueuedWorkBatch>> {
468        let observation = self.runtime.observe();
469        let store = observation.queue_store.as_ref().ok_or_else(|| {
470            EmbedError::Runtime(lash_core::RuntimeError::new(
471                lash_core::RuntimeErrorCode::StoreCommitFailed,
472                "queued work inspection requires a persistent runtime store",
473            ))
474        })?;
475        store
476            .list_pending_queued_work(observation.session_id())
477            .await
478            .map_err(|err| {
479                EmbedError::Runtime(lash_core::RuntimeError::new(
480                    lash_core::RuntimeErrorCode::StoreCommitFailed,
481                    err.to_string(),
482                ))
483            })
484    }
485
486    pub async fn cancel_queued_work_batch(
487        &self,
488        batch_id: &str,
489    ) -> Result<Option<QueuedWorkBatch>> {
490        let session_id = self.session_id();
491        self.runtime
492            .cancel_queued_work_batch(&session_id, batch_id)
493            .await
494            .map_err(EmbedError::Runtime)
495    }
496
497    /// Resolve once `batch_id` is no longer pending in the queue store —
498    /// drained by whoever runs queued work (a queued-work runner, a durable
499    /// worker, or another handle's [`queued_turn`](Self::queued_turn)) or
500    /// cancelled. This is the enqueue-and-observe side of the queue: the
501    /// caller never claims the work itself.
502    ///
503    /// Completion is read from the persistent queue store, so it observes
504    /// drains performed by other session handles and other processes alike.
505    /// There is no built-in deadline — nothing resolves if nothing drains the
506    /// queue, so bound it with `tokio::time::timeout` when the worker may be
507    /// unavailable. A batch id the store has never seen resolves immediately.
508    pub async fn await_queued_work_batch(&self, batch_id: &str) -> Result<()> {
509        let observation = self.runtime.observe();
510        let store = observation.queue_store.clone().ok_or_else(|| {
511            EmbedError::Runtime(lash_core::RuntimeError::new(
512                lash_core::RuntimeErrorCode::StoreCommitFailed,
513                "queued work inspection requires a persistent runtime store",
514            ))
515        })?;
516        let session_id = observation.session_id().to_string();
517        drop(observation);
518        let mut delay = std::time::Duration::from_millis(25);
519        loop {
520            let pending = store
521                .list_pending_queued_work(&session_id)
522                .await
523                .map_err(|err| {
524                    EmbedError::Runtime(lash_core::RuntimeError::new(
525                        lash_core::RuntimeErrorCode::StoreCommitFailed,
526                        err.to_string(),
527                    ))
528                })?;
529            if !pending.iter().any(|batch| batch.batch_id == batch_id) {
530                return Ok(());
531            }
532            tokio::time::sleep(delay).await;
533            delay = (delay * 2).min(std::time::Duration::from_millis(400));
534        }
535    }
536
537    pub fn read_view(&self) -> SessionReadView {
538        self.runtime.observe().read_view.clone()
539    }
540
541    pub fn usage_report(&self) -> SessionUsageReport {
542        self.runtime.observe().usage_report.clone()
543    }
544
545    pub async fn set_turn_phase_probe(
546        &self,
547        probe: Arc<dyn lash_core::runtime::RuntimeTurnPhaseProbe>,
548    ) {
549        let writer = self.runtime.writer();
550        let mut runtime = writer.lock().await;
551        runtime.set_turn_phase_probe(probe);
552        self.runtime.publish_from(&runtime);
553    }
554}
555
556#[derive(Clone)]
557pub struct ObservableSession {
558    pub(crate) runtime: RuntimeHandle,
559}
560
561impl ObservableSession {
562    fn snapshot(&self) -> Arc<RuntimeObservation> {
563        self.runtime.observe()
564    }
565
566    pub fn current_observation(&self) -> SessionObservation {
567        self.runtime.current_session_observation()
568    }
569
570    pub fn resume_from_cursor(&self, cursor: &SessionCursor) -> Result<SessionResume> {
571        self.runtime
572            .resume_session_observation(cursor)
573            .map_err(live_replay_error)
574    }
575
576    pub fn subscribe_from_cursor(
577        &self,
578        cursor: &SessionCursor,
579    ) -> Result<SessionObservationSubscription> {
580        self.runtime
581            .subscribe_session_observation(cursor)
582            .map_err(live_replay_error)
583    }
584
585    pub fn session_id(&self) -> String {
586        self.snapshot().session_id().to_string()
587    }
588
589    pub fn policy_snapshot(&self) -> SessionPolicy {
590        self.snapshot().policy.clone()
591    }
592
593    pub fn read_view(&self) -> SessionReadView {
594        self.snapshot().read_view.clone()
595    }
596
597    pub fn usage_report(&self) -> SessionUsageReport {
598        self.snapshot().usage_report.clone()
599    }
600
601    pub fn tool_state(&self) -> Option<ToolState> {
602        self.snapshot().tool_state.clone()
603    }
604
605    pub fn active_tool_manifests(&self) -> Vec<ToolManifest> {
606        self.snapshot()
607            .tool_state
608            .as_ref()
609            .map(ToolState::tool_manifests)
610            .unwrap_or_default()
611    }
612
613    pub async fn list_process_handles(&self) -> Vec<ProcessHandleSummary> {
614        self.snapshot().list_process_handles().await
615    }
616
617    pub async fn list_all_process_handles(&self) -> Vec<ProcessHandleSummary> {
618        self.snapshot().list_all_process_handles().await
619    }
620
621    pub fn process_scope(&self) -> ProcessScope {
622        self.snapshot().process_scope()
623    }
624}
625
626fn live_replay_error(err: lash_core::LiveReplayStoreError) -> EmbedError {
627    EmbedError::Runtime(lash_core::RuntimeError::new(
628        RuntimeErrorCode::Other("live_replay".to_string()),
629        err.to_string(),
630    ))
631}
632
633pub struct EnqueueTurnBuilder<'a> {
634    session: &'a LashSession,
635    input: TurnInput,
636    id: Option<String>,
637    delivery_policy: DeliveryPolicy,
638    slot_policy: SlotPolicy,
639}
640
641impl<'a> EnqueueTurnBuilder<'a> {
642    pub fn id(mut self, id: impl Into<String>) -> Self {
643        self.id = Some(id.into());
644        self
645    }
646
647    pub fn delivery_policy(mut self, policy: DeliveryPolicy) -> Self {
648        self.delivery_policy = policy;
649        self
650    }
651
652    pub fn slot_policy(mut self, policy: SlotPolicy) -> Self {
653        self.slot_policy = policy;
654        self
655    }
656
657    pub async fn send(self) -> Result<QueuedWorkBatch> {
658        let source_key = self.id.map(|id| format!("host:{id}"));
659        self.session
660            .runtime
661            .enqueue_turn_input(
662                self.input,
663                self.delivery_policy,
664                self.slot_policy,
665                source_key,
666            )
667            .await
668            .map_err(EmbedError::Runtime)
669    }
670}
671
672impl<'a> std::future::IntoFuture for EnqueueTurnBuilder<'a> {
673    type Output = Result<QueuedWorkBatch>;
674    type IntoFuture =
675        std::pin::Pin<Box<dyn std::future::Future<Output = Result<QueuedWorkBatch>> + 'a>>;
676
677    fn into_future(self) -> Self::IntoFuture {
678        Box::pin(self.send())
679    }
680}