Skip to main content

monoloop_loop/transaction/
bootstrap.rs

1//! Runtime bootstrap inputs.
2
3use super::channel_registry::ChannelRegistry;
4use super::host_tools::HostToolRegistry;
5use monoloop_contracts::TransactionLimits;
6use std::time::Duration;
7use tokio::runtime::Handle;
8
9/// Runtime-wide configuration validated at startup.
10#[derive(Clone, Debug)]
11pub struct RuntimeConfig {
12    /// Transaction / event / callback bounds.
13    pub transaction_limits: TransactionLimits,
14    /// When true, bind a loopback MCP listener shell (WP-07 fills protocol).
15    pub enable_mcp_listener: bool,
16    /// Maximum time to wait for graceful drain during shutdown when not specified.
17    pub default_shutdown_deadline: Duration,
18}
19
20impl Default for RuntimeConfig {
21    fn default() -> Self {
22        Self {
23            transaction_limits: TransactionLimits::default(),
24            enable_mcp_listener: true,
25            default_shutdown_deadline: Duration::from_secs(30),
26        }
27    }
28}
29
30impl RuntimeConfig {
31    /// Validate non-zero and consistent bounds.
32    pub fn validate(&self) -> Result<(), super::StartupError> {
33        self.transaction_limits.validate().map_err(|e| match e {
34            monoloop_contracts::LimitsError::ZeroCapacity(f) => {
35                super::StartupError::InvalidConfig(f)
36            }
37            monoloop_contracts::LimitsError::Inconsistent(_) => {
38                super::StartupError::InvalidConfig("inconsistent transaction limits")
39            }
40        })?;
41        if self.default_shutdown_deadline.is_zero() {
42            return Err(super::StartupError::InvalidConfig(
43                "default_shutdown_deadline",
44            ));
45        }
46        Ok(())
47    }
48}
49
50/// Only construction path for [`super::DefaultTransactionRuntime`].
51///
52/// # Host Tokio pattern
53///
54/// `executor` must be a multi-thread Tokio [`Handle`]. CLI samples may use
55/// `#[tokio::main]` + `Handle::current()`. Embedded hosts (e.g. Tauri) should
56/// start a dedicated multi-thread runtime at process setup and pass
57/// `runtime.handle().clone()` here for the process lifetime.
58pub struct RuntimeBootstrap {
59    /// Limits and feature flags.
60    pub config: RuntimeConfig,
61    /// Immutable Channel bindings (factories not yet realized).
62    pub channels: ChannelRegistry,
63    /// Immutable host tool shell (empty allowed).
64    pub tools: HostToolRegistry,
65    /// Tokio multi-thread handle used for owned tasks.
66    pub executor: Handle,
67}