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 pub(crate) plugin_options: PluginOptions,
32}
33
34impl SessionBuilder {
35 pub fn plugin_options(mut self, plugin_options: PluginOptions) -> Self {
37 self.plugin_options = plugin_options;
38 self
39 }
40
41 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 pub fn session_execution_owner(mut self, owner: lash_core::LeaseOwnerIdentity) -> Self {
76 self.session_execution_owner = Some(owner);
77 self
78 }
79
80 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 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 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 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
375pub struct ParkedSession {
388 pub(crate) inner: lash_core::ParkedSession,
389}
390
391impl ParkedSession {
392 pub fn session_id(&self) -> &str {
395 self.inner.session_id()
396 }
397}
398
399impl LashSession {
400 pub async fn close(self) -> Result<()> {
418 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 runtime.park().await?;
428 }
429 Ok(())
431 }
432
433 pub async fn park(self) -> Result<ParkedSession> {
458 let runtime = self.into_owned_runtime()?;
459 runtime.unregister_plugin_session()?;
462 let parked = runtime.park().await?;
463 Ok(ParkedSession { inner: parked })
464 }
465
466 fn into_owned_runtime(self) -> Result<LashRuntime> {
472 let LashSession { runtime, .. } = self;
473 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 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 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 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 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 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 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 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 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 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 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 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#[allow(clippy::large_enum_variant)]
947#[derive(Clone, Debug)]
948pub enum SessionObservationStreamItem {
949 Event(SessionObservationEvent),
951 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 Event(RemoteSessionObservationEvent),
970 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
1015pub 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
1050pub 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}