Skip to main content

lash_lashlang_runtime/
lib.rs

1use std::sync::{Arc, Mutex};
2
3use sha2::{Digest, Sha256};
4
5pub use lash_trace::{
6    TraceLashlangChildExecution, TraceLashlangEdgeSelection, TraceLashlangExecutionEvent,
7    TraceLashlangExecutionIdentity, TraceLashlangGraph, TraceLashlangGraphChildLink,
8    TraceLashlangGraphEdge, TraceLashlangGraphNode, TraceLashlangGraphStore, TraceLashlangMap,
9    TraceLashlangMapEdge, TraceLashlangMapNode, TraceLashlangNodeStatus, TraceLashlangStatus,
10};
11pub use lashlang::{
12    CompiledProcessCache, DurabilityTier as LashlangDurabilityTier, InMemoryLashlangArtifactStore,
13    LASH_TYPE_KEY, LashlangAbilities, LashlangArtifactStore, LashlangHostCatalog,
14    LashlangHostEnvironment, LashlangLanguageFeatures,
15};
16
17pub const LASHLANG_ENGINE_KIND: &str = "lashlang";
18pub const LASHLANG_TOOL_BINDING_KEY: &str = "lashlang.tool";
19pub const LASHLANG_SURFACE_EXTENSION_ID: &str = "lashlang.surface";
20
21#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
22#[serde(default)]
23pub struct LashlangSurfaceContribution {
24    pub abilities: LashlangAbilities,
25    pub language_features: LashlangLanguageFeatures,
26    pub resources: LashlangHostCatalog,
27}
28
29impl LashlangSurfaceContribution {
30    pub fn new(
31        abilities: LashlangAbilities,
32        language_features: LashlangLanguageFeatures,
33        resources: LashlangHostCatalog,
34    ) -> Self {
35        Self {
36            abilities,
37            language_features,
38            resources,
39        }
40    }
41
42    pub fn from_surface(surface: LashlangSurface) -> Self {
43        Self {
44            abilities: surface.abilities,
45            language_features: surface.language_features,
46            resources: surface.resources,
47        }
48    }
49}
50
51#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
52pub struct LashlangToolBinding {
53    #[serde(default, skip_serializing_if = "Vec::is_empty")]
54    pub module_path: Vec<String>,
55    #[serde(default, skip_serializing_if = "Option::is_none")]
56    pub operation: Option<String>,
57    #[serde(default, skip_serializing_if = "Option::is_none")]
58    pub authority_type: Option<String>,
59    #[serde(default, skip_serializing_if = "Vec::is_empty")]
60    pub aliases: Vec<String>,
61}
62
63impl LashlangToolBinding {
64    pub fn new(
65        module_path: impl IntoIterator<Item = impl Into<String>>,
66        operation: impl Into<String>,
67    ) -> Self {
68        Self {
69            module_path: module_path.into_iter().map(Into::into).collect(),
70            operation: Some(operation.into()),
71            authority_type: None,
72            aliases: Vec::new(),
73        }
74    }
75
76    pub fn with_authority_type(mut self, authority_type: impl Into<String>) -> Self {
77        self.authority_type = Some(authority_type.into());
78        self
79    }
80
81    pub fn with_aliases(mut self, aliases: impl IntoIterator<Item = impl Into<String>>) -> Self {
82        self.aliases = aliases.into_iter().map(Into::into).collect();
83        self
84    }
85
86    pub fn executable_for(&self, tool_name: &str) -> Result<ResolvedLashlangToolBinding, String> {
87        if self.module_path.is_empty() {
88            return Err(format!(
89                "tool `{tool_name}` is missing an explicit Lashlang module path"
90            ));
91        }
92        for segment in &self.module_path {
93            validate_lashlang_identifier(tool_name, "module path segment", segment)?;
94        }
95        let operation = self
96            .operation
97            .as_deref()
98            .filter(|operation| !operation.trim().is_empty())
99            .ok_or_else(|| {
100                format!("tool `{tool_name}` is missing an explicit Lashlang operation name")
101            })?;
102        validate_lashlang_identifier(tool_name, "operation name", operation)?;
103        let authority_type = self
104            .authority_type
105            .as_deref()
106            .filter(|authority_type| !authority_type.trim().is_empty())
107            .map(ToOwned::to_owned)
108            .unwrap_or_else(|| default_authority_type(&self.module_path));
109        Ok(ResolvedLashlangToolBinding {
110            module_path: self.module_path.clone(),
111            operation: operation.to_string(),
112            authority_type,
113            aliases: self.aliases.clone(),
114        })
115    }
116
117    pub fn required_for_remote(
118        manifest: &lash_core::ToolManifest,
119    ) -> Result<ResolvedLashlangToolBinding, String> {
120        required_tool_lashlang_executable(manifest)
121    }
122
123    pub fn required_executable_for_remote(
124        &self,
125        tool_name: &str,
126    ) -> Result<ResolvedLashlangToolBinding, String> {
127        self.executable_for(tool_name)
128    }
129}
130
131#[derive(Clone, Debug, PartialEq, Eq)]
132pub struct ResolvedLashlangToolBinding {
133    pub module_path: Vec<String>,
134    pub operation: String,
135    pub authority_type: String,
136    pub aliases: Vec<String>,
137}
138
139impl ResolvedLashlangToolBinding {
140    pub fn module_path_string(&self) -> String {
141        self.module_path.join(".")
142    }
143
144    pub fn call_path(&self) -> String {
145        format!("{}.{}", self.module_path_string(), self.operation)
146    }
147}
148
149fn default_authority_type(module_path: &[String]) -> String {
150    module_path
151        .last()
152        .map(|segment| {
153            let mut chars = segment.chars();
154            match chars.next() {
155                Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
156                None => "Tool".to_string(),
157            }
158        })
159        .unwrap_or_else(|| "Tool".to_string())
160}
161
162fn validate_lashlang_identifier(tool_name: &str, label: &str, value: &str) -> Result<(), String> {
163    let value = value.trim();
164    let mut chars = value.chars();
165    let Some(first) = chars.next() else {
166        return Err(format!("tool `{tool_name}` has an empty Lashlang {label}"));
167    };
168    if !(first == '_' || first.is_ascii_alphabetic()) {
169        return Err(format!(
170            "tool `{tool_name}` has invalid Lashlang {label} `{value}`"
171        ));
172    }
173    if !chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric()) {
174        return Err(format!(
175            "tool `{tool_name}` has invalid Lashlang {label} `{value}`"
176        ));
177    }
178    Ok(())
179}
180
181pub fn tool_lashlang_binding(
182    manifest: &lash_core::ToolManifest,
183) -> Result<Option<LashlangToolBinding>, String> {
184    manifest
185        .bindings
186        .get(LASHLANG_TOOL_BINDING_KEY)
187        .cloned()
188        .map(serde_json::from_value)
189        .transpose()
190        .map_err(|err| {
191            format!(
192                "tool `{}` has invalid `{LASHLANG_TOOL_BINDING_KEY}` binding: {err}",
193                manifest.name
194            )
195        })
196}
197
198pub fn required_tool_lashlang_binding(
199    manifest: &lash_core::ToolManifest,
200) -> Result<LashlangToolBinding, String> {
201    tool_lashlang_binding(manifest)?.ok_or_else(|| {
202        format!(
203            "tool `{}` is missing an explicit `{LASHLANG_TOOL_BINDING_KEY}` binding",
204            manifest.name
205        )
206    })
207}
208
209pub fn required_tool_lashlang_executable(
210    manifest: &lash_core::ToolManifest,
211) -> Result<ResolvedLashlangToolBinding, String> {
212    required_tool_lashlang_binding(manifest)?.executable_for(&manifest.name)
213}
214
215pub trait ToolManifestLashlangExt {
216    fn lashlang_binding(&self) -> Result<Option<LashlangToolBinding>, serde_json::Error>;
217}
218
219impl ToolManifestLashlangExt for lash_core::ToolManifest {
220    fn lashlang_binding(&self) -> Result<Option<LashlangToolBinding>, serde_json::Error> {
221        self.bindings
222            .get(LASHLANG_TOOL_BINDING_KEY)
223            .cloned()
224            .map(serde_json::from_value)
225            .transpose()
226    }
227}
228
229pub trait ToolDefinitionLashlangExt {
230    fn with_lashlang_binding(self, lashlang_binding: LashlangToolBinding) -> Self;
231}
232
233impl ToolDefinitionLashlangExt for lash_core::ToolDefinition {
234    fn with_lashlang_binding(mut self, lashlang_binding: LashlangToolBinding) -> Self {
235        let value = serde_json::to_value(lashlang_binding)
236            .expect("lashlang tool binding must serialize to JSON");
237        self.manifest
238            .bindings
239            .insert(LASHLANG_TOOL_BINDING_KEY.to_string(), value);
240        self
241    }
242}
243
244pub trait RemoteToolGrantLashlangExt {
245    fn with_lashlang_binding(self, lashlang_binding: LashlangToolBinding) -> Self;
246    fn lashlang_binding(&self) -> Result<Option<LashlangToolBinding>, serde_json::Error>;
247}
248
249impl RemoteToolGrantLashlangExt for lash_remote_protocol::RemoteToolGrant {
250    fn with_lashlang_binding(mut self, lashlang_binding: LashlangToolBinding) -> Self {
251        let value = serde_json::to_value(lashlang_binding)
252            .expect("lashlang tool binding must serialize to JSON");
253        self.bindings
254            .insert(LASHLANG_TOOL_BINDING_KEY.to_string(), value);
255        self
256    }
257
258    fn lashlang_binding(&self) -> Result<Option<LashlangToolBinding>, serde_json::Error> {
259        self.bindings
260            .get(LASHLANG_TOOL_BINDING_KEY)
261            .cloned()
262            .map(serde_json::from_value)
263            .transpose()
264    }
265}
266
267#[derive(Clone, Debug)]
268pub struct LashlangSurface {
269    pub abilities: LashlangAbilities,
270    pub language_features: LashlangLanguageFeatures,
271    pub resources: LashlangHostCatalog,
272}
273
274impl Default for LashlangSurface {
275    fn default() -> Self {
276        Self {
277            abilities: LashlangAbilities::default().with_sleep(),
278            language_features: LashlangLanguageFeatures::default(),
279            resources: LashlangHostCatalog::new(),
280        }
281    }
282}
283
284impl LashlangSurface {
285    pub fn new(
286        abilities: LashlangAbilities,
287        language_features: LashlangLanguageFeatures,
288        resources: LashlangHostCatalog,
289    ) -> Self {
290        Self {
291            abilities,
292            language_features,
293            resources,
294        }
295    }
296
297    pub fn for_process_registry(mut self, process_registry_available: bool) -> Self {
298        self.abilities = self.abilities.with_sleep();
299        if process_registry_available {
300            self.abilities = self.abilities.with_processes().with_process_signals();
301        } else {
302            self.abilities.processes = false;
303            self.abilities.process_signals = false;
304        }
305        self
306    }
307
308    pub fn with_resources(mut self, resources: LashlangHostCatalog) -> Self {
309        self.resources.extend(resources);
310        self
311    }
312
313    pub fn with_plugin_extensions(
314        mut self,
315        extensions: &lash_core::PluginExtensions,
316    ) -> Result<Self, String> {
317        for payload in extensions.payloads(LASHLANG_SURFACE_EXTENSION_ID) {
318            let contribution: LashlangSurfaceContribution = serde_json::from_value(payload.clone())
319                .map_err(|err| {
320                    format!("invalid `{LASHLANG_SURFACE_EXTENSION_ID}` extension payload: {err}")
321                })?;
322            self.abilities = self.abilities.union(contribution.abilities);
323            self.language_features = self.language_features.union(contribution.language_features);
324            self.resources.extend(contribution.resources);
325        }
326        Ok(self)
327    }
328
329    pub fn host_environment(
330        &self,
331        catalog: &lash_core::ToolCatalog,
332    ) -> Result<LashlangHostEnvironment, String> {
333        lashlang_host_environment_from_tool_catalog(
334            catalog,
335            self.abilities,
336            self.language_features,
337            self.resources.clone(),
338        )
339    }
340}
341
342pub fn lashlang_host_environment_from_tool_catalog(
343    catalog: &lash_core::ToolCatalog,
344    abilities: LashlangAbilities,
345    language_features: LashlangLanguageFeatures,
346    host_resources: LashlangHostCatalog,
347) -> Result<LashlangHostEnvironment, String> {
348    let mut resources = lashlang_resources_from_tool_catalog(catalog)?;
349    resources.extend(host_resources);
350    if abilities.triggers {
351        lashlang::add_trigger_resource_operations(&mut resources);
352    }
353    Ok(
354        LashlangHostEnvironment::new(resources, abilities)
355            .with_language_features(language_features),
356    )
357}
358
359pub fn lashlang_resources_from_tool_catalog(
360    catalog: &lash_core::ToolCatalog,
361) -> Result<LashlangHostCatalog, String> {
362    let mut host_catalog = LashlangHostCatalog::new();
363    for entry in catalog.tools.iter() {
364        if entry.availability.is_callable() {
365            let lashlang_binding = required_tool_lashlang_executable(&entry.manifest)?;
366            host_catalog.add_module_operation(
367                lashlang_binding.module_path.iter().map(String::as_str),
368                lashlang_binding.authority_type.clone(),
369                lashlang_binding.operation.clone(),
370                entry.manifest.id.to_string(),
371                lashlang::TypeExpr::Any,
372                lashlang::TypeExpr::Any,
373            );
374        }
375    }
376    Ok(host_catalog)
377}
378
379pub fn lashlang_host_environment_satisfies_requirements(
380    required: &lashlang::HostRequirements,
381    current: &LashlangHostEnvironment,
382) -> Result<(), String> {
383    let abilities = required.abilities;
384    let current_abilities = current.abilities;
385    if abilities.processes && !current_abilities.processes {
386        return Err("processes are not available".to_string());
387    }
388    if abilities.sleep && !current_abilities.sleep {
389        return Err("sleep is not available".to_string());
390    }
391    if abilities.process_signals && !current_abilities.process_signals {
392        return Err("process signals are not available".to_string());
393    }
394    if abilities.triggers && !current_abilities.triggers {
395        return Err("triggers are not available".to_string());
396    }
397    if required.language_features.label_annotations && !current.language_features.label_annotations
398    {
399        return Err("label annotations are not available".to_string());
400    }
401
402    for (_, module) in required.resources.module_instances() {
403        let current_module = current
404            .resources
405            .resolve_module_path(&module.path)
406            .ok_or_else(|| format!("module `{}` is not available", module.alias))?;
407        if current_module.resource_type != module.resource_type {
408            return Err(format!(
409                "module `{}` has type `{}`, expected `{}`",
410                module.alias, current_module.resource_type, module.resource_type
411            ));
412        }
413        for (operation, required_binding) in &module.operations {
414            match current.resources.resolve_module_operation(
415                &module.resource_type,
416                &module.alias,
417                operation,
418            ) {
419                Some(current_binding) if current_binding == required_binding => {}
420                Some(current_binding) => {
421                    return Err(format!(
422                        "module `{}` operation `{operation}` resolves to `{}`, expected `{}`",
423                        module.alias,
424                        current_binding.host_operation,
425                        required_binding.host_operation
426                    ));
427                }
428                None => {
429                    return Err(format!(
430                        "module `{}` does not expose operation `{operation}`",
431                        module.alias
432                    ));
433                }
434            }
435        }
436    }
437
438    for (resource_type, required_type) in required.resources.resource_types() {
439        if !current.resources.has_resource_type(resource_type) {
440            return Err(format!("resource type `{resource_type}` is not available"));
441        }
442        for (operation, required_binding) in &required_type.operations {
443            let current_binding = current
444                .resources
445                .resolve_operation(resource_type, operation)
446                .ok_or_else(|| {
447                    format!(
448                        "resource type `{resource_type}` does not expose operation `{operation}`"
449                    )
450                })?;
451            if current_binding.input_ty != required_binding.input_ty {
452                return Err(format!(
453                    "resource type `{resource_type}` operation `{operation}` has incompatible input type"
454                ));
455            }
456            if current_binding.output_ty != required_binding.output_ty {
457                return Err(format!(
458                    "resource type `{resource_type}` operation `{operation}` has incompatible output type"
459                ));
460            }
461        }
462    }
463    for (name, required_data_type) in required.resources.named_data_types() {
464        let current_data_type = current
465            .resources
466            .resolve_named_data_type(name)
467            .ok_or_else(|| format!("host data type `{name}` is not available"))?;
468        if current_data_type != required_data_type {
469            return Err(format!(
470                "host data type `{name}` has incompatible structure"
471            ));
472        }
473    }
474    for (path, required_binding) in required.resources.value_constructors() {
475        let current_binding = current
476            .resources
477            .resolve_value_constructor(&path.split('.').collect::<Vec<_>>())
478            .ok_or_else(|| format!("value constructor `{path}` is not available"))?;
479        if current_binding.input_ty != required_binding.input_ty {
480            return Err(format!(
481                "value constructor `{path}` has incompatible input type"
482            ));
483        }
484        if current_binding.output_ty != required_binding.output_ty {
485            return Err(format!(
486                "value constructor `{path}` has incompatible output type"
487            ));
488        }
489    }
490    for (source_ty, required_binding) in required.resources.trigger_sources() {
491        let current_binding = current
492            .resources
493            .resolve_trigger_source(source_ty)
494            .ok_or_else(|| format!("trigger source type `{source_ty}` is not available"))?;
495        if current_binding != required_binding {
496            return Err(format!(
497                "trigger source type `{source_ty}` has incompatible event type"
498            ));
499        }
500    }
501
502    Ok(())
503}
504
505#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
506pub struct LashlangProcessInput {
507    pub module_ref: lashlang::ModuleRef,
508    pub process_ref: lashlang::ProcessRef,
509    pub host_requirements_ref: lashlang::HostRequirementsRef,
510    pub process_name: String,
511    #[serde(default)]
512    pub args: serde_json::Map<String, serde_json::Value>,
513}
514
515impl LashlangProcessInput {
516    pub fn process_identity(&self) -> lash_core::ProcessIdentity {
517        lashlang_process_identity(self)
518    }
519
520    pub fn remote_identity(&self) -> lash_remote_protocol::RemoteProcessIdentity {
521        lash_remote_protocol::RemoteProcessIdentity {
522            kind: LASHLANG_ENGINE_KIND.to_string(),
523            label: Some(self.process_name.clone()),
524            definition: Some(lash_remote_protocol::RemoteProcessDefinitionIdentity {
525                value: self.definition(),
526            }),
527        }
528    }
529
530    pub fn to_process_input(&self) -> Result<lash_core::ProcessInput, serde_json::Error> {
531        Ok(lash_core::ProcessInput::Engine {
532            kind: LASHLANG_ENGINE_KIND.to_string(),
533            payload: serde_json::to_value(self)?,
534        })
535    }
536
537    pub fn into_process_input(self) -> Result<lash_core::ProcessInput, serde_json::Error> {
538        self.to_process_input()
539    }
540
541    pub fn remote_trigger_subscription_draft(
542        &self,
543        registrant: lash_remote_protocol::RemoteProcessOriginator,
544        env_ref: lash_remote_protocol::RemoteProcessExecutionEnvRef,
545        source_type: impl Into<String>,
546        source_key: impl Into<String>,
547    ) -> Result<lash_remote_protocol::RemoteTriggerSubscriptionDraft, serde_json::Error> {
548        Ok(
549            lash_remote_protocol::RemoteTriggerSubscriptionDraft::for_process(
550                registrant,
551                env_ref,
552                source_type,
553                source_key,
554                self.clone().try_into()?,
555                self.remote_identity(),
556            ),
557        )
558    }
559
560    pub fn from_payload(payload: serde_json::Value) -> Result<Self, serde_json::Error> {
561        serde_json::from_value(payload)
562    }
563
564    pub fn definition(&self) -> serde_json::Value {
565        serde_json::json!({
566            "module_ref": self.module_ref,
567            "process_ref": self.process_ref,
568            "host_requirements_ref": self.host_requirements_ref,
569            "process_name": self.process_name,
570        })
571    }
572}
573
574impl TryFrom<LashlangProcessInput> for lash_remote_protocol::RemoteProcessInput {
575    type Error = serde_json::Error;
576
577    fn try_from(value: LashlangProcessInput) -> Result<Self, Self::Error> {
578        Ok(Self::Engine {
579            kind: LASHLANG_ENGINE_KIND.to_string(),
580            payload: serde_json::to_value(value)?,
581        })
582    }
583}
584
585#[derive(Clone, Debug)]
586pub struct PreparedLashlangProcessStart {
587    pub registration: lash_core::ProcessRegistration,
588    pub label: Option<String>,
589}
590
591pub async fn prepare_lashlang_process_start(
592    artifact_store: Arc<dyn LashlangArtifactStore>,
593    parent_start_seed: &str,
594    start: lashlang::ProcessStart,
595) -> Result<PreparedLashlangProcessStart, String> {
596    let display_name = Some(start.process_name.clone());
597    let artifact = artifact_store
598        .get_module_artifact(&start.module_ref)
599        .await
600        .map_err(|err| format!("failed to load lashlang module artifact: {err}"))?
601        .ok_or_else(|| {
602            format!(
603                "missing lashlang module artifact `{}` for process `{}`",
604                start.module_ref, start.process_name
605            )
606        })?;
607    if artifact.host_requirements_ref != start.host_requirements_ref {
608        return Err(format!(
609            "lashlang module artifact `{}` host requirements mismatch: process requested {}, artifact has {}",
610            start.module_ref, start.host_requirements_ref, artifact.host_requirements_ref
611        ));
612    }
613    if artifact.process_ref(&start.process_name) != Some(&start.process_ref) {
614        return Err(format!(
615            "lashlang module artifact `{}` does not export process `{}` as requested ref {:?}",
616            start.module_ref, start.process_name, start.process_ref
617        ));
618    }
619    let args = match serde_json::to_value(lashlang::Value::Record(Arc::new(start.args)))
620        .map_err(|err| format!("failed to serialize process args: {err}"))?
621    {
622        serde_json::Value::Object(map) => map,
623        _ => return Err("process args must serialize as a record".to_string()),
624    };
625    let signal_event_types = artifact
626        .canonical_ir
627        .process(&start.process_name)
628        .map(lashlang_process_signal_event_types)
629        .unwrap_or_default();
630    let process_input = LashlangProcessInput {
631        module_ref: start.module_ref,
632        process_ref: start.process_ref,
633        host_requirements_ref: start.host_requirements_ref,
634        process_name: start.process_name,
635        args,
636    };
637    let identity = lashlang_process_identity(&process_input);
638    let process_id =
639        deterministic_lashlang_process_id(parent_start_seed, &start.start_site, &process_input)
640            .map_err(|err| format!("failed to derive deterministic process id: {err}"))?;
641    let process_input = process_input
642        .into_process_input()
643        .map_err(|err| format!("failed to encode process input: {err}"))?;
644    let registration = lash_core::ProcessRegistration::new(
645        process_id,
646        process_input,
647        lash_core::ProcessProvenance::host(),
648    )
649    .with_identity(identity)
650    .with_extra_event_types(
651        lashlang_process_event_types()
652            .into_iter()
653            .chain(signal_event_types),
654    );
655    Ok(PreparedLashlangProcessStart {
656        registration,
657        label: display_name,
658    })
659}
660
661pub fn deterministic_lashlang_process_id(
662    parent_start_seed: &str,
663    start_site: &lashlang::LashlangExecutionCallSite,
664    input: &LashlangProcessInput,
665) -> Result<String, serde_json::Error> {
666    let args = serde_json::to_string(&input.args)?;
667    let occurrence = start_site.occurrence.to_string();
668    let process_ref = lashlang::process_ref_key(&input.process_ref);
669    let mut hasher = Sha256::new();
670    for part in [
671        "lashlang-process-start:v1",
672        parent_start_seed,
673        start_site.site.node_id.as_str(),
674        occurrence.as_str(),
675        input.module_ref.as_str(),
676        process_ref.as_str(),
677        input.host_requirements_ref.as_str(),
678        input.process_name.as_str(),
679        args.as_str(),
680    ] {
681        hasher.update(part.as_bytes());
682        hasher.update([0]);
683    }
684    let hash = format!("{:x}", hasher.finalize());
685    Ok(format!("process:lashlang:sha256:{hash}"))
686}
687
688pub fn resolve_lashlang_module_operation(
689    host_environment: &lashlang::LashlangHostEnvironment,
690    receiver: &lashlang::ResourceHandle,
691    operation: &str,
692) -> Result<String, lashlang::ExecutionHostError> {
693    host_environment
694        .resources
695        .resolve_module_operation(&receiver.resource_type, &receiver.alias, operation)
696        .map(|binding| binding.host_operation.clone())
697        .ok_or_else(|| {
698            lashlang::ExecutionHostError::new(format!(
699                "module `{}` of type `{}` does not expose operation `{operation}`",
700                receiver.alias, receiver.resource_type
701            ))
702        })
703}
704
705fn lashlang_process_identity(input: &LashlangProcessInput) -> lash_core::ProcessIdentity {
706    lash_core::ProcessIdentity::new(LASHLANG_ENGINE_KIND)
707        .with_label(Some(input.process_name.clone()))
708        .with_definition(Some(input.definition()))
709}
710
711#[derive(Clone)]
712pub struct LashlangProcessEngine {
713    artifact_store: Arc<dyn LashlangArtifactStore>,
714    process_cache: Arc<Mutex<CompiledProcessCache>>,
715    surface: LashlangSurface,
716    execution_sink: Option<Arc<dyn lash_trace::TraceSink>>,
717    trace_context: lash_trace::TraceContext,
718}
719
720impl LashlangProcessEngine {
721    pub fn new(artifact_store: Arc<dyn LashlangArtifactStore>, surface: LashlangSurface) -> Self {
722        Self {
723            artifact_store,
724            process_cache: Arc::new(Mutex::new(CompiledProcessCache::new())),
725            surface,
726            execution_sink: None,
727            trace_context: lash_trace::TraceContext::default(),
728        }
729    }
730
731    pub fn in_memory(surface: LashlangSurface) -> Self {
732        Self::new(
733            lashlang::global_in_memory_lashlang_artifact_store(),
734            surface,
735        )
736    }
737
738    pub fn with_execution_trace(
739        mut self,
740        sink: Option<Arc<dyn lash_trace::TraceSink>>,
741        trace_context: lash_trace::TraceContext,
742    ) -> Self {
743        self.execution_sink = sink;
744        self.trace_context = trace_context;
745        self
746    }
747
748    pub fn artifact_store(&self) -> Arc<dyn LashlangArtifactStore> {
749        Arc::clone(&self.artifact_store)
750    }
751}
752
753#[async_trait::async_trait]
754impl lash_core::ProcessEngine for LashlangProcessEngine {
755    fn kind(&self) -> &'static str {
756        LASHLANG_ENGINE_KIND
757    }
758
759    async fn validate_start(
760        &self,
761        context: lash_core::ProcessEngineValidationContext<'_>,
762        payload: &serde_json::Value,
763        _env_spec: Option<&lash_core::ProcessExecutionEnvSpec>,
764    ) -> Result<(), lash_core::PluginError> {
765        let input: LashlangProcessInput =
766            serde_json::from_value(payload.clone()).map_err(|err| {
767                lash_core::PluginError::Session(format!("invalid lashlang process payload: {err}"))
768            })?;
769        let artifact = self
770            .artifact_store
771            .get_module_artifact(&input.module_ref)
772            .await
773            .map_err(|err| lash_core::PluginError::Session(format!("load module artifact: {err}")))?
774            .ok_or_else(|| {
775                lash_core::PluginError::Session(format!(
776                    "missing lashlang module artifact `{}`",
777                    input.module_ref
778                ))
779            })?;
780        if artifact.host_requirements_ref != input.host_requirements_ref {
781            return Err(lash_core::PluginError::Session(format!(
782                "lashlang process `{}` requested surface {}, artifact has {}",
783                input.process_name, input.host_requirements_ref, artifact.host_requirements_ref
784            )));
785        }
786        if artifact.process_ref(&input.process_name) != Some(&input.process_ref) {
787            return Err(lash_core::PluginError::Session(format!(
788                "lashlang module `{}` does not export process `{}` as requested ref {:?}",
789                input.module_ref, input.process_name, input.process_ref
790            )));
791        }
792        let surface = self
793            .surface
794            .clone()
795            .for_process_registry(context.process_registry_available());
796        let host_environment = surface
797            .host_environment(context.tool_catalog())
798            .map_err(lash_core::PluginError::Session)?;
799        if let Err(err) = lashlang_host_environment_satisfies_requirements(
800            &artifact.host_requirements,
801            &host_environment,
802        ) {
803            return Err(lash_core::PluginError::Session(format!(
804                "lashlang process `{}` is incompatible with this host surface: {err}",
805                input.process_name
806            )));
807        }
808        Ok(())
809    }
810
811    async fn run(
812        &self,
813        context: lash_core::ProcessEngineRunContext<'_>,
814        payload: serde_json::Value,
815    ) -> lash_core::ProcessAwaitOutput {
816        process::run_lashlang_process(self.clone(), context, payload).await
817    }
818
819    fn identity(&self, payload: &serde_json::Value) -> lash_core::ProcessIdentity {
820        match LashlangProcessInput::from_payload(payload.clone()) {
821            Ok(input) => lashlang_process_identity(&input),
822            Err(_) => lash_core::ProcessIdentity::new(LASHLANG_ENGINE_KIND),
823        }
824    }
825}
826
827mod bridge;
828mod process;
829
830pub use bridge::{
831    lashlang_value_to_json, process_event_payload, protocol_tool_output_to_lashlang_value,
832    protocol_tool_reply_to_lashlang_value, sleep_duration_ms,
833};
834pub use process::{
835    lashlang_process_event_types, lashlang_process_signal_event_types, lashlang_type_expr_schema,
836};
837
838#[cfg(test)]
839mod tests {
840    use super::*;
841
842    #[test]
843    fn process_input_serializes_as_generic_engine_payload() {
844        let hash = lashlang::ContentHash::new("abc123");
845        let input = LashlangProcessInput {
846            module_ref: lashlang::ModuleRef::new(&hash),
847            process_ref: lashlang::ProcessRef::new(hash.clone(), 7),
848            host_requirements_ref: lashlang::HostRequirementsRef::new(&hash),
849            process_name: "main".to_string(),
850            args: serde_json::Map::from_iter([("prompt".to_string(), serde_json::json!("go"))]),
851        };
852
853        let process_input = input
854            .clone()
855            .into_process_input()
856            .expect("lashlang process input serializes");
857
858        let lash_core::ProcessInput::Engine { kind, payload } = process_input else {
859            panic!("lashlang runtime must use the generic engine process input");
860        };
861        assert_eq!(kind, LASHLANG_ENGINE_KIND);
862        assert_eq!(
863            LashlangProcessInput::from_payload(payload)
864                .expect("engine payload decodes")
865                .process_name,
866            input.process_name
867        );
868    }
869
870    #[test]
871    fn process_input_remote_helpers_use_generic_engine_and_identity() {
872        let hash = lashlang::ContentHash::new("abc123");
873        let input = LashlangProcessInput {
874            module_ref: lashlang::ModuleRef::new(&hash),
875            process_ref: lashlang::ProcessRef::new(hash.clone(), 7),
876            host_requirements_ref: lashlang::HostRequirementsRef::new(&hash),
877            process_name: "main".to_string(),
878            args: serde_json::Map::from_iter([("prompt".to_string(), serde_json::json!("go"))]),
879        };
880
881        let remote_input: lash_remote_protocol::RemoteProcessInput = input
882            .clone()
883            .try_into()
884            .expect("lashlang process input serializes remotely");
885        let lash_remote_protocol::RemoteProcessInput::Engine { kind, payload } = remote_input
886        else {
887            panic!("lashlang runtime must use the generic remote engine process input");
888        };
889        assert_eq!(kind, LASHLANG_ENGINE_KIND);
890        assert_eq!(
891            LashlangProcessInput::from_payload(payload)
892                .expect("remote payload decodes")
893                .process_name,
894            "main"
895        );
896
897        let identity = input.process_identity();
898        assert_eq!(identity.kind, LASHLANG_ENGINE_KIND);
899        assert_eq!(identity.label.as_deref(), Some("main"));
900        assert_eq!(input.remote_identity().label.as_deref(), Some("main"));
901
902        let draft = input
903            .remote_trigger_subscription_draft(
904                lash_remote_protocol::RemoteProcessOriginator::Host,
905                "process-env:sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
906                    .parse()
907                    .expect("canonical env ref"),
908                "ui.button.pressed",
909                "source-key",
910            )
911            .expect("remote trigger draft");
912        draft.validate().expect("draft validates");
913        assert_eq!(draft.target_label.as_deref(), Some("main"));
914        assert_eq!(draft.target_identity.label.as_deref(), Some("main"));
915    }
916
917    #[test]
918    fn missing_tool_binding_is_not_fabricated() {
919        let tool = lash_core::ToolDefinition::raw(
920            "tool:test/read_file",
921            "read_file",
922            "read a file",
923            lash_core::ToolDefinition::default_input_schema(),
924            serde_json::Value::Null,
925        );
926
927        let err = required_tool_lashlang_executable(&tool.manifest)
928            .expect_err("missing explicit binding should fail");
929
930        assert!(err.contains("missing an explicit `lashlang.tool` binding"));
931    }
932
933    #[test]
934    fn explicit_tool_binding_attaches_lashlang_metadata() {
935        let tool = lash_core::ToolDefinition::raw(
936            "tool:test/read_file",
937            "read_file",
938            "read a file",
939            lash_core::ToolDefinition::default_input_schema(),
940            serde_json::Value::Null,
941        )
942        .with_lashlang_binding(
943            LashlangToolBinding::new(["fs"], "read")
944                .with_authority_type("Filesystem")
945                .with_aliases(["cat"]),
946        );
947
948        let binding =
949            required_tool_lashlang_executable(&tool.manifest).expect("explicit binding resolves");
950
951        assert_eq!(binding.module_path, vec!["fs"]);
952        assert_eq!(binding.operation, "read");
953        assert_eq!(binding.authority_type, "Filesystem");
954        assert_eq!(binding.aliases, vec!["cat"]);
955    }
956
957    #[test]
958    fn dotted_operation_names_are_rejected() {
959        let tool = lash_core::ToolDefinition::raw(
960            "tool:test/update_plan",
961            "update_plan",
962            "update a plan",
963            lash_core::ToolDefinition::default_input_schema(),
964            serde_json::Value::Null,
965        )
966        .with_lashlang_binding(LashlangToolBinding::new(["tools"], "update.plan"));
967
968        let err = required_tool_lashlang_executable(&tool.manifest)
969            .expect_err("dotted operation cannot compile as one Lashlang operation");
970
971        assert!(err.contains("invalid Lashlang operation name `update.plan`"));
972    }
973
974    #[test]
975    fn manifest_lashlang_binding_accessor_reports_absent_valid_and_malformed() {
976        let mut manifest = lash_core::ToolDefinition::raw(
977            "tool:test/read_file",
978            "read_file",
979            "read a file",
980            lash_core::ToolDefinition::default_input_schema(),
981            serde_json::Value::Null,
982        )
983        .manifest;
984        assert_eq!(manifest.lashlang_binding().expect("absent binding"), None);
985
986        manifest.bindings.insert(
987            LASHLANG_TOOL_BINDING_KEY.to_string(),
988            serde_json::json!({
989                "module_path": ["fs"],
990                "operation": "read"
991            }),
992        );
993        let binding = manifest
994            .lashlang_binding()
995            .expect("valid binding")
996            .expect("present binding");
997        assert_eq!(binding.module_path, vec!["fs"]);
998        assert_eq!(binding.operation.as_deref(), Some("read"));
999
1000        manifest.bindings.insert(
1001            LASHLANG_TOOL_BINDING_KEY.to_string(),
1002            serde_json::json!({ "module_path": "fs" }),
1003        );
1004        assert!(manifest.lashlang_binding().is_err());
1005    }
1006
1007    #[test]
1008    fn remote_grant_lashlang_binding_accessor_reports_absent_valid_and_malformed() {
1009        let grant = remote_tool_grant("read_file");
1010        assert_eq!(grant.lashlang_binding().expect("absent binding"), None);
1011
1012        let grant = grant.with_lashlang_binding(LashlangToolBinding::new(["fs"], "read"));
1013        let binding = grant
1014            .lashlang_binding()
1015            .expect("valid binding")
1016            .expect("present binding");
1017        assert_eq!(binding.module_path, vec!["fs"]);
1018        assert_eq!(binding.operation.as_deref(), Some("read"));
1019
1020        let mut malformed = grant;
1021        malformed.bindings.insert(
1022            LASHLANG_TOOL_BINDING_KEY.to_string(),
1023            serde_json::json!({ "module_path": "fs" }),
1024        );
1025        assert!(malformed.lashlang_binding().is_err());
1026    }
1027
1028    #[test]
1029    fn deterministic_process_id_reuses_replayed_start_site_and_args() {
1030        let input = test_process_input(serde_json::json!({ "root": "." }));
1031        let site = test_start_site("child_process:scan", 1);
1032
1033        let first = deterministic_lashlang_process_id("parent:root", &site, &input)
1034            .expect("process id derives");
1035        let second = deterministic_lashlang_process_id("parent:root", &site, &input)
1036            .expect("process id derives");
1037
1038        assert_eq!(first, second);
1039        assert!(first.starts_with("process:lashlang:sha256:"));
1040    }
1041
1042    #[test]
1043    fn deterministic_process_id_separates_parallel_sites_ordinals_and_parents() {
1044        let input = test_process_input(serde_json::json!({ "root": "." }));
1045        let left = deterministic_lashlang_process_id(
1046            "parent:root",
1047            &test_start_site("child_process:left", 1),
1048            &input,
1049        )
1050        .expect("left id derives");
1051        let right = deterministic_lashlang_process_id(
1052            "parent:root",
1053            &test_start_site("child_process:right", 1),
1054            &input,
1055        )
1056        .expect("right id derives");
1057        let second_ordinal = deterministic_lashlang_process_id(
1058            "parent:root",
1059            &test_start_site("child_process:left", 2),
1060            &input,
1061        )
1062        .expect("second ordinal id derives");
1063        let nested_parent = deterministic_lashlang_process_id(
1064            "parent:nested",
1065            &test_start_site("child_process:left", 1),
1066            &input,
1067        )
1068        .expect("nested parent id derives");
1069
1070        assert_ne!(left, right);
1071        assert_ne!(left, second_ordinal);
1072        assert_ne!(left, nested_parent);
1073    }
1074
1075    #[tokio::test(flavor = "current_thread")]
1076    async fn prepared_start_replays_same_registration_id_without_duplicate_child_identity() {
1077        let store = Arc::new(InMemoryLashlangArtifactStore::new());
1078        let environment = LashlangHostEnvironment::new(
1079            lashlang::LashlangHostCatalog::new(),
1080            LashlangAbilities::default().with_processes(),
1081        );
1082        let output = lashlang::compile_module(lashlang::ModuleCompileRequest {
1083            source: r#"process scan(root: str) -> str { finish root }"#,
1084            environment: &environment,
1085            artifact_store: Some(store.as_ref()),
1086        })
1087        .await
1088        .expect("module compiles and persists");
1089        let artifact_store: Arc<dyn LashlangArtifactStore> = store;
1090        let site = test_start_site("child_process:scan", 1);
1091
1092        let first = prepare_lashlang_process_start(
1093            Arc::clone(&artifact_store),
1094            "parent:root",
1095            test_process_start(&output, site.clone(), "."),
1096        )
1097        .await
1098        .expect("first start prepares");
1099        let replayed = prepare_lashlang_process_start(
1100            Arc::clone(&artifact_store),
1101            "parent:root",
1102            test_process_start(&output, site.clone(), "."),
1103        )
1104        .await
1105        .expect("replayed start prepares");
1106        let sibling = prepare_lashlang_process_start(
1107            Arc::clone(&artifact_store),
1108            "parent:root",
1109            test_process_start(&output, test_start_site("child_process:scan", 2), "."),
1110        )
1111        .await
1112        .expect("sibling start prepares");
1113
1114        assert_eq!(first.registration.id, replayed.registration.id);
1115        assert_eq!(first.registration.identity, replayed.registration.identity);
1116        assert_ne!(first.registration.id, sibling.registration.id);
1117    }
1118
1119    #[test]
1120    fn surface_merges_plugin_extensions() {
1121        let contribution = LashlangSurfaceContribution::new(
1122            LashlangAbilities::default().with_processes(),
1123            LashlangLanguageFeatures::default().with_label_annotations(),
1124            LashlangHostCatalog::tool_default(["lookup"]),
1125        );
1126        let extensions = lash_core::PluginExtensions::from_contributions([
1127            lash_core::PluginExtensionContribution::new(
1128                LASHLANG_SURFACE_EXTENSION_ID,
1129                contribution,
1130            )
1131            .expect("extension payload serializes"),
1132        ]);
1133
1134        let surface = LashlangSurface::default()
1135            .with_plugin_extensions(&extensions)
1136            .expect("lashlang surface extension merges");
1137        let environment = surface
1138            .host_environment(&lash_core::ToolCatalog::default())
1139            .expect("empty tool catalog has no Lashlang bindings to validate");
1140
1141        assert!(environment.abilities.sleep);
1142        assert!(environment.abilities.processes);
1143        assert!(environment.language_features.label_annotations);
1144        assert!(
1145            environment
1146                .resources
1147                .resolve_module_operation("Tools", "tools", "lookup")
1148                .is_some()
1149        );
1150    }
1151
1152    fn remote_tool_grant(name: &str) -> lash_remote_protocol::RemoteToolGrant {
1153        lash_remote_protocol::RemoteToolGrant {
1154            protocol_version: lash_remote_protocol::REMOTE_PROTOCOL_VERSION,
1155            id: format!("remote-tool:{name}"),
1156            name: name.to_string(),
1157            description: String::new(),
1158            input_schema: lash_core::ToolDefinition::default_input_schema(),
1159            output_schema: serde_json::Value::Null,
1160            input_schema_projections: Vec::new(),
1161            output_schema_projections: Vec::new(),
1162            output_contract: lash_remote_protocol::RemoteToolOutputContract::Static,
1163            examples: Vec::new(),
1164            availability: None,
1165            activation: None,
1166            argument_projection: None,
1167            scheduling: None,
1168            retry_policy: None,
1169            bindings: Default::default(),
1170        }
1171    }
1172
1173    fn test_process_input(args: serde_json::Value) -> LashlangProcessInput {
1174        let hash = lashlang::ContentHash::new("abc123");
1175        let args = args
1176            .as_object()
1177            .expect("test args must be an object")
1178            .clone();
1179        LashlangProcessInput {
1180            module_ref: lashlang::ModuleRef::new(&hash),
1181            process_ref: lashlang::ProcessRef::new(hash.clone(), 7),
1182            host_requirements_ref: lashlang::HostRequirementsRef::new(&hash),
1183            process_name: "scan".to_string(),
1184            args,
1185        }
1186    }
1187
1188    fn test_start_site(node_id: &str, occurrence: u64) -> lashlang::LashlangExecutionCallSite {
1189        lashlang::LashlangExecutionCallSite {
1190            site: lashlang::LashlangExecutionSite {
1191                node_id: node_id.to_string(),
1192                node_kind: "child_process".to_string(),
1193                label: "start scan".to_string(),
1194                branch: None,
1195            },
1196            occurrence,
1197        }
1198    }
1199
1200    fn test_process_start(
1201        output: &lashlang::ModuleCompileOutput,
1202        start_site: lashlang::LashlangExecutionCallSite,
1203        root: &str,
1204    ) -> lashlang::ProcessStart {
1205        let mut args = lashlang::Record::new();
1206        args.insert("root".to_string(), lashlang::Value::String(root.into()));
1207        lashlang::ProcessStart {
1208            module_ref: output.module_ref.clone(),
1209            process_ref: output
1210                .artifact
1211                .process_ref("scan")
1212                .expect("scan process export")
1213                .clone(),
1214            host_requirements_ref: output.host_requirements_ref.clone(),
1215            start_site,
1216            process_name: "scan".to_string(),
1217            args,
1218        }
1219    }
1220}