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