grove/Binary/Build/
RuntimeBuildMod.rs

1//! Runtime Build Module
2//!
3//! Provides runtime construction for the Grove extension host.
4//! Handles building and initializing the host runtime.
5
6use std::sync::Arc;
7
8use anyhow::{Context, Result};
9use tracing::{debug, info, instrument, warn};
10
11use crate::{
12	Host::{ExtensionHost::ExtensionHostImpl, HostConfig},
13	Transport::Transport,
14	WASM::Runtime::{WASMConfig, WASMRuntime},
15};
16
17/// Runtime build utilities
18pub struct RuntimeBuild;
19
20impl RuntimeBuild {
21	/// Build a Groove extension host with the specified configuration
22	#[instrument(skip(transport, wasm_runtime))]
23	pub async fn build_host(
24		transport:Transport,
25		wasm_runtime:Arc<WASMRuntime>,
26		host_config:HostConfig,
27	) -> Result<ExtensionHostImpl> {
28		info!("Building Grove extension host");
29
30		// In a real implementation, we would use the provided wasm_runtime
31		// For now, we create the host with default configuration
32
33		let host = ExtensionHostImpl::with_config(transport, host_config.clone())
34			.await
35			.context("Failed to build extension host")?;
36
37		info!("Extension host built successfully");
38
39		Ok(host)
40	}
41
42	/// Build a Grove extension host with default WASM configuration
43	pub async fn build_host_with_defaults(
44		transport:Transport,
45		wasi:bool,
46		memory_limit_mb:u64,
47		max_execution_time_ms:u64,
48	) -> Result<ExtensionHostImpl> {
49		info!("Building Grove extension host with defaults");
50
51		let wasm_config = WASMConfig::new(memory_limit_mb, max_execution_time_ms, wasi);
52		let wasm_runtime = Arc::new(WASMRuntime::new(wasm_config).await?);
53
54		let host_config = HostConfig::default().with_activation_timeout(max_execution_time_ms);
55
56		Self::build_host(transport, wasm_runtime, host_config).await
57	}
58
59	/// Build a minimal extension host for testing
60	#[instrument(skip(transport))]
61	pub async fn build_minimal_host(transport:Transport) -> Result<ExtensionHostImpl> {
62		debug!("Building minimal extension host");
63
64		let host_config = HostConfig::default().with_max_extensions(10).with_lazy_activation(true);
65
66		let wasm_config = WASMConfig::new(64, 10000, false);
67		let wasm_runtime = Arc::new(WASMRuntime::new(wasm_config).await?);
68
69		Self::build_host(transport, wasm_runtime, host_config).await
70	}
71
72	/// Validate build configuration
73	pub fn validate_config(config:&HostConfig) -> Result<()> {
74		if config.max_extensions == 0 {
75			return Err(anyhow::anyhow!("max_extensions must be at least 1"));
76		}
77
78		if config.activation_timeout_ms == 0 {
79			return Err(anyhow::anyhow!("activation_timeout_ms must be at least 1"));
80		}
81
82		Ok(())
83	}
84}
85
86impl Default for RuntimeBuild {
87	fn default() -> Self { Self }
88}
89
90#[cfg(test)]
91mod tests {
92	use super::*;
93
94	#[test]
95	fn test_runtime_build_default() {
96		let builder = RuntimeBuild::default();
97		// Just test that it can be created
98		let _ = builder;
99	}
100
101	#[test]
102	fn test_validate_config() {
103		let valid_config = HostConfig::default();
104		assert!(RuntimeBuild::validate_config(&valid_config).is_ok());
105
106		let mut invalid_config = HostConfig::default();
107		invalid_config.max_extensions = 0;
108		assert!(RuntimeBuild::validate_config(&invalid_config).is_err());
109	}
110}