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