Skip to main content

lash/
session.rs

1use std::pin::Pin;
2use std::task::{Context, Poll};
3
4use crate::support::*;
5use futures_util::Stream;
6use lash_core::runtime::{
7    PendingTurnInput, PendingTurnInputCancelOutcome, PendingTurnInputCancelResult,
8    PendingTurnInputCancelTarget, PendingTurnInputSuffixCancelOutcome, QueuedWorkBatch,
9    QueuedWorkClaim, TurnInputClaim, TurnInputIngress,
10};
11use lash_core::{LiveReplayGap, LiveReplayStoreError, SessionObservationEvent};
12use lash_remote_protocol::{
13    RemoteLiveReplayGap, RemoteSessionCursor, RemoteSessionObservation,
14    RemoteSessionObservationEvent,
15};
16
17pub struct SessionBuilder {
18    pub(crate) core: LashCore,
19    pub(crate) session_id: String,
20    pub(crate) spec: SessionSpec,
21    pub(crate) parent_session_id: Option<String>,
22    pub(crate) session_execution_owner: Option<lash_core::LeaseOwnerIdentity>,
23    pub(crate) store: Option<Arc<dyn RuntimePersistence>>,
24    pub(crate) provider: Option<ProviderHandle>,
25    pub(crate) active_plugins: Vec<ActivePluginBinding>,
26    pub(crate) plugin_factories: Vec<Arc<dyn PluginFactory>>,
27    /// Plugin-keyed, serializable open-time options. They ride the protocol
28    /// materialization seam (the same `PluginOptions` bag a child
29    /// `SessionCreateRequest` carries) so every plugin gets open-time options
30    /// through one hook.
31    pub(crate) plugin_options: PluginOptions,
32}
33
34impl SessionBuilder {
35    /// Set the plugin-keyed open-time options bag wholesale.
36    pub fn plugin_options(mut self, plugin_options: PluginOptions) -> Self {
37        self.plugin_options = plugin_options;
38        self
39    }
40
41    /// Merge a single plugin's typed options into the open-time options bag,
42    /// preserving any options already set for other plugin keys.
43    pub fn plugin_option<T: serde::Serialize>(
44        mut self,
45        plugin_id: impl Into<String>,
46        extras: T,
47    ) -> Result<Self> {
48        self.plugin_options
49            .insert_typed(plugin_id, extras)
50            .map_err(EmbedError::ProtocolTurnOptions)?;
51        Ok(self)
52    }
53
54    pub fn provider(mut self, provider: ProviderHandle) -> Self {
55        self.spec = self.spec.provider_id(provider.kind());
56        self.provider = Some(provider);
57        self
58    }
59
60    pub fn session_spec(mut self, spec: SessionSpec) -> Self {
61        self.spec = spec;
62        self
63    }
64
65    pub fn parent(mut self, parent_session_id: impl Into<String>) -> Self {
66        self.parent_session_id = Some(parent_session_id.into());
67        self
68    }
69
70    /// Use an explicit owner identity for durable session execution leases.
71    ///
72    /// This is only for hosts that already serialize one logical execution lane
73    /// and intentionally choose stable owner + incarnation values. Normal
74    /// embedders should keep the default per-open identity.
75    pub fn session_execution_owner(mut self, owner: lash_core::LeaseOwnerIdentity) -> Self {
76        self.session_execution_owner = Some(owner);
77        self
78    }
79
80    /// Use a specific persistence store for this root session.
81    ///
82    /// This is the right API for a host-owned, pre-opened session database.
83    /// Managed child sessions never reuse this store; configure
84    /// `LashCoreBuilder::child_store_factory` when child sessions should also
85    /// persist.
86    pub fn store(mut self, store: Arc<dyn RuntimePersistence>) -> Self {
87        self.store = Some(store);
88        self
89    }
90
91    pub fn plugin<P: PluginBinding>(mut self, config: P::SessionConfig) -> Self {
92        self.active_plugins.push(ActivePluginBinding {
93            id: P::ID,
94            requires_turn_input: P::requires_turn_input(&config),
95        });
96        self.plugin_factories.push(P::factory(&config));
97        self
98    }
99
100    pub async fn open(self) -> Result<LashSession> {
101        let policy = self.session_policy();
102        let store = self.create_store(&policy).await?;
103        let state = self
104            .load_or_default_state(&policy, store.as_deref())
105            .await?;
106        self.open_resolved(policy, state, store).await
107    }
108
109    /// Open this session with a fresh resident graph, ignoring any persisted
110    /// session graph/checkpoint state that may already exist for the same
111    /// session id.
112    ///
113    /// The next successful commit writes a full replacement graph, so normal
114    /// embedders can use this to start over without manually calling
115    /// `load_persisted_session_state` or constructing a `RuntimeSessionState`.
116    /// Use [`Self::open`] for resume and [`Self::open_with_state`] only when
117    /// restoring explicit host-owned state.
118    pub async fn open_fresh(self) -> Result<LashSession> {
119        let policy = self.session_policy();
120        let store = self.create_store(&policy).await?;
121        let state = RuntimeSessionState {
122            session_id: self.session_id.clone(),
123            policy: policy.clone(),
124            graph_replace_required: true,
125            ..RuntimeSessionState::default()
126        };
127        self.open_resolved(policy, state, store).await
128    }
129
130    /// Open with an explicitly supplied runtime state.
131    ///
132    /// This is for advanced hosts that already own a complete state snapshot.
133    /// Normal embedders should use [`Self::open`] to resume according to Lash's
134    /// residency policy or [`Self::open_fresh`] to start over and replace prior
135    /// persisted state on the next commit.
136    pub async fn open_with_state(self, mut state: RuntimeSessionState) -> Result<LashSession> {
137        let policy = self.session_policy();
138        let store = self.create_store(&policy).await?;
139        if state.session_id != self.session_id {
140            return Err(EmbedError::StoreSessionMismatch {
141                loaded: state.session_id,
142                requested: self.session_id,
143            });
144        }
145        reconcile_loaded_state_policy(&mut state, &policy);
146        self.open_resolved(policy, state, store).await
147    }
148
149    fn session_policy(&self) -> SessionPolicy {
150        let mut policy = self.spec.resolve_against(&self.core.policy);
151        policy.session_id = Some(self.session_id.clone());
152        policy
153    }
154
155    async fn load_or_default_state(
156        &self,
157        policy: &SessionPolicy,
158        store: Option<&dyn RuntimePersistence>,
159    ) -> Result<RuntimeSessionState> {
160        let state = match store {
161            Some(store) => {
162                let loaded = self.load_persisted_state_for_residency(store).await?;
163                let mut state = loaded.unwrap_or_else(|| RuntimeSessionState {
164                    session_id: self.session_id.clone(),
165                    policy: policy.clone(),
166                    ..RuntimeSessionState::default()
167                });
168                if state.session_id != self.session_id {
169                    return Err(EmbedError::StoreSessionMismatch {
170                        loaded: state.session_id,
171                        requested: self.session_id.clone(),
172                    });
173                }
174                reconcile_loaded_state_policy(&mut state, policy);
175                state
176            }
177            None => RuntimeSessionState {
178                session_id: self.session_id.clone(),
179                policy: policy.clone(),
180                ..RuntimeSessionState::default()
181            },
182        };
183        Ok(state)
184    }
185
186    async fn load_persisted_state_for_residency(
187        &self,
188        store: &dyn RuntimePersistence,
189    ) -> Result<Option<RuntimeSessionState>> {
190        load_persisted_state_for_residency(self.core.env.residency, store).await
191    }
192
193    async fn open_resolved(
194        self,
195        policy: SessionPolicy,
196        state: RuntimeSessionState,
197        store: Option<Arc<dyn RuntimePersistence>>,
198    ) -> Result<LashSession> {
199        let mut env = self.core.env.clone();
200        if let Some(provider) = self.provider.clone().or_else(|| self.core.provider.clone()) {
201            env.core.providers.provider_resolver =
202                Arc::new(lash_core::SingleProviderResolver::new(provider));
203        }
204        let plugin_host = build_plugin_host(
205            self.core.protocol_factory.as_ref(),
206            self.core.plugin_factories.as_ref(),
207            self.plugin_factories,
208        )?;
209        env.core = plugin_host.install_process_engine_contributions(
210            env.core.clone(),
211            self.core.process_lifecycle_available,
212        )?;
213        env.plugin_host = Some(Arc::new(plugin_host));
214        let effect_host = Arc::clone(&env.core.control.effect_host);
215        let drivers = self.core.work_driver.drivers().await;
216        env.process_work_driver = drivers.process.clone();
217        env.queued_work_driver = drivers.queued.clone();
218        let mut runtime = LashRuntime::from_environment(&env, policy, state, store).await?;
219        // Fire the protocol materialization hook for this root/builder open
220        // (including resume): the protocol plugin applies and defaults its
221        // per-session options at open time.
222        runtime.configure_protocol_on_materialize(
223            &self.plugin_options,
224            self.parent_session_id.is_none(),
225        )?;
226        if let Some(owner) = self.session_execution_owner {
227            runtime.set_runtime_lease_owner(owner);
228        }
229        if drivers.drive_process_on_open
230            && let Some(driver) = drivers.process.as_ref()
231        {
232            driver.claim_and_run_pending("session_open").await?;
233        }
234        let handle = RuntimeHandle::with_live_replay_store(
235            runtime,
236            Arc::clone(&self.core.live_replay_store),
237        );
238        Ok(LashSession {
239            runtime: handle,
240            effect_host,
241            parent_session_id: self.parent_session_id,
242            active_plugins: self.active_plugins,
243            process_phase_probe_slot: self.core.work_driver.phase_probe_slot(),
244            turn_cancels: crate::turn::TurnCancelRegistry::default(),
245        })
246    }
247
248    async fn create_store(
249        &self,
250        policy: &SessionPolicy,
251    ) -> Result<Option<Arc<dyn RuntimePersistence>>> {
252        if let Some(store) = self.store.as_ref() {
253            return Ok(Some(Arc::clone(store)));
254        }
255        let Some(factory) = self.core.store_factory.as_ref() else {
256            return Ok(None);
257        };
258        let request = SessionStoreCreateRequest {
259            session_id: self.session_id.clone(),
260            relation: self
261                .parent_session_id
262                .as_ref()
263                .map(|parent_session_id| lash_core::SessionRelation::Child {
264                    parent_session_id: parent_session_id.clone(),
265                    caused_by: None,
266                })
267                .unwrap_or_default(),
268            policy: policy.clone(),
269        };
270        factory
271            .create_store(&request)
272            .await
273            .map(Some)
274            .map_err(|message| EmbedError::StoreFactory {
275                session_id: self.session_id.clone(),
276                message,
277            })
278    }
279}
280
281pub(crate) async fn load_state_for_residency(
282    residency: Residency,
283    session_id: &str,
284    policy: &SessionPolicy,
285    store: &dyn RuntimePersistence,
286) -> Result<RuntimeSessionState> {
287    let mut state = load_persisted_state_for_residency(residency, store)
288        .await?
289        .unwrap_or_else(|| RuntimeSessionState {
290            session_id: session_id.to_string(),
291            policy: policy.clone(),
292            ..RuntimeSessionState::default()
293        });
294    if state.session_id != session_id {
295        return Err(EmbedError::StoreSessionMismatch {
296            loaded: state.session_id,
297            requested: session_id.to_string(),
298        });
299    }
300    reconcile_loaded_state_policy(&mut state, policy);
301    Ok(state)
302}
303
304fn reconcile_loaded_state_policy(state: &mut RuntimeSessionState, policy: &SessionPolicy) {
305    let recorded_provider_id = state.policy.recorded_provider_id().to_string();
306    state.policy = policy.clone();
307    state.policy.provider_id = recorded_provider_id;
308    let reconciled_policy = state.policy.clone();
309    if let Some(frame) = state.current_agent_frame_mut() {
310        frame.assignment.policy = reconciled_policy;
311    }
312}
313
314async fn load_persisted_state_for_residency(
315    residency: Residency,
316    store: &dyn RuntimePersistence,
317) -> Result<Option<RuntimeSessionState>> {
318    match residency {
319        Residency::KeepAll => {
320            let loaded = lash_core::store::load_persisted_session_state(store)
321                .await
322                .map_err(|err| SessionError::Protocol(format!("failed to load store: {err}")))?;
323            Ok(loaded)
324        }
325        Residency::ActivePathOnly => {
326            let active = lash_core::store::load_persisted_session_state_active_path(store, None)
327                .await
328                .map_err(|err| {
329                    SessionError::Protocol(format!("failed to load active-path store: {err}"))
330                })?;
331            if active
332                .as_ref()
333                .is_some_and(|state| state.session_graph.nodes.is_empty())
334            {
335                let mut full = lash_core::store::load_persisted_session_state(store)
336                    .await
337                    .map_err(|err| {
338                        SessionError::Protocol(format!(
339                            "failed to heal active-path store from full graph: {err}"
340                        ))
341                    })?;
342                if let Some(state) = full.as_mut() {
343                    state.graph_replace_required = true;
344                }
345                return Ok(full);
346            }
347            Ok(active)
348        }
349    }
350}
351
352impl PromptLayerSink for SessionBuilder {
353    fn prompt_layer_mut(&mut self) -> &mut PromptLayer {
354        self.spec.prompt.get_or_insert_with(PromptLayer::new)
355    }
356}
357
358#[derive(Clone)]
359pub struct LashSession {
360    pub(crate) runtime: RuntimeHandle,
361    pub(crate) effect_host: Arc<dyn EffectHost>,
362    pub(crate) parent_session_id: Option<String>,
363    pub(crate) active_plugins: Vec<ActivePluginBinding>,
364    pub(crate) process_phase_probe_slot: Option<lash_core::runtime::RuntimeTurnPhaseProbeSlot>,
365    pub(crate) turn_cancels: crate::turn::TurnCancelRegistry,
366}
367
368#[derive(Clone, Debug, Default)]
369pub struct SessionConfigPatch {
370    pub provider: Option<ProviderHandle>,
371    pub model: Option<ModelSpec>,
372    pub prompt: Option<PromptLayer>,
373}
374
375/// Lightweight, consuming handle returned by [`LashSession::park`].
376///
377/// Parking flushes a session's dirty state to its store and drops the live
378/// in-memory runtime, keeping only enough to rebuild it: the session id, its
379/// policy, and the store reference. This is the webserver-embedder quiesce /
380/// handoff primitive — cache one of these per idle session at bounded memory
381/// cost regardless of transcript size, then rebuild with
382/// [`LashCore::resume`](crate::LashCore::resume).
383///
384/// The facade owns this vocabulary; it wraps the core parking handle so the
385/// resume path stays a facade capability rather than exposing `lash-core`
386/// environment plumbing to hosts.
387pub struct ParkedSession {
388    pub(crate) inner: lash_core::ParkedSession,
389}
390
391impl ParkedSession {
392    /// The parked session's id. Use it to key a per-session cache of parked
393    /// handles on the host.
394    pub fn session_id(&self) -> &str {
395        self.inner.session_id()
396    }
397}
398
399impl LashSession {
400    /// Durably close this session, then release its in-memory runtime.
401    ///
402    /// `close` is the honest teardown verb: a persistent session flushes its
403    /// dirty state (via a fresh-lease commit) so the store reflects the final
404    /// transcript, its in-memory plugin session is unregistered, and the live
405    /// runtime is dropped. A store-less (ephemeral) session has nothing to
406    /// persist, so closing it is exactly the plugin-session unregister plus
407    /// dropping the runtime.
408    ///
409    /// This consumes the session and requires exclusive ownership: any cloned
410    /// [`LashSession`] handle or in-flight turn keeps a live reference to the
411    /// same runtime, so `close` returns [`EmbedError::SessionStillInUse`] until
412    /// those are dropped or finished. Cancel running turns first with
413    /// [`cancel_running_turns`](Self::cancel_running_turns) if needed.
414    ///
415    /// To keep a handle for later resumption instead of discarding the session,
416    /// use [`park`](Self::park).
417    pub async fn close(self) -> Result<()> {
418        // Persistence is decided before we consume `self`; the observation's
419        // queue store is the facade's canonical "is this session persistent"
420        // signal (the same source `park`'s commit uses).
421        let persistent = self.runtime.observe().queue_store.is_some();
422        let runtime = self.into_owned_runtime()?;
423        runtime.unregister_plugin_session()?;
424        if persistent {
425            // Reuse the core parking primitive to flush + release the lease,
426            // discarding the returned handle: close does not resume.
427            runtime.park().await?;
428        }
429        // Ephemeral sessions: `runtime` drops here, releasing in-memory state.
430        Ok(())
431    }
432
433    /// Quiesce this session for later resumption, returning a lightweight
434    /// [`ParkedSession`] handle.
435    ///
436    /// Parking flushes dirty state to the store (a fresh-lease commit), drops
437    /// the live runtime and its plugin session, and hands back a cheap handle
438    /// the host can cache and later rebuild with
439    /// [`LashCore::resume`](crate::LashCore::resume). This is the
440    /// quiesce/handoff lever for webserver embedders that hold many idle
441    /// sessions: it bounds resident memory per session without deleting durable
442    /// state.
443    ///
444    /// Contract:
445    /// - **Persistent runtime required.** Parking flushes to the store, so a
446    ///   store-less session cannot be parked and returns an error. Use
447    ///   [`close`](Self::close) to tear down an ephemeral session.
448    /// - **Exclusive ownership required.** `park` consumes the session and drops
449    ///   the in-memory runtime, so it needs the sole live reference. A cloned
450    ///   [`LashSession`] or an in-flight turn holds another reference and makes
451    ///   `park` return [`EmbedError::SessionStillInUse`]. Because an executing
452    ///   turn holds such a reference, parking is effectively an *idle-session*
453    ///   operation: finish or [`cancel_running_turns`](Self::cancel_running_turns)
454    ///   first. The store commit itself does not observe an active turn; the
455    ///   exclusive-ownership guard is what makes mid-turn parking an explicit
456    ///   error rather than a silent partial flush.
457    pub async fn park(self) -> Result<ParkedSession> {
458        let runtime = self.into_owned_runtime()?;
459        // We now own the runtime exclusively; release the in-memory plugin
460        // session registration before flushing and dropping it.
461        runtime.unregister_plugin_session()?;
462        let parked = runtime.park().await?;
463        Ok(ParkedSession { inner: parked })
464    }
465
466    /// Consume the session and take sole ownership of the underlying runtime.
467    ///
468    /// Fails with [`EmbedError::SessionStillInUse`] when another live handle
469    /// (a cloned session or an in-flight turn) shares the runtime, so
470    /// consuming operations never proceed on a still-shared runtime.
471    fn into_owned_runtime(self) -> Result<LashRuntime> {
472        let LashSession { runtime, .. } = self;
473        // `writer()` clones the shared `Arc<Mutex<LashRuntime>>`; dropping the
474        // handle then leaves this clone as the sole strong reference iff no
475        // other handle exists, so `try_unwrap` doubles as the exclusive-owner
476        // check.
477        let writer = runtime.writer();
478        drop(runtime);
479        Arc::try_unwrap(writer)
480            .map(|mutex| mutex.into_inner())
481            .map_err(|_| EmbedError::SessionStillInUse)
482    }
483
484    pub fn session_id(&self) -> String {
485        self.runtime.observe().session_id().to_string()
486    }
487
488    pub fn policy_snapshot(&self) -> SessionPolicy {
489        self.runtime.observe().policy.clone()
490    }
491
492    pub fn observe(&self) -> ObservableSession {
493        ObservableSession {
494            runtime: self.runtime.clone(),
495        }
496    }
497
498    pub fn parent_session_id(&self) -> Option<&str> {
499        self.parent_session_id.as_deref()
500    }
501
502    pub fn effect_host(&self) -> Arc<dyn EffectHost> {
503        Arc::clone(&self.effect_host)
504    }
505
506    pub fn turn(&self, input: TurnInput) -> TurnBuilder {
507        TurnBuilder {
508            runtime: self.runtime.clone(),
509            effect_host: Arc::clone(&self.effect_host),
510            active_plugins: self.active_plugins.clone(),
511            input,
512            cancel: CancellationToken::new(),
513            cancels: self.turn_cancels.clone(),
514            protocol_turn_options: None,
515            provider: None,
516            turn_id: None,
517        }
518    }
519
520    pub fn queued_turn(&self) -> QueuedTurnBuilder {
521        QueuedTurnBuilder {
522            runtime: self.runtime.clone(),
523            effect_host: Arc::clone(&self.effect_host),
524            cancel: CancellationToken::new(),
525            cancels: self.turn_cancels.clone(),
526            batch_ids: Vec::new(),
527            drain_id: None,
528        }
529    }
530
531    /// Cancel every turn currently executing through this opened session
532    /// (including its clones) and report how many were signalled.
533    ///
534    /// This is the affordance behind a UI "stop" control: hold a clone of the
535    /// session wherever the stop arrives and call this, instead of threading a
536    /// [`CancellationToken`](crate::CancellationToken) into every turn call
537    /// ([`TurnBuilder::cancel`](crate::TurnBuilder::cancel) remains the
538    /// per-turn hook when you need one). A cancelled turn finishes with
539    /// `TurnOutcome::Stopped(TurnStop::Cancelled)` and commits like any other
540    /// turn; the session stays usable.
541    ///
542    /// Scope: turns started from this `LashSession` instance and its clones.
543    /// A handle opened separately for the same session id has its own
544    /// registry and is not reached.
545    pub fn cancel_running_turns(&self) -> usize {
546        self.turn_cancels.cancel_all()
547    }
548
549    pub fn admin(&self) -> SessionAdmin {
550        SessionAdmin {
551            runtime: self.runtime.clone(),
552        }
553    }
554
555    pub async fn configure(&self, patch: SessionConfigPatch) -> Result<()> {
556        self.admin().config().update(patch).await
557    }
558
559    pub fn tools(&self) -> ToolAdmin {
560        ToolAdmin::new(self.admin())
561    }
562
563    pub fn commands(&self) -> SessionCommandAdmin {
564        self.admin().commands()
565    }
566
567    pub fn triggers(&self) -> SessionTriggerAdmin {
568        self.admin().triggers()
569    }
570
571    pub fn processes(&self) -> SessionProcessAdmin {
572        SessionProcessAdmin::new(self.admin())
573    }
574
575    /// Refresh the session graph from any background process that signalled it
576    /// changed. This is the honest name for the former
577    /// `processes().await_all()` misnomer (ADR 0019 grill): a session-graph
578    /// resync, not a terminal wait on background work — wait on a process with
579    /// [`SessionProcessAdmin::await_output`]. It lives on the session surface
580    /// because it refreshes the session graph, not the global process registry.
581    pub async fn refresh_background_graph(&self) -> Result<()> {
582        self.admin().refresh_background_graph().await
583    }
584
585    pub fn plugin_operations(&self) -> PluginOperations {
586        PluginOperations {
587            control: self.admin(),
588        }
589    }
590
591    pub fn enqueue(&self, input: TurnInput) -> EnqueueTurnBuilder<'_> {
592        EnqueueTurnBuilder {
593            session: self,
594            input,
595            id: None,
596            ingress: TurnInputIngress::NextTurn,
597        }
598    }
599
600    /// Return all pending durable queued-work batches for this session.
601    ///
602    /// This is an admin/introspection view for non-user queued work such as
603    /// process wakes and session commands. User-visible model input is stored
604    /// separately as pending turn input and is exposed by
605    /// [`pending_turn_inputs`](Self::pending_turn_inputs).
606    pub async fn queued_work(&self) -> Result<Vec<QueuedWorkBatch>> {
607        let observation = self.runtime.observe();
608        let store = observation.queue_store.as_ref().ok_or_else(|| {
609            EmbedError::Runtime(lash_core::RuntimeError::new(
610                lash_core::RuntimeErrorCode::StoreCommitFailed,
611                "queued work inspection requires a persistent runtime store",
612            ))
613        })?;
614        store
615            .list_pending_queued_work(observation.session_id())
616            .await
617            .map_err(|err| {
618                EmbedError::Runtime(lash_core::RuntimeError::new(
619                    lash_core::RuntimeErrorCode::StoreCommitFailed,
620                    err.to_string(),
621                ))
622            })
623    }
624
625    pub async fn pending_turn_inputs(&self) -> Result<Vec<PendingTurnInput>> {
626        let observation = self.runtime.observe();
627        let store = observation.queue_store.as_ref().ok_or_else(|| {
628            EmbedError::Runtime(lash_core::RuntimeError::new(
629                lash_core::RuntimeErrorCode::StoreCommitFailed,
630                "pending turn input inspection requires a persistent runtime store",
631            ))
632        })?;
633        store
634            .list_pending_turn_inputs(observation.session_id())
635            .await
636            .map_err(|err| {
637                EmbedError::Runtime(lash_core::RuntimeError::new(
638                    lash_core::RuntimeErrorCode::StoreCommitFailed,
639                    err.to_string(),
640                ))
641            })
642    }
643
644    pub async fn cancel_pending_turn_input(
645        &self,
646        input_id: &str,
647    ) -> Result<PendingTurnInputCancelOutcome> {
648        let session_id = self.session_id();
649        self.runtime
650            .cancel_pending_turn_input(&session_id, input_id)
651            .await
652            .map_err(EmbedError::Runtime)
653    }
654
655    /// Atomically cancel a set of pending user inputs by runtime input id or
656    /// app source key.
657    ///
658    /// This is the app reconciliation path for explicit selections such as
659    /// "remove these pending drafts". Returned outcomes distinguish newly
660    /// cancelled input from input that was already claimed, completed,
661    /// cancelled, or missing.
662    pub async fn cancel_pending_turn_inputs(
663        &self,
664        targets: impl IntoIterator<Item = PendingTurnInputCancelTarget>,
665    ) -> Result<Vec<PendingTurnInputCancelResult>> {
666        let session_id = self.session_id();
667        let targets = targets.into_iter().collect::<Vec<_>>();
668        self.runtime
669            .cancel_pending_turn_inputs(&session_id, &targets)
670            .await
671            .map_err(EmbedError::Runtime)
672    }
673
674    /// Atomically cancel the same-session pending-input suffix from `anchor`.
675    ///
676    /// Apps that let users edit previously submitted product messages should
677    /// map the edited message to the stored pending-input `input_id` or
678    /// `source_key`, call this method, and only restore/edit drafts that return
679    /// [`PendingTurnInputCancelOutcome::Cancelled`]. Claimed or completed
680    /// inputs have already crossed the runtime boundary and should be treated
681    /// as reconciliation state, not local editable drafts.
682    pub async fn cancel_pending_turn_input_suffix(
683        &self,
684        anchor: PendingTurnInputCancelTarget,
685    ) -> Result<PendingTurnInputSuffixCancelOutcome> {
686        let session_id = self.session_id();
687        self.runtime
688            .cancel_pending_turn_input_suffix(&session_id, &anchor)
689            .await
690            .map_err(EmbedError::Runtime)
691    }
692
693    pub async fn cancel_queued_work_batch(
694        &self,
695        batch_id: &str,
696    ) -> Result<Option<QueuedWorkBatch>> {
697        let session_id = self.session_id();
698        self.runtime
699            .cancel_queued_work_batch(&session_id, batch_id)
700            .await
701            .map_err(EmbedError::Runtime)
702    }
703
704    /// Release a held queued-work claim without completing it, returning its
705    /// batches to the pending queue immediately.
706    ///
707    /// A host stopping an external queued-work driver mid-claim calls this
708    /// with the claims that driver still holds so the work becomes claimable
709    /// again at once instead of waiting out the claim's lease TTL.
710    pub async fn abandon_queued_work_claim(&self, claim: &QueuedWorkClaim) -> Result<()> {
711        self.runtime
712            .abandon_queued_work_claim(claim)
713            .await
714            .map_err(EmbedError::Runtime)
715    }
716
717    /// Release a held pending-turn-input claim without completing it, returning
718    /// its inputs to the pending queue immediately. The turn-input counterpart
719    /// of [`abandon_queued_work_claim`](Self::abandon_queued_work_claim).
720    pub async fn abandon_turn_input_claim(&self, claim: &TurnInputClaim) -> Result<()> {
721        self.runtime
722            .abandon_turn_input_claim(claim)
723            .await
724            .map_err(EmbedError::Runtime)
725    }
726
727    /// Cancel every outstanding durable wait for this session without deleting
728    /// the session.
729    ///
730    /// Each waiter receives a terminal [`Resolution::Cancelled`](crate::Resolution)
731    /// instead of hanging until an external completion arrives, and late
732    /// resolves observe that terminal. The session itself stays usable: new
733    /// durable waits registered afterwards behave normally, unlike the
734    /// tombstoning revocation [`LashCore::delete_session`](crate::LashCore::delete_session)
735    /// performs.
736    pub async fn revoke_durable_waits(&self) -> Result<()> {
737        let session_id = self.session_id();
738        self.effect_host
739            .cancel_await_events_for_session(&session_id)
740            .await
741            .map_err(EmbedError::Runtime)
742    }
743
744    /// Resolve once `batch_id` is no longer pending in the queue store —
745    /// drained by whoever runs queued work (a queued-work runner, a durable
746    /// worker, or another handle's [`queued_turn`](Self::queued_turn)) or
747    /// cancelled. This is the enqueue-and-observe side of the queue: the
748    /// caller never claims the work itself.
749    ///
750    /// Completion is read from the persistent queue store, so it observes
751    /// drains performed by other session handles and other processes alike.
752    /// There is no built-in deadline — nothing resolves if nothing drains the
753    /// queue, so bound it with `tokio::time::timeout` when the worker may be
754    /// unavailable. A batch id the store has never seen resolves immediately.
755    pub async fn await_queued_work_batch(&self, batch_id: &str) -> Result<()> {
756        let observation = self.runtime.observe();
757        let store = observation.queue_store.clone().ok_or_else(|| {
758            EmbedError::Runtime(lash_core::RuntimeError::new(
759                lash_core::RuntimeErrorCode::StoreCommitFailed,
760                "queued work inspection requires a persistent runtime store",
761            ))
762        })?;
763        let session_id = observation.session_id().to_string();
764        drop(observation);
765        let mut delay = std::time::Duration::from_millis(25);
766        loop {
767            let pending = store
768                .list_pending_queued_work(&session_id)
769                .await
770                .map_err(|err| {
771                    EmbedError::Runtime(lash_core::RuntimeError::new(
772                        lash_core::RuntimeErrorCode::StoreCommitFailed,
773                        err.to_string(),
774                    ))
775                })?;
776            if !pending.iter().any(|batch| batch.batch_id == batch_id) {
777                return Ok(());
778            }
779            tokio::time::sleep(delay).await;
780            delay = (delay * 2).min(std::time::Duration::from_millis(400));
781        }
782    }
783
784    pub fn read_view(&self) -> SessionReadView {
785        self.runtime.observe().read_view.clone()
786    }
787
788    pub fn usage_report(&self) -> SessionUsageReport {
789        self.runtime.observe().usage_report.clone()
790    }
791
792    pub async fn set_turn_phase_probe(
793        &self,
794        probe: Arc<dyn lash_core::runtime::RuntimeTurnPhaseProbe>,
795    ) {
796        let writer = self.runtime.writer();
797        let mut runtime = writer.lock().await;
798        runtime.set_turn_phase_probe(Arc::clone(&probe));
799        self.runtime.publish_from(&runtime);
800        if let Some(slot) = &self.process_phase_probe_slot {
801            let observation = self.runtime.observe();
802            slot.set_for_session(observation.session_id(), Arc::clone(&probe));
803            let current_frame = observation.persisted_state.current_agent_frame_id.as_str();
804            if !current_frame.is_empty() {
805                let scope = lash_core::SessionScope::for_agent_frame(
806                    observation.session_id(),
807                    current_frame,
808                );
809                slot.set_for_scope(&scope, probe);
810            }
811        }
812    }
813}
814
815#[derive(Clone)]
816pub struct ObservableSession {
817    pub(crate) runtime: RuntimeHandle,
818}
819
820impl ObservableSession {
821    fn snapshot(&self) -> Arc<RuntimeObservation> {
822        self.runtime.observe()
823    }
824
825    pub fn current_observation(&self) -> SessionObservation {
826        self.runtime.current_session_observation()
827    }
828
829    pub fn current_remote_observation(&self) -> RemoteSessionObservation {
830        RemoteSessionObservation::from_core(self.current_observation())
831    }
832
833    pub fn resume_from_cursor(&self, cursor: &SessionCursor) -> Result<SessionResume> {
834        self.runtime
835            .resume_session_observation(cursor)
836            .map_err(live_replay_error)
837    }
838
839    pub fn subscribe_from_cursor(
840        &self,
841        cursor: &SessionCursor,
842    ) -> Result<SessionObservationSubscription> {
843        self.runtime
844            .subscribe_session_observation(cursor)
845            .map_err(live_replay_error)
846    }
847
848    pub fn subscribe_from_remote_cursor(
849        &self,
850        cursor: &RemoteSessionCursor,
851    ) -> Result<RemoteSessionObservationSubscription> {
852        cursor.validate()?;
853        let cursor = lash_core::SessionCursor::try_from(cursor.clone())?;
854        match self.subscribe_from_cursor(&cursor)? {
855            SessionObservationSubscription::Subscribed(subscription) => {
856                Ok(RemoteSessionObservationSubscription::Subscribed(
857                    RemoteSessionObservationEventStream::new(subscription),
858                ))
859            }
860            SessionObservationSubscription::Gap { observation, gap } => {
861                Ok(RemoteSessionObservationSubscription::Gap {
862                    observation: observation.into(),
863                    gap: gap.into(),
864                })
865            }
866        }
867    }
868
869    /// Subscribe to session observation events and keep the subscription alive
870    /// across recoverable live-replay gaps.
871    ///
872    /// The returned stream yields [`SessionObservationStreamItem::Gap`] when
873    /// the cursor missed the bounded replay window. Callers should replace
874    /// their UI/projection from the included fresh observation, persist
875    /// `gap.latest_cursor`, and keep polling the same stream; it resubscribes
876    /// from that cursor internally.
877    pub fn subscribe_and_recover(&self, cursor: SessionCursor) -> SessionObservationStream {
878        SessionObservationStream {
879            observable: self.clone(),
880            cursor,
881            subscription: None,
882            done: false,
883        }
884    }
885
886    /// Subscribe to remote DTO session observation events and keep the
887    /// subscription alive across recoverable live-replay gaps.
888    pub fn subscribe_and_recover_remote(
889        &self,
890        cursor: RemoteSessionCursor,
891    ) -> Result<RemoteSessionObservationStream> {
892        cursor.validate()?;
893        let cursor = lash_core::SessionCursor::try_from(cursor)?;
894        Ok(RemoteSessionObservationStream {
895            inner: self.subscribe_and_recover(cursor),
896            next_sequence: 0,
897        })
898    }
899
900    pub fn session_id(&self) -> String {
901        self.snapshot().session_id().to_string()
902    }
903
904    pub fn policy_snapshot(&self) -> SessionPolicy {
905        self.snapshot().policy.clone()
906    }
907
908    pub fn read_view(&self) -> SessionReadView {
909        self.snapshot().read_view.clone()
910    }
911
912    pub fn usage_report(&self) -> SessionUsageReport {
913        self.snapshot().usage_report.clone()
914    }
915
916    pub fn tool_state(&self) -> Option<ToolState> {
917        self.snapshot().tool_state.clone()
918    }
919
920    pub fn active_tool_manifests(&self) -> Vec<ToolManifest> {
921        self.snapshot()
922            .tool_state
923            .as_ref()
924            .map(ToolState::tool_manifests)
925            .unwrap_or_default()
926    }
927
928    pub async fn list_process_handles(&self) -> Vec<ProcessHandleSummary> {
929        self.snapshot().list_process_handles().await
930    }
931
932    pub async fn list_all_process_handles(&self) -> Vec<ProcessHandleSummary> {
933        self.snapshot().list_all_process_handles().await
934    }
935
936    pub fn process_scope(&self) -> SessionScope {
937        self.snapshot().process_scope()
938    }
939}
940
941// A public streaming yield produced one item at a time by `Stream::poll_next`;
942// the variant-size spread is transient (never accumulated in a collection), so
943// boxing would only add a per-event heap allocation on the observation hot path
944// and force `*`-deref churn on every SDK consumer. The sibling
945// `RemoteSessionObservationStreamItem` keeps the same inline shape.
946#[allow(clippy::large_enum_variant)]
947#[derive(Clone, Debug)]
948pub enum SessionObservationStreamItem {
949    /// A replayed or live session observation event.
950    Event(SessionObservationEvent),
951    /// A recoverable replay gap with a fresh durable observation.
952    Gap {
953        observation: SessionObservation,
954        gap: LiveReplayGap,
955    },
956}
957
958pub enum RemoteSessionObservationSubscription {
959    Subscribed(RemoteSessionObservationEventStream),
960    Gap {
961        observation: RemoteSessionObservation,
962        gap: RemoteLiveReplayGap,
963    },
964}
965
966#[derive(Clone, Debug)]
967pub enum RemoteSessionObservationStreamItem {
968    /// A replayed or live session observation event encoded as remote DTOs.
969    Event(RemoteSessionObservationEvent),
970    /// A recoverable replay gap with a fresh remote observation snapshot.
971    Gap {
972        observation: RemoteSessionObservation,
973        gap: RemoteLiveReplayGap,
974    },
975}
976
977pub struct RemoteSessionObservationEventStream {
978    inner: lash_core::LiveReplaySubscription,
979    next_sequence: u64,
980}
981
982impl RemoteSessionObservationEventStream {
983    fn new(inner: lash_core::LiveReplaySubscription) -> Self {
984        Self {
985            inner,
986            next_sequence: 0,
987        }
988    }
989
990    pub async fn next_event(&mut self) -> Result<RemoteSessionObservationEvent> {
991        futures_util::future::poll_fn(|cx| Pin::new(&mut *self).poll_next(cx))
992            .await
993            .transpose()?
994            .ok_or_else(|| live_replay_error(LiveReplayStoreError::Closed))
995    }
996}
997
998impl Stream for RemoteSessionObservationEventStream {
999    type Item = Result<RemoteSessionObservationEvent>;
1000
1001    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
1002        match Pin::new(&mut self.inner).poll_next(cx) {
1003            Poll::Pending => Poll::Pending,
1004            Poll::Ready(Some(Ok(event))) => {
1005                let remote = RemoteSessionObservationEvent::from_core(self.next_sequence, event);
1006                self.next_sequence = self.next_sequence.saturating_add(1);
1007                Poll::Ready(Some(Ok(remote)))
1008            }
1009            Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(live_replay_error(err)))),
1010            Poll::Ready(None) => Poll::Ready(None),
1011        }
1012    }
1013}
1014
1015/// Remote DTO stream returned by [`ObservableSession::subscribe_and_recover_remote`].
1016pub struct RemoteSessionObservationStream {
1017    inner: SessionObservationStream,
1018    next_sequence: u64,
1019}
1020
1021impl RemoteSessionObservationStream {
1022    pub fn cursor(&self) -> RemoteSessionCursor {
1023        RemoteSessionCursor::from(self.inner.cursor())
1024    }
1025}
1026
1027impl Stream for RemoteSessionObservationStream {
1028    type Item = Result<RemoteSessionObservationStreamItem>;
1029
1030    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
1031        match Pin::new(&mut self.inner).poll_next(cx) {
1032            Poll::Pending => Poll::Pending,
1033            Poll::Ready(Some(Ok(SessionObservationStreamItem::Event(event)))) => {
1034                let remote = RemoteSessionObservationEvent::from_core(self.next_sequence, event);
1035                self.next_sequence = self.next_sequence.saturating_add(1);
1036                Poll::Ready(Some(Ok(RemoteSessionObservationStreamItem::Event(remote))))
1037            }
1038            Poll::Ready(Some(Ok(SessionObservationStreamItem::Gap { observation, gap }))) => {
1039                Poll::Ready(Some(Ok(RemoteSessionObservationStreamItem::Gap {
1040                    observation: observation.into(),
1041                    gap: gap.into(),
1042                })))
1043            }
1044            Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(err))),
1045            Poll::Ready(None) => Poll::Ready(None),
1046        }
1047    }
1048}
1049
1050/// Stream returned by [`ObservableSession::subscribe_and_recover`].
1051pub struct SessionObservationStream {
1052    observable: ObservableSession,
1053    cursor: SessionCursor,
1054    subscription: Option<lash_core::LiveReplaySubscription>,
1055    done: bool,
1056}
1057
1058impl SessionObservationStream {
1059    pub fn cursor(&self) -> &SessionCursor {
1060        &self.cursor
1061    }
1062}
1063
1064impl Stream for SessionObservationStream {
1065    type Item = Result<SessionObservationStreamItem>;
1066
1067    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
1068        loop {
1069            if self.done {
1070                return Poll::Ready(None);
1071            }
1072            if self.subscription.is_none() {
1073                match self.observable.subscribe_from_cursor(&self.cursor) {
1074                    Ok(SessionObservationSubscription::Subscribed(subscription)) => {
1075                        self.subscription = Some(subscription);
1076                    }
1077                    Ok(SessionObservationSubscription::Gap { observation, gap }) => {
1078                        self.cursor = gap.latest_cursor.clone();
1079                        return Poll::Ready(Some(Ok(SessionObservationStreamItem::Gap {
1080                            observation,
1081                            gap,
1082                        })));
1083                    }
1084                    Err(err) => {
1085                        self.done = true;
1086                        return Poll::Ready(Some(Err(err)));
1087                    }
1088                }
1089            }
1090
1091            let Some(subscription) = self.subscription.as_mut() else {
1092                continue;
1093            };
1094            match Pin::new(subscription).poll_next(cx) {
1095                Poll::Pending => return Poll::Pending,
1096                Poll::Ready(Some(Ok(event))) => {
1097                    self.cursor = event.cursor.clone();
1098                    return Poll::Ready(Some(Ok(SessionObservationStreamItem::Event(event))));
1099                }
1100                Poll::Ready(Some(Err(LiveReplayStoreError::SubscriberLagged(_)))) => {
1101                    self.subscription = None;
1102                    continue;
1103                }
1104                Poll::Ready(Some(Err(err))) => {
1105                    self.done = true;
1106                    return Poll::Ready(Some(Err(live_replay_error(err))));
1107                }
1108                Poll::Ready(None) => {
1109                    self.done = true;
1110                    return Poll::Ready(None);
1111                }
1112            }
1113        }
1114    }
1115}
1116
1117fn live_replay_error(err: lash_core::LiveReplayStoreError) -> EmbedError {
1118    EmbedError::Runtime(lash_core::RuntimeError::new(
1119        RuntimeErrorCode::Other("live_replay".to_string()),
1120        err.to_string(),
1121    ))
1122}
1123
1124pub struct EnqueueTurnBuilder<'a> {
1125    session: &'a LashSession,
1126    input: TurnInput,
1127    id: Option<String>,
1128    ingress: TurnInputIngress,
1129}
1130
1131impl<'a> EnqueueTurnBuilder<'a> {
1132    pub fn id(mut self, id: impl Into<String>) -> Self {
1133        self.id = Some(id.into());
1134        self
1135    }
1136
1137    pub fn ingress(mut self, ingress: TurnInputIngress) -> Self {
1138        self.ingress = ingress;
1139        self
1140    }
1141
1142    pub async fn send(self) -> Result<PendingTurnInput> {
1143        let source_key = self.id.map(|id| format!("host:{id}"));
1144        self.session
1145            .runtime
1146            .enqueue_turn_input(self.input, self.ingress, source_key)
1147            .await
1148            .map_err(EmbedError::Runtime)
1149    }
1150}
1151
1152impl<'a> std::future::IntoFuture for EnqueueTurnBuilder<'a> {
1153    type Output = Result<PendingTurnInput>;
1154    type IntoFuture =
1155        std::pin::Pin<Box<dyn std::future::Future<Output = Result<PendingTurnInput>> + 'a>>;
1156
1157    fn into_future(self) -> Self::IntoFuture {
1158        Box::pin(self.send())
1159    }
1160}