Skip to main content

lash_core/runtime/
builder.rs

1use std::sync::Arc;
2
3use crate::plugin::{PluginFactory, PluginHost, PluginSession};
4use crate::{
5    EffectHost, EmbeddedRuntimeHost, LashRuntime, PluginStack, ProcessRegistry, Residency,
6    RuntimeHostConfig, RuntimePersistence, RuntimeSessionState, SessionError, SessionPolicy,
7    SessionStoreFactory, TerminationPolicy,
8};
9
10enum PluginSource {
11    Host(PluginHost),
12    Session(Arc<PluginSession>),
13}
14
15pub(super) fn lashlang_abilities_for_process_registry(
16    mut abilities: lashlang::LashlangAbilities,
17    process_registry_available: bool,
18) -> lashlang::LashlangAbilities {
19    abilities = abilities.with_sleep();
20    if process_registry_available {
21        abilities.with_processes().with_process_signals()
22    } else {
23        abilities.processes = false;
24        abilities.process_signals = false;
25        abilities
26    }
27}
28
29pub struct EmbeddedRuntimeBuilder {
30    session_id: Option<String>,
31    policy: Option<SessionPolicy>,
32    initial_state: Option<RuntimeSessionState>,
33    plugin_source: PluginSource,
34    core: RuntimeHostConfig,
35    session_store_factory: Option<Arc<dyn SessionStoreFactory>>,
36    host_event_store: Option<Arc<dyn crate::HostEventStore>>,
37    store: Option<Arc<dyn RuntimePersistence>>,
38    process_registry: Option<Arc<dyn ProcessRegistry>>,
39    residency: Residency,
40}
41
42impl Default for EmbeddedRuntimeBuilder {
43    fn default() -> Self {
44        Self {
45            session_id: None,
46            policy: None,
47            initial_state: None,
48            plugin_source: PluginSource::Host(PluginHost::empty()),
49            // `RuntimeHostConfig` has no `Default`; start from an explicitly
50            // named in-memory core. Callers that need durable stores override
51            // it with `with_runtime_host`.
52            core: RuntimeHostConfig::in_memory(),
53            session_store_factory: None,
54            host_event_store: Some(Arc::new(crate::InMemoryHostEventStore::default())),
55            store: None,
56            process_registry: None,
57            residency: Residency::default(),
58        }
59    }
60}
61
62impl EmbeddedRuntimeBuilder {
63    pub fn new() -> Self {
64        Self::default()
65    }
66
67    pub fn session_id(&self) -> Option<&str> {
68        self.session_id.as_deref()
69    }
70
71    pub fn policy(&self) -> Option<&SessionPolicy> {
72        self.policy.as_ref()
73    }
74
75    pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
76        self.session_id = Some(session_id.into());
77        self
78    }
79
80    pub fn with_policy(mut self, policy: SessionPolicy) -> Self {
81        self.policy = Some(policy);
82        self
83    }
84
85    pub fn with_initial_state(mut self, state: RuntimeSessionState) -> Self {
86        self.initial_state = Some(state);
87        self
88    }
89
90    pub fn with_plugin_host(mut self, plugin_host: PluginHost) -> Self {
91        self.plugin_source = PluginSource::Host(plugin_host);
92        self
93    }
94
95    pub fn with_plugin_session(mut self, plugin_session: Arc<PluginSession>) -> Self {
96        self.plugin_source = PluginSource::Session(plugin_session);
97        self
98    }
99
100    pub fn with_plugin_factories(mut self, factories: Vec<Arc<dyn PluginFactory>>) -> Self {
101        let host = PluginHost::new(factories);
102        self.plugin_source = PluginSource::Host(host);
103        self
104    }
105
106    pub fn with_plugin_stack(self, stack: PluginStack) -> Self {
107        self.with_plugin_factories(stack.into_factories())
108    }
109
110    pub fn with_runtime_host(mut self, core: RuntimeHostConfig) -> Self {
111        self.core = core;
112        self
113    }
114
115    pub fn with_attachment_store(
116        mut self,
117        attachment_store: Arc<dyn crate::AttachmentStore>,
118    ) -> Self {
119        self.core.durability.attachment_store = attachment_store;
120        self
121    }
122
123    pub fn with_prompt_template(mut self, prompt_template: crate::PromptTemplate) -> Self {
124        self.core.prompt.prompt.template = Some(prompt_template);
125        self
126    }
127
128    pub fn with_prompt_contribution(mut self, contribution: crate::PromptContribution) -> Self {
129        self.core.prompt.prompt.add_contribution(contribution);
130        self
131    }
132
133    pub fn with_replaced_prompt_slot(
134        mut self,
135        slot: crate::PromptSlot,
136        contributions: impl IntoIterator<Item = crate::PromptContribution>,
137    ) -> Self {
138        self.core.prompt.prompt.replace_slot(slot, contributions);
139        self
140    }
141
142    pub fn with_cleared_prompt_slot(mut self, slot: crate::PromptSlot) -> Self {
143        self.core.prompt.prompt.clear_slot(slot);
144        self
145    }
146
147    pub fn with_prompt_layer(mut self, prompt: crate::PromptLayer) -> Self {
148        self.core.prompt.prompt = prompt;
149        self
150    }
151
152    pub fn with_trace_sink(mut self, sink: Option<Arc<dyn lash_trace::TraceSink>>) -> Self {
153        self.core.tracing.trace_sink = sink;
154        self
155    }
156
157    pub fn with_lashlang_execution_sink(
158        mut self,
159        sink: Option<Arc<dyn lash_trace::TraceSink>>,
160    ) -> Self {
161        self.core.tracing.lashlang_execution_sink = sink;
162        self
163    }
164
165    pub fn with_lashlang_execution_jsonl_path(mut self, path: Option<std::path::PathBuf>) -> Self {
166        self.core.tracing.lashlang_execution_sink = path.map(|path| {
167            Arc::new(lash_trace::JsonlTraceSink::new(path)) as Arc<dyn lash_trace::TraceSink>
168        });
169        self
170    }
171
172    pub fn with_trace_level(mut self, level: lash_trace::TraceLevel) -> Self {
173        self.core.tracing.trace_level = level;
174        self
175    }
176
177    pub fn with_trace_context(mut self, context: lash_trace::TraceContext) -> Self {
178        self.core.tracing.trace_context = context;
179        self
180    }
181
182    pub fn with_termination(mut self, termination: TerminationPolicy) -> Self {
183        self.core.control.termination = termination;
184        self
185    }
186
187    pub fn with_effect_host(mut self, effect_host: Arc<dyn EffectHost>) -> Self {
188        self.core.control.effect_host = effect_host;
189        self
190    }
191
192    pub fn with_provider_resolver(
193        mut self,
194        provider_resolver: Arc<dyn crate::RuntimeProviderResolver>,
195    ) -> Self {
196        self.core.providers.provider_resolver = provider_resolver;
197        self
198    }
199
200    pub fn with_session_store_factory(
201        mut self,
202        session_store_factory: Arc<dyn SessionStoreFactory>,
203    ) -> Self {
204        self.session_store_factory = Some(session_store_factory);
205        self
206    }
207
208    pub fn with_host_event_store(mut self, store: Arc<dyn crate::HostEventStore>) -> Self {
209        self.host_event_store = Some(store);
210        self
211    }
212
213    pub fn with_store(mut self, store: Arc<dyn RuntimePersistence>) -> Self {
214        self.store = Some(store);
215        self
216    }
217
218    pub fn with_process_registry(mut self, process_registry: Arc<dyn ProcessRegistry>) -> Self {
219        self.process_registry = Some(process_registry);
220        if let PluginSource::Host(host) = &mut self.plugin_source {
221            let abilities =
222                lashlang_abilities_for_process_registry(host.lashlang_abilities(), true);
223            *host = host.clone().with_lashlang_abilities(abilities);
224        }
225        self
226    }
227
228    /// Trim a rebuilt session's resident graph to match the host's residency.
229    ///
230    /// Defaults to [`Residency::KeepAll`]. Setting [`Residency::ActivePathOnly`]
231    /// makes a rebuilt runtime (e.g. a durable worker reconstructing a session to
232    /// run a background process) keep only the active path resident, matching the
233    /// live runtime's behavior instead of silently retaining the full graph.
234    pub fn with_residency(mut self, residency: Residency) -> Self {
235        self.residency = residency;
236        self
237    }
238
239    fn resolve_state_from_defaults(&self) -> RuntimeSessionState {
240        let mut state = self.initial_state.clone().unwrap_or_default();
241        if let Some(session_id) = &self.session_id {
242            state.session_id = session_id.clone();
243        }
244        if let Some(policy) = &self.policy {
245            state.policy = policy.clone();
246        }
247        state
248    }
249
250    async fn resolve_state(&self) -> Result<RuntimeSessionState, SessionError> {
251        if let Some(state) = &self.initial_state {
252            return Ok({
253                let mut state = state.clone();
254                if let Some(session_id) = &self.session_id {
255                    state.session_id = session_id.clone();
256                }
257                if let Some(policy) = &self.policy {
258                    let recorded_provider_id = state.policy.recorded_provider_id().to_string();
259                    state.policy.provider_id = recorded_provider_id;
260                    state.policy.session_id = policy.session_id.clone();
261                    if state.policy.model.id.trim().is_empty() {
262                        state.policy.model = policy.model.clone();
263                    }
264                }
265                state
266            });
267        }
268        if let Some(store) = &self.store {
269            if let Some(mut state) = crate::store::load_persisted_session_state(store.as_ref())
270                .await
271                .map_err(|err| SessionError::Protocol(format!("failed to load store: {err}")))?
272            {
273                if let Some(session_id) = &self.session_id
274                    && &state.session_id != session_id
275                {
276                    return Err(SessionError::Protocol(format!(
277                        "store is bound to session `{}` but builder requested `{session_id}`",
278                        state.session_id
279                    )));
280                }
281                if let Some(policy) = &self.policy {
282                    let recorded_provider_id = state.policy.recorded_provider_id().to_string();
283                    state.policy.provider_id = recorded_provider_id;
284                    state.policy.session_id = policy.session_id.clone();
285                    if state.policy.model.id.trim().is_empty() {
286                        state.policy.model = policy.model.clone();
287                    }
288                }
289                return Ok(state);
290            }
291            let mut state = self.resolve_state_from_defaults();
292            if let Some(policy) = &self.policy {
293                state.policy = policy.clone();
294            }
295            return Ok(state);
296        }
297        Ok(self.resolve_state_from_defaults())
298    }
299
300    fn resolve_plugins(
301        &self,
302        state: &RuntimeSessionState,
303    ) -> Result<Arc<PluginSession>, SessionError> {
304        match &self.plugin_source {
305            PluginSource::Session(session) => Ok(Arc::clone(session)),
306            PluginSource::Host(host) => host
307                .clone()
308                .with_lashlang_abilities(lashlang_abilities_for_process_registry(
309                    host.lashlang_abilities(),
310                    self.process_registry.is_some(),
311                ))
312                .isolated_registry()
313                .build_session(state.session_id.clone(), None)
314                .map_err(|err| SessionError::Protocol(err.to_string())),
315        }
316    }
317
318    pub async fn build(self) -> Result<LashRuntime, SessionError> {
319        let state = self.resolve_state().await?;
320        let plugins = self.resolve_plugins(&state)?;
321        let embedded_host = EmbeddedRuntimeHost::new(self.core)
322            .with_session_store_factory_option(self.session_store_factory.clone())
323            .with_host_event_store_option(self.host_event_store.clone());
324        // `assemble_runtime` owns the (store, registry) wiring + residency so the
325        // worker rebuild cannot drift from the live open path.
326        LashRuntime::assemble_runtime(
327            state.policy.clone(),
328            embedded_host,
329            plugins,
330            self.store,
331            self.process_registry,
332            state,
333            self.residency,
334        )
335        .await
336    }
337
338    pub async fn build_ephemeral(mut self) -> Result<LashRuntime, SessionError> {
339        self.store = None;
340        self.build().await
341    }
342
343    pub async fn build_persistent(
344        mut self,
345        store: Arc<dyn RuntimePersistence>,
346    ) -> Result<LashRuntime, SessionError> {
347        self.store = Some(store);
348        self.build().await
349    }
350
351    pub async fn build_background_persistent(
352        mut self,
353        store: Arc<dyn RuntimePersistence>,
354        process_registry: Arc<dyn ProcessRegistry>,
355    ) -> Result<LashRuntime, SessionError> {
356        self.store = Some(store);
357        self = self.with_process_registry(process_registry);
358        self.build().await
359    }
360}
361
362impl LashRuntime {
363    pub fn builder() -> EmbeddedRuntimeBuilder {
364        EmbeddedRuntimeBuilder::new()
365    }
366}
367
368trait EmbeddedRuntimeHostExt {
369    fn with_session_store_factory_option(
370        self,
371        session_store_factory: Option<Arc<dyn SessionStoreFactory>>,
372    ) -> Self;
373
374    fn with_host_event_store_option(
375        self,
376        host_event_store: Option<Arc<dyn crate::HostEventStore>>,
377    ) -> Self;
378}
379
380impl EmbeddedRuntimeHostExt for EmbeddedRuntimeHost {
381    fn with_session_store_factory_option(
382        mut self,
383        session_store_factory: Option<Arc<dyn SessionStoreFactory>>,
384    ) -> Self {
385        self.session_store_factory = session_store_factory;
386        self
387    }
388
389    fn with_host_event_store_option(
390        mut self,
391        host_event_store: Option<Arc<dyn crate::HostEventStore>>,
392    ) -> Self {
393        self.host_event_store = host_event_store;
394        self
395    }
396}