Skip to main content

lash/
core.rs

1use crate::support::*;
2use lash_core::runtime::{
3    ProcessCommand, ProcessEffectOutcome, RuntimeEffectCommand, RuntimeEffectEnvelope,
4    RuntimeEffectKind, RuntimeEffectLocalExecutor, RuntimeEffectOutcome, RuntimeInvocation,
5    RuntimeScope,
6};
7
8#[derive(Clone)]
9pub struct LashCore {
10    pub(crate) env: RuntimeEnvironment,
11    pub(crate) policy: SessionPolicy,
12    pub(crate) protocol_factory: Option<Arc<dyn PluginFactory>>,
13    pub(crate) store_factory: Option<Arc<dyn SessionStoreFactory>>,
14    pub(crate) plugin_factories: Arc<Vec<Arc<dyn PluginFactory>>>,
15    pub(crate) provider: Option<ProviderHandle>,
16    pub(crate) live_replay_store: Arc<dyn LiveReplayStore>,
17    /// Whether this deployment has process lifecycle available (a process
18    /// registry is wired). Threaded to every plugin host so core can install the
19    /// same plugin-contributed process engines when it rebuilds a runtime.
20    pub(crate) process_lifecycle_available: bool,
21    /// Shared resolution of host-owned work drivers. Shared across `LashCore`
22    /// clones so inline process and queued drivers are constructed at most once.
23    pub(crate) work_driver: Arc<InlineWorkDriverSlot>,
24}
25
26/// How a [`LashCore`] resolves its process work driver, decided at `build()`
27/// and shared across clones.
28pub(crate) enum ProcessWorkDriverSetup {
29    /// No process registry is wired; there is nothing to run.
30    None,
31    /// Lazily construct the default inline process driver on first
32    /// `session().open()`. A store factory is required to build the config (the
33    /// worker rebuilds a session runtime per process); a registry with no store
34    /// factory is rejected at build with
35    /// [`EmbedError::ProcessRegistryRequiresStoreFactory`].
36    LazyDefault {
37        config: Box<DurableProcessWorkerConfig>,
38    },
39    /// The host wired an external driver.
40    External { driver: ProcessWorkDriver },
41}
42
43#[derive(Clone, Default)]
44pub(crate) enum ProcessWorkSource {
45    #[default]
46    None,
47    Inline {
48        registry: Arc<dyn ProcessRegistry>,
49        hub: Option<lash_core::ProcessChangeHub>,
50    },
51    External(ProcessWorkDriver),
52}
53
54impl ProcessWorkSource {
55    fn process_registry(&self) -> Option<Arc<dyn ProcessRegistry>> {
56        match self {
57            Self::None => None,
58            Self::Inline { registry, .. } => Some(Arc::clone(registry)),
59            Self::External(driver) => Some(driver.process_registry()),
60        }
61    }
62
63    fn has_registry(&self) -> bool {
64        !matches!(self, Self::None)
65    }
66
67    fn watched(self, sink: Option<Arc<dyn lash_core::ProcessEventSink>>) -> Self {
68        match self {
69            Self::Inline {
70                registry,
71                hub: None,
72            } => {
73                let (registry, hub) = lash_core::watch_process_registry_with_sink(registry, sink);
74                Self::Inline {
75                    registry,
76                    hub: Some(hub),
77                }
78            }
79            // An external driver was wrapped by its host, which installs any
80            // sink through the driver constructor; the inline sink does not
81            // apply here. Already-watched inline sources keep their wrap.
82            other => other,
83        }
84    }
85}
86
87#[derive(Clone, Default)]
88pub(crate) enum QueuedWorkSource {
89    None,
90    #[default]
91    LazyDefault,
92    External(QueuedWorkDriver),
93}
94
95pub(crate) enum QueuedWorkDriverSetup {
96    None,
97    LazyDefault {
98        config: Arc<InlineQueuedWorkRunConfig>,
99    },
100    External {
101        driver: QueuedWorkDriver,
102    },
103}
104
105pub(crate) struct InlineWorkDriverSetup {
106    process: ProcessWorkDriverSetup,
107    queued: QueuedWorkDriverSetup,
108}
109
110#[derive(Clone, Default)]
111pub(crate) struct ResolvedWorkDrivers {
112    pub(crate) process: Option<ProcessWorkDriver>,
113    pub(crate) queued: Option<QueuedWorkDriver>,
114    pub(crate) drive_process_on_open: bool,
115}
116
117/// Shared, lazily-initialized host-work state for a [`LashCore`].
118///
119/// The once-guard ([`tokio::sync::OnceCell`]) constructs inline drivers exactly
120/// once across `LashCore` clones, on the first `session().open()` or admin path
121/// that needs them.
122pub(crate) struct InlineWorkDriverSlot {
123    setup: InlineWorkDriverSetup,
124    drivers: tokio::sync::OnceCell<ResolvedWorkDrivers>,
125    phase_probe_slot: Option<lash_core::runtime::RuntimeTurnPhaseProbeSlot>,
126}
127
128impl InlineWorkDriverSlot {
129    fn new(setup: InlineWorkDriverSetup) -> Self {
130        let phase_probe_slot = match &setup.process {
131            ProcessWorkDriverSetup::LazyDefault { config } => {
132                Some(config.turn_phase_probe_slot.clone())
133            }
134            ProcessWorkDriverSetup::None | ProcessWorkDriverSetup::External { .. } => None,
135        };
136        Self {
137            setup,
138            drivers: tokio::sync::OnceCell::new(),
139            phase_probe_slot,
140        }
141    }
142
143    /// Resolve host work drivers for a session host. Idempotent: the once-guard
144    /// ensures inline drivers are constructed once.
145    pub(crate) async fn drivers(&self) -> ResolvedWorkDrivers {
146        self.drivers
147            .get_or_init(|| async {
148                let queued = match &self.setup.queued {
149                    QueuedWorkDriverSetup::None => None,
150                    QueuedWorkDriverSetup::External { driver } => Some(driver.clone()),
151                    QueuedWorkDriverSetup::LazyDefault { config } => Some(QueuedWorkDriver::new(
152                        Arc::new(InlineQueuedWorkRunHandle::new(Arc::clone(config))),
153                    )),
154                };
155                let (process, drive_process_on_open) = match &self.setup.process {
156                    ProcessWorkDriverSetup::None => (None, false),
157                    ProcessWorkDriverSetup::External { driver } => (Some(driver.clone()), false),
158                    ProcessWorkDriverSetup::LazyDefault { config } => {
159                        let mut config = (**config).clone();
160                        if let Some(driver) = queued.clone() {
161                            config = config.with_queued_work_driver(driver);
162                        }
163                        let registry = Arc::clone(&config.process_registry);
164                        let hub = config.process_change_hub.clone();
165                        let worker = DurableProcessWorker::new(config);
166                        let driver = if let Some(hub) = hub {
167                            ProcessWorkDriver::from_watched(
168                                registry,
169                                hub,
170                                Arc::new(lash_core::InlineProcessRunHandle::new(worker)),
171                            )
172                        } else {
173                            ProcessWorkDriver::inline(registry, worker)
174                        };
175                        (Some(driver), true)
176                    }
177                };
178                ResolvedWorkDrivers {
179                    process,
180                    queued,
181                    drive_process_on_open,
182                }
183            })
184            .await
185            .clone()
186    }
187
188    pub(crate) fn phase_probe_slot(&self) -> Option<lash_core::runtime::RuntimeTurnPhaseProbeSlot> {
189        self.phase_probe_slot.clone()
190    }
191
192    fn configured_process_work_driver(&self) -> Option<ProcessWorkDriver> {
193        match &self.setup.process {
194            ProcessWorkDriverSetup::External { driver } => Some(driver.clone()),
195            ProcessWorkDriverSetup::None | ProcessWorkDriverSetup::LazyDefault { .. } => None,
196        }
197    }
198
199    fn configured_queued_work_driver(&self) -> Option<QueuedWorkDriver> {
200        match &self.setup.queued {
201            QueuedWorkDriverSetup::External { driver } => Some(driver.clone()),
202            QueuedWorkDriverSetup::None | QueuedWorkDriverSetup::LazyDefault { .. } => None,
203        }
204    }
205}
206
207pub(crate) struct InlineQueuedWorkRunConfig {
208    env: RuntimeEnvironment,
209    policy: SessionPolicy,
210    protocol_factory: Option<Arc<dyn PluginFactory>>,
211    plugin_factories: Arc<Vec<Arc<dyn PluginFactory>>>,
212    store_factory: Arc<dyn SessionStoreFactory>,
213    live_replay_store: Arc<dyn LiveReplayStore>,
214    process_lifecycle_available: bool,
215}
216
217impl InlineQueuedWorkRunConfig {
218    fn new(
219        env: RuntimeEnvironment,
220        policy: SessionPolicy,
221        protocol_factory: Option<Arc<dyn PluginFactory>>,
222        plugin_factories: Arc<Vec<Arc<dyn PluginFactory>>>,
223        store_factory: Arc<dyn SessionStoreFactory>,
224        live_replay_store: Arc<dyn LiveReplayStore>,
225        process_lifecycle_available: bool,
226    ) -> Self {
227        Self {
228            env,
229            policy,
230            protocol_factory,
231            plugin_factories,
232            store_factory,
233            live_replay_store,
234            process_lifecycle_available,
235        }
236    }
237}
238
239struct InlineQueuedWorkRunHandle {
240    config: Arc<InlineQueuedWorkRunConfig>,
241}
242
243impl InlineQueuedWorkRunHandle {
244    fn new(config: Arc<InlineQueuedWorkRunConfig>) -> Self {
245        Self { config }
246    }
247}
248
249#[async_trait]
250impl QueuedWorkRunHandle for InlineQueuedWorkRunHandle {
251    async fn run_queued_work(
252        &self,
253        request: QueuedWorkRunRequest,
254    ) -> std::result::Result<(), lash_core::PluginError> {
255        let Some(session_id) = request.session_id else {
256            return Ok(());
257        };
258        let reason = request.reason;
259        let mut policy = self.config.policy.clone();
260        policy.session_id = Some(session_id.clone());
261        let store = self
262            .config
263            .store_factory
264            .create_store(&SessionStoreCreateRequest {
265                session_id: session_id.clone(),
266                relation: SessionRelation::default(),
267                policy: policy.clone(),
268            })
269            .await
270            .map_err(lash_core::PluginError::Session)?;
271        let state = crate::session::load_state_for_residency(
272            self.config.env.residency,
273            &session_id,
274            &policy,
275            store.as_ref(),
276        )
277        .await
278        .map_err(|err| lash_core::PluginError::Session(err.to_string()))?;
279        let plugin_host = build_plugin_host(
280            self.config.protocol_factory.as_ref(),
281            self.config.plugin_factories.as_ref(),
282            Vec::new(),
283        )
284        .map_err(|err| lash_core::PluginError::Session(err.to_string()))?;
285        let mut env = self.config.env.clone();
286        env.core = plugin_host
287            .install_process_engine_contributions(
288                env.core.clone(),
289                self.config.process_lifecycle_available,
290            )
291            .map_err(|err| lash_core::PluginError::Session(err.to_string()))?;
292        env.plugin_host = Some(Arc::new(plugin_host));
293        let effect_host = Arc::clone(&env.core.control.effect_host);
294        let runtime = LashRuntime::from_environment(&env, policy, state, Some(store))
295            .await
296            .map_err(|err| lash_core::PluginError::Session(err.to_string()))?;
297        let handle = RuntimeHandle::with_live_replay_store(
298            runtime,
299            Arc::clone(&self.config.live_replay_store),
300        );
301        let scope = lash_core::ExecutionScope::queue_drain(session_id, reason);
302        let scoped = effect_host
303            .scoped(scope)
304            .map_err(|err| lash_core::PluginError::Session(err.to_string()))?;
305        crate::turn::stream_next_queued_prepared_turn(
306            &handle,
307            crate::turn::TurnSinks::default(),
308            scoped,
309            CancellationToken::new(),
310            lash_core::TurnCancelOriginHint::default(),
311            &[],
312        )
313        .await
314        .map_err(|err| lash_core::PluginError::Session(err.to_string()))?;
315        Ok(())
316    }
317}
318
319#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
320pub struct SessionDeleteReport {
321    pub session_id: String,
322    pub process: Option<lash_core::ProcessSessionDeleteReport>,
323}
324
325impl LashCore {
326    pub fn builder() -> LashCoreBuilder {
327        LashCoreBuilder::default()
328    }
329
330    /// Sugar entry point: a [`LashCoreBuilder`] pre-seeded with the standard
331    /// protocol plugin and the default runtime plugin stack.
332    pub fn standard_builder() -> LashCoreBuilder {
333        LashCore::builder()
334            .protocol_plugin(Arc::new(
335                lash_protocol_standard::StandardProtocolPluginFactory::new(),
336            ))
337            .plugins(default_runtime_stack())
338    }
339
340    /// Sugar entry point: a [`LashCoreBuilder`] pre-seeded with a
341    /// host-configured RLM protocol factory and the default runtime plugin
342    /// stack.
343    ///
344    /// The host configures the factory (projection resolver, deferred tool
345    /// resolver, execution sink/jsonl path, and — required at construction — the
346    /// Lashlang artifact store) before passing it in.
347    #[cfg(feature = "rlm")]
348    pub fn rlm_builder(factory: crate::rlm::RlmProtocolPluginFactory) -> LashCoreBuilder {
349        LashCore::builder()
350            .protocol_plugin(Arc::new(factory))
351            .plugins(default_runtime_stack())
352    }
353
354    pub fn session(&self, session_id: impl Into<String>) -> SessionBuilder {
355        SessionBuilder {
356            core: self.clone(),
357            session_id: session_id.into(),
358            spec: SessionSpec::inherit(),
359            parent_session_id: None,
360            session_execution_owner: None,
361            store: None,
362            provider: None,
363            active_plugins: Vec::new(),
364            plugin_factories: Vec::new(),
365            plugin_options: PluginOptions::default(),
366        }
367    }
368
369    /// Rebuild a live session from a [`ParkedSession`](crate::ParkedSession)
370    /// handle produced by [`LashSession::park`](crate::LashSession::park).
371    ///
372    /// Resume reloads the flushed state from the parked store (honoring this
373    /// core's residency), reinstalls this core's plugin configuration and work
374    /// drivers, and returns a ready [`LashSession`]. The parked store instance
375    /// is reused directly, so the transcript the session flushed at park time is
376    /// visible again after resume.
377    ///
378    /// This restores the core-level plugin stack. Session-specific plugins added
379    /// per open via [`SessionBuilder::plugin`] are not re-applied here; parking
380    /// is the round-trip for the core's own configuration.
381    pub async fn resume(&self, parked: ParkedSession) -> Result<LashSession> {
382        // Build the per-session env exactly like `SessionBuilder::open_resolved`
383        // (minus builder-scoped plugins): a fresh plugin host with this core's
384        // factories, the shared work drivers, and the core provider resolver
385        // already carried on `self.env`.
386        let plugin_host = build_plugin_host(
387            self.protocol_factory.as_ref(),
388            self.plugin_factories.as_ref(),
389            Vec::new(),
390        )?;
391        let mut env = self.env.clone();
392        env.core = plugin_host.install_process_engine_contributions(
393            env.core.clone(),
394            self.process_lifecycle_available,
395        )?;
396        env.plugin_host = Some(Arc::new(plugin_host));
397        let effect_host = Arc::clone(&env.core.control.effect_host);
398        let drivers = self.work_driver.drivers().await;
399        env.process_work_driver = drivers.process.clone();
400        env.queued_work_driver = drivers.queued.clone();
401        let runtime = LashRuntime::resume(parked.inner, &env).await?;
402        let handle =
403            RuntimeHandle::with_live_replay_store(runtime, Arc::clone(&self.live_replay_store));
404        Ok(LashSession {
405            runtime: handle,
406            effect_host,
407            parent_session_id: None,
408            active_plugins: Vec::new(),
409            process_phase_probe_slot: self.work_driver.phase_probe_slot(),
410            turn_cancels: crate::turn::TurnCancelRegistry::default(),
411        })
412    }
413
414    /// Flush this core's configured trace sink, if any.
415    ///
416    /// Hosts that hand `lash` a trace sink via
417    /// [`LashCoreBuilder::trace_sink`] already hold their own `Arc` and can
418    /// flush it directly; this is the equivalent lever for hosts that did not
419    /// retain the handle. It flushes the core's copy — for a
420    /// [`JsonlTraceSink`](lash_trace::JsonlTraceSink) that fsyncs the file, and
421    /// for an OTel sink it is a no-op (the host still owns provider flush; see
422    /// the tracing docs). Call it before process exit alongside the host's own
423    /// exporter/provider shutdown.
424    pub fn flush_trace_sink(&self) -> Result<()> {
425        if let Some(sink) = self.env.core.tracing.trace_sink.as_ref() {
426            sink.flush()?;
427        }
428        Ok(())
429    }
430
431    pub fn triggers(&self) -> crate::admin::CoreTriggerAdmin {
432        crate::admin::CoreTriggerAdmin { core: self.clone() }
433    }
434
435    pub fn processes(&self) -> crate::process_admin::Processes {
436        crate::process_admin::Processes { core: self.clone() }
437    }
438
439    pub fn completions(&self) -> crate::admin::Completions {
440        crate::admin::Completions { core: self.clone() }
441    }
442
443    pub fn effect_host(&self) -> Arc<dyn EffectHost> {
444        Arc::clone(&self.env.core.control.effect_host)
445    }
446
447    /// Exact-turn cooperative control for this deployment's effect host.
448    ///
449    /// The returned driver is independently usable from any session handle.
450    /// Session and turn ids are routing identity, not authorization; authorize
451    /// requests in the host API before forwarding them to Lash.
452    pub fn turn_work_driver(&self) -> lash_core::TurnWorkDriver {
453        lash_core::TurnWorkDriver::new(self.effect_host())
454    }
455
456    pub async fn delete_session(
457        &self,
458        session_id: impl AsRef<str>,
459        scoped_effect_controller: ScopedEffectController<'_>,
460    ) -> Result<SessionDeleteReport> {
461        let session_id = session_id.as_ref().to_string();
462        let Some(store_factory) = self.store_factory.as_ref() else {
463            return Err(EmbedError::MissingSessionStoreFactory);
464        };
465        let process = if let Some(process_registry) = self.env.process_registry.as_ref() {
466            let invocation = RuntimeInvocation::effect(
467                RuntimeScope::new(session_id.clone()),
468                format!("process:delete-session:{session_id}"),
469                RuntimeEffectKind::Process,
470                format!("{session_id}:delete-session"),
471            );
472            let outcome = scoped_effect_controller
473                .controller()
474                .execute_effect(
475                    RuntimeEffectEnvelope::new(
476                        invocation,
477                        RuntimeEffectCommand::process(ProcessCommand::DeleteSession {
478                            session_id: session_id.clone(),
479                        }),
480                    ),
481                    RuntimeEffectLocalExecutor::processes(
482                        Arc::clone(process_registry),
483                        self.env.process_work_driver.clone(),
484                    ),
485                )
486                .await
487                .map_err(|err| EmbedError::SessionDeleteProcess {
488                    session_id: session_id.clone(),
489                    message: err.to_string(),
490                })?;
491            match outcome {
492                RuntimeEffectOutcome::Process {
493                    result: ProcessEffectOutcome::DeleteSession { report },
494                } => Some(report),
495                other => {
496                    return Err(EmbedError::SessionDeleteProcess {
497                        session_id,
498                        message: format!(
499                            "process delete returned the wrong outcome: {}",
500                            other.kind().as_str()
501                        ),
502                    });
503                }
504            }
505        } else {
506            None
507        };
508        if let Some(trigger_store) = self.env.trigger_store.as_ref() {
509            trigger_store
510                .delete_session_subscriptions(&session_id)
511                .await
512                .map_err(|err| EmbedError::SessionDeleteProcess {
513                    session_id: session_id.clone(),
514                    message: err.to_string(),
515                })?;
516        }
517        self.env
518            .core
519            .control
520            .effect_host
521            .revoke_await_events_for_session(&session_id)
522            .await
523            .map_err(|err| EmbedError::SessionDeleteProcess {
524                session_id: session_id.clone(),
525                message: err.to_string(),
526            })?;
527        store_factory
528            .delete_session(&session_id)
529            .await
530            .map_err(|message| EmbedError::StoreFactory {
531                session_id: session_id.clone(),
532                message,
533            })?;
534        Ok(SessionDeleteReport {
535            session_id,
536            process,
537        })
538    }
539
540    pub fn process_registry(&self) -> Option<Arc<dyn ProcessRegistry>> {
541        self.env.process_registry.as_ref().cloned()
542    }
543
544    pub fn durable_process_worker_config(&self) -> Result<DurableProcessWorkerConfig> {
545        self.durable_process_worker_config_with_plugins(std::iter::empty::<Arc<dyn PluginFactory>>())
546    }
547
548    pub fn durable_process_worker_config_with_plugins(
549        &self,
550        extra_plugin_factories: impl IntoIterator<Item = Arc<dyn PluginFactory>>,
551    ) -> Result<DurableProcessWorkerConfig> {
552        let Some(process_registry) = self.process_registry() else {
553            return Err(EmbedError::MissingProcessRegistry);
554        };
555        let Some(store_factory) = self.store_factory.as_ref() else {
556            return Err(EmbedError::MissingProcessWorkerStoreFactory);
557        };
558        let plugin_host = build_plugin_host(
559            self.protocol_factory.as_ref(),
560            self.plugin_factories.as_ref(),
561            extra_plugin_factories.into_iter().collect(),
562        )?;
563        let runtime_host = plugin_host.install_process_engine_contributions(
564            self.env.core.clone(),
565            self.process_lifecycle_available,
566        )?;
567        let mut config = DurableProcessWorkerConfig::new(
568            Arc::new(plugin_host),
569            runtime_host,
570            Arc::clone(store_factory),
571            process_registry,
572        )
573        .with_session_policy(self.policy.clone())
574        .with_residency(self.env.residency);
575        if let Some(trigger_store) = self.env.trigger_store.as_ref() {
576            config = config.with_trigger_store(Arc::clone(trigger_store));
577        }
578        if let Some(driver) = self.work_driver.configured_process_work_driver() {
579            config = config
580                .with_change_hub(driver.change_hub())
581                .with_process_work_driver(driver);
582        }
583        if let Some(driver) = self.work_driver.configured_queued_work_driver() {
584            config = config.with_queued_work_driver(driver);
585        }
586        Ok(config)
587    }
588}
589
590fn default_runtime_stack() -> PluginStack {
591    lash_plugin_tool_output_budget::tool_output_budget_stack()
592}
593
594#[derive(Default)]
595pub struct LashCoreBuilder {
596    pub(crate) protocol_factory: Option<Arc<dyn PluginFactory>>,
597    session_spec: SessionSpec,
598    provider: Option<ProviderHandle>,
599    pub(crate) store_factory: Option<Arc<dyn SessionStoreFactory>>,
600    child_store_factory: Option<Arc<dyn SessionStoreFactory>>,
601    // `RuntimeHostConfig` has no `Default`: the generic host-owned durability
602    // dependencies must be named. They are collected here and resolved in
603    // `build()`, which errors if any is unset.
604    effect_host: Option<Arc<dyn EffectHost>>,
605    attachment_store: Option<Arc<dyn AttachmentStore>>,
606    process_env_store: Option<Arc<dyn ProcessExecutionEnvStore>>,
607    trigger_store: Option<Arc<dyn lash_core::TriggerStore>>,
608    // Benign core overrides applied on top of the resolved core.
609    prompt: Option<PromptLayer>,
610    trace_sink: Option<Arc<dyn lash_trace::TraceSink>>,
611    trace_level: Option<lash_trace::TraceLevel>,
612    trace_context: Option<lash_trace::TraceContext>,
613    termination: Option<TerminationPolicy>,
614    // Advanced full-config override; used as the base core when present.
615    runtime_host_config: Option<RuntimeHostConfig>,
616    tool_providers: Vec<Arc<dyn ToolProvider>>,
617    plugin_stack: PluginStack,
618    plugin_host: Option<PluginHost>,
619    residency: Option<Residency>,
620    lease_timings: Option<lash_core::LeaseTimings>,
621    clock: Option<Arc<dyn lash_core::Clock>>,
622    // Single source of truth for process lifecycle support and process-work
623    // consumption.
624    process_work_source: ProcessWorkSource,
625    // Optional host-facing best-effort feed of appended process events,
626    // installed on the inline process-registry decorator at build time.
627    process_event_sink: Option<Arc<dyn lash_core::ProcessEventSink>>,
628    queued_work_source: QueuedWorkSource,
629    live_replay_store: Option<Arc<dyn LiveReplayStore>>,
630}
631
632impl LashCoreBuilder {
633    pub fn protocol_plugin(mut self, plugin: Arc<dyn PluginFactory>) -> Self {
634        self.protocol_factory = Some(plugin);
635        self
636    }
637
638    pub fn provider(mut self, provider: ProviderHandle) -> Self {
639        self.session_spec = self.session_spec.provider_id(provider.kind());
640        self.provider = Some(provider);
641        self
642    }
643
644    pub fn model(mut self, model: lash_core::ModelSpec) -> Self {
645        self.session_spec = self.session_spec.model(model);
646        self
647    }
648
649    pub fn max_turns(mut self, max_turns: usize) -> Self {
650        self.session_spec = self.session_spec.max_turns(max_turns);
651        self
652    }
653
654    pub fn session_spec(mut self, spec: SessionSpec) -> Self {
655        self.session_spec = spec;
656        self
657    }
658
659    /// Configure a factory that can create a persistence store for any root
660    /// session opened from this core.
661    ///
662    /// The factory must honor `SessionStoreCreateRequest::session_id` and
663    /// return a store for that specific session. Do not use this to wrap one
664    /// pre-opened root store; pass root-only stores with
665    /// `LashCore::session(...).store(store)` instead.
666    pub fn store_factory(mut self, store_factory: Arc<dyn SessionStoreFactory>) -> Self {
667        self.store_factory = Some(store_factory);
668        self
669    }
670
671    /// Configure the persistence factory used by managed child sessions, such
672    /// as local subagents.
673    ///
674    /// Child factories must return a distinct store bound to the requested
675    /// child session id. Hosts that pass an explicit root store with
676    /// `SessionBuilder::store` should set this when child sessions need
677    /// persistence.
678    pub fn child_store_factory(mut self, store_factory: Arc<dyn SessionStoreFactory>) -> Self {
679        self.child_store_factory = Some(store_factory);
680        self
681    }
682
683    pub fn attachment_store(mut self, attachment_store: Arc<dyn AttachmentStore>) -> Self {
684        self.attachment_store = Some(attachment_store);
685        self
686    }
687
688    pub fn process_env_store(
689        mut self,
690        process_env_store: Arc<dyn ProcessExecutionEnvStore>,
691    ) -> Self {
692        self.process_env_store = Some(process_env_store);
693        self
694    }
695
696    /// Set the deployment effect host — the durability boundary every operation
697    /// crosses. Pass [`InlineEffectHost`](crate::durability::InlineEffectHost)
698    /// for in-process execution, or a workflow-backed host for durable
699    /// execution.
700    pub fn effect_host(mut self, effect_host: Arc<dyn EffectHost>) -> Self {
701        self.effect_host = Some(effect_host);
702        self
703    }
704
705    pub fn tools(mut self, tools: Arc<dyn ToolProvider>) -> Self {
706        self.tool_providers.push(tools);
707        self
708    }
709
710    pub fn plugin(mut self, plugin: Arc<dyn PluginFactory>) -> Self {
711        self.plugin_stack.push(plugin);
712        self
713    }
714
715    pub fn plugins(mut self, stack: PluginStack) -> Self {
716        self.plugin_stack = stack;
717        self
718    }
719
720    pub fn configure_plugins(mut self, configure: impl FnOnce(&mut PluginStack)) -> Self {
721        configure(&mut self.plugin_stack);
722        self
723    }
724
725    pub fn trace_sink(mut self, trace_sink: Arc<dyn lash_trace::TraceSink>) -> Self {
726        self.trace_sink = Some(trace_sink);
727        self
728    }
729
730    pub fn trace_jsonl_path(mut self, path: impl Into<std::path::PathBuf>) -> Self {
731        self.trace_sink = Some(Arc::new(lash_trace::JsonlTraceSink::new(path.into())));
732        self
733    }
734
735    pub fn trace_level(mut self, trace_level: lash_trace::TraceLevel) -> Self {
736        self.trace_level = Some(trace_level);
737        self
738    }
739
740    pub fn trace_context(mut self, trace_context: lash_trace::TraceContext) -> Self {
741        self.trace_context = Some(trace_context);
742        self
743    }
744
745    pub fn termination(mut self, termination: TerminationPolicy) -> Self {
746        self.termination = Some(termination);
747        self
748    }
749
750    pub fn residency(mut self, residency: Residency) -> Self {
751        self.residency = Some(residency);
752        self
753    }
754
755    /// Configure the lease timing capability for every durable single-writer
756    /// lane this deployment claims: session execution leases, turn-input and
757    /// queued-work claims, and process leases.
758    ///
759    /// This is the failover-latency vs false-takeover-risk knob. Like
760    /// [`residency`](Self::residency) it is an operational deployment decision,
761    /// so it lives on the main builder tier rather than behind
762    /// [`advanced`](Self::advanced). Construct the value with
763    /// [`LeaseTimings::new`](lash_core::LeaseTimings::new), which enforces
764    /// `ttl >= 3 * renew_interval`. Effect hosts accept the same type at
765    /// construction (e.g. SQLite/Postgres effect-replay options), so a host can
766    /// share one timing decision across both boundaries.
767    pub fn lease_timings(mut self, lease_timings: lash_core::LeaseTimings) -> Self {
768        self.lease_timings = Some(lease_timings);
769        self
770    }
771
772    /// Use one host clock for runtime sleeps and embedded-store time.
773    pub fn clock(mut self, clock: Arc<dyn lash_core::Clock>) -> Self {
774        self.clock = Some(clock);
775        self
776    }
777
778    /// Configure the bounded live replay buffer used by session observation
779    /// cursors. This is best-effort reconnect recovery only; durable state
780    /// still comes from the session store and [`SessionReadView`].
781    pub fn live_replay_store(mut self, live_replay_store: Arc<dyn LiveReplayStore>) -> Self {
782        self.live_replay_store = Some(live_replay_store);
783        self
784    }
785
786    /// Resolve the runtime host config, requiring the generic host-owned
787    /// durability dependencies to have been named.
788    fn resolve_runtime_host_config(&mut self) -> Result<RuntimeHostConfig> {
789        if let Some(base) = self.runtime_host_config.take() {
790            return Ok(self.apply_core_overrides(base));
791        }
792        let effect_host = self
793            .effect_host
794            .take()
795            .ok_or(EmbedError::MissingEffectHost)?;
796        let attachment_store = self
797            .attachment_store
798            .take()
799            .ok_or(EmbedError::MissingAttachmentStore)?;
800        let process_env_store = self
801            .process_env_store
802            .take()
803            .ok_or(EmbedError::MissingProcessEnvStore)?;
804        let core = RuntimeHostConfig::new(effect_host, attachment_store, process_env_store);
805        Ok(self.apply_core_overrides(core))
806    }
807
808    /// Apply benign + still-set dependency overrides on top of a base core.
809    fn apply_core_overrides(&mut self, mut core: RuntimeHostConfig) -> RuntimeHostConfig {
810        if let Some(effect_host) = self.effect_host.take() {
811            core.control.effect_host = effect_host;
812        }
813        if let Some(attachment_store) = self.attachment_store.take() {
814            core.durability.attachment_store = Arc::new(
815                lash_core::SessionAttachmentStore::ephemeral(attachment_store),
816            );
817        }
818        if let Some(process_env_store) = self.process_env_store.take() {
819            core.durability.process_env_store = process_env_store;
820        }
821        if let Some(prompt) = self.prompt.take() {
822            core.prompt.prompt = prompt;
823        }
824        if let Some(trace_sink) = self.trace_sink.take() {
825            core.tracing.trace_sink = Some(trace_sink);
826        }
827        if let Some(trace_level) = self.trace_level.take() {
828            core.tracing.trace_level = trace_level;
829        }
830        if let Some(trace_context) = self.trace_context.take() {
831            core.tracing.trace_context = trace_context;
832        }
833        if let Some(termination) = self.termination.take() {
834            core.control.termination = termination;
835        }
836        if let Some(lease_timings) = self.lease_timings.take() {
837            core.control.lease_timings = lease_timings;
838        }
839        if let Some(clock) = self.clock.take() {
840            core.clock = clock;
841        }
842        core
843    }
844
845    /// Validate store peer-coherence of the wired durability dependencies.
846    ///
847    /// Durability is established by what the host wired; the per-invocation
848    /// durable controller is not visible here (the build-time controller is
849    /// inline by construction), so this checks the stores against each other
850    /// only — never the controller (see A5 in the durable-first wiring spec):
851    ///
852    /// - a durable session store factory requires a durable attachment and
853    ///   artifact store (they back the same session state);
854    /// - a durable process registry requires a session store factory that is
855    ///   itself durable (the registry's process records are meaningless without
856    ///   a durable session behind them).
857    fn effective_session_store_tier(&self) -> Option<DurabilityTier> {
858        self.child_store_factory
859            .as_ref()
860            .or(self.store_factory.as_ref())
861            .map(|factory| factory.durability_tier())
862    }
863
864    /// Validate store peer-coherence, sweeping every registered process engine.
865    ///
866    /// Runs after the runtime host is resolved and process-engine contributions
867    /// are installed, so it reads the effective effect host / attachment /
868    /// process-env tiers off `core`, and the session-store / trigger-store /
869    /// process-registry tiers captured from the builder before its plugin stack
870    /// was consumed. A durable session store requires every registered engine to
871    /// be durable too.
872    fn ensure_store_peer_coherence(
873        session_store_tier: Option<DurabilityTier>,
874        trigger_store_tier: Option<DurabilityTier>,
875        process_registry_tier: Option<DurabilityTier>,
876        core: &RuntimeHostConfig,
877    ) -> Result<()> {
878        let attachment_tier = Some(
879            core.durability
880                .attachment_store
881                .persistence()
882                .durability_tier(),
883        );
884        let process_env_tier = Some(core.durability.process_env_store.durability_tier());
885        let effect_host_tier = Some(core.control.effect_host.durability_tier());
886
887        if session_store_tier == Some(DurabilityTier::Durable) {
888            if attachment_tier == Some(DurabilityTier::Inline) {
889                return Err(EmbedError::DurableStorePeerRequired {
890                    facet: "attachment store",
891                });
892            }
893            if process_env_tier == Some(DurabilityTier::Inline) {
894                return Err(EmbedError::DurableStorePeerRequired {
895                    facet: "process execution environment store",
896                });
897            }
898            // Every registered process engine must be durable behind a durable
899            // session store, regardless of how it was contributed.
900            for engine in core.process_engines.engines() {
901                if engine.durability_tier() == DurabilityTier::Inline {
902                    return Err(EmbedError::DurableStorePeerRequired {
903                        facet: engine.kind(),
904                    });
905                }
906            }
907        }
908
909        if process_registry_tier == Some(DurabilityTier::Durable) {
910            if session_store_tier != Some(DurabilityTier::Durable) {
911                return Err(EmbedError::DurableProcessRegistryRequiresStoreFactory);
912            }
913            if trigger_store_tier != Some(DurabilityTier::Durable) {
914                return Err(EmbedError::DurableStorePeerRequired {
915                    facet: "trigger store",
916                });
917            }
918            if process_env_tier != Some(DurabilityTier::Durable) {
919                return Err(EmbedError::DurableStorePeerRequired {
920                    facet: "process execution environment store",
921                });
922            }
923        }
924
925        if trigger_store_tier == Some(DurabilityTier::Durable) {
926            if session_store_tier != Some(DurabilityTier::Durable) {
927                return Err(EmbedError::DurableStorePeerRequired {
928                    facet: "session store factory",
929                });
930            }
931            if process_env_tier != Some(DurabilityTier::Durable) {
932                return Err(EmbedError::DurableStorePeerRequired {
933                    facet: "process execution environment store",
934                });
935            }
936            if process_registry_tier == Some(DurabilityTier::Inline) {
937                return Err(EmbedError::DurableStorePeerRequired {
938                    facet: "process registry",
939                });
940            }
941        }
942
943        if effect_host_tier == Some(DurabilityTier::Durable) {
944            if attachment_tier != Some(DurabilityTier::Durable) {
945                return Err(EmbedError::DurableStorePeerRequired {
946                    facet: "attachment store",
947                });
948            }
949            if process_env_tier != Some(DurabilityTier::Durable) {
950                return Err(EmbedError::DurableStorePeerRequired {
951                    facet: "process execution environment store",
952                });
953            }
954        }
955
956        Ok(())
957    }
958
959    pub fn build(mut self) -> Result<LashCore> {
960        let protocol_factory = self.protocol_factory.clone();
961        if protocol_factory.is_none() && self.plugin_host.is_none() {
962            return Err(EmbedError::MissingProtocolPlugin);
963        }
964        let provider_id = self
965            .session_spec
966            .provider_id
967            .clone()
968            .or_else(|| {
969                self.provider
970                    .as_ref()
971                    .map(|provider| provider.kind().to_string())
972            })
973            .unwrap_or_default();
974        let model = self
975            .session_spec
976            .model
977            .clone()
978            .ok_or(EmbedError::MissingModelSpec)?;
979
980        let base_policy = SessionPolicy {
981            provider_id,
982            model,
983            max_turns: self.session_spec.max_turns.flatten(),
984            ..SessionPolicy::default()
985        };
986        let policy = self.session_spec.resolve_against(&base_policy);
987
988        // Capture the store-peer tiers that live on builder fields before the
989        // plugin stack is consumed below; the engine sweep happens after install.
990        let session_store_tier = self.effective_session_store_tier();
991        let trigger_store_tier = self
992            .trigger_store
993            .as_ref()
994            .map(|store| store.durability_tier());
995        let process_work_source = self
996            .process_work_source
997            .clone()
998            .watched(self.process_event_sink.clone());
999        let process_registry_tier = process_work_source
1000            .process_registry()
1001            .map(|registry| registry.durability_tier());
1002
1003        let mut core = self.resolve_runtime_host_config()?;
1004        if let Some(provider) = self.provider.clone() {
1005            core.providers.provider_resolver =
1006                Arc::new(lash_core::SingleProviderResolver::new(provider));
1007        }
1008        let plugin_factories = if let Some(plugin_host) = self.plugin_host {
1009            plugin_host.factories().to_vec()
1010        } else {
1011            let mut factories = Vec::new();
1012            if !self.tool_providers.is_empty() {
1013                let spec = self
1014                    .tool_providers
1015                    .into_iter()
1016                    .fold(PluginSpec::new(), PluginSpec::with_tool_provider);
1017                factories.push(Arc::new(StaticPluginFactory::new("embed_tools", spec))
1018                    as Arc<dyn PluginFactory>);
1019            }
1020            factories.extend(self.plugin_stack.into_factories());
1021            factories
1022        };
1023        let default_plugin_host =
1024            build_plugin_host(protocol_factory.as_ref(), &plugin_factories, Vec::new())?;
1025        // Whether process lifecycle is available (a process registry is wired).
1026        // Threaded to every plugin host so core installs the same
1027        // plugin-contributed process engines wherever it rebuilds a runtime.
1028        let process_lifecycle_available = process_work_source.has_registry();
1029        // Install onto a throwaway clone purely to sweep every registered
1030        // engine's durability tier for the coherence check. `env.core` stays
1031        // free of plugin-contributed engines so that each runtime-construction
1032        // site (session open, queued-work drain, durable process worker)
1033        // installs them fresh onto a clean registry — keeping the unique-kind
1034        // enforcement in `try_with_engine` a genuine cross-factory check rather
1035        // than a self-collision on a registry that already carries them.
1036        let core_with_engines = default_plugin_host
1037            .install_process_engine_contributions(core.clone(), process_lifecycle_available)?;
1038        // Coherence runs after engines are installed so it can sweep every
1039        // registered engine's durability tier.
1040        Self::ensure_store_peer_coherence(
1041            session_store_tier,
1042            trigger_store_tier,
1043            process_registry_tier,
1044            &core_with_engines,
1045        )?;
1046
1047        let process_registry = process_work_source.process_registry();
1048
1049        // Resolve process work before the process source is moved into the
1050        // environment. The default inline driver's config is built
1051        // eagerly so a missing store factory fails loudly at build, not at
1052        // first open. It is built from the same single-protocol plugin host the
1053        // live runtime uses, so the worker can rebuild a runtime for a process.
1054        let process_work_driver = Self::resolve_process_work_driver(
1055            &process_work_source,
1056            &default_plugin_host,
1057            &core,
1058            process_lifecycle_available,
1059            // The worker rebuilds sessions with the same factory `build()` wires
1060            // below: `child_store_factory.or(store_factory)`.
1061            self.child_store_factory
1062                .as_ref()
1063                .or(self.store_factory.as_ref()),
1064            &policy,
1065            self.residency.unwrap_or_default(),
1066            self.trigger_store.as_ref(),
1067        )?;
1068
1069        let live_replay_clock = Arc::clone(&core.clock);
1070        let mut env_builder = RuntimeEnvironment::builder()
1071            .with_plugin_host(Arc::new(default_plugin_host))
1072            .with_runtime_host_config(core);
1073        if let Some(process_registry) = process_registry.as_ref() {
1074            env_builder = env_builder.with_process_registry(Arc::clone(process_registry));
1075        }
1076        if let Some(residency) = self.residency {
1077            env_builder = env_builder.with_residency(residency);
1078        }
1079        if let Some(child_store_factory) = self
1080            .child_store_factory
1081            .as_ref()
1082            .or(self.store_factory.as_ref())
1083        {
1084            env_builder = env_builder.with_session_store_factory(Arc::clone(child_store_factory));
1085        }
1086        if let Some(trigger_store) = self.trigger_store.as_ref() {
1087            env_builder = env_builder.with_trigger_store(Arc::clone(trigger_store));
1088        }
1089        let live_replay_store = self.live_replay_store.take().unwrap_or_else(|| {
1090            Arc::new(InMemoryLiveReplayStore::with_clock(
1091                lash_core::InMemoryLiveReplayStoreConfig::default(),
1092                live_replay_clock,
1093            ))
1094        });
1095        let env = env_builder.build();
1096        let queued_work_driver = Self::resolve_queued_work_driver(
1097            &self.queued_work_source,
1098            env.clone(),
1099            policy.clone(),
1100            protocol_factory.clone(),
1101            Arc::new(plugin_factories.clone()),
1102            self.child_store_factory
1103                .as_ref()
1104                .or(self.store_factory.as_ref()),
1105            Arc::clone(&live_replay_store),
1106            process_lifecycle_available,
1107        );
1108        let work_driver = InlineWorkDriverSetup {
1109            process: process_work_driver,
1110            queued: queued_work_driver,
1111        };
1112
1113        Ok(LashCore {
1114            env,
1115            policy,
1116            store_factory: self.store_factory,
1117            plugin_factories: Arc::new(plugin_factories),
1118            provider: self.provider,
1119            live_replay_store,
1120            protocol_factory,
1121            process_lifecycle_available,
1122            work_driver: Arc::new(InlineWorkDriverSlot::new(work_driver)),
1123        })
1124    }
1125
1126    /// Decide how a built [`LashCore`] sources its process work driver.
1127    ///
1128    /// - no registry => nothing to run ([`ProcessWorkDriverSetup::None`]);
1129    /// - external driver wired => use it ([`ProcessWorkDriverSetup::External`]);
1130    /// - inline registry wired => lazily construct the default inline driver on first open. Its
1131    ///   [`DurableProcessWorkerConfig`] is built eagerly when a store factory is
1132    ///   present; without one the inline worker cannot rebuild session runtimes.
1133    // Mirrors the sibling `resolve_queued_work_driver`: a builder helper whose
1134    // inputs are the heterogeneous, all-required driver-resolution state and
1135    // have no cohesive sub-grouping.
1136    #[allow(clippy::too_many_arguments)]
1137    fn resolve_process_work_driver(
1138        process_work_source: &ProcessWorkSource,
1139        worker_plugin_host: &PluginHost,
1140        core: &RuntimeHostConfig,
1141        process_lifecycle_available: bool,
1142        store_factory: Option<&Arc<dyn SessionStoreFactory>>,
1143        policy: &SessionPolicy,
1144        residency: lash_core::Residency,
1145        trigger_store: Option<&Arc<dyn lash_core::TriggerStore>>,
1146    ) -> Result<ProcessWorkDriverSetup> {
1147        let (process_registry, process_change_hub) = match process_work_source {
1148            ProcessWorkSource::None => return Ok(ProcessWorkDriverSetup::None),
1149            ProcessWorkSource::External(driver) => {
1150                return Ok(ProcessWorkDriverSetup::External {
1151                    driver: driver.clone(),
1152                });
1153            }
1154            ProcessWorkSource::Inline { registry, hub } => (Arc::clone(registry), hub.clone()),
1155        };
1156        // The worker rebuilds a session runtime per process, so it needs a store
1157        // factory; without one the default runner could not execute anything, so
1158        // fail loudly rather than silently leave processes unexecuted.
1159        let Some(store_factory) = store_factory else {
1160            return Err(EmbedError::ProcessRegistryRequiresStoreFactory);
1161        };
1162        // The worker rebuilds with the same plugin host the live runtime uses,
1163        // including the protocol plugin that supplies the protocol session
1164        // capability. Install its plugin-contributed process engines onto a
1165        // clean copy of the base host — `core` deliberately carries none.
1166        let runtime_host = worker_plugin_host
1167            .install_process_engine_contributions(core.clone(), process_lifecycle_available)?;
1168        let phase_probe_slot = lash_core::runtime::RuntimeTurnPhaseProbeSlot::default();
1169        let mut config = DurableProcessWorkerConfig::new(
1170            Arc::new(worker_plugin_host.clone()),
1171            runtime_host,
1172            Arc::clone(store_factory),
1173            process_registry,
1174        )
1175        .with_session_policy(policy.clone())
1176        .with_trigger_store(trigger_store.cloned().unwrap_or_else(|| {
1177            Arc::new(lash_core::InMemoryTriggerStore::with_clock(Arc::clone(
1178                &core.clock,
1179            )))
1180        }))
1181        .with_residency(residency)
1182        .with_turn_phase_probe_slot(phase_probe_slot);
1183        if let Some(hub) = process_change_hub {
1184            config = config.with_change_hub(hub);
1185        }
1186        let config = Box::new(config);
1187        Ok(ProcessWorkDriverSetup::LazyDefault { config })
1188    }
1189
1190    #[allow(clippy::too_many_arguments)]
1191    fn resolve_queued_work_driver(
1192        queued_work_source: &QueuedWorkSource,
1193        env: RuntimeEnvironment,
1194        policy: SessionPolicy,
1195        protocol_factory: Option<Arc<dyn PluginFactory>>,
1196        plugin_factories: Arc<Vec<Arc<dyn PluginFactory>>>,
1197        store_factory: Option<&Arc<dyn SessionStoreFactory>>,
1198        live_replay_store: Arc<dyn LiveReplayStore>,
1199        process_lifecycle_available: bool,
1200    ) -> QueuedWorkDriverSetup {
1201        match queued_work_source {
1202            QueuedWorkSource::None => QueuedWorkDriverSetup::None,
1203            QueuedWorkSource::External(driver) => QueuedWorkDriverSetup::External {
1204                driver: driver.clone(),
1205            },
1206            QueuedWorkSource::LazyDefault => match store_factory {
1207                Some(store_factory) => QueuedWorkDriverSetup::LazyDefault {
1208                    config: Arc::new(InlineQueuedWorkRunConfig::new(
1209                        env,
1210                        policy,
1211                        protocol_factory,
1212                        plugin_factories,
1213                        Arc::clone(store_factory),
1214                        live_replay_store,
1215                        process_lifecycle_available,
1216                    )),
1217                },
1218                None => QueuedWorkDriverSetup::None,
1219            },
1220        }
1221    }
1222
1223    pub fn advanced(self) -> AdvancedLashCoreBuilder {
1224        AdvancedLashCoreBuilder { builder: self }
1225    }
1226
1227    pub fn process_registry(mut self, process_registry: Arc<dyn ProcessRegistry>) -> Self {
1228        self.process_work_source = ProcessWorkSource::Inline {
1229            registry: process_registry,
1230            hub: None,
1231        };
1232        self
1233    }
1234
1235    /// Install a best-effort, host-facing [`ProcessEventSink`] on the inline
1236    /// process registry.
1237    ///
1238    /// Each appended process event is pushed to the sink after its durable
1239    /// write, in per-process append order. This is freshness, not truth: it
1240    /// never buffers or retries, terminal events are not emitted through it
1241    /// (observe completion via the await seam), and consumers reconcile from
1242    /// the durable event log. See [`ProcessEventSink`] for the full contract.
1243    ///
1244    /// Applies to the inline registry path ([`Self::process_registry`]); a host
1245    /// that supplies its own [`ProcessWorkDriver`](lash_core::ProcessWorkDriver)
1246    /// installs the sink through the driver's constructor instead.
1247    ///
1248    /// [`ProcessEventSink`]: lash_core::ProcessEventSink
1249    pub fn process_event_sink(mut self, sink: Arc<dyn lash_core::ProcessEventSink>) -> Self {
1250        self.process_event_sink = Some(sink);
1251        self
1252    }
1253
1254    pub fn trigger_store(mut self, store: Arc<dyn lash_core::TriggerStore>) -> Self {
1255        self.trigger_store = Some(store);
1256        self
1257    }
1258
1259    /// Configure an externally owned process work runner.
1260    ///
1261    /// Durable hosts construct a [`ProcessWorkDriver`] from the same process
1262    /// registry and wake handle used by their deployment runner, then pass it
1263    /// here. The driver registry becomes the core's process registry and no
1264    /// inline runner is spawned.
1265    pub fn process_work_driver(mut self, driver: ProcessWorkDriver) -> Self {
1266        self.process_work_source = ProcessWorkSource::External(driver);
1267        self
1268    }
1269
1270    /// Configure an externally owned queued-work driver.
1271    pub fn queued_work_driver(mut self, driver: QueuedWorkDriver) -> Self {
1272        self.queued_work_source = QueuedWorkSource::External(driver);
1273        self
1274    }
1275
1276    pub fn disable_queued_work_driver(mut self) -> Self {
1277        self.queued_work_source = QueuedWorkSource::None;
1278        self
1279    }
1280}
1281
1282pub(crate) fn build_plugin_host(
1283    protocol_factory: Option<&Arc<dyn PluginFactory>>,
1284    common_factories: &[Arc<dyn PluginFactory>],
1285    extra_factories: Vec<Arc<dyn PluginFactory>>,
1286) -> Result<PluginHost> {
1287    let mut factories = Vec::with_capacity(
1288        usize::from(protocol_factory.is_some()) + common_factories.len() + extra_factories.len(),
1289    );
1290    if let Some(protocol_factory) = protocol_factory {
1291        factories.push(Arc::clone(protocol_factory));
1292    }
1293    factories.extend(common_factories.iter().cloned());
1294    factories.extend(extra_factories);
1295    Ok(PluginHost::new(factories))
1296}
1297
1298impl PromptLayerSink for LashCoreBuilder {
1299    fn prompt_layer_mut(&mut self) -> &mut PromptLayer {
1300        self.prompt.get_or_insert_with(PromptLayer::new)
1301    }
1302}
1303
1304pub struct AdvancedLashCoreBuilder {
1305    builder: LashCoreBuilder,
1306}
1307
1308impl AdvancedLashCoreBuilder {
1309    pub fn runtime_host_config(mut self, core: lash_core::RuntimeHostConfig) -> Self {
1310        self.builder.runtime_host_config = Some(core);
1311        self
1312    }
1313
1314    pub fn plugin_host(mut self, plugin_host: PluginHost) -> Self {
1315        self.builder.plugin_host = Some(plugin_host);
1316        self
1317    }
1318
1319    pub fn build(self) -> Result<LashCore> {
1320        self.builder.build()
1321    }
1322}