Skip to main content

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